cef3知识点1

以下在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 app;

  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 browser,
CefRefPtr frame,
CefRefPtr context)

{

// Create an instance of my CefV8Handler object.
CefRefPtr handler = new MyV8Handler();


// Create the "myfunc" function.
CefRefPtr func = CefV8Value::CreateFunction("OpenGame", handler);


// 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 object,
const CefV8ValueList& arguments,
CefRefPtr& retval,
CefString& exception) OVERRIDE {
if (name == "OpenGame") {
// Return my string value.
retval = CefV8Value::CreateString("My Value!");

CefRefPtr msg = CefProcessMessage::Create("OpenGame");


// Retrieve the argument list object.
CefRefPtr args = msg->GetArgumentList();


// Populate the argument values.
CefRefPtr urlvalue = arguments.at(0);
CefRefPtr accountvalue = arguments.at(1);
CefRefPtr servervalue = arguments.at(2);
CefRefPtr servernamevalue = arguments.at(3);
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 command_line) {

//知识点 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");

  }

你可能感兴趣的:(cef)