[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "extensions/renderer/programmatic_script_injector.h" |
| 6 | |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 7 | #include <utility> |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
| 10 | #include "base/values.h" |
rob | a9e6e642 | 2015-03-30 21:14:02 | [diff] [blame] | 11 | #include "content/public/common/url_constants.h" |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 12 | #include "content/public/renderer/render_frame.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 13 | #include "extensions/common/error_utils.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 14 | #include "extensions/common/extension_messages.h" |
| 15 | #include "extensions/common/manifest_constants.h" |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 16 | #include "extensions/common/permissions/api_permission.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 17 | #include "extensions/common/permissions/permissions_data.h" |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 18 | #include "extensions/renderer/injection_host.h" |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 19 | #include "extensions/renderer/renderer_extension_registry.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 20 | #include "extensions/renderer/script_context.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 21 | #include "third_party/blink/public/platform/web_string.h" |
| 22 | #include "third_party/blink/public/web/web_document.h" |
| 23 | #include "third_party/blink/public/web/web_local_frame.h" |
| 24 | #include "third_party/blink/public/web/web_script_source.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 25 | |
| 26 | namespace extensions { |
| 27 | |
| 28 | ProgrammaticScriptInjector::ProgrammaticScriptInjector( |
jam | 69e7115 | 2016-11-02 01:15:43 | [diff] [blame] | 29 | const ExtensionMsg_ExecuteCode_Params& params) |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 30 | : params_(new ExtensionMsg_ExecuteCode_Params(params)), |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 31 | finished_(false) { |
| 32 | } |
| 33 | |
| 34 | ProgrammaticScriptInjector::~ProgrammaticScriptInjector() { |
| 35 | } |
| 36 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 37 | UserScript::InjectionType ProgrammaticScriptInjector::script_type() |
| 38 | const { |
| 39 | return UserScript::PROGRAMMATIC_SCRIPT; |
| 40 | } |
| 41 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 42 | bool ProgrammaticScriptInjector::ShouldExecuteInMainWorld() const { |
| 43 | return params_->in_main_world; |
| 44 | } |
| 45 | |
| 46 | bool ProgrammaticScriptInjector::IsUserGesture() const { |
| 47 | return params_->user_gesture; |
| 48 | } |
| 49 | |
Manish Jethani | 9494d72 | 2018-01-20 00:28:47 | [diff] [blame] | 50 | base::Optional<CSSOrigin> ProgrammaticScriptInjector::GetCssOrigin() const { |
| 51 | return params_->css_origin; |
| 52 | } |
| 53 | |
Manish Jethani | ff6ff85 | 2018-02-23 07:24:55 | [diff] [blame] | 54 | const base::Optional<std::string> |
| 55 | ProgrammaticScriptInjector::GetInjectionKey() const { |
| 56 | return params_->injection_key; |
| 57 | } |
| 58 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 59 | bool ProgrammaticScriptInjector::ExpectsResults() const { |
| 60 | return params_->wants_result; |
| 61 | } |
| 62 | |
| 63 | bool ProgrammaticScriptInjector::ShouldInjectJs( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 64 | UserScript::RunLocation run_location, |
| 65 | const std::set<std::string>& executing_scripts) const { |
Devlin Cronin | a9ec04b | 2017-11-28 19:27:29 | [diff] [blame] | 66 | return params_->run_at == run_location && params_->is_javascript; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | bool ProgrammaticScriptInjector::ShouldInjectCss( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 70 | UserScript::RunLocation run_location, |
| 71 | const std::set<std::string>& injected_stylesheets) const { |
Devlin Cronin | a9ec04b | 2017-11-28 19:27:29 | [diff] [blame] | 72 | return params_->run_at == run_location && !params_->is_javascript; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 73 | } |
| 74 | |
Devlin Cronin | 3e532b8 | 2018-05-03 21:27:19 | [diff] [blame] | 75 | PermissionsData::PageAccess ProgrammaticScriptInjector::CanExecuteOnFrame( |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 76 | const InjectionHost* injection_host, |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 77 | blink::WebLocalFrame* frame, |
jam | 69e7115 | 2016-11-02 01:15:43 | [diff] [blame] | 78 | int tab_id) { |
| 79 | // Note: we calculate url_ now and not in the constructor because with |
| 80 | // PlzNavigate we won't have the URL at that point when loads start. The |
| 81 | // browser issues the request and only when it has a response does the |
| 82 | // renderer see the provisional data source which the method below uses. |
Takeshi Yoshino | 41b671a | 2017-08-01 12:17:51 | [diff] [blame] | 83 | url_ = ScriptContext::GetDocumentLoaderURLForFrame(frame); |
jam | 69e7115 | 2016-11-02 01:15:43 | [diff] [blame] | 84 | if (url_.SchemeIs(url::kAboutScheme)) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 85 | origin_for_about_error_ = frame->GetSecurityOrigin().ToString().Utf8(); |
jam | 69e7115 | 2016-11-02 01:15:43 | [diff] [blame] | 86 | } |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 87 | GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 88 | frame, frame->GetDocument().Url(), params_->match_about_blank); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 89 | if (params_->is_web_view) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 90 | if (frame->Parent()) { |
lazyboy | 2254f0e | 2014-12-10 07:10:54 | [diff] [blame] | 91 | // This is a subframe inside <webview>, so allow it. |
Devlin Cronin | 3e532b8 | 2018-05-03 21:27:19 | [diff] [blame] | 92 | return PermissionsData::PageAccess::kAllowed; |
lazyboy | 2254f0e | 2014-12-10 07:10:54 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 95 | return effective_document_url == params_->webview_src |
Devlin Cronin | 3e532b8 | 2018-05-03 21:27:19 | [diff] [blame] | 96 | ? PermissionsData::PageAccess::kAllowed |
| 97 | : PermissionsData::PageAccess::kDenied; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 98 | } |
hanxi | 79f7a57 | 2015-03-09 20:46:59 | [diff] [blame] | 99 | DCHECK_EQ(injection_host->id().type(), HostID::EXTENSIONS); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 100 | |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 101 | return injection_host->CanExecuteOnFrame( |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 102 | effective_document_url, |
| 103 | content::RenderFrame::FromWebFrame(frame), |
| 104 | tab_id, |
| 105 | true /* is_declarative */); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | std::vector<blink::WebScriptSource> ProgrammaticScriptInjector::GetJsSources( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 109 | UserScript::RunLocation run_location, |
| 110 | std::set<std::string>* executing_scripts, |
| 111 | size_t* num_injected_js_scripts) const { |
Devlin Cronin | a9ec04b | 2017-11-28 19:27:29 | [diff] [blame] | 112 | DCHECK_EQ(params_->run_at, run_location); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 113 | DCHECK(params_->is_javascript); |
| 114 | |
| 115 | return std::vector<blink::WebScriptSource>( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 116 | 1, blink::WebScriptSource(blink::WebString::FromUTF8(params_->code), |
| 117 | params_->file_url)); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 118 | } |
| 119 | |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 120 | std::vector<blink::WebString> ProgrammaticScriptInjector::GetCssSources( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 121 | UserScript::RunLocation run_location, |
| 122 | std::set<std::string>* injected_stylesheets, |
| 123 | size_t* num_injected_stylesheets) const { |
Devlin Cronin | a9ec04b | 2017-11-28 19:27:29 | [diff] [blame] | 124 | DCHECK_EQ(params_->run_at, run_location); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 125 | DCHECK(!params_->is_javascript); |
| 126 | |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 127 | return std::vector<blink::WebString>( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 128 | 1, blink::WebString::FromUTF8(params_->code)); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void ProgrammaticScriptInjector::OnInjectionComplete( |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 132 | std::unique_ptr<base::Value> execution_result, |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 133 | UserScript::RunLocation run_location, |
| 134 | content::RenderFrame* render_frame) { |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 135 | DCHECK(results_.empty()); |
| 136 | if (execution_result) |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 137 | results_.Append(std::move(execution_result)); |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 138 | Finish(std::string(), render_frame); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 139 | } |
| 140 | |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 141 | void ProgrammaticScriptInjector::OnWillNotInject( |
| 142 | InjectFailureReason reason, |
| 143 | content::RenderFrame* render_frame) { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 144 | std::string error; |
| 145 | switch (reason) { |
| 146 | case NOT_ALLOWED: |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 147 | if (!CanShowUrlInError()) { |
| 148 | error = manifest_errors::kCannotAccessPage; |
| 149 | } else if (!origin_for_about_error_.empty()) { |
rob | a9e6e642 | 2015-03-30 21:14:02 | [diff] [blame] | 150 | error = ErrorUtils::FormatErrorMessage( |
| 151 | manifest_errors::kCannotAccessAboutUrl, url_.spec(), |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 152 | origin_for_about_error_); |
rob | a9e6e642 | 2015-03-30 21:14:02 | [diff] [blame] | 153 | } else { |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 154 | error = ErrorUtils::FormatErrorMessage( |
| 155 | manifest_errors::kCannotAccessPageWithUrl, url_.spec()); |
rob | a9e6e642 | 2015-03-30 21:14:02 | [diff] [blame] | 156 | } |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 157 | break; |
| 158 | case EXTENSION_REMOVED: // no special error here. |
| 159 | case WONT_INJECT: |
| 160 | break; |
| 161 | } |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 162 | Finish(error, render_frame); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 163 | } |
| 164 | |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 165 | bool ProgrammaticScriptInjector::CanShowUrlInError() const { |
| 166 | if (params_->host_id.type() != HostID::EXTENSIONS) |
| 167 | return false; |
| 168 | const Extension* extension = |
| 169 | RendererExtensionRegistry::Get()->GetByID(params_->host_id.id()); |
| 170 | if (!extension) |
| 171 | return false; |
| 172 | return extension->permissions_data()->active_permissions().HasAPIPermission( |
| 173 | APIPermission::kTab); |
| 174 | } |
| 175 | |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 176 | void ProgrammaticScriptInjector::Finish(const std::string& error, |
| 177 | content::RenderFrame* render_frame) { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 178 | DCHECK(!finished_); |
| 179 | finished_ = true; |
| 180 | |
rdevlin.cronin | 3ae4a3201 | 2015-06-30 17:43:19 | [diff] [blame] | 181 | // It's possible that the render frame was destroyed in the course of |
| 182 | // injecting scripts. Don't respond if it was (the browser side watches for |
| 183 | // frame deletions so nothing is left hanging). |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 184 | if (render_frame) { |
| 185 | render_frame->Send( |
rdevlin.cronin | 3ae4a3201 | 2015-06-30 17:43:19 | [diff] [blame] | 186 | new ExtensionHostMsg_ExecuteCodeFinished( |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 187 | render_frame->GetRoutingID(), params_->request_id, |
rdevlin.cronin | 3ae4a3201 | 2015-06-30 17:43:19 | [diff] [blame] | 188 | error, url_, results_)); |
| 189 | } |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // namespace extensions |