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/chrome_view_type.h" | 15 #include "chrome/common/chrome_view_type.h" |
16 #include "chrome/common/extensions/extension_messages.h" | 16 #include "chrome/common/extensions/extension_messages.h" |
17 #include "chrome/common/render_messages.h" | 17 #include "chrome/common/render_messages.h" |
18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.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/extension_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_idle_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 "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedUserGesture.
h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedUserGesture.
h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
30 #include "webkit/glue/image_resource_fetcher.h" | 30 #include "webkit/glue/image_resource_fetcher.h" |
31 #include "webkit/glue/resource_fetcher.h" | 31 #include "webkit/glue/resource_fetcher.h" |
32 | 32 |
33 using extensions::MiscellaneousBindings; | 33 using extensions::MiscellaneousBindings; |
34 using WebKit::WebConsoleMessage; | 34 using WebKit::WebConsoleMessage; |
35 using WebKit::WebDataSource; | 35 using WebKit::WebDataSource; |
36 using WebKit::WebFrame; | 36 using WebKit::WebFrame; |
37 using WebKit::WebURLRequest; | 37 using WebKit::WebURLRequest; |
38 using WebKit::WebScopedUserGesture; | 38 using WebKit::WebScopedUserGesture; |
39 using WebKit::WebView; | 39 using WebKit::WebView; |
40 using webkit_glue::ImageResourceFetcher; | 40 using webkit_glue::ImageResourceFetcher; |
41 using webkit_glue::ResourceFetcher; | 41 using webkit_glue::ResourceFetcher; |
42 | 42 |
43 namespace { | 43 namespace { |
44 // Keeps a mapping from the frame pointer to a UserScriptIdleScheduler object. | 44 // Keeps a mapping from the frame pointer to a UserScriptScheduler object. |
45 // We store this mapping per process, because a frame can jump from one | 45 // We store this mapping per process, because a frame can jump from one |
46 // document to another with adoptNode, and so having the object be a | 46 // document to another with adoptNode, and so having the object be a |
47 // RenderViewObserver means it might miss some notifications after it moves. | 47 // RenderViewObserver means it might miss some notifications after it moves. |
48 typedef std::map<WebFrame*, UserScriptIdleScheduler*> SchedulerMap; | 48 typedef std::map<WebFrame*, UserScriptScheduler*> SchedulerMap; |
49 static base::LazyInstance<SchedulerMap> g_schedulers = | 49 static base::LazyInstance<SchedulerMap> g_schedulers = |
50 LAZY_INSTANCE_INITIALIZER; | 50 LAZY_INSTANCE_INITIALIZER; |
51 } | 51 } |
52 | 52 |
53 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, | 53 ExtensionHelper::ExtensionHelper(content::RenderView* render_view, |
54 ExtensionDispatcher* extension_dispatcher) | 54 ExtensionDispatcher* extension_dispatcher) |
55 : content::RenderViewObserver(render_view), | 55 : content::RenderViewObserver(render_view), |
56 content::RenderViewObserverTracker<ExtensionHelper>(render_view), | 56 content::RenderViewObserverTracker<ExtensionHelper>(render_view), |
57 extension_dispatcher_(extension_dispatcher), | 57 extension_dispatcher_(extension_dispatcher), |
58 pending_app_icon_requests_(0), | 58 pending_app_icon_requests_(0), |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 142 |
143 void ExtensionHelper::DidFinishLoad(WebKit::WebFrame* frame) { | 143 void ExtensionHelper::DidFinishLoad(WebKit::WebFrame* frame) { |
144 SchedulerMap::iterator i = g_schedulers.Get().find(frame); | 144 SchedulerMap::iterator i = g_schedulers.Get().find(frame); |
145 if (i != g_schedulers.Get().end()) | 145 if (i != g_schedulers.Get().end()) |
146 i->second->DidFinishLoad(); | 146 i->second->DidFinishLoad(); |
147 } | 147 } |
148 | 148 |
149 void ExtensionHelper::DidCreateDocumentElement(WebFrame* frame) { | 149 void ExtensionHelper::DidCreateDocumentElement(WebFrame* frame) { |
150 extension_dispatcher_->user_script_slave()->InjectScripts( | 150 extension_dispatcher_->user_script_slave()->InjectScripts( |
151 frame, UserScript::DOCUMENT_START); | 151 frame, UserScript::DOCUMENT_START); |
| 152 SchedulerMap::iterator i = g_schedulers.Get().find(frame); |
| 153 if (i != g_schedulers.Get().end()) |
| 154 i->second->DidCreateDocumentElement(); |
152 } | 155 } |
153 | 156 |
154 void ExtensionHelper::DidStartProvisionalLoad(WebKit::WebFrame* frame) { | 157 void ExtensionHelper::DidStartProvisionalLoad(WebKit::WebFrame* frame) { |
155 SchedulerMap::iterator i = g_schedulers.Get().find(frame); | 158 SchedulerMap::iterator i = g_schedulers.Get().find(frame); |
156 if (i != g_schedulers.Get().end()) | 159 if (i != g_schedulers.Get().end()) |
157 i->second->DidStartProvisionalLoad(); | 160 i->second->DidStartProvisionalLoad(); |
158 } | 161 } |
159 | 162 |
160 void ExtensionHelper::FrameDetached(WebFrame* frame) { | 163 void ExtensionHelper::FrameDetached(WebFrame* frame) { |
161 // This could be called before DidCreateDataSource, in which case the frame | 164 // This could be called before DidCreateDataSource, in which case the frame |
(...skipping 12 matching lines...) Expand all Loading... |
174 if (!frame->parent()) { | 177 if (!frame->parent()) { |
175 app_icon_fetchers_.clear(); | 178 app_icon_fetchers_.clear(); |
176 app_definition_fetcher_.reset(NULL); | 179 app_definition_fetcher_.reset(NULL); |
177 } | 180 } |
178 | 181 |
179 // Check first if we created a scheduler for the frame, since this function | 182 // Check first if we created a scheduler for the frame, since this function |
180 // gets called for navigations within the document. | 183 // gets called for navigations within the document. |
181 if (g_schedulers.Get().count(frame)) | 184 if (g_schedulers.Get().count(frame)) |
182 return; | 185 return; |
183 | 186 |
184 g_schedulers.Get()[frame] = new UserScriptIdleScheduler( | 187 g_schedulers.Get()[frame] = new UserScriptScheduler( |
185 frame, extension_dispatcher_); | 188 frame, extension_dispatcher_); |
186 } | 189 } |
187 | 190 |
188 void ExtensionHelper::OnExtensionResponse(int request_id, | 191 void ExtensionHelper::OnExtensionResponse(int request_id, |
189 bool success, | 192 bool success, |
190 const std::string& response, | 193 const std::string& response, |
191 const std::string& error) { | 194 const std::string& error) { |
192 extension_dispatcher_->OnExtensionResponse(request_id, | 195 extension_dispatcher_->OnExtensionResponse(request_id, |
193 success, | 196 success, |
194 response, | 197 response, |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 routing_id(), *pending_app_info_)); | 382 routing_id(), *pending_app_info_)); |
380 pending_app_info_.reset(NULL); | 383 pending_app_info_.reset(NULL); |
381 } | 384 } |
382 | 385 |
383 void ExtensionHelper::AddErrorToRootConsole(const string16& message) { | 386 void ExtensionHelper::AddErrorToRootConsole(const string16& message) { |
384 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 387 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
385 render_view()->GetWebView()->mainFrame()->addMessageToConsole( | 388 render_view()->GetWebView()->mainFrame()->addMessageToConsole( |
386 WebConsoleMessage(WebConsoleMessage::LevelError, message)); | 389 WebConsoleMessage(WebConsoleMessage::LevelError, message)); |
387 } | 390 } |
388 } | 391 } |
OLD | NEW |