以下在cefsimple.exe中测试过
1:禁止图片
void SimpleApp::OnContextInitialized()添加如下代码
browser_settings是创建浏览窗口的一个控制变量
browser_settings.image_loading = STATE_DISABLED;
browser_settings.local_storage = STATE_DISABLED;
CefBrowserHost::CreateBrowser(window_info, handler.get(), url,
browser_settings, NULL);
2:自定义渲染进程信息回调处理 js调用cef3方法
在winmain主函数中添加如下
CefRefPtr
const std::string& processType = command_line->GetSwitchValue("type");
if (processType == "renderer")
{
app = new ClientAppRenderer();
}
//第二参数NULL时其他进程使用默认处理
int exit_code = CefExecuteProcess(main_args, app, sandbox_info);
if (exit_code >= 0) {
// The sub-process has completed so return here.
return exit_code;
}
通过自定义类ClientAppRenderer实现CefRenderProcessHandler接口并重写
void OnContextCreated(
CefRefPtr
CefRefPtr
CefRefPtr
{
// Create an instance of my CefV8Handler object.
CefRefPtr
// Create the "myfunc" function.
CefRefPtr
// Add the "myfunc" function to the "window" object.
object->SetValue("OpenGame", func, V8_PROPERTY_ATTRIBUTE_NONE);
}
此方法提供了一个CEF3回调方法给页面调用,MyV8Handler则是回调方法的实现
该该回调方法实现如下:
class MyV8Handler : public CefV8Handler {
public:
MyV8Handler() {}
virtual bool Execute(const CefString& name,
CefRefPtr
const CefV8ValueList& arguments,
CefRefPtr
CefString& exception) OVERRIDE {
if (name == "OpenGame") {
// Return my string value.
retval = CefV8Value::CreateString("My Value!");
CefRefPtr
// Retrieve the argument list object.
CefRefPtr
// Populate the argument values.
CefRefPtr
CefRefPtr
CefRefPtr
CefRefPtr
args->SetSize(4);
args->SetString(0, urlvalue.get()->GetStringValue());
args->SetString(1, accountvalue.get()->GetStringValue());
args->SetString(2, servervalue.get()->GetStringValue());
args->SetString(3, servernamevalue.get()->GetStringValue());
// Send the process message to the browser process.
CefV8Context::GetCurrentContext()->GetBrowser()->SendProcessMessage(PID_BROWSER, msg);
return true;
}
// Function does not exist.
return false;
}
// Provide the reference counting implementation for this class.
IMPLEMENT_REFCOUNTING(MyV8Handler);
};
3:winmain主函数中添加如下代码实现缓存和本地化
//知识点 缓存
cef_string_from_ascii("C:\\cef_test", strlen("C:\\cef_test"), &settings.cache_path);
//知识点 本地化
cef_string_from_ascii("zh-CN", strlen("zh-CN"), &settings.locale);
4:处理命令行 开启flash 等
void OnBeforeCommandLineProcessing(
const CefString& process_type,
CefRefPtr
//知识点 flash
command_line->AppendSwitch("--disable-web-security");//关闭同源策略
command_line->AppendSwitch("--enable-system-flash");//使用系统flash
//知识点 指定路径flash,注意32/64
command_line->AppendSwitchWithValue("ppapi-flash-version", "29.0.0.140");
command_line->AppendSwitchWithValue("ppapi-flash-path", "plugins\\pepflashplayer32_29_0_0_140.dll");
}