OLD | NEW |
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" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 : ChromeV8Extension(dispatcher) { | 53 : ChromeV8Extension(dispatcher) { |
54 RouteFunction("GetView", | 54 RouteFunction("GetView", |
55 base::Bind(&AppWindowCustomBindings::GetView, | 55 base::Bind(&AppWindowCustomBindings::GetView, |
56 base::Unretained(this))); | 56 base::Unretained(this))); |
57 RouteFunction("OnContextReady", | 57 RouteFunction("OnContextReady", |
58 base::Bind(&AppWindowCustomBindings::OnContextReady, | 58 base::Bind(&AppWindowCustomBindings::OnContextReady, |
59 base::Unretained(this))); | 59 base::Unretained(this))); |
60 } | 60 } |
61 | 61 |
62 namespace { | 62 namespace { |
63 class FindViewByID : public content::RenderViewVisitor { | |
64 public: | |
65 explicit FindViewByID(int route_id) : route_id_(route_id), view_(NULL) { | |
66 } | |
67 | |
68 content::RenderView* view() { return view_; } | |
69 | |
70 // Returns false to terminate the iteration. | |
71 virtual bool Visit(content::RenderView* render_view) { | |
72 if (render_view->GetRoutingID() == route_id_) { | |
73 view_ = render_view; | |
74 return false; | |
75 } | |
76 return true; | |
77 } | |
78 | |
79 private: | |
80 int route_id_; | |
81 content::RenderView* view_; | |
82 }; | |
83 | |
84 class LoadWatcher : public content::RenderViewObserver { | 63 class LoadWatcher : public content::RenderViewObserver { |
85 public: | 64 public: |
86 LoadWatcher( | 65 LoadWatcher( |
87 content::RenderView* view, v8::Handle<v8::Function> cb) | 66 content::RenderView* view, v8::Handle<v8::Function> cb) |
88 : content::RenderViewObserver(view), | 67 : content::RenderViewObserver(view), |
89 callback_(v8::Persistent<v8::Function>::New(cb)) { | 68 callback_(v8::Persistent<v8::Function>::New(cb)) { |
90 } | 69 } |
91 | 70 |
92 virtual void DidCreateDocumentElement(WebKit::WebFrame* frame) OVERRIDE { | 71 virtual void DidCreateDocumentElement(WebKit::WebFrame* frame) OVERRIDE { |
93 CallbackAndDie(frame, true); | 72 CallbackAndDie(frame, true); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 if (args.Length() != 2) | 106 if (args.Length() != 2) |
128 return v8::Undefined(); | 107 return v8::Undefined(); |
129 | 108 |
130 if (!args[0]->IsInt32()) | 109 if (!args[0]->IsInt32()) |
131 return v8::Undefined(); | 110 return v8::Undefined(); |
132 if (!args[1]->IsFunction()) | 111 if (!args[1]->IsFunction()) |
133 return v8::Undefined(); | 112 return v8::Undefined(); |
134 | 113 |
135 int view_id = args[0]->Int32Value(); | 114 int view_id = args[0]->Int32Value(); |
136 | 115 |
137 FindViewByID view_finder(view_id); | 116 content::RenderView* view = content::RenderView::FromRoutingID(view_id); |
138 content::RenderView::ForEach(&view_finder); | |
139 content::RenderView* view = view_finder.view(); | |
140 if (!view) | 117 if (!view) |
141 return v8::Undefined(); | 118 return v8::Undefined(); |
142 | 119 |
143 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(args[1]); | 120 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(args[1]); |
144 new LoadWatcher(view, func); | 121 new LoadWatcher(view, func); |
145 | 122 |
146 return v8::True(); | 123 return v8::True(); |
147 } | 124 } |
148 | 125 |
149 v8::Handle<v8::Value> AppWindowCustomBindings::GetView( | 126 v8::Handle<v8::Value> AppWindowCustomBindings::GetView( |
150 const v8::Arguments& args) { | 127 const v8::Arguments& args) { |
151 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn | 128 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn |
152 // these argument checks into CHECK(). | 129 // these argument checks into CHECK(). |
153 if (args.Length() != 2) | 130 if (args.Length() != 2) |
154 return v8::Undefined(); | 131 return v8::Undefined(); |
155 | 132 |
156 if (!args[0]->IsInt32()) | 133 if (!args[0]->IsInt32()) |
157 return v8::Undefined(); | 134 return v8::Undefined(); |
158 | 135 |
159 if (!args[1]->IsBoolean()) | 136 if (!args[1]->IsBoolean()) |
160 return v8::Undefined(); | 137 return v8::Undefined(); |
161 | 138 |
162 int view_id = args[0]->Int32Value(); | 139 int view_id = args[0]->Int32Value(); |
163 | 140 |
164 bool inject_titlebar = args[1]->BooleanValue(); | 141 bool inject_titlebar = args[1]->BooleanValue(); |
165 | 142 |
166 if (view_id == MSG_ROUTING_NONE) | 143 if (view_id == MSG_ROUTING_NONE) |
167 return v8::Undefined(); | 144 return v8::Undefined(); |
168 | 145 |
169 // TODO(jeremya): there exists a direct map of routing_id -> RenderView as | 146 content::RenderView* view = content::RenderView::FromRoutingID(view_id); |
170 // ChildThread::current()->ResolveRoute(), but ResolveRoute returns an | |
171 // IPC::Listener*, which RenderView doesn't inherit from (RenderViewImpl | |
172 // does, indirectly, via RenderWidget, but the link isn't exposed outside of | |
173 // content/.) | |
174 FindViewByID view_finder(view_id); | |
175 content::RenderView::ForEach(&view_finder); | |
176 content::RenderView* view = view_finder.view(); | |
177 if (!view) | 147 if (!view) |
178 return v8::Undefined(); | 148 return v8::Undefined(); |
179 | 149 |
180 if (inject_titlebar) | 150 if (inject_titlebar) |
181 new DidCreateDocumentElementObserver(view, dispatcher()); | 151 new DidCreateDocumentElementObserver(view, dispatcher()); |
182 | 152 |
183 // TODO(jeremya): it doesn't really make sense to set the opener here, but we | 153 // TODO(jeremya): it doesn't really make sense to set the opener here, but we |
184 // need to make sure the security origin is set up before returning the DOM | 154 // need to make sure the security origin is set up before returning the DOM |
185 // reference. A better way to do this would be to have the browser pass the | 155 // reference. A better way to do this would be to have the browser pass the |
186 // opener through so opener_id is set in RenderViewImpl's constructor. | 156 // opener through so opener_id is set in RenderViewImpl's constructor. |
187 content::RenderView* render_view = GetCurrentRenderView(); | 157 content::RenderView* render_view = GetCurrentRenderView(); |
188 if (!render_view) | 158 if (!render_view) |
189 return v8::Undefined(); | 159 return v8::Undefined(); |
190 WebKit::WebFrame* opener = render_view->GetWebView()->mainFrame(); | 160 WebKit::WebFrame* opener = render_view->GetWebView()->mainFrame(); |
191 WebKit::WebFrame* frame = view->GetWebView()->mainFrame(); | 161 WebKit::WebFrame* frame = view->GetWebView()->mainFrame(); |
192 frame->setOpener(opener); | 162 frame->setOpener(opener); |
193 content::RenderThread::Get()->Send( | 163 content::RenderThread::Get()->Send( |
194 new ExtensionHostMsg_ResumeRequests(view->GetRoutingID())); | 164 new ExtensionHostMsg_ResumeRequests(view->GetRoutingID())); |
195 | 165 |
196 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global(); | 166 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global(); |
197 return window; | 167 return window; |
198 } | 168 } |
199 | 169 |
200 } // namespace extensions | 170 } // namespace extensions |
OLD | NEW |