|
10 | 10 |
|
11 | 11 | #### 步骤
|
12 | 12 | 1. 首先在CefRenderProcessHandler的子类里覆写虚方法OnWebKitInitialized,并在该方法的实现里注册一个C++方法给JavaScript
|
13 |
| -``` |
14 |
| -//假设CefRenderProcessHandler的子类为CefRenderProcessHandlerImpl |
15 |
| -void CefRenderProcessHandlerImpl::OnWebKitInitialized(){ |
16 |
| - std::string app_code = |
17 |
| - //----------------------------------- |
18 |
| - //声明JavaScript里要调用的Cpp方法 |
19 |
| - "var app;" |
20 |
| - "if (!app)" |
21 |
| - " app = {};" |
22 |
| - "(function() {" |
23 |
| -
|
24 |
| - // Send message |
25 |
| - " app.sendMessage = function(name, arguments) {" |
26 |
| - " native function sendMessage();" |
27 |
| - " return sendMessage(name, arguments);" |
28 |
| - " };" |
29 |
| -
|
30 |
| - // Registered Javascript Function, which will be called by Cpp |
31 |
| - " app.registerJavascriptFunction = function(name,callback) {" |
32 |
| - " native function registerJavascriptFunction();" |
33 |
| - " return registerJavascriptFunction(name,callback);" |
34 |
| - " };" |
35 |
| -
|
36 |
| - "})();"; |
37 |
| - //------------------------------------ |
38 |
| -
|
39 |
| - // Register app extension module |
40 |
| - |
41 |
| - // JavaScript里调用app.registerJavascriptFunction时,就会去通过CefRegisterExtension注册的CefV8Handler列表里查找 |
42 |
| - // 找到"v8/app"对应的CefV8HandlerImpl,就调用它的Execute方法 |
43 |
| - // 假设m_v8Handler是CefRenderProcessHandlerImpl的一个成员变量 |
44 |
| - m_v8Handler = new CefV8HandlerImpl(); |
45 |
| - CefRegisterExtension("v8/app", app_code,m_v8Handler); |
46 |
| -} |
47 |
| -``` |
48 |
| -2. 在CefV8Handler的子类的Execute方法里实现sendMessage和registerJavascriptFunction |
49 |
| -``` |
50 |
| -// in CefV8HandlerImpl.h |
51 |
| -class CefV8HandlerImpl : publice CefV8Handler{ |
52 |
| -public: |
53 |
| - CefV8HandlerImpl(); |
54 |
| - ~CefV8HandlerImpl(); |
55 |
| -public: |
56 |
| - /** |
57 |
| - * CefV8Handler Methods, Which will be called when the V8 extension |
58 |
| - * is called in the Javascript environment |
59 |
| - */ |
60 |
| - virtual bool Execute(const CefString& name |
61 |
| - ,CefRefPtr<CefV8Value> object |
62 |
| - ,const CefV8ValueList& arguments |
63 |
| - ,CefRefPtr<CefV8Value>& retval |
64 |
| - ,CefString& exception); |
65 |
| -private: |
66 |
| - // Map of message callbacks. |
67 |
| -typedef std::map<std::pair<std::string, int>, |
68 |
| - std::pair<CefRefPtr<CefV8Context>, CefRefPtr<CefV8Value> > > |
69 |
| - CallbackMap; |
70 |
| -CallbackMap callback_map_; |
71 |
| -} |
72 |
| -``` |
73 |
| -``` |
74 |
| -CefV8HandlerImpl::CefV8HandlerImpl() |
75 |
| -{ |
76 |
| -
|
77 |
| -} |
78 |
| -
|
79 |
| -CefV8HandlerImpl::~CefV8HandlerImpl() |
80 |
| -{ |
81 |
| - // Remove any JavaScript callbacks registered for the context that has been released. |
82 |
| - if (!callback_map_.empty()) { |
83 |
| - CallbackMap::iterator it = callback_map_.begin(); |
84 |
| - for (; it != callback_map_.end();) { |
85 |
| - if (it->second.first->IsSame(context)) |
86 |
| - callback_map_.erase(it++); |
87 |
| - else |
88 |
| - ++it; |
89 |
| - } |
90 |
| - } |
91 |
| -} |
92 |
| -
|
93 |
| -// in CefV8HandlerImpl.cpp |
94 |
| -bool CefV8HandlerImpl::Execute(const CefString& name //JavaScript调用的C++方法名字 |
95 |
| - ,CefRefPtr<CefV8Value> object //JavaScript调用者对象 |
96 |
| - ,const CefV8ValueList& arguments //JavaScript传递的参数 |
97 |
| - ,CefRefPtr<CefV8Value>& retval //需要返回给JavaScript的值设置给这个对象 |
98 |
| - ,CefString& exception) //通知异常信息给JavaScript |
99 |
| -{ |
100 |
| - // In the CefV8Handler::Execute implementation for “registerJavascriptFunction”. |
101 |
| - bool handle=false; |
102 |
| - if(name=="registerJavascriptFunction"){ |
103 |
| - //保存JavaScript设置的回答函数 |
104 |
| - if (arguments.size() == 2 && arguments[0]->IsString() && |
105 |
| - arguments[1]->IsFunction()) { |
106 |
| - std::string message_name = arguments[0]->GetStringValue(); |
107 |
| - CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext(); |
108 |
| - int browser_id = context->GetBrowser()->GetIdentifier(); |
109 |
| - callback_map_.insert( |
110 |
| - std::make_pair(std::make_pair(message_name, browser_id), |
111 |
| - std::make_pair(context, arguments[1]))); |
112 |
| - handle = true; |
| 13 | + ``` |
| 14 | + //假设CefRenderProcessHandler的子类为CefRenderProcessHandlerImpl |
| 15 | + void CefRenderProcessHandlerImpl::OnWebKitInitialized(){ |
| 16 | + std::string app_code = |
| 17 | + //----------------------------------- |
| 18 | + //声明JavaScript里要调用的Cpp方法 |
| 19 | + "var app;" |
| 20 | + "if (!app)" |
| 21 | + " app = {};" |
| 22 | + "(function() {" |
| 23 | + |
| 24 | + // Send message |
| 25 | + " app.sendMessage = function(name, arguments) {" |
| 26 | + " native function sendMessage();" |
| 27 | + " return sendMessage(name, arguments);" |
| 28 | + " };" |
| 29 | + |
| 30 | + // Registered Javascript Function, which will be called by Cpp |
| 31 | + " app.registerJavascriptFunction = function(name,callback) {" |
| 32 | + " native function registerJavascriptFunction();" |
| 33 | + " return registerJavascriptFunction(name,callback);" |
| 34 | + " };" |
| 35 | + |
| 36 | + "})();"; |
| 37 | + //------------------------------------ |
| 38 | + |
| 39 | + // Register app extension module |
113 | 40 |
|
114 |
| - // 此时可以发送一个信息给Browser进程,见第4个步骤 |
| 41 | + // JavaScript里调用app.registerJavascriptFunction时,就会去通过CefRegisterExtension注册的CefV8Handler列表里查找 |
| 42 | + // 找到"v8/app"对应的CefV8HandlerImpl,就调用它的Execute方法 |
| 43 | + // 假设m_v8Handler是CefRenderProcessHandlerImpl的一个成员变量 |
| 44 | + m_v8Handler = new CefV8HandlerImpl(); |
| 45 | + CefRegisterExtension("v8/app", app_code,m_v8Handler); |
| 46 | + } |
| 47 | + ``` |
| 48 | +2. 在CefV8Handler的子类的Execute方法里实现sendMessage和registerJavascriptFunction |
| 49 | + ``` |
| 50 | + // in CefV8HandlerImpl.h |
| 51 | + class CefV8HandlerImpl : publice CefV8Handler{ |
| 52 | + public: |
| 53 | + CefV8HandlerImpl(); |
| 54 | + ~CefV8HandlerImpl(); |
| 55 | + public: |
| 56 | + /** |
| 57 | + * CefV8Handler Methods, Which will be called when the V8 extension |
| 58 | + * is called in the Javascript environment |
| 59 | + */ |
| 60 | + virtual bool Execute(const CefString& name |
| 61 | + ,CefRefPtr<CefV8Value> object |
| 62 | + ,const CefV8ValueList& arguments |
| 63 | + ,CefRefPtr<CefV8Value>& retval |
| 64 | + ,CefString& exception); |
| 65 | + private: |
| 66 | + // Map of message callbacks. |
| 67 | + typedef std::map<std::pair<std::string, int>, |
| 68 | + std::pair<CefRefPtr<CefV8Context>, CefRefPtr<CefV8Value> > > |
| 69 | + CallbackMap; |
| 70 | + CallbackMap callback_map_; |
115 | 71 | }
|
| 72 | + ``` |
| 73 | + ``` |
| 74 | + CefV8HandlerImpl::CefV8HandlerImpl() |
| 75 | + { |
116 | 76 |
|
117 |
| - if(name=="sendMessage"){ |
118 |
| - //处理SendMessage, |
119 |
| - handle = true; |
120 | 77 | }
|
121 | 78 |
|
122 |
| - // 如果没有处理,则发异常信息给js |
123 |
| - if(!handle){ |
124 |
| - exception="not implement function"; |
| 79 | + CefV8HandlerImpl::~CefV8HandlerImpl() |
| 80 | + { |
| 81 | + // Remove any JavaScript callbacks registered for the context that has been released. |
| 82 | + if (!callback_map_.empty()) { |
| 83 | + CallbackMap::iterator it = callback_map_.begin(); |
| 84 | + for (; it != callback_map_.end();) { |
| 85 | + if (it->second.first->IsSame(context)) |
| 86 | + callback_map_.erase(it++); |
| 87 | + else |
| 88 | + ++it; |
| 89 | + } |
| 90 | + } |
125 | 91 | }
|
126 | 92 |
|
127 |
| - return true; |
128 |
| -} |
129 |
| -``` |
| 93 | + // in CefV8HandlerImpl.cpp |
| 94 | + bool CefV8HandlerImpl::Execute(const CefString& name //JavaScript调用的C++方法名字 |
| 95 | + ,CefRefPtr<CefV8Value> object //JavaScript调用者对象 |
| 96 | + ,const CefV8ValueList& arguments //JavaScript传递的参数 |
| 97 | + ,CefRefPtr<CefV8Value>& retval //需要返回给JavaScript的值设置给这个对象 |
| 98 | + ,CefString& exception) //通知异常信息给JavaScript |
| 99 | + { |
| 100 | + // In the CefV8Handler::Execute implementation for “registerJavascriptFunction”. |
| 101 | + bool handle=false; |
| 102 | + if(name=="registerJavascriptFunction"){ |
| 103 | + //保存JavaScript设置的回答函数 |
| 104 | + if (arguments.size() == 2 && arguments[0]->IsString() && |
| 105 | + arguments[1]->IsFunction()) { |
| 106 | + std::string message_name = arguments[0]->GetStringValue(); |
| 107 | + CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext(); |
| 108 | + int browser_id = context->GetBrowser()->GetIdentifier(); |
| 109 | + callback_map_.insert( |
| 110 | + std::make_pair(std::make_pair(message_name, browser_id), |
| 111 | + std::make_pair(context, arguments[1]))); |
| 112 | + handle = true; |
| 113 | + |
| 114 | + // 此时可以发送一个信息给Browser进程,见第4个步骤 |
| 115 | + } |
| 116 | + |
| 117 | + if(name=="sendMessage"){ |
| 118 | + //处理SendMessage, |
| 119 | + handle = true; |
| 120 | + } |
| 121 | + |
| 122 | + // 如果没有处理,则发异常信息给js |
| 123 | + if(!handle){ |
| 124 | + exception="not implement function"; |
| 125 | + } |
| 126 | + |
| 127 | + return true; |
| 128 | + } |
| 129 | + ``` |
130 | 130 | 3. 在HTML的JavaScript里,通过上面注册的方法向Render进程注册一个回调函数。
|
131 |
| -``` |
132 |
| -// In JavaScript register the callback function. |
133 |
| -app.setMessageCallback('binding_test', function(name, args) { |
134 |
| - document.getElementById('result').value = "Response: "+args[0]; |
135 |
| -}); |
136 |
| -``` |
| 131 | + ``` |
| 132 | + // In JavaScript register the callback function. |
| 133 | + app.setMessageCallback('binding_test', function(name, args) { |
| 134 | + document.getElementById('result').value = "Response: "+args[0]; |
| 135 | + }); |
| 136 | + ``` |
137 | 137 | 4. Render进程发送异步进程间通信到Browser进程。
|
138 | 138 | 5. Browser进程接收到进程间消息,并处理。
|
139 | 139 | 6. Browser进程处理完毕后,发送一个异步进程间消息给Render进程,返回结果。
|
140 | 140 | 7. Render进程接收到进程间消息,则调用最开始保存的JavaScript注册的回调函数处理之。
|
141 |
| -``` |
142 |
| -// Execute the registered JavaScript callback if any. |
143 |
| -if (!callback_map_.empty()) { |
144 |
| - const CefString& message_name = message->GetName(); |
145 |
| - CallbackMap::const_iterator it = callback_map_.find( |
146 |
| - std::make_pair(message_name.ToString(), |
147 |
| - browser->GetIdentifier())); |
148 |
| - if (it != callback_map_.end()) { |
149 |
| - // Keep a local reference to the objects. The callback may remove itself |
150 |
| - // from the callback map. |
151 |
| - CefRefPtr<CefV8Context> context = it->second.first; |
152 |
| - CefRefPtr<CefV8Value> callback = it->second.second; |
153 |
| -
|
154 |
| - // Enter the context. |
155 |
| - context->Enter(); |
156 |
| -
|
157 |
| - CefV8ValueList arguments; |
158 |
| -
|
159 |
| - // First argument is the message name. |
160 |
| - arguments.push_back(CefV8Value::CreateString(message_name)); |
161 |
| -
|
162 |
| - // Second argument is the list of message arguments. |
163 |
| - CefRefPtr<CefListValue> list = message->GetArgumentList(); |
164 |
| - CefRefPtr<CefV8Value> args = CefV8Value::CreateArray(list->GetSize()); |
165 |
| - SetList(list, args); // Helper function to convert CefListValue to CefV8Value. |
166 |
| - arguments.push_back(args); |
167 |
| -
|
168 |
| - // Execute the callback. |
169 |
| - CefRefPtr<CefV8Value> retval = callback->ExecuteFunction(NULL, arguments); |
170 |
| - if (retval.get()) { |
171 |
| - if (retval->IsBool()) |
172 |
| - handled = retval->GetBoolValue(); |
173 |
| - } |
174 |
| -
|
175 |
| - // Exit the context. |
176 |
| - context->Exit(); |
177 |
| - } |
178 |
| -} |
179 |
| -``` |
| 141 | + ``` |
| 142 | + // Execute the registered JavaScript callback if any. |
| 143 | + if (!callback_map_.empty()) { |
| 144 | + const CefString& message_name = message->GetName(); |
| 145 | + CallbackMap::const_iterator it = callback_map_.find( |
| 146 | + std::make_pair(message_name.ToString(), |
| 147 | + browser->GetIdentifier())); |
| 148 | + if (it != callback_map_.end()) { |
| 149 | + // Keep a local reference to the objects. The callback may remove itself |
| 150 | + // from the callback map. |
| 151 | + CefRefPtr<CefV8Context> context = it->second.first; |
| 152 | + CefRefPtr<CefV8Value> callback = it->second.second; |
| 153 | + |
| 154 | + // Enter the context. |
| 155 | + context->Enter(); |
| 156 | + |
| 157 | + CefV8ValueList arguments; |
| 158 | + |
| 159 | + // First argument is the message name. |
| 160 | + arguments.push_back(CefV8Value::CreateString(message_name)); |
| 161 | + |
| 162 | + // Second argument is the list of message arguments. |
| 163 | + CefRefPtr<CefListValue> list = message->GetArgumentList(); |
| 164 | + CefRefPtr<CefV8Value> args = CefV8Value::CreateArray(list->GetSize()); |
| 165 | + SetList(list, args); // Helper function to convert CefListValue to CefV8Value. |
| 166 | + arguments.push_back(args); |
| 167 | + |
| 168 | + // Execute the callback. |
| 169 | + CefRefPtr<CefV8Value> retval = callback->ExecuteFunction(NULL, arguments); |
| 170 | + if (retval.get()) { |
| 171 | + if (retval->IsBool()) |
| 172 | + handled = retval->GetBoolValue(); |
| 173 | + } |
| 174 | + |
| 175 | + // Exit the context. |
| 176 | + context->Exit(); |
| 177 | + } |
| 178 | + } |
| 179 | + ``` |
180 | 180 | 8. 在CefRenderProcessHandlerImpl::OnContextReleased()里释放JavaScript注册的回调函数以及其他V8资源。
|
181 |
| -``` |
182 |
| -void CefRenderProcessHandlerImpl::OnContextReleased(CefRefPtr<CefBrowser> browser, |
183 |
| - CefRefPtr<CefFrame> frame, |
184 |
| - CefRefPtr<CefV8Context> context) { |
185 |
| - if(m_v8Handler->Get()){ |
186 |
| - m_v8Handler->Release(); |
| 181 | + ``` |
| 182 | + void CefRenderProcessHandlerImpl::OnContextReleased(CefRefPtr<CefBrowser> browser, |
| 183 | + CefRefPtr<CefFrame> frame, |
| 184 | + CefRefPtr<CefV8Context> context) { |
| 185 | + if(m_v8Handler->Get()){ |
| 186 | + m_v8Handler->Release(); |
| 187 | + } |
187 | 188 | }
|
188 |
| -} |
189 |
| -``` |
| 189 | + ``` |
0 commit comments