Chromium Code Reviews| Index: chrome/renderer/extensions/render_view_observer_natives.cc |
| diff --git a/chrome/renderer/extensions/render_view_observer_natives.cc b/chrome/renderer/extensions/render_view_observer_natives.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f9bccaaf5a04abbf44f7cf2b4e66ed8ca012e33c |
| --- /dev/null |
| +++ b/chrome/renderer/extensions/render_view_observer_natives.cc |
| @@ -0,0 +1,102 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/renderer/extensions/render_view_observer_natives.h" |
| + |
| +#include "chrome/common/extensions/api/extension_api.h" |
| +#include "chrome/renderer/extensions/dispatcher.h" |
| +#include "content/public/renderer/render_view.h" |
| +#include "content/public/renderer/render_view_observer.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSuppression.h" |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| +class LoadWatcher : public content::RenderViewObserver { |
| + public: |
| + LoadWatcher(content::RenderView* view, |
| + v8::Handle<v8::Function> cb) |
| + : content::RenderViewObserver(view), |
| + callback_(cb) { |
| + } |
| + |
| + virtual void DidCreateDocumentElement(WebKit::WebFrame* frame) OVERRIDE { |
| + CallbackAndDie(frame, true); |
| + } |
| + |
| + virtual void DidFailProvisionalLoad( |
| + WebKit::WebFrame* frame, |
| + const WebKit::WebURLError& error) OVERRIDE { |
| + CallbackAndDie(frame, false); |
| + } |
| + |
| + private: |
| + ScopedPersistent<v8::Function> callback_; |
| + |
| + void CallbackAndDie(WebKit::WebFrame* frame, bool succeeded) { |
| + v8::HandleScope handle_scope; |
| + v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
|
not at google - send to devlin
2013/06/14 00:58:32
I suspect it has something to do with this context
|
| + v8::Context::Scope scope(context); |
| + v8::Local<v8::Object> global = context->Global(); |
| + { |
| + WebKit::WebScopedMicrotaskSuppression suppression; |
| + v8::Handle<v8::Value> args[] = { |
| + succeeded ? v8::True() : v8::False() |
| + }; |
| + callback_->Call(global, 1, args); |
|
not at google - send to devlin
2013/06/14 05:26:27
To answer your comment:
The problem I think is he
lazyboy
2013/06/14 16:23:32
Why are there two frames? My assumption was the ch
not at google - send to devlin
2013/06/14 16:45:27
The first frame is the one that created the app wi
|
| + } |
| + delete this; |
| + } |
| +}; |
| +} // namespace |
| + |
| + |
| +RenderViewObserverNatives::RenderViewObserverNatives(Dispatcher* dispatcher, |
| + ChromeV8Context* context) |
| + : ChromeV8Extension(dispatcher, context) { |
| + RouteFunction("OnDocumentCreated", |
| + base::Bind(&RenderViewObserverNatives::OnDocumentCreated, |
| + base::Unretained(this))); |
| + RouteFunction( |
| + "OnDocumentCreatedForCurrentContext", |
| + base::Bind(&RenderViewObserverNatives::OnDocumentCreatedForCurrentContext, |
| + base::Unretained(this))); |
| +} |
| + |
| +v8::Handle<v8::Value> RenderViewObserverNatives::OnDocumentCreated( |
| + const v8::Arguments& args) { |
| + CHECK(args.Length() == 2); |
| + CHECK(args[0]->IsInt32()); |
| + CHECK(args[1]->IsFunction()); |
| + |
| + int view_id = args[0]->Int32Value(); |
| + |
| + content::RenderView* view = content::RenderView::FromRoutingID(view_id); |
| + if (!view) |
| + return v8::Undefined(); |
| + |
| + v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(args[1]); |
| + new LoadWatcher(view, func); |
| + |
| + return v8::True(); |
| +} |
| + |
| +v8::Handle<v8::Value> |
| +RenderViewObserverNatives::OnDocumentCreatedForCurrentContext( |
| + const v8::Arguments& args) { |
| + CHECK(args.Length() == 1); |
| + CHECK(args[0]->IsFunction()); |
| + |
| + content::RenderView* view = context()->GetRenderView(); |
| + if (!view) |
| + return v8::Undefined(); |
| + |
| + v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(args[0]); |
| + new LoadWatcher(view, func); |
| + |
| + return v8::True(); |
| +} |
| + |
| +} // namespace extensions |