OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/render_view_observer_natives.h" | 5 #include "extensions/renderer/render_view_observer_natives.h" |
6 | 6 |
7 #include "chrome/renderer/extensions/dispatcher.h" | |
8 #include "content/public/renderer/render_view.h" | 7 #include "content/public/renderer/render_view.h" |
9 #include "content/public/renderer/render_view_observer.h" | 8 #include "content/public/renderer/render_view_observer.h" |
10 #include "extensions/common/extension_api.h" | 9 #include "extensions/common/extension_api.h" |
11 #include "extensions/renderer/script_context.h" | 10 #include "extensions/renderer/script_context.h" |
12 #include "third_party/WebKit/public/web/WebFrame.h" | 11 #include "third_party/WebKit/public/web/WebFrame.h" |
13 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 12 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
14 | 13 |
15 namespace extensions { | 14 namespace extensions { |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 // Deletes itself when done. | 18 // Deletes itself when done. |
20 class LoadWatcher : public content::RenderViewObserver { | 19 class LoadWatcher : public content::RenderViewObserver { |
21 public: | 20 public: |
22 LoadWatcher(ScriptContext* context, | 21 LoadWatcher(ScriptContext* context, |
23 content::RenderView* view, | 22 content::RenderView* view, |
24 v8::Handle<v8::Function> cb) | 23 v8::Handle<v8::Function> cb) |
25 : content::RenderViewObserver(view), context_(context), callback_(cb) {} | 24 : content::RenderViewObserver(view), context_(context), callback_(cb) {} |
26 | 25 |
27 virtual void DidCreateDocumentElement(blink::WebLocalFrame* frame) OVERRIDE { | 26 virtual void DidCreateDocumentElement(blink::WebLocalFrame* frame) OVERRIDE { |
28 CallbackAndDie(true); | 27 CallbackAndDie(true); |
29 } | 28 } |
30 | 29 |
31 virtual void DidFailProvisionalLoad( | 30 virtual void DidFailProvisionalLoad(blink::WebLocalFrame* frame, |
32 blink::WebLocalFrame* frame, | 31 const blink::WebURLError& error) |
33 const blink::WebURLError& error) OVERRIDE { | 32 OVERRIDE { |
34 CallbackAndDie(false); | 33 CallbackAndDie(false); |
35 } | 34 } |
36 | 35 |
37 private: | 36 private: |
38 void CallbackAndDie(bool succeeded) { | 37 void CallbackAndDie(bool succeeded) { |
39 v8::Isolate* isolate = context_->isolate(); | 38 v8::Isolate* isolate = context_->isolate(); |
40 v8::HandleScope handle_scope(isolate); | 39 v8::HandleScope handle_scope(isolate); |
41 v8::Handle<v8::Value> args[] = { v8::Boolean::New(isolate, succeeded) }; | 40 v8::Handle<v8::Value> args[] = {v8::Boolean::New(isolate, succeeded)}; |
42 context_->CallFunction(callback_.NewHandle(isolate), 1, args); | 41 context_->CallFunction(callback_.NewHandle(isolate), 1, args); |
43 delete this; | 42 delete this; |
44 } | 43 } |
45 | 44 |
46 ScriptContext* context_; | 45 ScriptContext* context_; |
47 ScopedPersistent<v8::Function> callback_; | 46 ScopedPersistent<v8::Function> callback_; |
48 DISALLOW_COPY_AND_ASSIGN(LoadWatcher); | 47 DISALLOW_COPY_AND_ASSIGN(LoadWatcher); |
49 }; | 48 }; |
50 } // namespace | 49 } // namespace |
51 | 50 |
52 | 51 RenderViewObserverNatives::RenderViewObserverNatives(ScriptContext* context) |
53 RenderViewObserverNatives::RenderViewObserverNatives(Dispatcher* dispatcher, | 52 : ObjectBackedNativeHandler(context) { |
54 ChromeV8Context* context) | |
55 : ChromeV8Extension(dispatcher, context) { | |
56 RouteFunction("OnDocumentElementCreated", | 53 RouteFunction("OnDocumentElementCreated", |
57 base::Bind(&RenderViewObserverNatives::OnDocumentElementCreated, | 54 base::Bind(&RenderViewObserverNatives::OnDocumentElementCreated, |
58 base::Unretained(this))); | 55 base::Unretained(this))); |
59 } | 56 } |
60 | 57 |
61 void RenderViewObserverNatives::OnDocumentElementCreated( | 58 void RenderViewObserverNatives::OnDocumentElementCreated( |
62 const v8::FunctionCallbackInfo<v8::Value>& args) { | 59 const v8::FunctionCallbackInfo<v8::Value>& args) { |
63 CHECK(args.Length() == 2); | 60 CHECK(args.Length() == 2); |
64 CHECK(args[0]->IsInt32()); | 61 CHECK(args[0]->IsInt32()); |
65 CHECK(args[1]->IsFunction()); | 62 CHECK(args[1]->IsFunction()); |
66 | 63 |
67 int view_id = args[0]->Int32Value(); | 64 int view_id = args[0]->Int32Value(); |
68 | 65 |
69 content::RenderView* view = content::RenderView::FromRoutingID(view_id); | 66 content::RenderView* view = content::RenderView::FromRoutingID(view_id); |
70 if (!view) { | 67 if (!view) { |
71 LOG(WARNING) << "No render view found to register LoadWatcher."; | 68 LOG(WARNING) << "No render view found to register LoadWatcher."; |
72 return; | 69 return; |
73 } | 70 } |
74 | 71 |
75 new LoadWatcher(context(), view, args[1].As<v8::Function>()); | 72 new LoadWatcher(context(), view, args[1].As<v8::Function>()); |
76 | 73 |
77 args.GetReturnValue().Set(true); | 74 args.GetReturnValue().Set(true); |
78 } | 75 } |
79 | 76 |
80 } // namespace extensions | 77 } // namespace extensions |
OLD | NEW |