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/render_frame_observer_natives.h" | 5 #include "extensions/renderer/render_frame_observer_natives.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "content/public/renderer/render_frame.h" | 10 #include "content/public/renderer/render_frame.h" |
11 #include "content/public/renderer/render_frame_observer.h" | 11 #include "content/public/renderer/render_frame_observer.h" |
12 #include "extensions/renderer/extension_frame_helper.h" | 12 #include "extensions/renderer/extension_frame_helper.h" |
13 #include "extensions/renderer/script_context.h" | 13 #include "extensions/renderer/script_context.h" |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Deletes itself when done. | 19 // Deletes itself when done. |
20 class LoadWatcher : public content::RenderFrameObserver { | 20 class LoadWatcher : public content::RenderFrameObserver { |
21 public: | 21 public: |
22 LoadWatcher(ScriptContext* context, | 22 LoadWatcher(content::RenderFrame* frame, base::Callback<void(bool)> callback) |
Devlin
2016/02/10 19:04:25
nit: const& on the callback.
robwu
2016/02/10 20:18:52
Done.
| |
23 content::RenderFrame* frame, | 23 : content::RenderFrameObserver(frame), callback_(callback) {} |
24 v8::Local<v8::Function> cb) | 24 |
25 : content::RenderFrameObserver(frame), | 25 void DidCreateDocumentElement() override { |
26 context_(context), | 26 // The callback must be run as soon as the root element is available. |
27 callback_(context->isolate(), cb) { | 27 // Running the callback may trigger DidCreateDocumentElement or |
28 if (ExtensionFrameHelper::Get(frame)-> | 28 // DidFailProvisionalLoad, so delete this before running the callback. |
29 did_create_current_document_element()) { | 29 base::Callback<void(bool)> callback = callback_; |
30 // If the document element is already created, then we can call the | 30 delete this; |
31 // callback immediately (though post it to the message loop so as to not | 31 callback.Run(true); |
32 // call it re-entrantly). | |
33 // The Unretained is safe because this class manages its own lifetime. | |
34 base::MessageLoop::current()->PostTask( | |
35 FROM_HERE, | |
36 base::Bind(&LoadWatcher::CallbackAndDie, base::Unretained(this), | |
37 true)); | |
38 } | |
39 } | 32 } |
40 | 33 |
41 void DidCreateDocumentElement() override { CallbackAndDie(true); } | |
42 | |
43 void DidFailProvisionalLoad(const blink::WebURLError& error) override { | 34 void DidFailProvisionalLoad(const blink::WebURLError& error) override { |
44 CallbackAndDie(false); | 35 // Use PostTask to avoid running user scripts while handling this |
36 // DidFailProvisionalLoad notification. | |
37 base::MessageLoop::current()->PostTask(FROM_HERE, | |
38 base::Bind(callback_, false)); | |
39 delete this; | |
45 } | 40 } |
46 | 41 |
47 private: | 42 private: |
48 void CallbackAndDie(bool succeeded) { | 43 base::Callback<void(bool)> callback_; |
49 v8::Isolate* isolate = context_->isolate(); | |
50 v8::HandleScope handle_scope(isolate); | |
51 v8::Local<v8::Value> args[] = {v8::Boolean::New(isolate, succeeded)}; | |
52 context_->CallFunction(v8::Local<v8::Function>::New(isolate, callback_), | |
53 arraysize(args), args); | |
54 delete this; | |
55 } | |
56 | |
57 ScriptContext* context_; | |
58 v8::Global<v8::Function> callback_; | |
59 | 44 |
60 DISALLOW_COPY_AND_ASSIGN(LoadWatcher); | 45 DISALLOW_COPY_AND_ASSIGN(LoadWatcher); |
61 }; | 46 }; |
62 | 47 |
63 } // namespace | 48 } // namespace |
64 | 49 |
65 RenderFrameObserverNatives::RenderFrameObserverNatives(ScriptContext* context) | 50 RenderFrameObserverNatives::RenderFrameObserverNatives(ScriptContext* context) |
66 : ObjectBackedNativeHandler(context) { | 51 : ObjectBackedNativeHandler(context), weak_ptr_factory_(this) { |
67 RouteFunction( | 52 RouteFunction( |
68 "OnDocumentElementCreated", | 53 "OnDocumentElementCreated", |
69 base::Bind(&RenderFrameObserverNatives::OnDocumentElementCreated, | 54 base::Bind(&RenderFrameObserverNatives::OnDocumentElementCreated, |
70 base::Unretained(this))); | 55 base::Unretained(this))); |
71 } | 56 } |
72 | 57 |
58 RenderFrameObserverNatives::~RenderFrameObserverNatives() {} | |
59 | |
73 void RenderFrameObserverNatives::OnDocumentElementCreated( | 60 void RenderFrameObserverNatives::OnDocumentElementCreated( |
74 const v8::FunctionCallbackInfo<v8::Value>& args) { | 61 const v8::FunctionCallbackInfo<v8::Value>& args) { |
75 CHECK(args.Length() == 2); | 62 CHECK(args.Length() == 2); |
76 CHECK(args[0]->IsInt32()); | 63 CHECK(args[0]->IsInt32()); |
77 CHECK(args[1]->IsFunction()); | 64 CHECK(args[1]->IsFunction()); |
78 | 65 |
79 int frame_id = args[0]->Int32Value(); | 66 int frame_id = args[0]->Int32Value(); |
80 | 67 |
81 content::RenderFrame* frame = content::RenderFrame::FromRoutingID(frame_id); | 68 content::RenderFrame* frame = content::RenderFrame::FromRoutingID(frame_id); |
82 if (!frame) { | 69 if (!frame) { |
83 LOG(WARNING) << "No render frame found to register LoadWatcher."; | 70 LOG(WARNING) << "No render frame found to register LoadWatcher."; |
84 return; | 71 return; |
85 } | 72 } |
86 | 73 |
87 new LoadWatcher(context(), frame, args[1].As<v8::Function>()); | 74 v8::Global<v8::Function> callback(context()->isolate(), |
75 args[1].As<v8::Function>()); | |
76 if (ExtensionFrameHelper::Get(frame)->did_create_current_document_element()) { | |
77 // If the document element is already created, then we can call the callback | |
78 // immediately (though use PostTask to ensure that the callback is called | |
79 // asynchronously). | |
80 base::MessageLoop::current()->PostTask( | |
81 FROM_HERE, | |
82 base::Bind(&RenderFrameObserverNatives::InvokeCallback, | |
83 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback), | |
Devlin
2016/02/10 19:04:25
nit: I think this might be cleaner if we cache the
robwu
2016/02/10 20:18:52
Done.
| |
84 true)); | |
85 } else { | |
86 new LoadWatcher( | |
87 frame, | |
88 base::Bind(&RenderFrameObserverNatives::InvokeCallback, | |
89 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); | |
90 } | |
88 | 91 |
89 args.GetReturnValue().Set(true); | 92 args.GetReturnValue().Set(true); |
90 } | 93 } |
91 | 94 |
95 void RenderFrameObserverNatives::InvokeCallback( | |
96 v8::Global<v8::Function> callback, bool succeeded) { | |
97 v8::Isolate* isolate = context()->isolate(); | |
Devlin
2016/02/10 19:04:25
Is there any chance that the context could be inva
robwu
2016/02/10 20:18:52
After your comment, I checked again when context()
Devlin
2016/02/10 20:30:47
The explicit destruction would happen with ScriptC
| |
98 v8::HandleScope handle_scope(isolate); | |
99 v8::Local<v8::Value> args[] = {v8::Boolean::New(isolate, succeeded)}; | |
100 context()->CallFunction(v8::Local<v8::Function>::New(isolate, callback), | |
101 arraysize(args), args); | |
102 } | |
103 | |
92 } // namespace extensions | 104 } // namespace extensions |
OLD | NEW |