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

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

Issue 16975007: Run shim's watchForTag on document.DOMContentLoaded (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments from kalman. 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/app_window_custom_bindings.h" 5 #include "chrome/renderer/extensions/app_window_custom_bindings.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "chrome/common/extensions/extension_messages.h" 9 #include "chrome/common/extensions/extension_messages.h"
10 #include "chrome/renderer/extensions/chrome_v8_context.h" 10 #include "chrome/renderer/extensions/chrome_v8_context.h"
11 #include "chrome/renderer/extensions/dispatcher.h" 11 #include "chrome/renderer/extensions/dispatcher.h"
12 #include "chrome/renderer/extensions/scoped_persistent.h" 12 #include "chrome/renderer/extensions/scoped_persistent.h"
13 #include "content/public/renderer/render_thread.h" 13 #include "content/public/renderer/render_thread.h"
14 #include "content/public/renderer/render_view.h" 14 #include "content/public/renderer/render_view.h"
15 #include "content/public/renderer/render_view_observer.h" 15 #include "content/public/renderer/render_view_observer.h"
16 #include "content/public/renderer/render_view_visitor.h" 16 #include "content/public/renderer/render_view_visitor.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup pression.h"
20 #include "v8/include/v8.h" 19 #include "v8/include/v8.h"
21 20
22 namespace extensions { 21 namespace extensions {
23 22
24 class DidCreateDocumentElementObserver : public content::RenderViewObserver { 23 class DidCreateDocumentElementObserver : public content::RenderViewObserver {
25 public: 24 public:
26 DidCreateDocumentElementObserver( 25 DidCreateDocumentElementObserver(
27 content::RenderView* view, Dispatcher* dispatcher) 26 content::RenderView* view, Dispatcher* dispatcher)
28 : content::RenderViewObserver(view), dispatcher_(dispatcher) { 27 : content::RenderViewObserver(view), dispatcher_(dispatcher) {
29 } 28 }
(...skipping 19 matching lines...) Expand all
49 private: 48 private:
50 Dispatcher* dispatcher_; 49 Dispatcher* dispatcher_;
51 }; 50 };
52 51
53 AppWindowCustomBindings::AppWindowCustomBindings( 52 AppWindowCustomBindings::AppWindowCustomBindings(
54 Dispatcher* dispatcher, 53 Dispatcher* dispatcher,
55 ChromeV8Context* context) : ChromeV8Extension(dispatcher, context) { 54 ChromeV8Context* context) : ChromeV8Extension(dispatcher, context) {
56 RouteFunction("GetView", 55 RouteFunction("GetView",
57 base::Bind(&AppWindowCustomBindings::GetView, 56 base::Bind(&AppWindowCustomBindings::GetView,
58 base::Unretained(this))); 57 base::Unretained(this)));
59 RouteFunction("OnContextReady",
60 base::Bind(&AppWindowCustomBindings::OnContextReady,
61 base::Unretained(this)));
62 }
63
64 namespace {
65 class LoadWatcher : public content::RenderViewObserver {
66 public:
67 LoadWatcher(v8::Isolate* isolate,
68 content::RenderView* view,
69 v8::Handle<v8::Function> cb)
70 : content::RenderViewObserver(view),
71 callback_(cb) {
72 }
73
74 virtual void DidCreateDocumentElement(WebKit::WebFrame* frame) OVERRIDE {
75 CallbackAndDie(frame, true);
76 }
77
78 virtual void DidFailProvisionalLoad(
79 WebKit::WebFrame* frame,
80 const WebKit::WebURLError& error) OVERRIDE {
81 CallbackAndDie(frame, false);
82 }
83
84 private:
85 ScopedPersistent<v8::Function> callback_;
86
87 void CallbackAndDie(WebKit::WebFrame* frame, bool succeeded) {
88 v8::HandleScope handle_scope;
89 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
90 v8::Context::Scope scope(context);
91 v8::Local<v8::Object> global = context->Global();
92 {
93 WebKit::WebScopedMicrotaskSuppression suppression;
94 v8::Handle<v8::Value> args[] = {
95 succeeded ? v8::True() : v8::False()
96 };
97 callback_->Call(global, 1, args);
98 }
99 delete this;
100 }
101 };
102 } // namespace
103
104 v8::Handle<v8::Value> AppWindowCustomBindings::OnContextReady(
105 const v8::Arguments& args) {
106 if (args.Length() != 2)
107 return v8::Undefined();
108
109 if (!args[0]->IsInt32())
110 return v8::Undefined();
111 if (!args[1]->IsFunction())
112 return v8::Undefined();
113
114 int view_id = args[0]->Int32Value();
115
116 content::RenderView* view = content::RenderView::FromRoutingID(view_id);
117 if (!view)
118 return v8::Undefined();
119
120 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(args[1]);
121 new LoadWatcher(args.GetIsolate(), view, func);
122
123 return v8::True();
124 } 58 }
125 59
126 v8::Handle<v8::Value> AppWindowCustomBindings::GetView( 60 v8::Handle<v8::Value> AppWindowCustomBindings::GetView(
127 const v8::Arguments& args) { 61 const v8::Arguments& args) {
128 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn 62 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn
129 // these argument checks into CHECK(). 63 // these argument checks into CHECK().
130 if (args.Length() != 2) 64 if (args.Length() != 2)
131 return v8::Undefined(); 65 return v8::Undefined();
132 66
133 if (!args[0]->IsInt32()) 67 if (!args[0]->IsInt32())
(...skipping 27 matching lines...) Expand all
161 WebKit::WebFrame* frame = view->GetWebView()->mainFrame(); 95 WebKit::WebFrame* frame = view->GetWebView()->mainFrame();
162 frame->setOpener(opener); 96 frame->setOpener(opener);
163 content::RenderThread::Get()->Send( 97 content::RenderThread::Get()->Send(
164 new ExtensionHostMsg_ResumeRequests(view->GetRoutingID())); 98 new ExtensionHostMsg_ResumeRequests(view->GetRoutingID()));
165 99
166 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global(); 100 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global();
167 return window; 101 return window;
168 } 102 }
169 103
170 } // namespace extensions 104 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698