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