[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [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 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 5 | #include "extensions/renderer/user_script_injector.h" |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 6 | |
jsbell | 9a9ef2b8 | 2015-11-20 19:37:14 | [diff] [blame] | 7 | #include <tuple> |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
| 10 | #include "base/lazy_instance.h" |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [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" |
thakis | 54f7dc9 | 2017-02-23 22:09:59 | [diff] [blame] | 13 | #include "content/public/renderer/render_thread.h" |
hanxi | ccff496a | 2015-03-03 23:14:06 | [diff] [blame] | 14 | #include "content/public/renderer/render_view.h" |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 15 | #include "extensions/common/extension.h" |
fsamuel | b0dc17d | 2015-04-21 18:41:39 | [diff] [blame] | 16 | #include "extensions/common/guest_view/extensions_guest_view_messages.h" |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 17 | #include "extensions/common/permissions/permissions_data.h" |
thakis | 54f7dc9 | 2017-02-23 22:09:59 | [diff] [blame] | 18 | #include "extensions/grit/extensions_renderer_resources.h" |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 19 | #include "extensions/renderer/injection_host.h" |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 20 | #include "extensions/renderer/script_context.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 21 | #include "extensions/renderer/scripts_run_info.h" |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 22 | #include "third_party/WebKit/public/web/WebDocument.h" |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 23 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 24 | #include "third_party/WebKit/public/web/WebScriptSource.h" |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 25 | #include "ui/base/resource/resource_bundle.h" |
| 26 | #include "url/gurl.h" |
| 27 | |
| 28 | namespace extensions { |
| 29 | |
| 30 | namespace { |
| 31 | |
hanxi | 05a02505 | 2015-04-16 19:15:32 | [diff] [blame] | 32 | struct RoutingInfoKey { |
| 33 | int routing_id; |
| 34 | int script_id; |
| 35 | |
| 36 | RoutingInfoKey(int routing_id, int script_id) |
| 37 | : routing_id(routing_id), script_id(script_id) {} |
| 38 | |
| 39 | bool operator<(const RoutingInfoKey& other) const { |
jsbell | 9a9ef2b8 | 2015-11-20 19:37:14 | [diff] [blame] | 40 | return std::tie(routing_id, script_id) < |
| 41 | std::tie(other.routing_id, other.script_id); |
hanxi | 05a02505 | 2015-04-16 19:15:32 | [diff] [blame] | 42 | } |
| 43 | }; |
| 44 | |
| 45 | using RoutingInfoMap = std::map<RoutingInfoKey, bool>; |
| 46 | |
hanxi | 05a02505 | 2015-04-16 19:15:32 | [diff] [blame] | 47 | // A map records whether a given |script_id| from a webview-added user script |
| 48 | // is allowed to inject on the render of given |routing_id|. |
| 49 | // Once a script is added, the decision of whether or not allowed to inject |
| 50 | // won't be changed. |
| 51 | // After removed by the webview, the user scipt will also be removed |
| 52 | // from the render. Therefore, there won't be any query from the same |
| 53 | // |script_id| and |routing_id| pair. |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 54 | base::LazyInstance<RoutingInfoMap>::DestructorAtExit g_routing_info_map = |
hanxi | 05a02505 | 2015-04-16 19:15:32 | [diff] [blame] | 55 | LAZY_INSTANCE_INITIALIZER; |
| 56 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 57 | // Greasemonkey API source that is injected with the scripts. |
| 58 | struct GreasemonkeyApiJsString { |
| 59 | GreasemonkeyApiJsString(); |
[email protected] | 503ae7cd | 2014-06-25 05:59:31 | [diff] [blame] | 60 | blink::WebScriptSource GetSource() const; |
| 61 | |
| 62 | private: |
lazyboy | de26faa | 2016-08-22 18:36:28 | [diff] [blame] | 63 | blink::WebString source_; |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // The below constructor, monstrous as it is, just makes a WebScriptSource from |
| 67 | // the GreasemonkeyApiJs resource. |
lazyboy | de26faa | 2016-08-22 18:36:28 | [diff] [blame] | 68 | GreasemonkeyApiJsString::GreasemonkeyApiJsString() { |
| 69 | base::StringPiece source_piece = |
Lei Zhang | cf30efc | 2017-10-04 21:31:24 | [diff] [blame^] | 70 | ui::ResourceBundle::GetSharedInstance().GetRawDataResource( |
lazyboy | de26faa | 2016-08-22 18:36:28 | [diff] [blame] | 71 | IDR_GREASEMONKEY_API_JS); |
| 72 | source_ = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 73 | blink::WebString::FromUTF8(source_piece.data(), source_piece.length()); |
[email protected] | 503ae7cd | 2014-06-25 05:59:31 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | blink::WebScriptSource GreasemonkeyApiJsString::GetSource() const { |
lazyboy | de26faa | 2016-08-22 18:36:28 | [diff] [blame] | 77 | return blink::WebScriptSource(source_); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 78 | } |
| 79 | |
lazyboy | 1096190 | 2016-09-10 03:08:35 | [diff] [blame] | 80 | base::LazyInstance<GreasemonkeyApiJsString>::Leaky g_greasemonkey_api = |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 81 | LAZY_INSTANCE_INITIALIZER; |
| 82 | |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 83 | bool ShouldInjectScripts(const UserScript::FileList& scripts, |
| 84 | const std::set<std::string>& injected_files) { |
| 85 | for (const std::unique_ptr<UserScript::File>& file : scripts) { |
| 86 | // Check if the script is already injected. |
| 87 | if (injected_files.count(file->url().path()) == 0) { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 94 | } // namespace |
| 95 | |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 96 | UserScriptInjector::UserScriptInjector(const UserScript* script, |
| 97 | UserScriptSet* script_list, |
| 98 | bool is_declarative) |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 99 | : script_(script), |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame] | 100 | user_script_set_(script_list), |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 101 | script_id_(script_->id()), |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 102 | host_id_(script_->host_id()), |
markdittmer | 9ea140f | 2014-08-29 02:46:15 | [diff] [blame] | 103 | is_declarative_(is_declarative), |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 104 | user_script_set_observer_(this) { |
| 105 | user_script_set_observer_.Add(script_list); |
| 106 | } |
| 107 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 108 | UserScriptInjector::~UserScriptInjector() { |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 111 | void UserScriptInjector::OnUserScriptsUpdated( |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 112 | const std::set<HostID>& changed_hosts, |
lazyboy | 12c77d7 | 2016-08-19 20:06:09 | [diff] [blame] | 113 | const UserScriptList& scripts) { |
rdevlin.cronin | dd7a63a | 2016-08-30 06:07:17 | [diff] [blame] | 114 | // When user scripts are updated, all the old script pointers are invalidated. |
| 115 | script_ = nullptr; |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 116 | // If the host causing this injection changed, then this injection |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 117 | // will be removed, and there's no guarantee the backing script still exists. |
rdevlin.cronin | dd7a63a | 2016-08-30 06:07:17 | [diff] [blame] | 118 | if (changed_hosts.count(host_id_) > 0) |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 119 | return; |
| 120 | |
lazyboy | 8c0f270 | 2016-07-29 16:34:13 | [diff] [blame] | 121 | for (const std::unique_ptr<UserScript>& script : scripts) { |
lazyboy | 8c0f270 | 2016-07-29 16:34:13 | [diff] [blame] | 122 | if (script->id() == script_id_) { |
| 123 | script_ = script.get(); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | } |
rdevlin.cronin | dd7a63a | 2016-08-30 06:07:17 | [diff] [blame] | 127 | // If |host_id_| wasn't in |changed_hosts|, then the script for this injection |
| 128 | // should be guaranteed to exist. |
| 129 | DCHECK(script_); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 132 | UserScript::InjectionType UserScriptInjector::script_type() const { |
| 133 | return UserScript::CONTENT_SCRIPT; |
| 134 | } |
| 135 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 136 | bool UserScriptInjector::ShouldExecuteInMainWorld() const { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | bool UserScriptInjector::IsUserGesture() const { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | bool UserScriptInjector::ExpectsResults() const { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | bool UserScriptInjector::ShouldInjectJs( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 149 | UserScript::RunLocation run_location, |
| 150 | const std::set<std::string>& executing_scripts) const { |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 151 | return script_ && script_->run_location() == run_location && |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 152 | !script_->js_scripts().empty() && |
| 153 | ShouldInjectScripts(script_->js_scripts(), executing_scripts); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool UserScriptInjector::ShouldInjectCss( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 157 | UserScript::RunLocation run_location, |
| 158 | const std::set<std::string>& injected_stylesheets) const { |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 159 | return script_ && run_location == UserScript::DOCUMENT_START && |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 160 | !script_->css_scripts().empty() && |
| 161 | ShouldInjectScripts(script_->css_scripts(), injected_stylesheets); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 164 | PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 165 | const InjectionHost* injection_host, |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 166 | blink::WebLocalFrame* web_frame, |
jam | 69e7115 | 2016-11-02 01:15:43 | [diff] [blame] | 167 | int tab_id) { |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 168 | // There is no harm in allowing the injection when the script is gone, |
| 169 | // because there is nothing to inject. |
| 170 | if (!script_) |
| 171 | return PermissionsData::ACCESS_ALLOWED; |
| 172 | |
rdevlin.cronin | c318b93d | 2015-09-14 20:22:29 | [diff] [blame] | 173 | if (script_->consumer_instance_type() == |
| 174 | UserScript::ConsumerInstanceType::WEBVIEW) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 175 | int routing_id = content::RenderView::FromWebView(web_frame->Top()->View()) |
| 176 | ->GetRoutingID(); |
rdevlin.cronin | c318b93d | 2015-09-14 20:22:29 | [diff] [blame] | 177 | |
| 178 | RoutingInfoKey key(routing_id, script_->id()); |
| 179 | |
| 180 | RoutingInfoMap& map = g_routing_info_map.Get(); |
| 181 | auto iter = map.find(key); |
| 182 | |
| 183 | bool allowed = false; |
| 184 | if (iter != map.end()) { |
| 185 | allowed = iter->second; |
| 186 | } else { |
| 187 | // Send a SYNC IPC message to the browser to check if this is allowed. |
| 188 | // This is not ideal, but is mitigated by the fact that this is only done |
| 189 | // for webviews, and then only once per host. |
| 190 | // TODO(hanxi): Find a more efficient way to do this. |
| 191 | content::RenderThread::Get()->Send( |
| 192 | new ExtensionsGuestViewHostMsg_CanExecuteContentScriptSync( |
| 193 | routing_id, script_->id(), &allowed)); |
| 194 | map.insert(std::pair<RoutingInfoKey, bool>(key, allowed)); |
| 195 | } |
| 196 | |
| 197 | return allowed ? PermissionsData::ACCESS_ALLOWED |
| 198 | : PermissionsData::ACCESS_DENIED; |
| 199 | } |
| 200 | |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 201 | GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 202 | web_frame, web_frame->GetDocument().Url(), script_->match_about_blank()); |
rdevlin.cronin | c318b93d | 2015-09-14 20:22:29 | [diff] [blame] | 203 | |
| 204 | return injection_host->CanExecuteOnFrame( |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 205 | effective_document_url, |
| 206 | content::RenderFrame::FromWebFrame(web_frame), |
| 207 | tab_id, |
| 208 | is_declarative_); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 211 | std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 212 | UserScript::RunLocation run_location, |
| 213 | std::set<std::string>* executing_scripts, |
| 214 | size_t* num_injected_js_scripts) const { |
| 215 | DCHECK(script_); |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 216 | std::vector<blink::WebScriptSource> sources; |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 217 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 218 | DCHECK_EQ(script_->run_location(), run_location); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 219 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 220 | const UserScript::FileList& js_scripts = script_->js_scripts(); |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame] | 221 | sources.reserve(js_scripts.size() + |
| 222 | (script_->emulate_greasemonkey() ? 1 : 0)); |
rdevlin.cronin | 1256413 | 2016-04-19 00:30:07 | [diff] [blame] | 223 | // Emulate Greasemonkey API for scripts that were converted to extension |
| 224 | // user scripts. |
| 225 | if (script_->emulate_greasemonkey()) |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame] | 226 | sources.push_back(g_greasemonkey_api.Get().GetSource()); |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame] | 227 | for (const std::unique_ptr<UserScript::File>& file : js_scripts) { |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 228 | const GURL& script_url = file->url(); |
| 229 | // Check if the script is already injected. |
| 230 | if (executing_scripts->count(script_url.path()) != 0) |
| 231 | continue; |
| 232 | |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame] | 233 | sources.push_back(blink::WebScriptSource( |
| 234 | user_script_set_->GetJsSource(*file, script_->emulate_greasemonkey()), |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 235 | script_url)); |
| 236 | |
| 237 | (*num_injected_js_scripts) += 1; |
| 238 | executing_scripts->insert(script_url.path()); |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame] | 239 | } |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 240 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 241 | return sources; |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 242 | } |
| 243 | |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 244 | std::vector<blink::WebString> UserScriptInjector::GetCssSources( |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 245 | UserScript::RunLocation run_location, |
| 246 | std::set<std::string>* injected_stylesheets, |
| 247 | size_t* num_injected_stylesheets) const { |
| 248 | DCHECK(script_); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 249 | DCHECK_EQ(UserScript::DOCUMENT_START, run_location); |
| 250 | |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 251 | std::vector<blink::WebString> sources; |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 252 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 253 | const UserScript::FileList& css_scripts = script_->css_scripts(); |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 254 | sources.reserve(css_scripts.size()); |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 255 | for (const std::unique_ptr<UserScript::File>& file : script_->css_scripts()) { |
| 256 | const std::string& stylesheet_path = file->url().path(); |
| 257 | // Check if the stylesheet is already injected. |
| 258 | if (injected_stylesheets->count(stylesheet_path) != 0) |
| 259 | continue; |
| 260 | |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame] | 261 | sources.push_back(user_script_set_->GetCssSource(*file)); |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 262 | (*num_injected_stylesheets) += 1; |
| 263 | injected_stylesheets->insert(stylesheet_path); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 264 | } |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 265 | return sources; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 266 | } |
| 267 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 268 | void UserScriptInjector::OnInjectionComplete( |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 269 | std::unique_ptr<base::Value> execution_result, |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 270 | UserScript::RunLocation run_location, |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 271 | content::RenderFrame* render_frame) {} |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 272 | |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 273 | void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, |
| 274 | content::RenderFrame* render_frame) { |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | } // namespace extensions |