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/extension_helper.h" | 5 #include "chrome/renderer/extensions/extension_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/extensions/extension_messages.h" | 15 #include "chrome/common/extensions/extension_messages.h" |
16 #include "chrome/common/render_messages.h" | 16 #include "chrome/common/render_messages.h" |
17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
18 #include "chrome/common/view_type.h" | 18 #include "chrome/common/view_type.h" |
19 #include "chrome/renderer/extensions/chrome_v8_context.h" | 19 #include "chrome/renderer/extensions/chrome_v8_context.h" |
20 #include "chrome/renderer/extensions/extension_dispatcher.h" | 20 #include "chrome/renderer/extensions/dispatcher.h" |
21 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | 21 #include "chrome/renderer/extensions/miscellaneous_bindings.h" |
22 #include "chrome/renderer/extensions/user_script_scheduler.h" | 22 #include "chrome/renderer/extensions/user_script_scheduler.h" |
23 #include "chrome/renderer/extensions/user_script_slave.h" | 23 #include "chrome/renderer/extensions/user_script_slave.h" |
24 #include "content/public/renderer/render_view.h" | 24 #include "content/public/renderer/render_view.h" |
25 #include "content/public/renderer/render_view_visitor.h" | 25 #include "content/public/renderer/render_view_visitor.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedUserGesture.
h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedUserGesture.
h" |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
32 #include "webkit/glue/image_resource_fetcher.h" | 32 #include "webkit/glue/image_resource_fetcher.h" |
33 #include "webkit/glue/resource_fetcher.h" | 33 #include "webkit/glue/resource_fetcher.h" |
34 | 34 |
35 namespace base { | 35 namespace base { |
36 class ListValue; | 36 class ListValue; |
37 } // namespace base | 37 } // namespace base |
38 | 38 |
39 using content::ConsoleMessageLevel; | 39 using content::ConsoleMessageLevel; |
40 using extensions::MiscellaneousBindings; | |
41 using WebKit::WebConsoleMessage; | 40 using WebKit::WebConsoleMessage; |
42 using WebKit::WebDataSource; | 41 using WebKit::WebDataSource; |
43 using WebKit::WebFrame; | 42 using WebKit::WebFrame; |
44 using WebKit::WebURLRequest; | 43 using WebKit::WebURLRequest; |
45 using WebKit::WebScopedUserGesture; | 44 using WebKit::WebScopedUserGesture; |
46 using WebKit::WebView; | 45 using WebKit::WebView; |
47 using webkit_glue::ImageResourceFetcher; | 46 using webkit_glue::ImageResourceFetcher; |
48 using webkit_glue::ResourceFetcher; | 47 using webkit_glue::ResourceFetcher; |
49 | 48 |
| 49 namespace extensions { |
| 50 |
50 namespace { | 51 namespace { |
51 // Keeps a mapping from the frame pointer to a UserScriptScheduler object. | 52 // Keeps a mapping from the frame pointer to a UserScriptScheduler object. |
52 // We store this mapping per process, because a frame can jump from one | 53 // We store this mapping per process, because a frame can jump from one |
53 // document to another with adoptNode, and so having the object be a | 54 // document to another with adoptNode, and so having the object be a |
54 // RenderViewObserver means it might miss some notifications after it moves. | 55 // RenderViewObserver means it might miss some notifications after it moves. |
55 typedef std::map<WebFrame*, extensions::UserScriptScheduler*> SchedulerMap; | 56 typedef std::map<WebFrame*, UserScriptScheduler*> SchedulerMap; |
56 static base::LazyInstance<SchedulerMap> g_schedulers = | 57 static base::LazyInstance<SchedulerMap> g_schedulers = |
57 LAZY_INSTANCE_INITIALIZER; | 58 LAZY_INSTANCE_INITIALIZER; |
58 | 59 |
59 // A RenderViewVisitor class that iterates through the set of available | 60 // A RenderViewVisitor class that iterates through the set of available |
60 // views, looking for a view of the given type, in the given browser window | 61 // views, looking for a view of the given type, in the given browser window |
61 // and within the given extension. | 62 // and within the given extension. |
62 // Used to accumulate the list of views associated with an extension. | 63 // Used to accumulate the list of views associated with an extension. |
63 class ExtensionViewAccumulator : public content::RenderViewVisitor { | 64 class ViewAccumulator : public content::RenderViewVisitor { |
64 public: | 65 public: |
65 ExtensionViewAccumulator(const std::string& extension_id, | 66 ViewAccumulator(const std::string& extension_id, |
66 int browser_window_id, | 67 int browser_window_id, |
67 chrome::ViewType view_type) | 68 chrome::ViewType view_type) |
68 : extension_id_(extension_id), | 69 : extension_id_(extension_id), |
69 browser_window_id_(browser_window_id), | 70 browser_window_id_(browser_window_id), |
70 view_type_(view_type) { | 71 view_type_(view_type) { |
71 } | 72 } |
72 | 73 |
73 std::vector<content::RenderView*> views() { return views_; } | 74 std::vector<content::RenderView*> views() { return views_; } |
74 | 75 |
75 // Returns false to terminate the iteration. | 76 // Returns false to terminate the iteration. |
76 virtual bool Visit(content::RenderView* render_view) { | 77 virtual bool Visit(content::RenderView* render_view) { |
77 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | 78 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 std::vector<content::RenderView*> views_; | 117 std::vector<content::RenderView*> views_; |
117 }; | 118 }; |
118 | 119 |
119 } // namespace | 120 } // namespace |
120 | 121 |
121 // static | 122 // static |
122 std::vector<content::RenderView*> ExtensionHelper::GetExtensionViews( | 123 std::vector<content::RenderView*> ExtensionHelper::GetExtensionViews( |
123 const std::string& extension_id, | 124 const std::string& extension_id, |
124 int browser_window_id, | 125 int browser_window_id, |
125 chrome::ViewType view_type) { | 126 chrome::ViewType view_type) { |
126 ExtensionViewAccumulator accumulator( | 127 ViewAccumulator accumulator(extension_id, browser_window_id, view_type); |
127 extension_id, browser_window_id, view_type); | |
128 content::RenderView::ForEach(&accumulator); | 128 content::RenderView::ForEach(&accumulator); |
129 return accumulator.views(); | 129 return accumulator.views(); |
130 } | 130 } |
131 | 131 |
132 // static | 132 // static |
133 content::RenderView* ExtensionHelper::GetBackgroundPage( | 133 content::RenderView* ExtensionHelper::GetBackgroundPage( |
134 const std::string& extension_id) { | 134 const std::string& extension_id) { |
135 ExtensionViewAccumulator accumulator( | 135 ViewAccumulator accumulator(extension_id, extension_misc::kUnknownWindowId, |
136 extension_id, extension_misc::kUnknownWindowId, | 136 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
137 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | |
138 content::RenderView::ForEach(&accumulator); | 137 content::RenderView::ForEach(&accumulator); |
139 CHECK_LE(accumulator.views().size(), 1u); | 138 CHECK_LE(accumulator.views().size(), 1u); |
140 if (accumulator.views().size() == 0) | 139 if (accumulator.views().size() == 0) |
141 return NULL; | 140 return NULL; |
142 return accumulator.views()[0]; | 141 return accumulator.views()[0]; |
143 } | 142 } |
144 | 143 |
145 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, | 144 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, |
146 ExtensionDispatcher* extension_dispatcher) | 145 Dispatcher* dispatcher) |
147 : content::RenderViewObserver(render_view), | 146 : content::RenderViewObserver(render_view), |
148 content::RenderViewObserverTracker<ExtensionHelper>(render_view), | 147 content::RenderViewObserverTracker<ExtensionHelper>(render_view), |
149 extension_dispatcher_(extension_dispatcher), | 148 dispatcher_(dispatcher), |
150 pending_app_icon_requests_(0), | 149 pending_app_icon_requests_(0), |
151 view_type_(chrome::VIEW_TYPE_INVALID), | 150 view_type_(chrome::VIEW_TYPE_INVALID), |
152 tab_id_(-1), | 151 tab_id_(-1), |
153 browser_window_id_(-1) { | 152 browser_window_id_(-1) { |
154 } | 153 } |
155 | 154 |
156 ExtensionHelper::~ExtensionHelper() { | 155 ExtensionHelper::~ExtensionHelper() { |
157 } | 156 } |
158 | 157 |
159 bool ExtensionHelper::InstallWebApplicationUsingDefinitionFile( | 158 bool ExtensionHelper::InstallWebApplicationUsingDefinitionFile( |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 IPC_MESSAGE_HANDLER(ExtensionMsg_NotifyRenderViewType, | 220 IPC_MESSAGE_HANDLER(ExtensionMsg_NotifyRenderViewType, |
222 OnNotifyRendererViewType) | 221 OnNotifyRendererViewType) |
223 IPC_MESSAGE_HANDLER(ExtensionMsg_AddMessageToConsole, | 222 IPC_MESSAGE_HANDLER(ExtensionMsg_AddMessageToConsole, |
224 OnAddMessageToConsole) | 223 OnAddMessageToConsole) |
225 IPC_MESSAGE_UNHANDLED(handled = false) | 224 IPC_MESSAGE_UNHANDLED(handled = false) |
226 IPC_END_MESSAGE_MAP() | 225 IPC_END_MESSAGE_MAP() |
227 return handled; | 226 return handled; |
228 } | 227 } |
229 | 228 |
230 void ExtensionHelper::DidFinishDocumentLoad(WebFrame* frame) { | 229 void ExtensionHelper::DidFinishDocumentLoad(WebFrame* frame) { |
231 extension_dispatcher_->user_script_slave()->InjectScripts( | 230 dispatcher_->user_script_slave()->InjectScripts( |
232 frame, extensions::UserScript::DOCUMENT_END); | 231 frame, UserScript::DOCUMENT_END); |
233 | 232 |
234 SchedulerMap::iterator i = g_schedulers.Get().find(frame); | 233 SchedulerMap::iterator i = g_schedulers.Get().find(frame); |
235 if (i != g_schedulers.Get().end()) | 234 if (i != g_schedulers.Get().end()) |
236 i->second->DidFinishDocumentLoad(); | 235 i->second->DidFinishDocumentLoad(); |
237 } | 236 } |
238 | 237 |
239 void ExtensionHelper::DidFinishLoad(WebKit::WebFrame* frame) { | 238 void ExtensionHelper::DidFinishLoad(WebKit::WebFrame* frame) { |
240 SchedulerMap::iterator i = g_schedulers.Get().find(frame); | 239 SchedulerMap::iterator i = g_schedulers.Get().find(frame); |
241 if (i != g_schedulers.Get().end()) | 240 if (i != g_schedulers.Get().end()) |
242 i->second->DidFinishLoad(); | 241 i->second->DidFinishLoad(); |
243 } | 242 } |
244 | 243 |
245 void ExtensionHelper::DidCreateDocumentElement(WebFrame* frame) { | 244 void ExtensionHelper::DidCreateDocumentElement(WebFrame* frame) { |
246 extension_dispatcher_->user_script_slave()->InjectScripts( | 245 dispatcher_->user_script_slave()->InjectScripts( |
247 frame, extensions::UserScript::DOCUMENT_START); | 246 frame, UserScript::DOCUMENT_START); |
248 SchedulerMap::iterator i = g_schedulers.Get().find(frame); | 247 SchedulerMap::iterator i = g_schedulers.Get().find(frame); |
249 if (i != g_schedulers.Get().end()) | 248 if (i != g_schedulers.Get().end()) |
250 i->second->DidCreateDocumentElement(); | 249 i->second->DidCreateDocumentElement(); |
251 } | 250 } |
252 | 251 |
253 void ExtensionHelper::DidStartProvisionalLoad(WebKit::WebFrame* frame) { | 252 void ExtensionHelper::DidStartProvisionalLoad(WebKit::WebFrame* frame) { |
254 SchedulerMap::iterator i = g_schedulers.Get().find(frame); | 253 SchedulerMap::iterator i = g_schedulers.Get().find(frame); |
255 if (i != g_schedulers.Get().end()) | 254 if (i != g_schedulers.Get().end()) |
256 i->second->DidStartProvisionalLoad(); | 255 i->second->DidStartProvisionalLoad(); |
257 } | 256 } |
(...skipping 15 matching lines...) Expand all Loading... |
273 if (!frame->parent()) { | 272 if (!frame->parent()) { |
274 app_icon_fetchers_.clear(); | 273 app_icon_fetchers_.clear(); |
275 app_definition_fetcher_.reset(NULL); | 274 app_definition_fetcher_.reset(NULL); |
276 } | 275 } |
277 | 276 |
278 // Check first if we created a scheduler for the frame, since this function | 277 // Check first if we created a scheduler for the frame, since this function |
279 // gets called for navigations within the document. | 278 // gets called for navigations within the document. |
280 if (g_schedulers.Get().count(frame)) | 279 if (g_schedulers.Get().count(frame)) |
281 return; | 280 return; |
282 | 281 |
283 g_schedulers.Get()[frame] = new extensions::UserScriptScheduler( | 282 g_schedulers.Get()[frame] = new UserScriptScheduler(frame, dispatcher_); |
284 frame, extension_dispatcher_); | |
285 } | 283 } |
286 | 284 |
287 void ExtensionHelper::OnExtensionResponse(int request_id, | 285 void ExtensionHelper::OnExtensionResponse(int request_id, |
288 bool success, | 286 bool success, |
289 const base::ListValue& response, | 287 const base::ListValue& response, |
290 const std::string& error) { | 288 const std::string& error) { |
291 extension_dispatcher_->OnExtensionResponse(request_id, | 289 dispatcher_->OnExtensionResponse(request_id, |
292 success, | 290 success, |
293 response, | 291 response, |
294 error); | 292 error); |
295 } | 293 } |
296 | 294 |
297 void ExtensionHelper::OnExtensionMessageInvoke(const std::string& extension_id, | 295 void ExtensionHelper::OnExtensionMessageInvoke(const std::string& extension_id, |
298 const std::string& function_name, | 296 const std::string& function_name, |
299 const base::ListValue& args, | 297 const base::ListValue& args, |
300 const GURL& event_url, | 298 const GURL& event_url, |
301 bool user_gesture) { | 299 bool user_gesture) { |
302 scoped_ptr<WebScopedUserGesture> web_user_gesture; | 300 scoped_ptr<WebScopedUserGesture> web_user_gesture; |
303 if (user_gesture) { | 301 if (user_gesture) { |
304 web_user_gesture.reset(new WebScopedUserGesture); | 302 web_user_gesture.reset(new WebScopedUserGesture); |
305 } | 303 } |
306 | 304 |
307 extension_dispatcher_->v8_context_set().DispatchChromeHiddenMethod( | 305 dispatcher_->v8_context_set().DispatchChromeHiddenMethod( |
308 extension_id, function_name, args, render_view(), event_url); | 306 extension_id, function_name, args, render_view(), event_url); |
309 } | 307 } |
310 | 308 |
311 void ExtensionHelper::OnExtensionDispatchOnConnect( | 309 void ExtensionHelper::OnExtensionDispatchOnConnect( |
312 int target_port_id, | 310 int target_port_id, |
313 const std::string& channel_name, | 311 const std::string& channel_name, |
314 const std::string& tab_json, | 312 const std::string& tab_json, |
315 const std::string& source_extension_id, | 313 const std::string& source_extension_id, |
316 const std::string& target_extension_id) { | 314 const std::string& target_extension_id) { |
317 MiscellaneousBindings::DispatchOnConnect( | 315 MiscellaneousBindings::DispatchOnConnect( |
318 extension_dispatcher_->v8_context_set().GetAll(), | 316 dispatcher_->v8_context_set().GetAll(), |
319 target_port_id, channel_name, tab_json, | 317 target_port_id, channel_name, tab_json, |
320 source_extension_id, target_extension_id, | 318 source_extension_id, target_extension_id, |
321 render_view()); | 319 render_view()); |
322 } | 320 } |
323 | 321 |
324 void ExtensionHelper::OnExtensionDeliverMessage(int target_id, | 322 void ExtensionHelper::OnExtensionDeliverMessage(int target_id, |
325 const std::string& message) { | 323 const std::string& message) { |
326 MiscellaneousBindings::DeliverMessage( | 324 MiscellaneousBindings::DeliverMessage(dispatcher_->v8_context_set().GetAll(), |
327 extension_dispatcher_->v8_context_set().GetAll(), | 325 target_id, |
328 target_id, | 326 message, |
329 message, | 327 render_view()); |
330 render_view()); | |
331 } | 328 } |
332 | 329 |
333 void ExtensionHelper::OnExtensionDispatchOnDisconnect(int port_id, | 330 void ExtensionHelper::OnExtensionDispatchOnDisconnect(int port_id, |
334 bool connection_error) { | 331 bool connection_error) { |
335 MiscellaneousBindings::DispatchOnDisconnect( | 332 MiscellaneousBindings::DispatchOnDisconnect( |
336 extension_dispatcher_->v8_context_set().GetAll(), | 333 dispatcher_->v8_context_set().GetAll(), |
337 port_id, connection_error, | 334 port_id, connection_error, |
338 render_view()); | 335 render_view()); |
339 } | 336 } |
340 | 337 |
341 void ExtensionHelper::OnExecuteCode( | 338 void ExtensionHelper::OnExecuteCode( |
342 const ExtensionMsg_ExecuteCode_Params& params) { | 339 const ExtensionMsg_ExecuteCode_Params& params) { |
343 WebView* webview = render_view()->GetWebView(); | 340 WebView* webview = render_view()->GetWebView(); |
344 WebFrame* main_frame = webview->mainFrame(); | 341 WebFrame* main_frame = webview->mainFrame(); |
345 if (!main_frame) { | 342 if (!main_frame) { |
346 ListValue val; | 343 ListValue val; |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 target_level = WebConsoleMessage::LevelWarning; | 507 target_level = WebConsoleMessage::LevelWarning; |
511 break; | 508 break; |
512 case content::CONSOLE_MESSAGE_LEVEL_ERROR: | 509 case content::CONSOLE_MESSAGE_LEVEL_ERROR: |
513 target_level = WebConsoleMessage::LevelError; | 510 target_level = WebConsoleMessage::LevelError; |
514 break; | 511 break; |
515 } | 512 } |
516 render_view()->GetWebView()->mainFrame()->addMessageToConsole( | 513 render_view()->GetWebView()->mainFrame()->addMessageToConsole( |
517 WebConsoleMessage(target_level, message)); | 514 WebConsoleMessage(target_level, message)); |
518 } | 515 } |
519 } | 516 } |
| 517 |
| 518 } // namespace extensions |
OLD | NEW |