Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1052)

Side by Side Diff: chrome/renderer/extensions/render_view_observer_natives.cc

Issue 16975007: Run shim's watchForTag on document.DOMContentLoaded (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase + add test code from @kalman's comment Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/renderer/extensions/render_view_observer_natives.h"
6
7 #include "chrome/common/extensions/api/extension_api.h"
8 #include "chrome/renderer/extensions/dispatcher.h"
9 #include "content/public/renderer/render_view.h"
10 #include "content/public/renderer/render_view_observer.h"
11 #include "third_party/WebKit/public/web/WebFrame.h"
12 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
13
14 namespace extensions {
15
16 namespace {
17 class LoadWatcher : public content::RenderViewObserver {
18 public:
19 LoadWatcher(ChromeV8Context* context,
20 content::RenderView* view,
21 v8::Handle<v8::Function> cb)
22 : content::RenderViewObserver(view),
23 context_(context),
24 callback_(cb) {
25 }
26
27 virtual void DidCreateDocumentElement(WebKit::WebFrame* frame) OVERRIDE {
28 CallbackAndDie(true);
29 }
30
31 virtual void DidFailProvisionalLoad(
32 WebKit::WebFrame* frame,
33 const WebKit::WebURLError& error) OVERRIDE {
34 CallbackAndDie(false);
35 }
36
37 private:
38 ChromeV8Context* context_;
39 ScopedPersistent<v8::Function> callback_;
40
41 void CallbackAndDie(bool succeeded) {
42 v8::HandleScope handle_scope;
43 // Don't need Context::Scope, CallFunction does that for us.
44 v8::Handle<v8::Value> args[] = { v8::Boolean::New(succeeded) };
45 context_->CallFunction(callback_.get(), 1, args);
46 delete this;
47 }
48 };
49 } // namespace
50
51
52 RenderViewObserverNatives::RenderViewObserverNatives(Dispatcher* dispatcher,
53 ChromeV8Context* context)
54 : ChromeV8Extension(dispatcher, context) {
55 RouteFunction("OnDocumentCreated",
56 base::Bind(&RenderViewObserverNatives::OnDocumentCreated,
57 base::Unretained(this)));
58 RouteFunction(
59 "OnDocumentCreatedForCurrentContext",
60 base::Bind(&RenderViewObserverNatives::OnDocumentCreatedForCurrentContext,
61 base::Unretained(this)));
62 }
63
64 void RenderViewObserverNatives::OnDocumentCreated(
65 const v8::FunctionCallbackInfo<v8::Value>& args) {
66 CHECK(args.Length() == 2);
67 CHECK(args[0]->IsInt32());
68 CHECK(args[1]->IsFunction());
69
70 int view_id = args[0]->Int32Value();
71
72 content::RenderView* view = content::RenderView::FromRoutingID(view_id);
73 if (!view)
74 return;
75
76 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(args[1]);
77 new LoadWatcher(context(), view, func);
78
79 args.GetReturnValue().Set(true);
80 }
81
82 void RenderViewObserverNatives::OnDocumentCreatedForCurrentContext(
83 const v8::FunctionCallbackInfo<v8::Value>& args) {
84 CHECK(args.Length() == 1);
85 CHECK(args[0]->IsFunction());
86
87 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(args[0]);
88 new LoadWatcher(context(), context()->GetRenderView(), func);
89
90 args.GetReturnValue().Set(true);
91 }
92
93 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698