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" |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 const std::string& target_extension_id, | 233 const std::string& target_extension_id, |
234 const GURL& source_url, | 234 const GURL& source_url, |
235 content::RenderView* restrict_to_render_view) { | 235 content::RenderView* restrict_to_render_view) { |
236 v8::HandleScope handle_scope; | 236 v8::HandleScope handle_scope; |
237 | 237 |
238 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 238 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
239 | 239 |
240 bool port_created = false; | 240 bool port_created = false; |
241 std::string source_url_spec = source_url.spec(); | 241 std::string source_url_spec = source_url.spec(); |
242 | 242 |
| 243 // TODO(kalman): pass in the full ChromeV8ContextSet; call ForEach. |
243 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); | 244 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); |
244 it != contexts.end(); ++it) { | 245 it != contexts.end(); ++it) { |
245 if (restrict_to_render_view && | 246 if (restrict_to_render_view && |
246 restrict_to_render_view != (*it)->GetRenderView()) { | 247 restrict_to_render_view != (*it)->GetRenderView()) { |
247 continue; | 248 continue; |
248 } | 249 } |
249 | 250 |
250 // TODO(kalman): remove when ContextSet::ForEach is available. | 251 // TODO(kalman): remove when ContextSet::ForEach is available. |
251 if ((*it)->v8_context().IsEmpty()) | 252 if ((*it)->v8_context().IsEmpty()) |
252 continue; | 253 continue; |
253 | 254 |
254 v8::Handle<v8::Value> tab = v8::Null(); | 255 v8::Handle<v8::Value> tab = v8::Null(); |
255 if (!source_tab.empty()) | 256 if (!source_tab.empty()) |
256 tab = converter->ToV8Value(&source_tab, (*it)->v8_context()); | 257 tab = converter->ToV8Value(&source_tab, (*it)->v8_context()); |
257 | 258 |
258 v8::Handle<v8::Value> arguments[] = { | 259 v8::Handle<v8::Value> arguments[] = { |
259 v8::Integer::New(target_port_id), | 260 v8::Integer::New(target_port_id), |
260 v8::String::New(channel_name.c_str(), channel_name.size()), | 261 v8::String::New(channel_name.c_str(), channel_name.size()), |
261 tab, | 262 tab, |
262 v8::String::New(source_extension_id.c_str(), source_extension_id.size()), | 263 v8::String::New(source_extension_id.c_str(), source_extension_id.size()), |
263 v8::String::New(target_extension_id.c_str(), target_extension_id.size()), | 264 v8::String::New(target_extension_id.c_str(), target_extension_id.size()), |
264 v8::String::New(source_url_spec.c_str(), source_url_spec.size()) | 265 v8::String::New(source_url_spec.c_str(), source_url_spec.size()) |
265 }; | 266 }; |
266 | 267 |
267 v8::Handle<v8::Value> retval; | 268 v8::Handle<v8::Value> retval = (*it)->module_system()->CallModuleMethod( |
268 v8::TryCatch try_catch; | 269 "miscellaneous_bindings", |
269 if (!(*it)->CallChromeHiddenMethod("Port.dispatchOnConnect", | 270 "dispatchOnConnect", |
270 arraysize(arguments), arguments, | 271 arraysize(arguments), arguments); |
271 &retval)) { | |
272 continue; | |
273 } | |
274 | |
275 if (try_catch.HasCaught()) { | |
276 LOG(ERROR) << "Exception caught when calling Port.dispatchOnConnect."; | |
277 continue; | |
278 } | |
279 | 272 |
280 if (retval.IsEmpty()) { | 273 if (retval.IsEmpty()) { |
281 LOG(ERROR) << "Empty return value from Port.dispatchOnConnect."; | 274 LOG(ERROR) << "Empty return value from dispatchOnConnect."; |
282 continue; | 275 continue; |
283 } | 276 } |
284 | 277 |
285 CHECK(retval->IsBoolean()); | 278 CHECK(retval->IsBoolean()); |
286 if (retval->BooleanValue()) | 279 port_created |= retval->BooleanValue(); |
287 port_created = true; | |
288 } | 280 } |
289 | 281 |
290 // If we didn't create a port, notify the other end of the channel (treat it | 282 // If we didn't create a port, notify the other end of the channel (treat it |
291 // as a disconnect). | 283 // as a disconnect). |
292 if (!port_created) { | 284 if (!port_created) { |
293 content::RenderThread::Get()->Send( | 285 content::RenderThread::Get()->Send( |
294 new ExtensionHostMsg_CloseChannel( | 286 new ExtensionHostMsg_CloseChannel( |
295 target_port_id, kReceivingEndDoesntExistError)); | 287 target_port_id, kReceivingEndDoesntExistError)); |
296 } | 288 } |
297 } | 289 } |
298 | 290 |
299 // static | 291 // static |
300 void MiscellaneousBindings::DeliverMessage( | 292 void MiscellaneousBindings::DeliverMessage( |
301 const ChromeV8ContextSet::ContextSet& contexts, | 293 const ChromeV8ContextSet::ContextSet& contexts, |
302 int target_port_id, | 294 int target_port_id, |
303 const base::ListValue& message, | 295 const base::ListValue& message, |
304 content::RenderView* restrict_to_render_view) { | 296 content::RenderView* restrict_to_render_view) { |
305 v8::HandleScope handle_scope; | 297 v8::HandleScope handle_scope; |
306 | 298 |
| 299 // TODO(kalman): pass in the full ChromeV8ContextSet; call ForEach. |
307 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); | 300 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); |
308 it != contexts.end(); ++it) { | 301 it != contexts.end(); ++it) { |
309 if (restrict_to_render_view && | 302 if (restrict_to_render_view && |
310 restrict_to_render_view != (*it)->GetRenderView()) { | 303 restrict_to_render_view != (*it)->GetRenderView()) { |
311 continue; | 304 continue; |
312 } | 305 } |
313 | 306 |
314 // TODO(kalman): remove when ContextSet::ForEach is available. | 307 // TODO(kalman): remove when ContextSet::ForEach is available. |
315 if ((*it)->v8_context().IsEmpty()) | 308 if ((*it)->v8_context().IsEmpty()) |
316 continue; | 309 continue; |
317 | 310 |
318 v8::Handle<v8::Context> context = (*it)->v8_context(); | 311 v8::Handle<v8::Context> context = (*it)->v8_context(); |
319 v8::Context::Scope context_scope(context); | 312 v8::Context::Scope context_scope(context); |
320 | 313 |
321 // Check to see whether the context has this port before bothering to create | 314 // Check to see whether the context has this port before bothering to create |
322 // the message. | 315 // the message. |
323 v8::Handle<v8::Value> port_id_handle = v8::Integer::New(target_port_id); | 316 v8::Handle<v8::Value> port_id_handle = v8::Integer::New(target_port_id); |
324 v8::Handle<v8::Value> has_port; | 317 v8::Handle<v8::Value> has_port = (*it)->module_system()->CallModuleMethod( |
325 v8::TryCatch try_catch; | 318 "miscellaneous_bindings", |
326 if (!(*it)->CallChromeHiddenMethod("Port.hasPort", 1, &port_id_handle, | 319 "hasPort", |
327 &has_port)) { | 320 1, &port_id_handle); |
328 continue; | |
329 } | |
330 | |
331 if (try_catch.HasCaught()) { | |
332 LOG(ERROR) << "Exception caught when calling Port.hasPort."; | |
333 continue; | |
334 } | |
335 | 321 |
336 CHECK(!has_port.IsEmpty()); | 322 CHECK(!has_port.IsEmpty()); |
337 if (!has_port->BooleanValue()) | 323 if (!has_port->BooleanValue()) |
338 continue; | 324 continue; |
339 | 325 |
340 std::vector<v8::Handle<v8::Value> > arguments; | 326 std::vector<v8::Handle<v8::Value> > arguments; |
341 | 327 |
342 // Convert the message to a v8 object; either a value or undefined. | 328 // Convert the message to a v8 object; either a value or undefined. |
343 // See PostMessage for more details. | 329 // See PostMessage for more details. |
344 if (message.empty()) { | 330 if (message.empty()) { |
345 arguments.push_back(v8::Undefined()); | 331 arguments.push_back(v8::Undefined()); |
346 } else { | 332 } else { |
347 CHECK_EQ(1u, message.GetSize()); | 333 CHECK_EQ(1u, message.GetSize()); |
348 const base::Value* message_value = NULL; | 334 const base::Value* message_value = NULL; |
349 message.Get(0, &message_value); | 335 message.Get(0, &message_value); |
350 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 336 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
351 arguments.push_back(converter->ToV8Value(message_value, context)); | 337 arguments.push_back(converter->ToV8Value(message_value, context)); |
352 } | 338 } |
353 | 339 |
354 arguments.push_back(port_id_handle); | 340 arguments.push_back(port_id_handle); |
355 CHECK((*it)->CallChromeHiddenMethod("Port.dispatchOnMessage", | 341 (*it)->module_system()->CallModuleMethod("miscellaneous_bindings", |
356 arguments.size(), | 342 "dispatchOnMessage", |
357 &arguments[0], | 343 &arguments); |
358 NULL)); | |
359 } | 344 } |
360 } | 345 } |
361 | 346 |
362 // static | 347 // static |
363 void MiscellaneousBindings::DispatchOnDisconnect( | 348 void MiscellaneousBindings::DispatchOnDisconnect( |
364 const ChromeV8ContextSet::ContextSet& contexts, | 349 const ChromeV8ContextSet::ContextSet& contexts, |
365 int port_id, | 350 int port_id, |
366 const std::string& error_message, | 351 const std::string& error_message, |
367 content::RenderView* restrict_to_render_view) { | 352 content::RenderView* restrict_to_render_view) { |
368 v8::HandleScope handle_scope; | 353 v8::HandleScope handle_scope; |
369 | 354 |
| 355 // TODO(kalman): pass in the full ChromeV8ContextSet; call ForEach. |
370 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); | 356 for (ChromeV8ContextSet::ContextSet::const_iterator it = contexts.begin(); |
371 it != contexts.end(); ++it) { | 357 it != contexts.end(); ++it) { |
372 if (restrict_to_render_view && | 358 if (restrict_to_render_view && |
373 restrict_to_render_view != (*it)->GetRenderView()) { | 359 restrict_to_render_view != (*it)->GetRenderView()) { |
374 continue; | 360 continue; |
375 } | 361 } |
376 | 362 |
377 // TODO(kalman): remove when ContextSet::ForEach is available. | 363 // TODO(kalman): remove when ContextSet::ForEach is available. |
378 if ((*it)->v8_context().IsEmpty()) | 364 if ((*it)->v8_context().IsEmpty()) |
379 continue; | 365 continue; |
380 | 366 |
381 std::vector<v8::Handle<v8::Value> > arguments; | 367 std::vector<v8::Handle<v8::Value> > arguments; |
382 arguments.push_back(v8::Integer::New(port_id)); | 368 arguments.push_back(v8::Integer::New(port_id)); |
383 if (!error_message.empty()) { | 369 if (!error_message.empty()) { |
384 arguments.push_back(v8::String::New(error_message.c_str())); | 370 arguments.push_back(v8::String::New(error_message.c_str())); |
385 } else { | 371 } else { |
386 arguments.push_back(v8::Null()); | 372 arguments.push_back(v8::Null()); |
387 } | 373 } |
388 (*it)->CallChromeHiddenMethod("Port.dispatchOnDisconnect", | 374 (*it)->module_system()->CallModuleMethod("miscellaneous_bindings", |
389 arguments.size(), &arguments[0], | 375 "dispatchOnDisconnect", |
390 NULL); | 376 &arguments); |
391 } | 377 } |
392 } | 378 } |
393 | 379 |
394 } // namespace extensions | 380 } // namespace extensions |
OLD | NEW |