| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/messaging_bindings.h" | 5 #include "extensions/renderer/messaging_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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 content::RenderThread::Get()->Send( | 175 content::RenderThread::Get()->Send( |
| 176 new ExtensionHostMsg_CloseChannel(port_id, std::string())); | 176 new ExtensionHostMsg_CloseChannel(port_id, std::string())); |
| 177 ClearPortDataAndNotifyDispatcher(port_id); | 177 ClearPortDataAndNotifyDispatcher(port_id); |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 | 180 |
| 181 // Holds a |callback| to run sometime after |object| is GC'ed. |callback| will | 181 // Holds a |callback| to run sometime after |object| is GC'ed. |callback| will |
| 182 // not be executed re-entrantly to avoid running JS in an unexpected state. | 182 // not be executed re-entrantly to avoid running JS in an unexpected state. |
| 183 class GCCallback { | 183 class GCCallback { |
| 184 public: | 184 public: |
| 185 static void Bind(v8::Handle<v8::Object> object, | 185 static void Bind(v8::Local<v8::Object> object, |
| 186 v8::Handle<v8::Function> callback, | 186 v8::Local<v8::Function> callback, |
| 187 v8::Isolate* isolate) { | 187 v8::Isolate* isolate) { |
| 188 GCCallback* cb = new GCCallback(object, callback, isolate); | 188 GCCallback* cb = new GCCallback(object, callback, isolate); |
| 189 cb->object_.SetWeak(cb, FirstWeakCallback, | 189 cb->object_.SetWeak(cb, FirstWeakCallback, |
| 190 v8::WeakCallbackType::kParameter); | 190 v8::WeakCallbackType::kParameter); |
| 191 } | 191 } |
| 192 | 192 |
| 193 private: | 193 private: |
| 194 static void FirstWeakCallback( | 194 static void FirstWeakCallback( |
| 195 const v8::WeakCallbackInfo<GCCallback>& data) { | 195 const v8::WeakCallbackInfo<GCCallback>& data) { |
| 196 // v8 says we need to explicitly reset weak handles from their callbacks. | 196 // v8 says we need to explicitly reset weak handles from their callbacks. |
| 197 // It's not implicit as one might expect. | 197 // It's not implicit as one might expect. |
| 198 data.GetParameter()->object_.Reset(); | 198 data.GetParameter()->object_.Reset(); |
| 199 data.SetSecondPassCallback(SecondWeakCallback); | 199 data.SetSecondPassCallback(SecondWeakCallback); |
| 200 } | 200 } |
| 201 | 201 |
| 202 static void SecondWeakCallback( | 202 static void SecondWeakCallback( |
| 203 const v8::WeakCallbackInfo<GCCallback>& data) { | 203 const v8::WeakCallbackInfo<GCCallback>& data) { |
| 204 base::MessageLoop::current()->PostTask( | 204 base::MessageLoop::current()->PostTask( |
| 205 FROM_HERE, | 205 FROM_HERE, |
| 206 base::Bind(&GCCallback::RunCallback, | 206 base::Bind(&GCCallback::RunCallback, |
| 207 base::Owned(data.GetParameter()))); | 207 base::Owned(data.GetParameter()))); |
| 208 } | 208 } |
| 209 | 209 |
| 210 GCCallback(v8::Handle<v8::Object> object, | 210 GCCallback(v8::Local<v8::Object> object, |
| 211 v8::Handle<v8::Function> callback, | 211 v8::Local<v8::Function> callback, |
| 212 v8::Isolate* isolate) | 212 v8::Isolate* isolate) |
| 213 : object_(isolate, object), | 213 : object_(isolate, object), |
| 214 callback_(isolate, callback), | 214 callback_(isolate, callback), |
| 215 isolate_(isolate) {} | 215 isolate_(isolate) {} |
| 216 | 216 |
| 217 void RunCallback() { | 217 void RunCallback() { |
| 218 v8::HandleScope handle_scope(isolate_); | 218 v8::HandleScope handle_scope(isolate_); |
| 219 v8::Handle<v8::Function> callback = | 219 v8::Local<v8::Function> callback = |
| 220 v8::Local<v8::Function>::New(isolate_, callback_); | 220 v8::Local<v8::Function>::New(isolate_, callback_); |
| 221 v8::Handle<v8::Context> context = callback->CreationContext(); | 221 v8::Local<v8::Context> context = callback->CreationContext(); |
| 222 if (context.IsEmpty()) | 222 if (context.IsEmpty()) |
| 223 return; | 223 return; |
| 224 v8::Context::Scope context_scope(context); | 224 v8::Context::Scope context_scope(context); |
| 225 blink::WebScopedMicrotaskSuppression suppression; | 225 blink::WebScopedMicrotaskSuppression suppression; |
| 226 callback->Call(context->Global(), 0, NULL); | 226 callback->Call(context->Global(), 0, NULL); |
| 227 } | 227 } |
| 228 | 228 |
| 229 v8::Global<v8::Object> object_; | 229 v8::Global<v8::Object> object_; |
| 230 v8::Global<v8::Function> callback_; | 230 v8::Global<v8::Function> callback_; |
| 231 v8::Isolate* isolate_; | 231 v8::Isolate* isolate_; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 return; | 267 return; |
| 268 v8::Isolate* isolate = script_context->isolate(); | 268 v8::Isolate* isolate = script_context->isolate(); |
| 269 v8::HandleScope handle_scope(isolate); | 269 v8::HandleScope handle_scope(isolate); |
| 270 | 270 |
| 271 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 271 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 272 | 272 |
| 273 const std::string& source_url_spec = info.source_url.spec(); | 273 const std::string& source_url_spec = info.source_url.spec(); |
| 274 std::string target_extension_id = script_context->GetExtensionID(); | 274 std::string target_extension_id = script_context->GetExtensionID(); |
| 275 const Extension* extension = script_context->extension(); | 275 const Extension* extension = script_context->extension(); |
| 276 | 276 |
| 277 v8::Handle<v8::Value> tab = v8::Null(isolate); | 277 v8::Local<v8::Value> tab = v8::Null(isolate); |
| 278 v8::Handle<v8::Value> tls_channel_id_value = v8::Undefined(isolate); | 278 v8::Local<v8::Value> tls_channel_id_value = v8::Undefined(isolate); |
| 279 v8::Handle<v8::Value> guest_process_id = v8::Undefined(isolate); | 279 v8::Local<v8::Value> guest_process_id = v8::Undefined(isolate); |
| 280 | 280 |
| 281 if (extension) { | 281 if (extension) { |
| 282 if (!source->tab.empty() && !extension->is_platform_app()) | 282 if (!source->tab.empty() && !extension->is_platform_app()) |
| 283 tab = converter->ToV8Value(&source->tab, script_context->v8_context()); | 283 tab = converter->ToV8Value(&source->tab, script_context->v8_context()); |
| 284 | 284 |
| 285 ExternallyConnectableInfo* externally_connectable = | 285 ExternallyConnectableInfo* externally_connectable = |
| 286 ExternallyConnectableInfo::Get(extension); | 286 ExternallyConnectableInfo::Get(extension); |
| 287 if (externally_connectable && | 287 if (externally_connectable && |
| 288 externally_connectable->accepts_tls_channel_id) { | 288 externally_connectable->accepts_tls_channel_id) { |
| 289 tls_channel_id_value = v8::String::NewFromUtf8(isolate, | 289 tls_channel_id_value = v8::String::NewFromUtf8(isolate, |
| 290 tls_channel_id.c_str(), | 290 tls_channel_id.c_str(), |
| 291 v8::String::kNormalString, | 291 v8::String::kNormalString, |
| 292 tls_channel_id.size()); | 292 tls_channel_id.size()); |
| 293 } | 293 } |
| 294 | 294 |
| 295 if (info.guest_process_id != content::ChildProcessHost::kInvalidUniqueID) | 295 if (info.guest_process_id != content::ChildProcessHost::kInvalidUniqueID) |
| 296 guest_process_id = v8::Integer::New(isolate, info.guest_process_id); | 296 guest_process_id = v8::Integer::New(isolate, info.guest_process_id); |
| 297 } | 297 } |
| 298 | 298 |
| 299 v8::Handle<v8::Value> arguments[] = { | 299 v8::Local<v8::Value> arguments[] = { |
| 300 // portId | 300 // portId |
| 301 v8::Integer::New(isolate, target_port_id), | 301 v8::Integer::New(isolate, target_port_id), |
| 302 // channelName | 302 // channelName |
| 303 v8::String::NewFromUtf8(isolate, | 303 v8::String::NewFromUtf8(isolate, channel_name.c_str(), |
| 304 channel_name.c_str(), | 304 v8::String::kNormalString, channel_name.size()), |
| 305 v8::String::kNormalString, | |
| 306 channel_name.size()), | |
| 307 // sourceTab | 305 // sourceTab |
| 308 tab, | 306 tab, |
| 309 // source_frame_id | 307 // source_frame_id |
| 310 v8::Integer::New(isolate, source->frame_id), | 308 v8::Integer::New(isolate, source->frame_id), |
| 311 // guestProcessId | 309 // guestProcessId |
| 312 guest_process_id, | 310 guest_process_id, |
| 313 // sourceExtensionId | 311 // sourceExtensionId |
| 314 v8::String::NewFromUtf8(isolate, | 312 v8::String::NewFromUtf8(isolate, info.source_id.c_str(), |
| 315 info.source_id.c_str(), | 313 v8::String::kNormalString, info.source_id.size()), |
| 316 v8::String::kNormalString, | |
| 317 info.source_id.size()), | |
| 318 // targetExtensionId | 314 // targetExtensionId |
| 319 v8::String::NewFromUtf8(isolate, | 315 v8::String::NewFromUtf8(isolate, target_extension_id.c_str(), |
| 320 target_extension_id.c_str(), | |
| 321 v8::String::kNormalString, | 316 v8::String::kNormalString, |
| 322 target_extension_id.size()), | 317 target_extension_id.size()), |
| 323 // sourceUrl | 318 // sourceUrl |
| 324 v8::String::NewFromUtf8(isolate, | 319 v8::String::NewFromUtf8(isolate, source_url_spec.c_str(), |
| 325 source_url_spec.c_str(), | |
| 326 v8::String::kNormalString, | 320 v8::String::kNormalString, |
| 327 source_url_spec.size()), | 321 source_url_spec.size()), |
| 328 // tlsChannelId | 322 // tlsChannelId |
| 329 tls_channel_id_value, | 323 tls_channel_id_value, |
| 330 }; | 324 }; |
| 331 | 325 |
| 332 v8::Handle<v8::Value> retval = | 326 v8::Local<v8::Value> retval = |
| 333 script_context->module_system()->CallModuleMethod( | 327 script_context->module_system()->CallModuleMethod( |
| 334 "messaging", "dispatchOnConnect", arraysize(arguments), arguments); | 328 "messaging", "dispatchOnConnect", arraysize(arguments), arguments); |
| 335 | 329 |
| 336 if (!retval.IsEmpty()) { | 330 if (!retval.IsEmpty()) { |
| 337 CHECK(retval->IsBoolean()); | 331 CHECK(retval->IsBoolean()); |
| 338 *port_created |= retval->BooleanValue(); | 332 *port_created |= retval->BooleanValue(); |
| 339 } else { | 333 } else { |
| 340 LOG(ERROR) << "Empty return value from dispatchOnConnect."; | 334 LOG(ERROR) << "Empty return value from dispatchOnConnect."; |
| 341 } | 335 } |
| 342 } | 336 } |
| 343 | 337 |
| 344 void DeliverMessageToScriptContext(const Message& message, | 338 void DeliverMessageToScriptContext(const Message& message, |
| 345 int target_port_id, | 339 int target_port_id, |
| 346 ScriptContext* script_context) { | 340 ScriptContext* script_context) { |
| 347 v8::Isolate* isolate = script_context->isolate(); | 341 v8::Isolate* isolate = script_context->isolate(); |
| 348 v8::HandleScope handle_scope(isolate); | 342 v8::HandleScope handle_scope(isolate); |
| 349 | 343 |
| 350 // Check to see whether the context has this port before bothering to create | 344 // Check to see whether the context has this port before bothering to create |
| 351 // the message. | 345 // the message. |
| 352 v8::Handle<v8::Value> port_id_handle = | 346 v8::Local<v8::Value> port_id_handle = |
| 353 v8::Integer::New(isolate, target_port_id); | 347 v8::Integer::New(isolate, target_port_id); |
| 354 v8::Handle<v8::Value> has_port = | 348 v8::Local<v8::Value> has_port = |
| 355 script_context->module_system()->CallModuleMethod( | 349 script_context->module_system()->CallModuleMethod("messaging", "hasPort", |
| 356 "messaging", "hasPort", 1, &port_id_handle); | 350 1, &port_id_handle); |
| 357 | 351 |
| 358 CHECK(!has_port.IsEmpty()); | 352 CHECK(!has_port.IsEmpty()); |
| 359 if (!has_port->BooleanValue()) | 353 if (!has_port->BooleanValue()) |
| 360 return; | 354 return; |
| 361 | 355 |
| 362 std::vector<v8::Handle<v8::Value> > arguments; | 356 std::vector<v8::Local<v8::Value>> arguments; |
| 363 arguments.push_back(v8::String::NewFromUtf8(isolate, | 357 arguments.push_back(v8::String::NewFromUtf8(isolate, |
| 364 message.data.c_str(), | 358 message.data.c_str(), |
| 365 v8::String::kNormalString, | 359 v8::String::kNormalString, |
| 366 message.data.size())); | 360 message.data.size())); |
| 367 arguments.push_back(port_id_handle); | 361 arguments.push_back(port_id_handle); |
| 368 | 362 |
| 369 scoped_ptr<blink::WebScopedUserGesture> web_user_gesture; | 363 scoped_ptr<blink::WebScopedUserGesture> web_user_gesture; |
| 370 scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus; | 364 scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus; |
| 371 if (message.user_gesture) { | 365 if (message.user_gesture) { |
| 372 web_user_gesture.reset(new blink::WebScopedUserGesture); | 366 web_user_gesture.reset(new blink::WebScopedUserGesture); |
| 373 | 367 |
| 374 if (script_context->web_frame()) { | 368 if (script_context->web_frame()) { |
| 375 blink::WebDocument document = script_context->web_frame()->document(); | 369 blink::WebDocument document = script_context->web_frame()->document(); |
| 376 allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator( | 370 allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator( |
| 377 &document)); | 371 &document)); |
| 378 } | 372 } |
| 379 } | 373 } |
| 380 | 374 |
| 381 script_context->module_system()->CallModuleMethod( | 375 script_context->module_system()->CallModuleMethod( |
| 382 "messaging", "dispatchOnMessage", &arguments); | 376 "messaging", "dispatchOnMessage", &arguments); |
| 383 } | 377 } |
| 384 | 378 |
| 385 void DispatchOnDisconnectToScriptContext(int port_id, | 379 void DispatchOnDisconnectToScriptContext(int port_id, |
| 386 const std::string& error_message, | 380 const std::string& error_message, |
| 387 ScriptContext* script_context) { | 381 ScriptContext* script_context) { |
| 388 v8::Isolate* isolate = script_context->isolate(); | 382 v8::Isolate* isolate = script_context->isolate(); |
| 389 v8::HandleScope handle_scope(isolate); | 383 v8::HandleScope handle_scope(isolate); |
| 390 | 384 |
| 391 std::vector<v8::Handle<v8::Value> > arguments; | 385 std::vector<v8::Local<v8::Value>> arguments; |
| 392 arguments.push_back(v8::Integer::New(isolate, port_id)); | 386 arguments.push_back(v8::Integer::New(isolate, port_id)); |
| 393 if (!error_message.empty()) { | 387 if (!error_message.empty()) { |
| 394 arguments.push_back( | 388 arguments.push_back( |
| 395 v8::String::NewFromUtf8(isolate, error_message.c_str())); | 389 v8::String::NewFromUtf8(isolate, error_message.c_str())); |
| 396 } else { | 390 } else { |
| 397 arguments.push_back(v8::Null(isolate)); | 391 arguments.push_back(v8::Null(isolate)); |
| 398 } | 392 } |
| 399 | 393 |
| 400 script_context->module_system()->CallModuleMethod( | 394 script_context->module_system()->CallModuleMethod( |
| 401 "messaging", "dispatchOnDisconnect", &arguments); | 395 "messaging", "dispatchOnDisconnect", &arguments); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. | 453 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. |
| 460 content::RenderView* restrict_to_render_view = | 454 content::RenderView* restrict_to_render_view = |
| 461 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() | 455 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() |
| 462 : NULL; | 456 : NULL; |
| 463 context_set.ForEach( | 457 context_set.ForEach( |
| 464 restrict_to_render_view, | 458 restrict_to_render_view, |
| 465 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); | 459 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); |
| 466 } | 460 } |
| 467 | 461 |
| 468 } // namespace extensions | 462 } // namespace extensions |
| OLD | NEW |