[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" |
hanxi | 05a02505 | 2015-04-16 19:15:32 | [diff] [blame] | 12 | #include "content/public/renderer/render_thread.h" |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 13 | #include "content/public/renderer/render_frame.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" |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 18 | #include "extensions/renderer/injection_host.h" |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 19 | #include "extensions/renderer/script_context.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 20 | #include "extensions/renderer/scripts_run_info.h" |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 21 | #include "grit/extensions_renderer_resources.h" |
| 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. |
| 54 | base::LazyInstance<RoutingInfoMap> g_routing_info_map = |
| 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 = |
| 70 | ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 71 | IDR_GREASEMONKEY_API_JS); |
| 72 | source_ = |
| 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 | |
| 80 | base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api = |
| 81 | LAZY_INSTANCE_INITIALIZER; |
| 82 | |
| 83 | } // namespace |
| 84 | |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 85 | UserScriptInjector::UserScriptInjector(const UserScript* script, |
| 86 | UserScriptSet* script_list, |
| 87 | bool is_declarative) |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 88 | : script_(script), |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame^] | 89 | user_script_set_(script_list), |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 90 | script_id_(script_->id()), |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 91 | host_id_(script_->host_id()), |
markdittmer | 9ea140f | 2014-08-29 02:46:15 | [diff] [blame] | 92 | is_declarative_(is_declarative), |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 93 | user_script_set_observer_(this) { |
| 94 | user_script_set_observer_.Add(script_list); |
| 95 | } |
| 96 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 97 | UserScriptInjector::~UserScriptInjector() { |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 100 | void UserScriptInjector::OnUserScriptsUpdated( |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 101 | const std::set<HostID>& changed_hosts, |
lazyboy | 12c77d7 | 2016-08-19 20:06:09 | [diff] [blame] | 102 | const UserScriptList& scripts) { |
rdevlin.cronin | dd7a63a | 2016-08-30 06:07:17 | [diff] [blame] | 103 | // When user scripts are updated, all the old script pointers are invalidated. |
| 104 | script_ = nullptr; |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 105 | // If the host causing this injection changed, then this injection |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 106 | // will be removed, and there's no guarantee the backing script still exists. |
rdevlin.cronin | dd7a63a | 2016-08-30 06:07:17 | [diff] [blame] | 107 | if (changed_hosts.count(host_id_) > 0) |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 108 | return; |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 109 | |
lazyboy | 8c0f270 | 2016-07-29 16:34:13 | [diff] [blame] | 110 | for (const std::unique_ptr<UserScript>& script : scripts) { |
lazyboy | 8c0f270 | 2016-07-29 16:34:13 | [diff] [blame] | 111 | if (script->id() == script_id_) { |
| 112 | script_ = script.get(); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 113 | break; |
| 114 | } |
| 115 | } |
rdevlin.cronin | dd7a63a | 2016-08-30 06:07:17 | [diff] [blame] | 116 | // If |host_id_| wasn't in |changed_hosts|, then the script for this injection |
| 117 | // should be guaranteed to exist. |
| 118 | DCHECK(script_); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 121 | UserScript::InjectionType UserScriptInjector::script_type() const { |
| 122 | return UserScript::CONTENT_SCRIPT; |
| 123 | } |
| 124 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 125 | bool UserScriptInjector::ShouldExecuteInMainWorld() const { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | bool UserScriptInjector::IsUserGesture() const { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | bool UserScriptInjector::ExpectsResults() const { |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | bool UserScriptInjector::ShouldInjectJs( |
| 138 | UserScript::RunLocation run_location) const { |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 139 | return script_ && script_->run_location() == run_location && |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 140 | !script_->js_scripts().empty(); |
| 141 | } |
| 142 | |
| 143 | bool UserScriptInjector::ShouldInjectCss( |
| 144 | UserScript::RunLocation run_location) const { |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 145 | return script_ && run_location == UserScript::DOCUMENT_START && |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 146 | !script_->css_scripts().empty(); |
| 147 | } |
| 148 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 149 | PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 150 | const InjectionHost* injection_host, |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 151 | blink::WebLocalFrame* web_frame, |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 152 | int tab_id) const { |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 153 | // There is no harm in allowing the injection when the script is gone, |
| 154 | // because there is nothing to inject. |
| 155 | if (!script_) |
| 156 | return PermissionsData::ACCESS_ALLOWED; |
| 157 | |
rdevlin.cronin | c318b93d | 2015-09-14 20:22:29 | [diff] [blame] | 158 | if (script_->consumer_instance_type() == |
| 159 | UserScript::ConsumerInstanceType::WEBVIEW) { |
| 160 | int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) |
| 161 | ->GetRoutingID(); |
| 162 | |
| 163 | RoutingInfoKey key(routing_id, script_->id()); |
| 164 | |
| 165 | RoutingInfoMap& map = g_routing_info_map.Get(); |
| 166 | auto iter = map.find(key); |
| 167 | |
| 168 | bool allowed = false; |
| 169 | if (iter != map.end()) { |
| 170 | allowed = iter->second; |
| 171 | } else { |
| 172 | // Send a SYNC IPC message to the browser to check if this is allowed. |
| 173 | // This is not ideal, but is mitigated by the fact that this is only done |
| 174 | // for webviews, and then only once per host. |
| 175 | // TODO(hanxi): Find a more efficient way to do this. |
| 176 | content::RenderThread::Get()->Send( |
| 177 | new ExtensionsGuestViewHostMsg_CanExecuteContentScriptSync( |
| 178 | routing_id, script_->id(), &allowed)); |
| 179 | map.insert(std::pair<RoutingInfoKey, bool>(key, allowed)); |
| 180 | } |
| 181 | |
| 182 | return allowed ? PermissionsData::ACCESS_ALLOWED |
| 183 | : PermissionsData::ACCESS_DENIED; |
| 184 | } |
| 185 | |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 186 | GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 187 | web_frame, web_frame->document().url(), script_->match_about_blank()); |
rdevlin.cronin | c318b93d | 2015-09-14 20:22:29 | [diff] [blame] | 188 | |
| 189 | return injection_host->CanExecuteOnFrame( |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 190 | effective_document_url, |
| 191 | content::RenderFrame::FromWebFrame(web_frame), |
| 192 | tab_id, |
| 193 | is_declarative_); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 196 | std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
rdevlin.cronin | f05678d | 2016-08-02 01:10:46 | [diff] [blame] | 197 | UserScript::RunLocation run_location) const { |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 198 | std::vector<blink::WebScriptSource> sources; |
| 199 | if (!script_) |
| 200 | return sources; |
| 201 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 202 | DCHECK_EQ(script_->run_location(), run_location); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 203 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 204 | const UserScript::FileList& js_scripts = script_->js_scripts(); |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame^] | 205 | sources.reserve(js_scripts.size() + |
| 206 | (script_->emulate_greasemonkey() ? 1 : 0)); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 207 | |
rdevlin.cronin | 1256413 | 2016-04-19 00:30:07 | [diff] [blame] | 208 | // Emulate Greasemonkey API for scripts that were converted to extension |
| 209 | // user scripts. |
| 210 | if (script_->emulate_greasemonkey()) |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame^] | 211 | sources.push_back(g_greasemonkey_api.Get().GetSource()); |
| 212 | |
| 213 | for (const std::unique_ptr<UserScript::File>& file : js_scripts) { |
| 214 | sources.push_back(blink::WebScriptSource( |
| 215 | user_script_set_->GetJsSource(*file, script_->emulate_greasemonkey()), |
| 216 | file->url())); |
| 217 | } |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 218 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 219 | return sources; |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 220 | } |
| 221 | |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 222 | std::vector<blink::WebString> UserScriptInjector::GetCssSources( |
rdevlin.cronin | f05678d | 2016-08-02 01:10:46 | [diff] [blame] | 223 | UserScript::RunLocation run_location) const { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 224 | DCHECK_EQ(UserScript::DOCUMENT_START, run_location); |
| 225 | |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 226 | std::vector<blink::WebString> sources; |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 227 | if (!script_) |
| 228 | return sources; |
| 229 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 230 | const UserScript::FileList& css_scripts = script_->css_scripts(); |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 231 | sources.reserve(css_scripts.size()); |
lazyboy | 0f702e9 | 2016-09-07 19:03:45 | [diff] [blame^] | 232 | for (const std::unique_ptr<UserScript::File>& file : script_->css_scripts()) |
| 233 | sources.push_back(user_script_set_->GetCssSource(*file)); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 234 | return sources; |
| 235 | } |
| 236 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 237 | void UserScriptInjector::GetRunInfo( |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 238 | ScriptsRunInfo* scripts_run_info, |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 239 | UserScript::RunLocation run_location) const { |
rob | a807763 | 2016-07-04 23:45:35 | [diff] [blame] | 240 | if (!script_) |
| 241 | return; |
| 242 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 243 | if (ShouldInjectJs(run_location)) { |
| 244 | const UserScript::FileList& js_scripts = script_->js_scripts(); |
| 245 | scripts_run_info->num_js += js_scripts.size(); |
lazyboy | 12c77d7 | 2016-08-19 20:06:09 | [diff] [blame] | 246 | for (const std::unique_ptr<UserScript::File>& iter : js_scripts) { |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 247 | scripts_run_info->executing_scripts[host_id_.id()].insert( |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 248 | iter->url().path()); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if (ShouldInjectCss(run_location)) |
| 253 | scripts_run_info->num_css += script_->css_scripts().size(); |
| 254 | } |
| 255 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 256 | void UserScriptInjector::OnInjectionComplete( |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 257 | std::unique_ptr<base::Value> execution_result, |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 258 | UserScript::RunLocation run_location, |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 259 | content::RenderFrame* render_frame) {} |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 260 | |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 261 | void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, |
| 262 | content::RenderFrame* render_frame) { |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | } // namespace extensions |