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/location.h" |
8 #include "base/macros.h" | 9 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
10 #include "content/public/renderer/render_frame.h" | 12 #include "content/public/renderer/render_frame.h" |
11 #include "content/public/renderer/render_frame_observer.h" | 13 #include "content/public/renderer/render_frame_observer.h" |
12 #include "extensions/renderer/extension_frame_helper.h" | 14 #include "extensions/renderer/extension_frame_helper.h" |
13 #include "extensions/renderer/script_context.h" | 15 #include "extensions/renderer/script_context.h" |
14 | 16 |
15 namespace extensions { | 17 namespace extensions { |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 // Deletes itself when done. | 21 // Deletes itself when done. |
20 class LoadWatcher : public content::RenderFrameObserver { | 22 class LoadWatcher : public content::RenderFrameObserver { |
21 public: | 23 public: |
22 LoadWatcher(content::RenderFrame* frame, | 24 LoadWatcher(content::RenderFrame* frame, |
23 const base::Callback<void(bool)>& callback) | 25 const base::Callback<void(bool)>& callback) |
24 : content::RenderFrameObserver(frame), callback_(callback) {} | 26 : content::RenderFrameObserver(frame), callback_(callback) {} |
25 | 27 |
26 void DidCreateDocumentElement() override { | 28 void DidCreateDocumentElement() override { |
27 // Defer the callback instead of running it now to avoid re-entrancy caused | 29 // Defer the callback instead of running it now to avoid re-entrancy caused |
28 // by the JavaScript callback. | 30 // by the JavaScript callback. |
29 ExtensionFrameHelper::Get(render_frame()) | 31 ExtensionFrameHelper::Get(render_frame()) |
30 ->ScheduleAtDocumentStart(base::Bind(callback_, true)); | 32 ->ScheduleAtDocumentStart(base::Bind(callback_, true)); |
31 delete this; | 33 delete this; |
32 } | 34 } |
33 | 35 |
34 void DidFailProvisionalLoad(const blink::WebURLError& error) override { | 36 void DidFailProvisionalLoad(const blink::WebURLError& error) override { |
35 // Use PostTask to avoid running user scripts while handling this | 37 // Use PostTask to avoid running user scripts while handling this |
36 // DidFailProvisionalLoad notification. | 38 // DidFailProvisionalLoad notification. |
37 base::MessageLoop::current()->PostTask(FROM_HERE, | 39 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
38 base::Bind(callback_, false)); | 40 base::Bind(callback_, false)); |
39 delete this; | 41 delete this; |
40 } | 42 } |
41 | 43 |
42 private: | 44 private: |
43 base::Callback<void(bool)> callback_; | 45 base::Callback<void(bool)> callback_; |
44 | 46 |
45 DISALLOW_COPY_AND_ASSIGN(LoadWatcher); | 47 DISALLOW_COPY_AND_ASSIGN(LoadWatcher); |
46 }; | 48 }; |
47 | 49 |
48 } // namespace | 50 } // namespace |
(...skipping 29 matching lines...) Expand all Loading... |
78 | 80 |
79 v8::Global<v8::Function> v8_callback(context()->isolate(), | 81 v8::Global<v8::Function> v8_callback(context()->isolate(), |
80 args[1].As<v8::Function>()); | 82 args[1].As<v8::Function>()); |
81 base::Callback<void(bool)> callback( | 83 base::Callback<void(bool)> callback( |
82 base::Bind(&RenderFrameObserverNatives::InvokeCallback, | 84 base::Bind(&RenderFrameObserverNatives::InvokeCallback, |
83 weak_ptr_factory_.GetWeakPtr(), base::Passed(&v8_callback))); | 85 weak_ptr_factory_.GetWeakPtr(), base::Passed(&v8_callback))); |
84 if (ExtensionFrameHelper::Get(frame)->did_create_current_document_element()) { | 86 if (ExtensionFrameHelper::Get(frame)->did_create_current_document_element()) { |
85 // If the document element is already created, then we can call the callback | 87 // If the document element is already created, then we can call the callback |
86 // immediately (though use PostTask to ensure that the callback is called | 88 // immediately (though use PostTask to ensure that the callback is called |
87 // asynchronously). | 89 // asynchronously). |
88 base::MessageLoop::current()->PostTask(FROM_HERE, | 90 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
89 base::Bind(callback, true)); | 91 base::Bind(callback, true)); |
90 } else { | 92 } else { |
91 new LoadWatcher(frame, callback); | 93 new LoadWatcher(frame, callback); |
92 } | 94 } |
93 | 95 |
94 args.GetReturnValue().Set(true); | 96 args.GetReturnValue().Set(true); |
95 } | 97 } |
96 | 98 |
97 void RenderFrameObserverNatives::InvokeCallback( | 99 void RenderFrameObserverNatives::InvokeCallback( |
98 v8::Global<v8::Function> callback, | 100 v8::Global<v8::Function> callback, |
99 bool succeeded) { | 101 bool succeeded) { |
100 v8::Isolate* isolate = context()->isolate(); | 102 v8::Isolate* isolate = context()->isolate(); |
101 v8::HandleScope handle_scope(isolate); | 103 v8::HandleScope handle_scope(isolate); |
102 v8::Local<v8::Value> args[] = {v8::Boolean::New(isolate, succeeded)}; | 104 v8::Local<v8::Value> args[] = {v8::Boolean::New(isolate, succeeded)}; |
103 context()->CallFunction(v8::Local<v8::Function>::New(isolate, callback), | 105 context()->CallFunction(v8::Local<v8::Function>::New(isolate, callback), |
104 arraysize(args), args); | 106 arraysize(args), args); |
105 } | 107 } |
106 | 108 |
107 } // namespace extensions | 109 } // namespace extensions |
OLD | NEW |