1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/renderer/script_injection.h"
#include <map>
#include <utility>
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/lazy_instance.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/trace_event/typed_macros.h"
#include "base/tracing/protos/chrome_track_event.pbzero.h"
#include "base/values.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/mojom/host_id.mojom.h"
#include "extensions/common/mojom/injection_type.mojom-shared.h"
#include "extensions/renderer/dom_activity_logger.h"
#include "extensions/renderer/extension_frame_helper.h"
#include "extensions/renderer/extensions_renderer_client.h"
#include "extensions/renderer/isolated_world_manager.h"
#include "extensions/renderer/scripts_run_info.h"
#include "extensions/renderer/trace_util.h"
#include "third_party/blink/public/platform/web_isolated_world_info.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_script_execution_callback.h"
#include "third_party/blink/public/web/web_script_source.h"
using perfetto::protos::pbzero::ChromeTrackEvent;
namespace extensions {
namespace {
const int64_t kInvalidRequestId = -1;
// The id of the next pending injection.
int64_t g_next_pending_id = 0;
} // namespace
// Watches for the deletion of a RenderFrame, after which is_valid will return
// false.
class ScriptInjection::FrameWatcher : public content::RenderFrameObserver {
public:
FrameWatcher(content::RenderFrame* render_frame,
ScriptInjection* injection)
: content::RenderFrameObserver(render_frame),
injection_(injection) {}
FrameWatcher(const FrameWatcher&) = delete;
FrameWatcher& operator=(const FrameWatcher&) = delete;
~FrameWatcher() override {}
private:
void WillDetach(blink::DetachReason detach_reason) override {
injection_->invalidate_render_frame();
}
void OnDestruct() override { injection_->invalidate_render_frame(); }
raw_ptr<ScriptInjection> injection_;
};
ScriptInjection::ScriptInjection(
std::unique_ptr<ScriptInjector> injector,
content::RenderFrame* render_frame,
std::unique_ptr<const InjectionHost> injection_host,
mojom::RunLocation run_location,
bool log_activity)
: injector_(std::move(injector)),
render_frame_(render_frame),
injection_host_(std::move(injection_host)),
run_location_(run_location),
request_id_(kInvalidRequestId),
complete_(false),
did_inject_js_(false),
log_activity_(log_activity),
frame_watcher_(new FrameWatcher(render_frame, this)) {
CHECK(injection_host_.get());
TRACE_EVENT_BEGIN(
"extensions", "ScriptInjection", perfetto::Track::FromPointer(this),
ChromeTrackEvent::kRenderProcessHost, content::RenderThread::Get(),
ChromeTrackEvent::kChromeExtensionId,
ExtensionIdForTracing(host_id().id));
}
ScriptInjection::~ScriptInjection() {
if (!complete_)
NotifyWillNotInject(ScriptInjector::WONT_INJECT);
TRACE_EVENT_END("extensions", perfetto::Track::FromPointer(this),
ChromeTrackEvent::kRenderProcessHost,
content::RenderThread::Get(),
ChromeTrackEvent::kChromeExtensionId,
ExtensionIdForTracing(injection_host_ ? host_id().id : ""));
}
ScriptInjection::InjectionResult ScriptInjection::TryToInject(
mojom::RunLocation current_location,
ScriptsRunInfo* scripts_run_info,
StatusUpdatedCallback async_updated_callback) {
if (current_location == mojom::RunLocation::kUndefined &&
render_frame_->IsInFencedFrameTree() && render_frame_->IsMainFrame()) {
// Fenced frames do not navigate to about:blank by default the way iframes
// do. They cannot accept script injections until they perform an initial
// navigation.
NotifyWillNotInject(ScriptInjector::NOT_ALLOWED);
return INJECTION_FINISHED; // We're done.
}
if (current_location < run_location_)
return INJECTION_WAITING; // Wait for the right location.
if (request_id_ != kInvalidRequestId) {
// We're waiting for permission right now, try again later.
return INJECTION_WAITING;
}
if (!injection_host_) {
NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED);
return INJECTION_FINISHED; // We're done.
}
blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame();
switch (injector_->CanExecuteOnFrame(
injection_host_.get(), web_frame,
ExtensionFrameHelper::Get(render_frame_)->tab_id())) {
case PermissionsData::PageAccess::kDenied:
NotifyWillNotInject(ScriptInjector::NOT_ALLOWED);
return INJECTION_FINISHED; // We're done.
case PermissionsData::PageAccess::kWithheld:
RequestPermissionFromBrowser(std::move(async_updated_callback));
return INJECTION_WAITING; // Wait around for permission.
case PermissionsData::PageAccess::kAllowed:
InjectionResult result = Inject(scripts_run_info);
// If the injection is blocked, we need to set the manager so we can
// notify it upon completion.
if (result == INJECTION_BLOCKED)
async_completion_callback_ = std::move(async_updated_callback);
return result;
}
NOTREACHED();
}
ScriptInjection::InjectionResult ScriptInjection::OnPermissionGranted(
ScriptsRunInfo* scripts_run_info) {
if (!injection_host_) {
NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED);
return INJECTION_FINISHED;
}
return Inject(scripts_run_info);
}
void ScriptInjection::OnHostRemoved() {
injection_host_.reset(nullptr);
}
void ScriptInjection::RequestPermissionFromBrowser(
StatusUpdatedCallback async_updated_callback) {
// If we are just notifying the browser of the injection, then send an
// invalid request (which is treated like a notification).
request_id_ = g_next_pending_id++;
ExtensionFrameHelper::Get(render_frame_)
->GetLocalFrameHost()
->RequestScriptInjectionPermission(
host_id().id, injector_->script_type(), run_location_,
base::BindOnce(&ScriptInjection::HandlePermission,
weak_ptr_factory_.GetWeakPtr(),
std::move(async_updated_callback)));
}
void ScriptInjection::NotifyWillNotInject(
ScriptInjector::InjectFailureReason reason) {
complete_ = true;
injector_->OnWillNotInject(reason);
}
void ScriptInjection::HandlePermission(
StatusUpdatedCallback async_updated_callback,
bool granted) {
if (!granted)
return;
std::move(async_updated_callback).Run(InjectionStatus::kPermitted, this);
}
ScriptInjection::InjectionResult ScriptInjection::Inject(
ScriptsRunInfo* scripts_run_info) {
DCHECK(injection_host_);
DCHECK(scripts_run_info);
DCHECK(!complete_);
bool should_inject_js = injector_->ShouldInjectJs(
run_location_, scripts_run_info->executing_scripts[host_id().id]);
bool should_inject_or_remove_css = injector_->ShouldInjectOrRemoveCss(
run_location_, scripts_run_info->injected_stylesheets[host_id().id]);
// This can happen if the extension specified a script to
// be run in multiple rules, and the script has already run.
// See crbug.com/631247.
if (!should_inject_js && !should_inject_or_remove_css) {
return INJECTION_FINISHED;
}
if (should_inject_js)
InjectJs(&(scripts_run_info->executing_scripts[host_id().id]),
&(scripts_run_info->num_js));
if (should_inject_or_remove_css)
InjectOrRemoveCss(&(scripts_run_info->injected_stylesheets[host_id().id]),
&(scripts_run_info->num_css));
complete_ = did_inject_js_ || !should_inject_js;
if (complete_) {
injector_->OnInjectionComplete(std::move(execution_result_), run_location_);
} else {
++scripts_run_info->num_blocking_js;
}
return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED;
}
void ScriptInjection::InjectJs(std::set<std::string>* executing_scripts,
size_t* num_injected_js_scripts) {
TRACE_RENDERER_EXTENSION_EVENT("ScriptInjection::InjectJs", host_id().id);
DCHECK(!did_inject_js_);
std::vector<blink::WebScriptSource> sources = injector_->GetJsSources(
run_location_, executing_scripts, num_injected_js_scripts);
DCHECK(!sources.empty());
base::ElapsedTimer exec_timer;
// For content scripts executing during page load, we run them asynchronously
// in order to reduce UI jank experienced by the user. (We don't do this for
// kDocumentStart scripts, because there's no UI to jank until after those
// run, so we run them as soon as we can.)
// Note: We could potentially also run deferred and browser-driven scripts
// asynchronously; however, these are rare enough that there probably isn't
// UI jank. If this changes, we can update this.
bool should_execute_asynchronously =
injector_->script_type() == mojom::InjectionType::kContentScript &&
(run_location_ == mojom::RunLocation::kDocumentEnd ||
run_location_ == mojom::RunLocation::kDocumentIdle);
blink::mojom::EvaluationTiming execution_option =
should_execute_asynchronously
? blink::mojom::EvaluationTiming::kAsynchronous
: blink::mojom::EvaluationTiming::kSynchronous;
ExtensionFrameHelper* frame_helper = ExtensionFrameHelper::Get(render_frame_);
CHECK(frame_helper);
std::optional<std::string> world_id = injector_->GetExecutionWorldId();
const std::string& host_string_id = injection_host_->id().id;
const mojom::ExecutionWorld execution_world = injector_->GetExecutionWorld();
// We limit the number of user script worlds that may be active on a given
// document. Check if this is within bounds.
if (execution_world == mojom::ExecutionWorld::kUserScript) {
const std::set<std::optional<std::string>>* active_user_script_worlds =
frame_helper->GetActiveUserScriptWorlds(host_string_id);
// TODO(devlin): It'd be nice to isolate this logic into
// IsolatedWorldManager instead of having it shared with
// ExtensionFrameHelper and this class, but ExtensionFrameHelper is the
// one that's able to track this information (as a per-frame object that
// can be cleared on a new document). If we had something like
// DocumentUserData on the renderer, that would be a better fit.
constexpr size_t kMaxActiveUserScriptWorldCount = 10;
if (active_user_script_worlds &&
active_user_script_worlds->size() >= kMaxActiveUserScriptWorldCount &&
!base::Contains(*active_user_script_worlds, world_id)) {
// If there are 10 or more active user script worlds, we use the default
// world for future injections.
// Note: This *can* mean that up to 11 user script worlds for this
// exist on the document, since the first ten can correspond to "named"
// worlds and then we'll create a new one for the default world. However,
// that's better than needing to choose a new world "at random" to inject
// in.
world_id = std::nullopt;
}
// Register the world as active. This is a no-op if it's already registered.
frame_helper->AddActiveUserScriptWorld(host_string_id, world_id);
}
int32_t blink_world_id = blink::kMainDOMWorldId;
switch (execution_world) {
case mojom::ExecutionWorld::kIsolated:
case mojom::ExecutionWorld::kUserScript:
blink_world_id =
IsolatedWorldManager::GetInstance().GetOrCreateIsolatedWorldForHost(
*injection_host_, execution_world, world_id);
if (injection_host_->id().type == mojom::HostID::HostType::kExtensions &&
log_activity_) {
DOMActivityLogger::AttachToWorld(blink_world_id, host_string_id);
}
break;
case mojom::ExecutionWorld::kMain:
blink_world_id = blink::kMainDOMWorldId;
break;
}
render_frame_->GetWebFrame()->RequestExecuteScript(
blink_world_id, sources, injector_->IsUserGesture(), execution_option,
blink::mojom::LoadEventBlockingOption::kBlock,
base::BindOnce(&ScriptInjection::OnJsInjectionCompleted,
weak_ptr_factory_.GetWeakPtr()),
blink::BackForwardCacheAware::kPossiblyDisallow,
injector_->ExpectsResults(), injector_->ShouldWaitForPromise());
}
void ScriptInjection::OnJsInjectionCompleted(std::optional<base::Value> value,
base::TimeTicks start_time) {
DCHECK(!did_inject_js_);
base::TimeTicks timestamp(base::TimeTicks::Now());
std::optional<base::TimeDelta> elapsed;
// If the script will never execute (such as if the context is destroyed),
// `start_time` is null. Only log a time for execution if the script, in fact,
// executed.
if (!start_time.is_null())
elapsed = timestamp - start_time;
if (injection_host_->id().type == mojom::HostID::HostType::kExtensions &&
elapsed) {
UMA_HISTOGRAM_TIMES("Extensions.InjectedScriptExecutionTime", *elapsed);
switch (run_location_) {
case mojom::RunLocation::kDocumentStart:
UMA_HISTOGRAM_TIMES(
"Extensions.InjectedScriptExecutionTime.DocumentStart", *elapsed);
break;
case mojom::RunLocation::kDocumentEnd:
UMA_HISTOGRAM_TIMES(
"Extensions.InjectedScriptExecutionTime.DocumentEnd", *elapsed);
break;
case mojom::RunLocation::kDocumentIdle:
UMA_HISTOGRAM_TIMES(
"Extensions.InjectedScriptExecutionTime.DocumentIdle", *elapsed);
break;
default:
break;
}
}
execution_result_ = std::move(value);
did_inject_js_ = true;
// If |async_completion_callback_| is set, it means the script finished
// asynchronously, and we should run it.
if (!async_completion_callback_.is_null()) {
complete_ = true;
injector_->OnInjectionComplete(std::move(execution_result_), run_location_);
// Warning: this object can be destroyed after this line!
std::move(async_completion_callback_).Run(InjectionStatus::kFinished, this);
}
}
void ScriptInjection::InjectOrRemoveCss(
std::set<std::string>* injected_stylesheets,
size_t* num_injected_stylesheets) {
std::vector<ScriptInjector::CSSSource> css_sources = injector_->GetCssSources(
run_location_, injected_stylesheets, num_injected_stylesheets);
blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame();
auto blink_css_origin = blink::WebCssOrigin::kAuthor;
switch (injector_->GetCssOrigin()) {
case mojom::CSSOrigin::kUser:
blink_css_origin = blink::WebCssOrigin::kUser;
break;
case mojom::CSSOrigin::kAuthor:
blink_css_origin = blink::WebCssOrigin::kAuthor;
break;
}
mojom::CSSInjection::Operation operation =
injector_->GetCSSInjectionOperation();
for (const auto& source : css_sources) {
switch (operation) {
case mojom::CSSInjection::Operation::kRemove:
DCHECK(!source.key.IsEmpty())
<< "An injection key is required to remove CSS.";
// CSS deletion can be thought of as the inverse of CSS injection
// (i.e. x - y = x + -y and x | y = ~(~x & ~y)), so it is handled here
// in the injection function.
//
// TODO(crbug.com/40144586): Extend this API's capabilities to
// also remove CSS added by content scripts?
web_frame->GetDocument().RemoveInsertedStyleSheet(source.key,
blink_css_origin);
break;
case mojom::CSSInjection::Operation::kAdd:
web_frame->GetDocument().InsertStyleSheet(
source.code, &source.key, blink_css_origin,
blink::BackForwardCacheAware::kPossiblyDisallow);
break;
}
}
}
} // namespace extensions
|