| 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" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "content/public/child/v8_value_converter.h" | 16 #include "content/public/child/v8_value_converter.h" |
| 17 #include "content/public/common/child_process_host.h" | 17 #include "content/public/common/child_process_host.h" |
| 18 #include "content/public/renderer/render_frame.h" | 18 #include "content/public/renderer/render_frame.h" |
| 19 #include "content/public/renderer/render_thread.h" | 19 #include "content/public/renderer/render_thread.h" |
| 20 #include "extensions/common/api/messaging/message.h" | 20 #include "extensions/common/api/messaging/message.h" |
| 21 #include "extensions/common/extension_messages.h" | 21 #include "extensions/common/extension_messages.h" |
| 22 #include "extensions/common/guest_view/guest_view_constants.h" | 22 #include "extensions/common/guest_view/guest_view_constants.h" |
| 23 #include "extensions/common/manifest_handlers/externally_connectable.h" | 23 #include "extensions/common/manifest_handlers/externally_connectable.h" |
| 24 #include "extensions/renderer/dispatcher.h" | 24 #include "extensions/renderer/dispatcher.h" |
| 25 #include "extensions/renderer/event_bindings.h" | 25 #include "extensions/renderer/event_bindings.h" |
| 26 #include "extensions/renderer/object_backed_native_handler.h" | 26 #include "extensions/renderer/object_backed_native_handler.h" |
| 27 #include "extensions/renderer/scoped_persistent.h" | |
| 28 #include "extensions/renderer/script_context.h" | 27 #include "extensions/renderer/script_context.h" |
| 29 #include "extensions/renderer/script_context_set.h" | 28 #include "extensions/renderer/script_context_set.h" |
| 30 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 29 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 31 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 30 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
| 32 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 31 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
| 33 #include "third_party/WebKit/public/web/WebScopedWindowFocusAllowedIndicator.h" | 32 #include "third_party/WebKit/public/web/WebScopedWindowFocusAllowedIndicator.h" |
| 34 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 33 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| 35 #include "v8/include/v8.h" | 34 #include "v8/include/v8.h" |
| 36 | 35 |
| 37 // Message passing API example (in a content script): | 36 // Message passing API example (in a content script): |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 v8::Isolate* isolate) { | 186 v8::Isolate* isolate) { |
| 188 GCCallback* cb = new GCCallback(object, callback, isolate); | 187 GCCallback* cb = new GCCallback(object, callback, isolate); |
| 189 cb->object_.SetWeak(cb, NearDeathCallback); | 188 cb->object_.SetWeak(cb, NearDeathCallback); |
| 190 } | 189 } |
| 191 | 190 |
| 192 private: | 191 private: |
| 193 static void NearDeathCallback( | 192 static void NearDeathCallback( |
| 194 const v8::WeakCallbackData<v8::Object, GCCallback>& data) { | 193 const v8::WeakCallbackData<v8::Object, GCCallback>& data) { |
| 195 // v8 says we need to explicitly reset weak handles from their callbacks. | 194 // v8 says we need to explicitly reset weak handles from their callbacks. |
| 196 // It's not implicit as one might expect. | 195 // It's not implicit as one might expect. |
| 197 data.GetParameter()->object_.reset(); | 196 data.GetParameter()->object_.Reset(); |
| 198 base::MessageLoop::current()->PostTask( | 197 base::MessageLoop::current()->PostTask( |
| 199 FROM_HERE, | 198 FROM_HERE, |
| 200 base::Bind(&GCCallback::RunCallback, | 199 base::Bind(&GCCallback::RunCallback, |
| 201 base::Owned(data.GetParameter()))); | 200 base::Owned(data.GetParameter()))); |
| 202 } | 201 } |
| 203 | 202 |
| 204 GCCallback(v8::Handle<v8::Object> object, | 203 GCCallback(v8::Handle<v8::Object> object, |
| 205 v8::Handle<v8::Function> callback, | 204 v8::Handle<v8::Function> callback, |
| 206 v8::Isolate* isolate) | 205 v8::Isolate* isolate) |
| 207 : object_(object), callback_(callback), isolate_(isolate) {} | 206 : object_(isolate, object), |
| 207 callback_(isolate, callback), |
| 208 isolate_(isolate) {} |
| 208 | 209 |
| 209 void RunCallback() { | 210 void RunCallback() { |
| 210 v8::HandleScope handle_scope(isolate_); | 211 v8::HandleScope handle_scope(isolate_); |
| 211 v8::Handle<v8::Function> callback = callback_.NewHandle(isolate_); | 212 v8::Handle<v8::Function> callback = |
| 213 v8::Local<v8::Function>::New(isolate_, callback_); |
| 212 v8::Handle<v8::Context> context = callback->CreationContext(); | 214 v8::Handle<v8::Context> context = callback->CreationContext(); |
| 213 if (context.IsEmpty()) | 215 if (context.IsEmpty()) |
| 214 return; | 216 return; |
| 215 v8::Context::Scope context_scope(context); | 217 v8::Context::Scope context_scope(context); |
| 216 blink::WebScopedMicrotaskSuppression suppression; | 218 blink::WebScopedMicrotaskSuppression suppression; |
| 217 callback->Call(context->Global(), 0, NULL); | 219 callback->Call(context->Global(), 0, NULL); |
| 218 } | 220 } |
| 219 | 221 |
| 220 ScopedPersistent<v8::Object> object_; | 222 v8::UniquePersistent<v8::Object> object_; |
| 221 ScopedPersistent<v8::Function> callback_; | 223 v8::UniquePersistent<v8::Function> callback_; |
| 222 v8::Isolate* isolate_; | 224 v8::Isolate* isolate_; |
| 223 | 225 |
| 224 DISALLOW_COPY_AND_ASSIGN(GCCallback); | 226 DISALLOW_COPY_AND_ASSIGN(GCCallback); |
| 225 }; | 227 }; |
| 226 | 228 |
| 227 // void BindToGC(object, callback) | 229 // void BindToGC(object, callback) |
| 228 // | 230 // |
| 229 // Binds |callback| to be invoked *sometime after* |object| is garbage | 231 // Binds |callback| to be invoked *sometime after* |object| is garbage |
| 230 // collected. We don't call the method re-entrantly so as to avoid executing | 232 // collected. We don't call the method re-entrantly so as to avoid executing |
| 231 // JS in some bizarro undefined mid-GC state. | 233 // JS in some bizarro undefined mid-GC state. |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. | 446 // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*. |
| 445 content::RenderView* restrict_to_render_view = | 447 content::RenderView* restrict_to_render_view = |
| 446 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() | 448 restrict_to_render_frame ? restrict_to_render_frame->GetRenderView() |
| 447 : NULL; | 449 : NULL; |
| 448 context_set.ForEach( | 450 context_set.ForEach( |
| 449 restrict_to_render_view, | 451 restrict_to_render_view, |
| 450 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); | 452 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); |
| 451 } | 453 } |
| 452 | 454 |
| 453 } // namespace extensions | 455 } // namespace extensions |
| OLD | NEW |