[email protected] | 3c22078 | 2014-05-20 01:59:46 | [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/script_injection.h" |
| 6 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 7 | #include <map> |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 3c22078 | 2014-05-20 01:59:46 | [diff] [blame] | 9 | |
| 10 | #include "base/lazy_instance.h" |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 11 | #include "base/macros.h" |
jdoerrie | e067999a | 2017-04-07 06:39:00 | [diff] [blame] | 12 | #include "base/memory/ptr_util.h" |
asvitkine | f5d4ee56 | 2016-11-07 18:57:08 | [diff] [blame] | 13 | #include "base/metrics/histogram_macros.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 14 | #include "base/timer/elapsed_timer.h" |
| 15 | #include "base/values.h" |
mek | 87e0ab5 | 2015-02-13 01:18:26 | [diff] [blame] | 16 | #include "content/public/child/v8_value_converter.h" |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 17 | #include "content/public/renderer/render_frame.h" |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 18 | #include "extensions/common/extension_messages.h" |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 19 | #include "extensions/common/feature_switch.h" |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 20 | #include "extensions/common/host_id.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 21 | #include "extensions/renderer/dom_activity_logger.h" |
rdevlin.cronin | c318b93d | 2015-09-14 20:22:29 | [diff] [blame] | 22 | #include "extensions/renderer/extension_frame_helper.h" |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 23 | #include "extensions/renderer/extensions_renderer_client.h" |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 24 | #include "extensions/renderer/script_injection_callback.h" |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 25 | #include "extensions/renderer/scripts_run_info.h" |
mek | 966863c | 2016-02-04 23:39:05 | [diff] [blame] | 26 | #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 27 | #include "third_party/WebKit/public/platform/WebString.h" |
| 28 | #include "third_party/WebKit/public/web/WebDocument.h" |
dcheng | 2e44917 | 2014-09-24 04:30:56 | [diff] [blame] | 29 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 30 | #include "third_party/WebKit/public/web/WebScriptSource.h" |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 31 | #include "url/gurl.h" |
[email protected] | 3c22078 | 2014-05-20 01:59:46 | [diff] [blame] | 32 | |
| 33 | namespace extensions { |
| 34 | |
| 35 | namespace { |
| 36 | |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 37 | using IsolatedWorldMap = std::map<std::string, int>; |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 38 | base::LazyInstance<IsolatedWorldMap>::DestructorAtExit g_isolated_worlds = |
[email protected] | 3c22078 | 2014-05-20 01:59:46 | [diff] [blame] | 39 | LAZY_INSTANCE_INITIALIZER; |
| 40 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 41 | const int64_t kInvalidRequestId = -1; |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 42 | |
| 43 | // The id of the next pending injection. |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 44 | int64_t g_next_pending_id = 0; |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 45 | |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 46 | // Gets the isolated world ID to use for the given |injection_host| |
| 47 | // in the given |frame|. If no isolated world has been created for that |
| 48 | // |injection_host| one will be created and initialized. |
| 49 | int GetIsolatedWorldIdForInstance(const InjectionHost* injection_host, |
| 50 | blink::WebLocalFrame* frame) { |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 51 | static int g_next_isolated_world_id = |
| 52 | ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId(); |
[email protected] | 0d8d697 | 2014-06-03 22:41:02 | [diff] [blame] | 53 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 54 | IsolatedWorldMap& isolated_worlds = g_isolated_worlds.Get(); |
[email protected] | 0d8d697 | 2014-06-03 22:41:02 | [diff] [blame] | 55 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 56 | int id = 0; |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 57 | const std::string& key = injection_host->id().id(); |
| 58 | IsolatedWorldMap::iterator iter = isolated_worlds.find(key); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 59 | if (iter != isolated_worlds.end()) { |
| 60 | id = iter->second; |
| 61 | } else { |
| 62 | id = g_next_isolated_world_id++; |
| 63 | // This map will tend to pile up over time, but realistically, you're never |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 64 | // going to have enough injection hosts for it to matter. |
| 65 | isolated_worlds[key] = id; |
[email protected] | 0d8d697 | 2014-06-03 22:41:02 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 68 | // We need to set the isolated world origin and CSP even if it's not a new |
| 69 | // world since these are stored per frame, and we might not have used this |
| 70 | // isolated world in this frame before. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame^] | 71 | frame->SetIsolatedWorldSecurityOrigin( |
| 72 | id, blink::WebSecurityOrigin::Create(injection_host->url())); |
| 73 | frame->SetIsolatedWorldContentSecurityPolicy( |
| 74 | id, |
| 75 | blink::WebString::FromUTF8(injection_host->GetContentSecurityPolicy())); |
| 76 | frame->SetIsolatedWorldHumanReadableName( |
| 77 | id, blink::WebString::FromUTF8(injection_host->name())); |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 78 | |
| 79 | return id; |
[email protected] | 0d8d697 | 2014-06-03 22:41:02 | [diff] [blame] | 80 | } |
| 81 | |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 82 | // This class manages its own lifetime. |
| 83 | class TimedScriptInjectionCallback : public ScriptInjectionCallback { |
| 84 | public: |
| 85 | explicit TimedScriptInjectionCallback( |
| 86 | base::WeakPtr<ScriptInjection> injection) |
| 87 | : ScriptInjectionCallback( |
| 88 | base::Bind(&TimedScriptInjectionCallback::OnCompleted, |
| 89 | base::Unretained(this))), |
| 90 | injection_(injection) {} |
| 91 | ~TimedScriptInjectionCallback() override {} |
| 92 | |
| 93 | void OnCompleted(const std::vector<v8::Local<v8::Value>>& result) { |
| 94 | if (injection_) { |
ksakamoto | 4178f50 | 2017-04-07 04:40:21 | [diff] [blame] | 95 | base::Optional<base::TimeDelta> elapsed; |
| 96 | // If the script will never execute (such as if the context is destroyed), |
| 97 | // willExecute() will not be called, but OnCompleted() will. Only log a |
| 98 | // time for execution if the script, in fact, executed. |
| 99 | if (!start_time_.is_null()) |
| 100 | elapsed = base::TimeTicks::Now() - start_time_; |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 101 | injection_->OnJsInjectionCompleted(result, elapsed); |
| 102 | } |
| 103 | } |
| 104 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame^] | 105 | void WillExecute() override { start_time_ = base::TimeTicks::Now(); } |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | base::WeakPtr<ScriptInjection> injection_; |
| 109 | base::TimeTicks start_time_; |
| 110 | }; |
| 111 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 112 | } // namespace |
| 113 | |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 114 | // Watches for the deletion of a RenderFrame, after which is_valid will return |
| 115 | // false. |
| 116 | class ScriptInjection::FrameWatcher : public content::RenderFrameObserver { |
| 117 | public: |
| 118 | FrameWatcher(content::RenderFrame* render_frame, |
| 119 | ScriptInjection* injection) |
| 120 | : content::RenderFrameObserver(render_frame), |
| 121 | injection_(injection) {} |
| 122 | ~FrameWatcher() override {} |
| 123 | |
| 124 | private: |
| 125 | void FrameDetached() override { injection_->invalidate_render_frame(); } |
| 126 | void OnDestruct() override { injection_->invalidate_render_frame(); } |
| 127 | |
| 128 | ScriptInjection* injection_; |
| 129 | |
| 130 | DISALLOW_COPY_AND_ASSIGN(FrameWatcher); |
| 131 | }; |
| 132 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 133 | // static |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 134 | std::string ScriptInjection::GetHostIdForIsolatedWorld(int isolated_world_id) { |
| 135 | const IsolatedWorldMap& isolated_worlds = g_isolated_worlds.Get(); |
[email protected] | 0d8d697 | 2014-06-03 22:41:02 | [diff] [blame] | 136 | |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 137 | for (const auto& iter : isolated_worlds) { |
| 138 | if (iter.second == isolated_world_id) |
| 139 | return iter.first; |
[email protected] | 3c22078 | 2014-05-20 01:59:46 | [diff] [blame] | 140 | } |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 141 | return std::string(); |
[email protected] | 3c22078 | 2014-05-20 01:59:46 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | ac2f8937 | 2014-06-23 21:44:25 | [diff] [blame] | 144 | // static |
hanxi | a5c856cf | 2015-02-13 20:51:58 | [diff] [blame] | 145 | void ScriptInjection::RemoveIsolatedWorld(const std::string& host_id) { |
| 146 | g_isolated_worlds.Get().erase(host_id); |
[email protected] | 3c22078 | 2014-05-20 01:59:46 | [diff] [blame] | 147 | } |
| 148 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 149 | ScriptInjection::ScriptInjection( |
| 150 | std::unique_ptr<ScriptInjector> injector, |
| 151 | content::RenderFrame* render_frame, |
| 152 | std::unique_ptr<const InjectionHost> injection_host, |
rdevlin.cronin | 6fba7ec | 2016-06-24 16:15:05 | [diff] [blame] | 153 | UserScript::RunLocation run_location, |
| 154 | bool log_activity) |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 155 | : injector_(std::move(injector)), |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 156 | render_frame_(render_frame), |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 157 | injection_host_(std::move(injection_host)), |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 158 | run_location_(run_location), |
[email protected] | d205600 | 2014-07-03 06:18:06 | [diff] [blame] | 159 | request_id_(kInvalidRequestId), |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 160 | complete_(false), |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 161 | did_inject_js_(false), |
rdevlin.cronin | 6fba7ec | 2016-06-24 16:15:05 | [diff] [blame] | 162 | log_activity_(log_activity), |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 163 | frame_watcher_(new FrameWatcher(render_frame, this)), |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 164 | weak_ptr_factory_(this) { |
hanxi | 9b84166 | 2015-03-04 14:36:41 | [diff] [blame] | 165 | CHECK(injection_host_.get()); |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | ScriptInjection::~ScriptInjection() { |
| 169 | if (!complete_) |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 170 | NotifyWillNotInject(ScriptInjector::WONT_INJECT); |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 171 | } |
| 172 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 173 | ScriptInjection::InjectionResult ScriptInjection::TryToInject( |
| 174 | UserScript::RunLocation current_location, |
| 175 | ScriptsRunInfo* scripts_run_info, |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 176 | const CompletionCallback& async_completion_callback) { |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 177 | if (current_location < run_location_) |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 178 | return INJECTION_WAITING; // Wait for the right location. |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 179 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 180 | if (request_id_ != kInvalidRequestId) { |
| 181 | // We're waiting for permission right now, try again later. |
| 182 | return INJECTION_WAITING; |
| 183 | } |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 184 | |
hanxi | 9b84166 | 2015-03-04 14:36:41 | [diff] [blame] | 185 | if (!injection_host_) { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 186 | NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 187 | return INJECTION_FINISHED; // We're done. |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 188 | } |
| 189 | |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 190 | blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
hanxi | 9b84166 | 2015-03-04 14:36:41 | [diff] [blame] | 191 | switch (injector_->CanExecuteOnFrame( |
rdevlin.cronin | c318b93d | 2015-09-14 20:22:29 | [diff] [blame] | 192 | injection_host_.get(), web_frame, |
| 193 | ExtensionFrameHelper::Get(render_frame_)->tab_id())) { |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 194 | case PermissionsData::ACCESS_DENIED: |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 195 | NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 196 | return INJECTION_FINISHED; // We're done. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 197 | case PermissionsData::ACCESS_WITHHELD: |
rdevlin.cronin | b03d157 | 2015-10-02 02:23:52 | [diff] [blame] | 198 | RequestPermissionFromBrowser(); |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 199 | return INJECTION_WAITING; // Wait around for permission. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 200 | case PermissionsData::ACCESS_ALLOWED: |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 201 | InjectionResult result = Inject(scripts_run_info); |
| 202 | // If the injection is blocked, we need to set the manager so we can |
| 203 | // notify it upon completion. |
| 204 | if (result == INJECTION_BLOCKED) |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 205 | async_completion_callback_ = async_completion_callback; |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 206 | return result; |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 207 | } |
| 208 | |
rdevlin.cronin | 958e9f8 | 2015-02-17 21:57:14 | [diff] [blame] | 209 | NOTREACHED(); |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 210 | return INJECTION_FINISHED; |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 211 | } |
| 212 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 213 | ScriptInjection::InjectionResult ScriptInjection::OnPermissionGranted( |
| 214 | ScriptsRunInfo* scripts_run_info) { |
hanxi | 9b84166 | 2015-03-04 14:36:41 | [diff] [blame] | 215 | if (!injection_host_) { |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 216 | NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 217 | return INJECTION_FINISHED; |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 218 | } |
| 219 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 220 | return Inject(scripts_run_info); |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 221 | } |
| 222 | |
hanxi | 9b84166 | 2015-03-04 14:36:41 | [diff] [blame] | 223 | void ScriptInjection::OnHostRemoved() { |
| 224 | injection_host_.reset(nullptr); |
| 225 | } |
| 226 | |
rdevlin.cronin | b03d157 | 2015-10-02 02:23:52 | [diff] [blame] | 227 | void ScriptInjection::RequestPermissionFromBrowser() { |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 228 | // If we are just notifying the browser of the injection, then send an |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 229 | // invalid request (which is treated like a notification). |
rdevlin.cronin | b03d157 | 2015-10-02 02:23:52 | [diff] [blame] | 230 | request_id_ = g_next_pending_id++; |
rdevlin.cronin | 45dca7f | 2015-06-08 19:47:03 | [diff] [blame] | 231 | render_frame_->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 232 | render_frame_->GetRoutingID(), host_id().id(), injector_->script_type(), |
| 233 | run_location_, request_id_)); |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 236 | void ScriptInjection::NotifyWillNotInject( |
| 237 | ScriptInjector::InjectFailureReason reason) { |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 238 | complete_ = true; |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 239 | injector_->OnWillNotInject(reason, render_frame_); |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 240 | } |
| 241 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 242 | ScriptInjection::InjectionResult ScriptInjection::Inject( |
| 243 | ScriptsRunInfo* scripts_run_info) { |
hanxi | 9b84166 | 2015-03-04 14:36:41 | [diff] [blame] | 244 | DCHECK(injection_host_); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 245 | DCHECK(scripts_run_info); |
| 246 | DCHECK(!complete_); |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 247 | bool should_inject_js = injector_->ShouldInjectJs( |
| 248 | run_location_, scripts_run_info->executing_scripts[host_id().id()]); |
| 249 | bool should_inject_css = injector_->ShouldInjectCss( |
| 250 | run_location_, scripts_run_info->injected_stylesheets[host_id().id()]); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 251 | |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 252 | // This can happen if the extension specified a script to |
| 253 | // be run in multiple rules, and the script has already run. |
| 254 | // See crbug.com/631247. |
| 255 | if (!should_inject_js && !should_inject_css) { |
| 256 | return INJECTION_FINISHED; |
| 257 | } |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 258 | |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 259 | if (should_inject_js) |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 260 | InjectJs(&(scripts_run_info->executing_scripts[host_id().id()]), |
| 261 | &(scripts_run_info->num_js)); |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 262 | if (should_inject_css) |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 263 | InjectCss(&(scripts_run_info->injected_stylesheets[host_id().id()]), |
| 264 | &(scripts_run_info->num_css)); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 265 | |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 266 | complete_ = did_inject_js_ || !should_inject_js; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 267 | |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 268 | if (complete_) { |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 269 | injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 270 | render_frame_); |
| 271 | } else { |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 272 | ++scripts_run_info->num_blocking_js; |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 273 | } |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 274 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 275 | return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 276 | } |
| 277 | |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 278 | void ScriptInjection::InjectJs(std::set<std::string>* executing_scripts, |
| 279 | size_t* num_injected_js_scripts) { |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 280 | DCHECK(!did_inject_js_); |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 281 | blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 282 | std::vector<blink::WebScriptSource> sources = injector_->GetJsSources( |
| 283 | run_location_, executing_scripts, num_injected_js_scripts); |
| 284 | DCHECK(!sources.empty()); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 285 | bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
| 286 | int world_id = in_main_world |
| 287 | ? DOMActivityLogger::kMainWorldId |
hanxi | 9b84166 | 2015-03-04 14:36:41 | [diff] [blame] | 288 | : GetIsolatedWorldIdForInstance(injection_host_.get(), |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 289 | web_frame); |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 290 | bool is_user_gesture = injector_->IsUserGesture(); |
| 291 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 292 | std::unique_ptr<blink::WebScriptExecutionCallback> callback( |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 293 | new TimedScriptInjectionCallback(weak_ptr_factory_.GetWeakPtr())); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 294 | |
| 295 | base::ElapsedTimer exec_timer; |
rdevlin.cronin | 6fba7ec | 2016-06-24 16:15:05 | [diff] [blame] | 296 | if (injection_host_->id().type() == HostID::EXTENSIONS && log_activity_) |
hanxi | 9b84166 | 2015-03-04 14:36:41 | [diff] [blame] | 297 | DOMActivityLogger::AttachToWorld(world_id, injection_host_->id().id()); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 298 | if (in_main_world) { |
| 299 | // We only inject in the main world for javascript: urls. |
| 300 | DCHECK_EQ(1u, sources.size()); |
| 301 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame^] | 302 | web_frame->RequestExecuteScriptAndReturnValue( |
| 303 | sources.front(), is_user_gesture, callback.release()); |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 304 | } else { |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 305 | blink::WebLocalFrame::ScriptExecutionType option; |
| 306 | if (injector_->script_type() == UserScript::CONTENT_SCRIPT && |
| 307 | FeatureSwitch::yield_between_content_script_runs()->IsEnabled()) { |
| 308 | switch (run_location_) { |
| 309 | case UserScript::DOCUMENT_END: |
| 310 | case UserScript::DOCUMENT_IDLE: |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame^] | 311 | option = blink::WebLocalFrame::kAsynchronousBlockingOnload; |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 312 | break; |
| 313 | default: |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame^] | 314 | option = blink::WebLocalFrame::kSynchronous; |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | } else { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame^] | 318 | option = blink::WebLocalFrame::kSynchronous; |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 319 | } |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame^] | 320 | web_frame->RequestExecuteScriptInIsolatedWorld( |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 321 | world_id, &sources.front(), sources.size(), is_user_gesture, option, |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 322 | callback.release()); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 323 | } |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 324 | } |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 325 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 326 | void ScriptInjection::OnJsInjectionCompleted( |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 327 | const std::vector<v8::Local<v8::Value>>& results, |
ksakamoto | 4178f50 | 2017-04-07 04:40:21 | [diff] [blame] | 328 | base::Optional<base::TimeDelta> elapsed) { |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 329 | DCHECK(!did_inject_js_); |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 330 | |
ksakamoto | 4178f50 | 2017-04-07 04:40:21 | [diff] [blame] | 331 | if (injection_host_->id().type() == HostID::EXTENSIONS && elapsed) { |
| 332 | UMA_HISTOGRAM_TIMES("Extensions.InjectedScriptExecutionTime", *elapsed); |
ksakamoto | 049f833 | 2017-03-24 06:39:07 | [diff] [blame] | 333 | switch (run_location_) { |
| 334 | case UserScript::DOCUMENT_START: |
| 335 | UMA_HISTOGRAM_TIMES( |
ksakamoto | 4178f50 | 2017-04-07 04:40:21 | [diff] [blame] | 336 | "Extensions.InjectedScriptExecutionTime.DocumentStart", *elapsed); |
ksakamoto | 049f833 | 2017-03-24 06:39:07 | [diff] [blame] | 337 | break; |
| 338 | case UserScript::DOCUMENT_END: |
| 339 | UMA_HISTOGRAM_TIMES( |
ksakamoto | 4178f50 | 2017-04-07 04:40:21 | [diff] [blame] | 340 | "Extensions.InjectedScriptExecutionTime.DocumentEnd", *elapsed); |
ksakamoto | 049f833 | 2017-03-24 06:39:07 | [diff] [blame] | 341 | break; |
| 342 | case UserScript::DOCUMENT_IDLE: |
| 343 | UMA_HISTOGRAM_TIMES( |
ksakamoto | 4178f50 | 2017-04-07 04:40:21 | [diff] [blame] | 344 | "Extensions.InjectedScriptExecutionTime.DocumentIdle", *elapsed); |
ksakamoto | 049f833 | 2017-03-24 06:39:07 | [diff] [blame] | 345 | break; |
| 346 | default: |
| 347 | break; |
| 348 | } |
| 349 | } |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 350 | |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 351 | bool expects_results = injector_->ExpectsResults(); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 352 | if (expects_results) { |
rdevlin.cronin | 0d94256f | 2016-12-09 15:34:23 | [diff] [blame] | 353 | if (!results.empty() && !results[0].IsEmpty()) { |
kozyatinskiy | efaa59a | 2015-04-03 17:57:16 | [diff] [blame] | 354 | // Right now, we only support returning single results (per frame). |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 355 | std::unique_ptr<content::V8ValueConverter> v8_converter( |
kozyatinskiy | efaa59a | 2015-04-03 17:57:16 | [diff] [blame] | 356 | content::V8ValueConverter::create()); |
| 357 | // It's safe to always use the main world context when converting |
| 358 | // here. V8ValueConverterImpl shouldn't actually care about the |
| 359 | // context scope, and it switches to v8::Object's creation context |
| 360 | // when encountered. |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 361 | v8::Local<v8::Context> context = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame^] | 362 | render_frame_->GetWebFrame()->MainWorldScriptContext(); |
dcheng | 0232f57 | 2016-05-27 17:47:44 | [diff] [blame] | 363 | execution_result_ = v8_converter->FromV8Value(results[0], context); |
kozyatinskiy | efaa59a | 2015-04-03 17:57:16 | [diff] [blame] | 364 | } |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 365 | if (!execution_result_.get()) |
jdoerrie | e067999a | 2017-04-07 06:39:00 | [diff] [blame] | 366 | execution_result_ = base::MakeUnique<base::Value>(); |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 367 | } |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 368 | did_inject_js_ = true; |
kozyatinskiy | c8bc9a58 | 2015-03-06 09:33:41 | [diff] [blame] | 369 | |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 370 | // If |async_completion_callback_| is set, it means the script finished |
| 371 | // asynchronously, and we should run it. |
| 372 | if (!async_completion_callback_.is_null()) { |
ksakamoto | cbf167aa | 2017-03-17 06:45:48 | [diff] [blame] | 373 | complete_ = true; |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 374 | injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
rdevlin.cronin | d533be96 | 2015-10-02 17:01:18 | [diff] [blame] | 375 | render_frame_); |
rdevlin.cronin | 4bb32d7 | 2015-06-02 21:55:01 | [diff] [blame] | 376 | // Warning: this object can be destroyed after this line! |
| 377 | async_completion_callback_.Run(this); |
[email protected] | c11e659 | 2014-06-27 17:07:34 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
catmullings | d4faad4f | 2016-09-08 19:55:30 | [diff] [blame] | 381 | void ScriptInjection::InjectCss(std::set<std::string>* injected_stylesheets, |
| 382 | size_t* num_injected_stylesheets) { |
| 383 | std::vector<blink::WebString> css_sources = injector_->GetCssSources( |
| 384 | run_location_, injected_stylesheets, num_injected_stylesheets); |
rdevlin.cronin | 3e11c986 | 2015-06-04 19:54:25 | [diff] [blame] | 385 | blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
lazyboy | 49cc0b3a | 2016-08-18 21:55:12 | [diff] [blame] | 386 | for (const blink::WebString& css : css_sources) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame^] | 387 | web_frame->GetDocument().InsertStyleSheet(css); |
[email protected] | f8abc6e4 | 2014-06-24 21:14:43 | [diff] [blame] | 388 | } |
| 389 | |
[email protected] | 3c22078 | 2014-05-20 01:59:46 | [diff] [blame] | 390 | } // namespace extensions |