| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/script_injection.h" | 5 #include "extensions/renderer/script_injection.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/timer/elapsed_timer.h" | 11 #include "base/timer/elapsed_timer.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/public/child/v8_value_converter.h" | 13 #include "content/public/child/v8_value_converter.h" |
| 14 #include "content/public/renderer/render_view.h" | 14 #include "content/public/renderer/render_view.h" |
| 15 #include "extensions/common/extension_messages.h" | 15 #include "extensions/common/extension_messages.h" |
| 16 #include "extensions/common/feature_switch.h" | |
| 17 #include "extensions/common/host_id.h" | 16 #include "extensions/common/host_id.h" |
| 18 #include "extensions/common/manifest_handlers/csp_info.h" | 17 #include "extensions/common/manifest_handlers/csp_info.h" |
| 19 #include "extensions/renderer/dom_activity_logger.h" | 18 #include "extensions/renderer/dom_activity_logger.h" |
| 20 #include "extensions/renderer/extension_groups.h" | 19 #include "extensions/renderer/extension_groups.h" |
| 21 #include "extensions/renderer/extension_injection_host.h" | 20 #include "extensions/renderer/extension_injection_host.h" |
| 22 #include "extensions/renderer/extensions_renderer_client.h" | 21 #include "extensions/renderer/extensions_renderer_client.h" |
| 23 #include "third_party/WebKit/public/platform/WebString.h" | 22 #include "third_party/WebKit/public/platform/WebString.h" |
| 24 #include "third_party/WebKit/public/web/WebDocument.h" | 23 #include "third_party/WebKit/public/web/WebDocument.h" |
| 25 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 24 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 26 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 25 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
| 27 #include "third_party/WebKit/public/web/WebScriptSource.h" | 26 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 28 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 27 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
| 29 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 30 | 29 |
| 31 namespace extensions { | 30 namespace extensions { |
| 32 | 31 |
| 33 namespace { | 32 namespace { |
| 34 | 33 |
| 35 using IsolatedWorldMap = std::map<std::string, int>; | 34 using IsolatedWorldMap = std::map<std::string, int>; |
| 36 base::LazyInstance<IsolatedWorldMap> g_isolated_worlds = | 35 base::LazyInstance<IsolatedWorldMap> g_isolated_worlds = |
| 37 LAZY_INSTANCE_INITIALIZER; | 36 LAZY_INSTANCE_INITIALIZER; |
| 38 | 37 |
| 39 const int64 kInvalidRequestId = -1; | 38 const int64 kInvalidRequestId = -1; |
| 40 | 39 |
| 41 // The id of the next pending injection. | 40 // The id of the next pending injection. |
| 42 int64 g_next_pending_id = 0; | 41 int64 g_next_pending_id = 0; |
| 43 | 42 |
| 44 bool ShouldNotifyBrowserOfInjections() { | |
| 45 return !FeatureSwitch::scripts_require_action()->IsEnabled(); | |
| 46 } | |
| 47 | |
| 48 // Append all the child frames of |parent_frame| to |frames_vector|. | 43 // Append all the child frames of |parent_frame| to |frames_vector|. |
| 49 void AppendAllChildFrames(blink::WebFrame* parent_frame, | 44 void AppendAllChildFrames(blink::WebFrame* parent_frame, |
| 50 std::vector<blink::WebFrame*>* frames_vector) { | 45 std::vector<blink::WebFrame*>* frames_vector) { |
| 51 DCHECK(parent_frame); | 46 DCHECK(parent_frame); |
| 52 for (blink::WebFrame* child_frame = parent_frame->firstChild(); child_frame; | 47 for (blink::WebFrame* child_frame = parent_frame->firstChild(); child_frame; |
| 53 child_frame = child_frame->nextSibling()) { | 48 child_frame = child_frame->nextSibling()) { |
| 54 frames_vector->push_back(child_frame); | 49 frames_vector->push_back(child_frame); |
| 55 AppendAllChildFrames(child_frame, frames_vector); | 50 AppendAllChildFrames(child_frame, frames_vector); |
| 56 } | 51 } |
| 57 } | 52 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); | 138 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
| 144 return true; // We're done. | 139 return true; // We're done. |
| 145 } | 140 } |
| 146 | 141 |
| 147 switch (injector_->CanExecuteOnFrame(injection_host, web_frame_, tab_id_, | 142 switch (injector_->CanExecuteOnFrame(injection_host, web_frame_, tab_id_, |
| 148 web_frame_->top()->document().url())) { | 143 web_frame_->top()->document().url())) { |
| 149 case PermissionsData::ACCESS_DENIED: | 144 case PermissionsData::ACCESS_DENIED: |
| 150 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); | 145 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); |
| 151 return true; // We're done. | 146 return true; // We're done. |
| 152 case PermissionsData::ACCESS_WITHHELD: | 147 case PermissionsData::ACCESS_WITHHELD: |
| 153 RequestPermission(); | 148 SendInjectionMessage(true /* request permission */); |
| 154 return false; // Wait around for permission. | 149 return false; // Wait around for permission. |
| 155 case PermissionsData::ACCESS_ALLOWED: | 150 case PermissionsData::ACCESS_ALLOWED: |
| 156 Inject(injection_host, scripts_run_info); | 151 Inject(injection_host, scripts_run_info); |
| 157 return true; // We're done! | 152 return true; // We're done! |
| 158 } | 153 } |
| 159 | 154 |
| 160 // Some compilers don't realize that we always return from the switch() above. | 155 NOTREACHED(); |
| 161 // Make them happy. | |
| 162 return false; | 156 return false; |
| 163 } | 157 } |
| 164 | 158 |
| 165 bool ScriptInjection::OnPermissionGranted(const InjectionHost* injection_host, | 159 bool ScriptInjection::OnPermissionGranted(const InjectionHost* injection_host, |
| 166 ScriptsRunInfo* scripts_run_info) { | 160 ScriptsRunInfo* scripts_run_info) { |
| 167 if (!injection_host) { | 161 if (!injection_host) { |
| 168 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); | 162 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
| 169 return false; | 163 return false; |
| 170 } | 164 } |
| 171 | 165 |
| 172 Inject(injection_host, scripts_run_info); | 166 Inject(injection_host, scripts_run_info); |
| 173 return true; | 167 return true; |
| 174 } | 168 } |
| 175 | 169 |
| 176 void ScriptInjection::RequestPermission() { | 170 void ScriptInjection::SendInjectionMessage(bool request_permission) { |
| 177 content::RenderView* render_view = | 171 content::RenderView* render_view = |
| 178 content::RenderView::FromWebView(web_frame()->top()->view()); | 172 content::RenderView::FromWebView(web_frame()->top()->view()); |
| 179 | 173 |
| 180 // If we are just notifying the browser of the injection, then send an | 174 // If we are just notifying the browser of the injection, then send an |
| 181 // invalid request (which is treated like a notification). | 175 // invalid request (which is treated like a notification). |
| 182 request_id_ = ShouldNotifyBrowserOfInjections() ? kInvalidRequestId | 176 request_id_ = request_permission ? g_next_pending_id++ : kInvalidRequestId; |
| 183 : g_next_pending_id++; | |
| 184 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( | 177 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( |
| 185 render_view->GetRoutingID(), | 178 render_view->GetRoutingID(), |
| 186 host_id_.id(), | 179 host_id_.id(), |
| 187 injector_->script_type(), | 180 injector_->script_type(), |
| 188 request_id_)); | 181 request_id_)); |
| 189 } | 182 } |
| 190 | 183 |
| 191 void ScriptInjection::NotifyWillNotInject( | 184 void ScriptInjection::NotifyWillNotInject( |
| 192 ScriptInjector::InjectFailureReason reason) { | 185 ScriptInjector::InjectFailureReason reason) { |
| 193 complete_ = true; | 186 complete_ = true; |
| 194 injector_->OnWillNotInject(reason); | 187 injector_->OnWillNotInject(reason); |
| 195 } | 188 } |
| 196 | 189 |
| 197 void ScriptInjection::Inject(const InjectionHost* injection_host, | 190 void ScriptInjection::Inject(const InjectionHost* injection_host, |
| 198 ScriptsRunInfo* scripts_run_info) { | 191 ScriptsRunInfo* scripts_run_info) { |
| 199 DCHECK(injection_host); | 192 DCHECK(injection_host); |
| 200 DCHECK(scripts_run_info); | 193 DCHECK(scripts_run_info); |
| 201 DCHECK(!complete_); | 194 DCHECK(!complete_); |
| 202 | 195 |
| 203 if (ShouldNotifyBrowserOfInjections() && | 196 if (injection_host->ShouldNotifyBrowserOfInjection()) |
| 204 injection_host->id().type() == HostID::EXTENSIONS) | 197 SendInjectionMessage(false /* don't request permission */); |
| 205 RequestPermission(); | |
| 206 | 198 |
| 207 std::vector<blink::WebFrame*> frame_vector; | 199 std::vector<blink::WebFrame*> frame_vector; |
| 208 frame_vector.push_back(web_frame_); | 200 frame_vector.push_back(web_frame_); |
| 209 if (injector_->ShouldExecuteInChildFrames()) | 201 if (injector_->ShouldExecuteInChildFrames()) |
| 210 AppendAllChildFrames(web_frame_, &frame_vector); | 202 AppendAllChildFrames(web_frame_, &frame_vector); |
| 211 | 203 |
| 212 scoped_ptr<blink::WebScopedUserGesture> gesture; | 204 scoped_ptr<blink::WebScopedUserGesture> gesture; |
| 213 if (injector_->IsUserGesture()) | 205 if (injector_->IsUserGesture()) |
| 214 gesture.reset(new blink::WebScopedUserGesture()); | 206 gesture.reset(new blink::WebScopedUserGesture()); |
| 215 | 207 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 std::vector<std::string> css_sources = | 308 std::vector<std::string> css_sources = |
| 317 injector_->GetCssSources(run_location_); | 309 injector_->GetCssSources(run_location_); |
| 318 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); | 310 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); |
| 319 iter != css_sources.end(); | 311 iter != css_sources.end(); |
| 320 ++iter) { | 312 ++iter) { |
| 321 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); | 313 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); |
| 322 } | 314 } |
| 323 } | 315 } |
| 324 | 316 |
| 325 } // namespace extensions | 317 } // namespace extensions |
| OLD | NEW |