OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | 5 #include "chrome/renderer/extensions/miscellaneous_bindings.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/values.h" |
13 #include "chrome/common/extensions/extension_messages.h" | 14 #include "chrome/common/extensions/extension_messages.h" |
14 #include "chrome/common/extensions/message_bundle.h" | 15 #include "chrome/common/extensions/message_bundle.h" |
15 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
16 #include "chrome/renderer/extensions/chrome_v8_context.h" | 17 #include "chrome/renderer/extensions/chrome_v8_context.h" |
17 #include "chrome/renderer/extensions/chrome_v8_context_set.h" | 18 #include "chrome/renderer/extensions/chrome_v8_context_set.h" |
18 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 19 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
19 #include "chrome/renderer/extensions/dispatcher.h" | 20 #include "chrome/renderer/extensions/dispatcher.h" |
20 #include "chrome/renderer/extensions/event_bindings.h" | 21 #include "chrome/renderer/extensions/event_bindings.h" |
21 #include "chrome/renderer/extensions/scoped_persistent.h" | 22 #include "chrome/renderer/extensions/scoped_persistent.h" |
22 #include "content/public/renderer/render_thread.h" | 23 #include "content/public/renderer/render_thread.h" |
23 #include "content/public/renderer/render_view.h" | 24 #include "content/public/renderer/render_view.h" |
| 25 #include "content/public/renderer/v8_value_converter.h" |
24 #include "grit/renderer_resources.h" | 26 #include "grit/renderer_resources.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" |
26 #include "v8/include/v8.h" | 28 #include "v8/include/v8.h" |
27 | 29 |
28 // Message passing API example (in a content script): | 30 // Message passing API example (in a content script): |
29 // var extension = | 31 // var extension = |
30 // new chrome.Extension('00123456789abcdef0123456789abcdef0123456'); | 32 // new chrome.Extension('00123456789abcdef0123456789abcdef0123456'); |
31 // var port = runtime.connect(); | 33 // var port = runtime.connect(); |
32 // port.postMessage('Can you hear me now?'); | 34 // port.postMessage('Can you hear me now?'); |
33 // port.onmessage.addListener(function(msg, port) { | 35 // port.onmessage.addListener(function(msg, port) { |
34 // alert('response=' + msg); | 36 // alert('response=' + msg); |
35 // port.postMessage('I got your reponse'); | 37 // port.postMessage('I got your reponse'); |
36 // }); | 38 // }); |
37 | 39 |
38 using content::RenderThread; | 40 using content::RenderThread; |
| 41 using content::V8ValueConverter; |
39 | 42 |
40 namespace { | 43 namespace { |
41 | 44 |
42 struct ExtensionData { | 45 struct ExtensionData { |
43 struct PortData { | 46 struct PortData { |
44 int ref_count; // how many contexts have a handle to this port | 47 int ref_count; // how many contexts have a handle to this port |
45 PortData() : ref_count(0) {} | 48 PortData() : ref_count(0) {} |
46 }; | 49 }; |
47 std::map<int, PortData> ports; // port ID -> data | 50 std::map<int, PortData> ports; // port ID -> data |
48 }; | 51 }; |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 Dispatcher* dispatcher, | 197 Dispatcher* dispatcher, |
195 v8::Handle<v8::Context> context) { | 198 v8::Handle<v8::Context> context) { |
196 return new ExtensionImpl(dispatcher, context); | 199 return new ExtensionImpl(dispatcher, context); |
197 } | 200 } |
198 | 201 |
199 // static | 202 // static |
200 void MiscellaneousBindings::DispatchOnConnect( | 203 void MiscellaneousBindings::DispatchOnConnect( |
201 const ChromeV8ContextSet::ContextSet& contexts, | 204 const ChromeV8ContextSet::ContextSet& contexts, |
202 int target_port_id, | 205 int target_port_id, |
203 const std::string& channel_name, | 206 const std::string& channel_name, |
204 const std::string& tab_json, | 207 const base::DictionaryValue& source_tab, |
205 const std::string& source_extension_id, | 208 const std::string& source_extension_id, |
206 const std::string& target_extension_id, | 209 const std::string& target_extension_id, |
| 210 const GURL& source_url, |
207 content::RenderView* restrict_to_render_view) { | 211 content::RenderView* restrict_to_render_view) { |
208 v8::HandleScope handle_scope; | 212 v8::HandleScope handle_scope; |
209 | 213 |
| 214 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 215 |
210 bool port_created = false; | 216 bool port_created = false; |
| 217 std::string source_url_spec = source_url.spec(); |
211 | 218 |
212 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); | 219 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); |
213 it != contexts.end(); ++it) { | 220 it != contexts.end(); ++it) { |
214 if (restrict_to_render_view && | 221 if (restrict_to_render_view && |
215 restrict_to_render_view != (*it)->GetRenderView()) { | 222 restrict_to_render_view != (*it)->GetRenderView()) { |
216 continue; | 223 continue; |
217 } | 224 } |
218 | 225 |
219 std::vector<v8::Handle<v8::Value> > arguments; | 226 v8::Handle<v8::Value> tab = v8::Null(); |
220 arguments.push_back(v8::Integer::New(target_port_id)); | 227 if (!source_tab.empty()) |
221 arguments.push_back(v8::String::New(channel_name.c_str(), | 228 tab = converter->ToV8Value(&source_tab, (*it)->v8_context()); |
222 channel_name.size())); | 229 |
223 arguments.push_back(v8::String::New(tab_json.c_str(), | 230 v8::Handle<v8::Value> arguments[] = { |
224 tab_json.size())); | 231 v8::Integer::New(target_port_id), |
225 arguments.push_back(v8::String::New(source_extension_id.c_str(), | 232 v8::String::New(channel_name.c_str(), channel_name.size()), |
226 source_extension_id.size())); | 233 tab, |
227 arguments.push_back(v8::String::New(target_extension_id.c_str(), | 234 v8::String::New(source_extension_id.c_str(), source_extension_id.size()), |
228 target_extension_id.size())); | 235 v8::String::New(target_extension_id.c_str(), target_extension_id.size()), |
| 236 v8::String::New(source_url_spec.c_str(), source_url_spec.size()) |
| 237 }; |
| 238 |
229 v8::Handle<v8::Value> retval; | 239 v8::Handle<v8::Value> retval; |
230 v8::TryCatch try_catch; | 240 v8::TryCatch try_catch; |
231 if (!(*it)->CallChromeHiddenMethod("Port.dispatchOnConnect", | 241 if (!(*it)->CallChromeHiddenMethod("Port.dispatchOnConnect", |
232 arguments.size(), &arguments[0], | 242 arraysize(arguments), arguments, |
233 &retval)) { | 243 &retval)) { |
234 continue; | 244 continue; |
235 } | 245 } |
236 | 246 |
237 if (try_catch.HasCaught()) { | 247 if (try_catch.HasCaught()) { |
238 LOG(ERROR) << "Exception caught when calling Port.dispatchOnConnect."; | 248 LOG(ERROR) << "Exception caught when calling Port.dispatchOnConnect."; |
239 continue; | 249 continue; |
240 } | 250 } |
241 | 251 |
242 if (retval.IsEmpty()) { | 252 if (retval.IsEmpty()) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 } else { | 334 } else { |
325 arguments.push_back(v8::Null()); | 335 arguments.push_back(v8::Null()); |
326 } | 336 } |
327 (*it)->CallChromeHiddenMethod("Port.dispatchOnDisconnect", | 337 (*it)->CallChromeHiddenMethod("Port.dispatchOnDisconnect", |
328 arguments.size(), &arguments[0], | 338 arguments.size(), &arguments[0], |
329 NULL); | 339 NULL); |
330 } | 340 } |
331 } | 341 } |
332 | 342 |
333 } // namespace extensions | 343 } // namespace extensions |
OLD | NEW |