[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 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 47 | // These two strings are injected before and after the Greasemonkey API and |
| 48 | // user script to wrap it in an anonymous scope. |
| 49 | const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
| 50 | const char kUserScriptTail[] = "\n})(window);"; |
| 51 | |
hanxi | 05a02505 | 2015-04-16 19:15:32 | [diff] [blame] | 52 | // A map records whether a given |script_id| from a webview-added user script |
| 53 | // is allowed to inject on the render of given |routing_id|. |
| 54 | // Once a script is added, the decision of whether or not allowed to inject |
| 55 | // won't be changed. |
| 56 | // After removed by the webview, the user scipt will also be removed |
| 57 | // from the render. Therefore, there won't be any query from the same |
| 58 | // |script_id| and |routing_id| pair. |
| 59 | base::LazyInstance<RoutingInfoMap> g_routing_info_map = |
| 60 | LAZY_INSTANCE_INITIALIZER; |
| 61 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 62 | // Greasemonkey API source that is injected with the scripts. |
| 63 | struct GreasemonkeyApiJsString { |
| 64 | GreasemonkeyApiJsString(); |
[email protected] | 503ae7cd | 2014-06-25 05:59:31 | [diff] [blame] | 65 | blink::WebScriptSource GetSource() const; |
| 66 | |
| 67 | private: |
| 68 | std::string source_; |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | // The below constructor, monstrous as it is, just makes a WebScriptSource from |
| 72 | // the GreasemonkeyApiJs resource. |
| 73 | GreasemonkeyApiJsString::GreasemonkeyApiJsString() |
[email protected] | 503ae7cd | 2014-06-25 05:59:31 | [diff] [blame] | 74 | : source_(ResourceBundle::GetSharedInstance() |
| 75 | .GetRawDataResource(IDR_GREASEMONKEY_API_JS) |
| 76 | .as_string()) { |
| 77 | } |
| 78 | |
| 79 | blink::WebScriptSource GreasemonkeyApiJsString::GetSource() const { |
| 80 | return blink::WebScriptSource(blink::WebString::fromUTF8(source_)); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api = |
| 84 | LAZY_INSTANCE_INITIALIZER; |
| 85 | |
| 86 | } // namespace |
| 87 | |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 88 | UserScriptInjector::UserScriptInjector(const UserScript* script, |
| 89 | UserScriptSet* script_list, |
| 90 | bool is_declarative) |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 91 | : script_(script), |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 92 | script_id_(script_->id()), |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 93 | host_id_(script_->host_id()), |
markdittmer | 9ea140f | 2014-08-29 02:46:15 | [diff] [blame] | 94 | is_declarative_(is_declarative), |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 95 | user_script_set_observer_(this) { |
| 96 | user_script_set_observer_.Add(script_list); |
| 97 | } |
| 98 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 99 | UserScriptInjector::~UserScriptInjector() { |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 102 | void UserScriptInjector::OnUserScriptsUpdated( |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 103 | const std::set<HostID>& changed_hosts, |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 104 | const std::vector<UserScript*>& scripts) { |
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. |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 107 | if (changed_hosts.count(host_id_) > 0) |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 108 | return; |
| 109 | |
| 110 | for (std::vector<UserScript*>::const_iterator iter = scripts.begin(); |
| 111 | iter != scripts.end(); |
| 112 | ++iter) { |
| 113 | // We need to compare to |script_id_| (and not to script_->id()) because the |
| 114 | // old |script_| may be deleted by now. |
| 115 | if ((*iter)->id() == script_id_) { |
| 116 | script_ = *iter; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 122 | UserScript::InjectionType UserScriptInjector::script_type() const { |
| 123 | return UserScript::CONTENT_SCRIPT; |
| 124 | } |
| 125 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 126 | bool UserScriptInjector::ShouldExecuteInMainWorld() const { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | bool UserScriptInjector::IsUserGesture() const { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | bool UserScriptInjector::ExpectsResults() const { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | bool UserScriptInjector::ShouldInjectJs( |
| 139 | UserScript::RunLocation run_location) const { |
| 140 | return script_->run_location() == run_location && |
| 141 | !script_->js_scripts().empty(); |
| 142 | } |
| 143 | |
| 144 | bool UserScriptInjector::ShouldInjectCss( |
| 145 | UserScript::RunLocation run_location) const { |
| 146 | return run_location == UserScript::DOCUMENT_START && |
| 147 | !script_->css_scripts().empty(); |
| 148 | } |
| 149 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 150 | PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 151 | const InjectionHost* injection_host, |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 152 | blink::WebLocalFrame* web_frame, |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 153 | int tab_id) const { |
rdevlin.cronin | c318b93d | 2015-09-14 20:22:29 | [diff] [blame] | 154 | if (script_->consumer_instance_type() == |
| 155 | UserScript::ConsumerInstanceType::WEBVIEW) { |
| 156 | int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) |
| 157 | ->GetRoutingID(); |
| 158 | |
| 159 | RoutingInfoKey key(routing_id, script_->id()); |
| 160 | |
| 161 | RoutingInfoMap& map = g_routing_info_map.Get(); |
| 162 | auto iter = map.find(key); |
| 163 | |
| 164 | bool allowed = false; |
| 165 | if (iter != map.end()) { |
| 166 | allowed = iter->second; |
| 167 | } else { |
| 168 | // Send a SYNC IPC message to the browser to check if this is allowed. |
| 169 | // This is not ideal, but is mitigated by the fact that this is only done |
| 170 | // for webviews, and then only once per host. |
| 171 | // TODO(hanxi): Find a more efficient way to do this. |
| 172 | content::RenderThread::Get()->Send( |
| 173 | new ExtensionsGuestViewHostMsg_CanExecuteContentScriptSync( |
| 174 | routing_id, script_->id(), &allowed)); |
| 175 | map.insert(std::pair<RoutingInfoKey, bool>(key, allowed)); |
| 176 | } |
| 177 | |
| 178 | return allowed ? PermissionsData::ACCESS_ALLOWED |
| 179 | : PermissionsData::ACCESS_DENIED; |
| 180 | } |
| 181 | |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 182 | GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 183 | web_frame, web_frame->document().url(), script_->match_about_blank()); |
rdevlin.cronin | c318b93d | 2015-09-14 20:22:29 | [diff] [blame] | 184 | |
| 185 | return injection_host->CanExecuteOnFrame( |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 186 | effective_document_url, |
| 187 | content::RenderFrame::FromWebFrame(web_frame), |
| 188 | tab_id, |
| 189 | is_declarative_); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 192 | std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
| 193 | UserScript::RunLocation run_location) const { |
| 194 | DCHECK_EQ(script_->run_location(), run_location); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 195 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 196 | std::vector<blink::WebScriptSource> sources; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 197 | const UserScript::FileList& js_scripts = script_->js_scripts(); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 198 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 199 | for (UserScript::FileList::const_iterator iter = js_scripts.begin(); |
| 200 | iter != js_scripts.end(); |
| 201 | ++iter) { |
| 202 | std::string content = iter->GetContent().as_string(); |
| 203 | |
rdevlin.cronin | 1256413 | 2016-04-19 00:30:07 | [diff] [blame] | 204 | // We add this dumb function wrapper for user scripts to emulate what |
| 205 | // Greasemonkey does. |
| 206 | if (script_->emulate_greasemonkey()) { |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 207 | content.insert(0, kUserScriptHead); |
| 208 | content += kUserScriptTail; |
| 209 | } |
| 210 | sources.push_back(blink::WebScriptSource( |
| 211 | blink::WebString::fromUTF8(content), iter->url())); |
| 212 | } |
| 213 | |
rdevlin.cronin | 1256413 | 2016-04-19 00:30:07 | [diff] [blame] | 214 | // Emulate Greasemonkey API for scripts that were converted to extension |
| 215 | // user scripts. |
| 216 | if (script_->emulate_greasemonkey()) |
[email protected] | 503ae7cd | 2014-06-25 05:59:31 | [diff] [blame] | 217 | sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); |
[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 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 222 | std::vector<std::string> UserScriptInjector::GetCssSources( |
| 223 | UserScript::RunLocation run_location) const { |
| 224 | DCHECK_EQ(UserScript::DOCUMENT_START, run_location); |
| 225 | |
| 226 | std::vector<std::string> sources; |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 227 | const UserScript::FileList& css_scripts = script_->css_scripts(); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 228 | for (UserScript::FileList::const_iterator iter = css_scripts.begin(); |
| 229 | iter != css_scripts.end(); |
| 230 | ++iter) { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 231 | sources.push_back(iter->GetContent().as_string()); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 232 | } |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 233 | return sources; |
| 234 | } |
| 235 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 236 | void UserScriptInjector::GetRunInfo( |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 237 | ScriptsRunInfo* scripts_run_info, |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 238 | UserScript::RunLocation run_location) const { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 239 | if (ShouldInjectJs(run_location)) { |
| 240 | const UserScript::FileList& js_scripts = script_->js_scripts(); |
| 241 | scripts_run_info->num_js += js_scripts.size(); |
| 242 | for (UserScript::FileList::const_iterator iter = js_scripts.begin(); |
| 243 | iter != js_scripts.end(); |
| 244 | ++iter) { |
hanxi | 3df97b2 | 2015-03-11 23:40:06 | [diff] [blame] | 245 | scripts_run_info->executing_scripts[host_id_.id()].insert( |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 246 | iter->url().path()); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (ShouldInjectCss(run_location)) |
| 251 | scripts_run_info->num_css += script_->css_scripts().size(); |
| 252 | } |
| 253 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 254 | void UserScriptInjector::OnInjectionComplete( |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame^] | 255 | std::unique_ptr<base::Value> execution_result, |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 256 | UserScript::RunLocation run_location, |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame^] | 257 | content::RenderFrame* render_frame) {} |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 258 | |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 259 | void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, |
| 260 | content::RenderFrame* render_frame) { |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | } // namespace extensions |