在cef中使用自定义协议(scheme)
在谷歌浏览器中点击设置,地址栏里出现的不是普通网址,而是chrome://settings/
这个地址就是谷歌浏览器的自定义scheme,cef也提供了自定义协议手段。主要是通过
以下几步:
1.继承一个工厂类MySchemeHandlerFactory : public CefSchemeHandlerFactory
需要包含#include "include/cef_scheme.h"
class MySchemeHandlerFactory : public CefSchemeHandlerFactory
{
public:
virtual CefRefPtr<CefResourceHandler> Create(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& scheme_name,
CefRefPtr<CefRequest> request)
OVERRIDE {
// Return a new resource handler instance to handle the request.
return new MyResourceHandler();
}
private:
IMPLEMENT_REFCOUNTING(MySchemeHandlerFactory);
};
2.继承一个资源类class MyResourceHandler : public CefResourceHandler
class MyResourceHandler : public CefResourceHandler
{
public:
MyResourceHandler() {} virtual bool ProcessRequest(CefRefPtr<CefRequest> request,
CefRefPtr<CefCallback> callback)
OVERRIDE {
std::string url = request->GetURL(); //if (strstr(url.c_str(), "handler.html") != NULL)
data_ = "hello cef";//返回到页面中的内容
callback->Continue();//这个一定要有
return true;//
} virtual void GetResponseHeaders(CefRefPtr<CefResponse> response,
int64& response_length,
CefString& redirectUrl) OVERRIDE {
response->SetMimeType("text/html");
response->SetStatus();
response_length = data_.length() ;
} virtual void Cancel() OVERRIDE {
// Cancel the response...
} virtual bool ReadResponse(void* data_out,
int bytes_to_read,
int& bytes_read,
CefRefPtr<CefCallback> callback)
OVERRIDE {
int size =static_cast<int>(data_.length());
memcpy(data_out, data_.c_str(), size);
bytes_read = size;
return true;
} private:
std::string data_;
IMPLEMENT_REFCOUNTING(MyResourceHandler);
};
3.在初始化cef那几行代码后面增加一句
CefRegisterSchemeHandlerFactory("sin", "test", new MySchemeHandlerFactory());
CefSettings settings;
CefSettingsTraits::init(&settings);
settings.multi_threaded_message_loop = true;
CefRefPtr<SimpleApp> app(new SimpleApp);
CefInitialize(main_args, settings, app.get(), sandbox_info);
//自定义scheme
CefRegisterSchemeHandlerFactory("sin", "test", new MySchemeHandlerFactory());
4.我看有的教程里写的还要在自定义的CefApp类中修改OnRegisterCustomSchemes函数,增加
registrar->AddCustomScheme("sin", true, false, false, false, true, false);
但是我添加还是不添加都没有问题,都可以出来。
5.这时,运行程序,在地址栏输入sin://test之后就会显示页面,内容是hello cef
6.在第2步中,如果不注释if语句if (strstr(url.c_str(), "handler.html") != NULL)
那么,就需要输入sin://test/handler.html才能显示内容hello cef
在cef中使用自定义协议(scheme)的更多相关文章
- 自定义 URL Scheme 完全指南
本文由 Migrant 翻译自 The Complete Tutorial on iOS/iPhone Custom URL Schemes,转载请注明出处. 注意: 自从自定义 URL 的引入,本文 ...
- C#综合揭秘——通过修改注册表建立Windows自定义协议
引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...
- 自定义 URL Scheme 完全指南(转载)
iPhone / iOS SDK 最酷的特性之一就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用. 注册自定义 URL Scheme ...
- java-cef系列视频第四集:自定义协议
上一集我们介绍了如何为java-cef添加flashplayer支持. 本视频介绍java-cef中的自定义协议 本作品采用知识共享署名-非商业性使用-禁止演绎 3.0 中国大陆许可协议进行许可.
- 【转】C#综合揭秘——通过修改注册表建立Windows自定义协议
引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...
- 自定义URL Scheme完全指南
iPhone / iOS SDK 最酷的特性之一就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用. 注册自定义 URL Scheme ...
- 通过修改注册表建立Windows自定义协议
引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...
- iOS 自定义 URL Scheme 完全指南
http://www.cocoachina.com/industry/20140522/8514.html “” 阅读器 自定义URL Scheme 本文转自Migrant的博客,原文:<T ...
- CEF 自定义用户协议(scheme)实现以二进制流的方式显示图片、视频、音频
转载:https://www.cnblogs.com/sinceret/p/10417941.html 转载:https://stackoverflow.com/questions/48811756/ ...
随机推荐
- linux下的ssh和rynsc
在ubuntu下有ssh的目录,但是没有使用 ps -e | grep ssh时没有任何输出,说明没有安装ssh或者是1.x版本,可以打开etc/ssh目录,看是否有文件,下装系统再看看吧.还有ryn ...
- python将字符串类型改成日期类型
将字符串类型的'2019-03-14'改成date类型,如下: import datetime b = datetime.date(*map(int,'2019-03-14'.split('-'))) ...
- RxJava 导入项目配置
在app 的 build.gradle 文件中添加 dependencies { // RxJava 引用implementation 'io.reactivex.rxjava2:rxjava:2.0 ...
- CString比较不区分大小写
第一种:都变为大写 或者都变成小写. str1.MakeUpper();str2.MakeUpper();or:str1.MakeLower();str2.MakeLower(); if(str1== ...
- vue中嵌套页面 iframe 标签
vue中嵌套iframe,将要嵌套的文件放在static下面: <iframe src="../../../static/bear.html" width="300 ...
- 转CB大佬的几个有用的MySQL知识
1.find_in_set函数 find_in_set(str,strlist); str是一个字符串 strlist是字符串列表--一个有多个子链被“,”分开的字符串 有多种情况: a.str为nu ...
- 请大神留言:使用static方法和从Spring IOC 容器里面取出的方法有什么区别????
类的静态方法,不用new出对象,因为它在类的初始化阶段加载到jvm内存的. 而spring容器,是在启动服务的时候,new出容器所管理的对象. 本质区别就是一个在堆中产生了对象,一个没产生对象只在方法 ...
- linux学习:【第3篇】远程连接及软件安装
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! linux学习:[第3篇]远程连接及软件安装 远程连接 xshell , xftp软件官网 : ...
- Linux sed命令 以行为单位编辑文本,或替换文本中的文字
sed -e 4a\newLine testfile 首先查看testfile中的内容如下: $ cat testfile #查看testfile 中的内容 HELLO LINUX! Linux is ...
- libvirt虚拟库
转载自:https://www.ibm.com/developerworks/cn/linux/l-libvirt/index.html Libvirt 虚拟化库剖析 讲到向外扩展计算(比如云计算 ...