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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/devtools/protocol/service_worker_handler.h"
#include <memory>
#include <variant>
#include "base/containers/flat_set.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/background_sync/background_sync_context_impl.h"
#include "content/browser/background_sync/background_sync_manager.h"
#include "content/browser/devtools/service_worker_devtools_agent_host.h"
#include "content/browser/devtools/service_worker_devtools_manager.h"
#include "content/browser/devtools/shared_worker_devtools_manager.h"
#include "content/browser/renderer_host/frame_tree.h"
#include "content/browser/service_worker/service_worker_context_watcher.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/browser/storage_partition_impl_map.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/push_messaging/push_messaging_status.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_object.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
namespace protocol {
namespace {
const std::string GetVersionRunningStatusString(
blink::EmbeddedWorkerStatus running_status) {
switch (running_status) {
case blink::EmbeddedWorkerStatus::kStopped:
return ServiceWorker::ServiceWorkerVersionRunningStatusEnum::Stopped;
case blink::EmbeddedWorkerStatus::kStarting:
return ServiceWorker::ServiceWorkerVersionRunningStatusEnum::Starting;
case blink::EmbeddedWorkerStatus::kRunning:
return ServiceWorker::ServiceWorkerVersionRunningStatusEnum::Running;
case blink::EmbeddedWorkerStatus::kStopping:
return ServiceWorker::ServiceWorkerVersionRunningStatusEnum::Stopping;
default:
NOTREACHED();
}
}
const std::string GetVersionStatusString(
content::ServiceWorkerVersion::Status status) {
switch (status) {
case content::ServiceWorkerVersion::NEW:
return ServiceWorker::ServiceWorkerVersionStatusEnum::New;
case content::ServiceWorkerVersion::INSTALLING:
return ServiceWorker::ServiceWorkerVersionStatusEnum::Installing;
case content::ServiceWorkerVersion::INSTALLED:
return ServiceWorker::ServiceWorkerVersionStatusEnum::Installed;
case content::ServiceWorkerVersion::ACTIVATING:
return ServiceWorker::ServiceWorkerVersionStatusEnum::Activating;
case content::ServiceWorkerVersion::ACTIVATED:
return ServiceWorker::ServiceWorkerVersionStatusEnum::Activated;
case content::ServiceWorkerVersion::REDUNDANT:
return ServiceWorker::ServiceWorkerVersionStatusEnum::Redundant;
default:
NOTREACHED();
}
}
Response CreateDomainNotEnabledErrorResponse() {
return Response::ServerError("ServiceWorker domain not enabled");
}
Response CreateContextErrorResponse() {
return Response::ServerError("Could not connect to the context");
}
Response CreateInvalidVersionIdErrorResponse() {
return Response::InvalidParams("Invalid version ID");
}
void DidFindRegistrationForDispatchSyncEvent(
scoped_refptr<BackgroundSyncContextImpl> sync_context,
const std::string& tag,
bool last_chance,
blink::ServiceWorkerStatusCode status,
scoped_refptr<content::ServiceWorkerRegistration> registration) {
if (status != blink::ServiceWorkerStatusCode::kOk ||
!registration->active_version())
return;
BackgroundSyncManager* background_sync_manager =
sync_context->background_sync_manager();
scoped_refptr<content::ServiceWorkerVersion> version(
registration->active_version());
// Keep the registration while dispatching the sync event.
background_sync_manager->EmulateDispatchSyncEvent(
tag, std::move(version), last_chance, base::DoNothing());
}
void DidFindRegistrationForDispatchPeriodicSyncEvent(
scoped_refptr<BackgroundSyncContextImpl> sync_context,
const std::string& tag,
blink::ServiceWorkerStatusCode status,
scoped_refptr<content::ServiceWorkerRegistration> registration) {
if (status != blink::ServiceWorkerStatusCode::kOk ||
!registration->active_version()) {
return;
}
BackgroundSyncManager* background_sync_manager =
sync_context->background_sync_manager();
scoped_refptr<content::ServiceWorkerVersion> version(
registration->active_version());
// Keep the registration while dispatching the sync event.
background_sync_manager->EmulateDispatchPeriodicSyncEvent(
tag, std::move(version), base::DoNothing());
}
} // namespace
ServiceWorkerHandler::ServiceWorkerHandler(bool allow_inspect_worker)
: DevToolsDomainHandler(ServiceWorker::Metainfo::domainName),
allow_inspect_worker_(allow_inspect_worker),
enabled_(false),
browser_context_(nullptr),
storage_partition_(nullptr) {}
ServiceWorkerHandler::~ServiceWorkerHandler() = default;
void ServiceWorkerHandler::Wire(UberDispatcher* dispatcher) {
frontend_ = std::make_unique<ServiceWorker::Frontend>(dispatcher->channel());
ServiceWorker::Dispatcher::wire(dispatcher, this);
}
void ServiceWorkerHandler::SetRenderer(int process_host_id,
RenderFrameHostImpl* frame_host) {
RenderProcessHost* process_host = RenderProcessHost::FromID(process_host_id);
// Do not call UpdateHosts yet, wait for load to commit.
if (!process_host) {
ClearForceUpdate();
context_ = nullptr;
return;
}
storage_partition_ =
static_cast<StoragePartitionImpl*>(process_host->GetStoragePartition());
DCHECK(storage_partition_);
browser_context_ = process_host->GetBrowserContext();
context_ = static_cast<ServiceWorkerContextWrapper*>(
storage_partition_->GetServiceWorkerContext());
}
Response ServiceWorkerHandler::Enable() {
if (enabled_)
return Response::Success();
if (!context_)
return CreateContextErrorResponse();
enabled_ = true;
context_watcher_ = base::MakeRefCounted<ServiceWorkerContextWatcher>(
context_,
base::BindRepeating(&ServiceWorkerHandler::OnWorkerRegistrationUpdated,
weak_factory_.GetWeakPtr()),
base::BindRepeating(&ServiceWorkerHandler::OnWorkerVersionUpdated,
weak_factory_.GetWeakPtr()),
base::BindRepeating(&ServiceWorkerHandler::OnErrorReported,
weak_factory_.GetWeakPtr()));
context_watcher_->Start();
return Response::Success();
}
Response ServiceWorkerHandler::Disable() {
if (!enabled_)
return Response::Success();
enabled_ = false;
ClearForceUpdate();
DCHECK(context_watcher_);
context_watcher_->Stop();
context_watcher_ = nullptr;
return Response::Success();
}
Response ServiceWorkerHandler::Unregister(const std::string& scope_url) {
if (!enabled_)
return CreateDomainNotEnabledErrorResponse();
if (!context_)
return CreateContextErrorResponse();
GURL url(scope_url);
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(url));
context_->UnregisterServiceWorker(url, key, base::DoNothing());
return Response::Success();
}
Response ServiceWorkerHandler::StartWorker(const std::string& scope_url) {
if (!enabled_)
return CreateDomainNotEnabledErrorResponse();
if (!context_)
return CreateContextErrorResponse();
context_->StartActiveServiceWorker(
GURL(scope_url),
blink::StorageKey::CreateFirstParty(url::Origin::Create(GURL(scope_url))),
base::DoNothing());
return Response::Success();
}
Response ServiceWorkerHandler::SkipWaiting(const std::string& scope_url) {
if (!enabled_)
return CreateDomainNotEnabledErrorResponse();
if (!context_)
return CreateContextErrorResponse();
context_->SkipWaitingWorker(GURL(scope_url),
blink::StorageKey::CreateFirstParty(
url::Origin::Create(GURL(scope_url))));
return Response::Success();
}
Response ServiceWorkerHandler::StopWorker(const std::string& version_id) {
if (!enabled_)
return CreateDomainNotEnabledErrorResponse();
if (!context_)
return CreateContextErrorResponse();
int64_t id = 0;
if (!base::StringToInt64(version_id, &id))
return CreateInvalidVersionIdErrorResponse();
if (content::ServiceWorkerVersion* version = context_->GetLiveVersion(id)) {
version->StopWorker(base::DoNothing());
}
return Response::Success();
}
void ServiceWorkerHandler::StopAllWorkers(
std::unique_ptr<StopAllWorkersCallback> callback) {
if (!enabled_) {
callback->sendFailure(CreateDomainNotEnabledErrorResponse());
return;
}
if (!context_) {
callback->sendFailure(CreateContextErrorResponse());
return;
}
context_->StopAllServiceWorkers(base::BindOnce(
&StopAllWorkersCallback::sendSuccess, std::move(callback)));
}
Response ServiceWorkerHandler::UpdateRegistration(
const std::string& scope_url) {
if (!enabled_)
return CreateDomainNotEnabledErrorResponse();
if (!context_)
return CreateContextErrorResponse();
context_->UpdateRegistration(GURL(scope_url),
blink::StorageKey::CreateFirstParty(
url::Origin::Create(GURL(scope_url))));
return Response::Success();
}
Response ServiceWorkerHandler::InspectWorker(const std::string& version_id) {
if (!enabled_)
return CreateDomainNotEnabledErrorResponse();
if (!context_)
return CreateContextErrorResponse();
if (!allow_inspect_worker_)
return Response::ServerError("Permission denied");
int64_t id = blink::mojom::kInvalidServiceWorkerVersionId;
if (!base::StringToInt64(version_id, &id))
return CreateInvalidVersionIdErrorResponse();
if (content::ServiceWorkerVersion* version = context_->GetLiveVersion(id)) {
OpenNewDevToolsWindow(
version->embedded_worker()->process_id(),
version->embedded_worker()->worker_devtools_agent_route_id());
}
return Response::Success();
}
Response ServiceWorkerHandler::SetForceUpdateOnPageLoad(
bool force_update_on_page_load) {
if (!context_)
return CreateContextErrorResponse();
context_->SetForceUpdateOnPageLoad(force_update_on_page_load);
return Response::Success();
}
Response ServiceWorkerHandler::DeliverPushMessage(
const std::string& origin,
const std::string& registration_id,
const std::string& data) {
if (!enabled_)
return CreateDomainNotEnabledErrorResponse();
if (!browser_context_)
return CreateContextErrorResponse();
int64_t id = 0;
if (!base::StringToInt64(registration_id, &id))
return CreateInvalidVersionIdErrorResponse();
std::optional<std::string> payload;
if (data.size() > 0)
payload = data;
browser_context_->DeliverPushMessage(GURL(origin), id,
/* message_id= */ std::string(),
std::move(payload), base::DoNothing());
return Response::Success();
}
Response ServiceWorkerHandler::DispatchSyncEvent(
const std::string& origin,
const std::string& registration_id,
const std::string& tag,
bool last_chance) {
if (!enabled_)
return CreateDomainNotEnabledErrorResponse();
if (!storage_partition_)
return CreateContextErrorResponse();
int64_t id = 0;
if (!base::StringToInt64(registration_id, &id))
return CreateInvalidVersionIdErrorResponse();
scoped_refptr<BackgroundSyncContextImpl> sync_context =
base::WrapRefCounted(storage_partition_->GetBackgroundSyncContext());
context_->FindReadyRegistrationForId(
id,
blink::StorageKey::CreateFirstParty(url::Origin::Create(GURL(origin))),
base::BindOnce(&DidFindRegistrationForDispatchSyncEvent,
std::move(sync_context), tag, last_chance));
return Response::Success();
}
Response ServiceWorkerHandler::DispatchPeriodicSyncEvent(
const std::string& origin,
const std::string& registration_id,
const std::string& tag) {
if (!enabled_)
return CreateDomainNotEnabledErrorResponse();
if (!storage_partition_)
return CreateContextErrorResponse();
int64_t id = 0;
if (!base::StringToInt64(registration_id, &id))
return CreateInvalidVersionIdErrorResponse();
scoped_refptr<BackgroundSyncContextImpl> sync_context =
base::WrapRefCounted(storage_partition_->GetBackgroundSyncContext());
context_->FindReadyRegistrationForId(
id,
blink::StorageKey::CreateFirstParty(url::Origin::Create(GURL(origin))),
base::BindOnce(&DidFindRegistrationForDispatchPeriodicSyncEvent,
std::move(sync_context), tag));
return Response::Success();
}
void ServiceWorkerHandler::OpenNewDevToolsWindow(int process_id,
int devtools_agent_route_id) {
scoped_refptr<DevToolsAgentHostImpl> agent_host(
ServiceWorkerDevToolsManager::GetInstance()
->GetDevToolsAgentHostForWorker(process_id, devtools_agent_route_id));
if (!agent_host.get())
return;
agent_host->Inspect();
}
void ServiceWorkerHandler::OnWorkerRegistrationUpdated(
const std::vector<ServiceWorkerRegistrationInfo>& registrations) {
using Registration = ServiceWorker::ServiceWorkerRegistration;
auto result = std::make_unique<protocol::Array<Registration>>();
for (const auto& registration : registrations) {
result->emplace_back(
Registration::Create()
.SetRegistrationId(
base::NumberToString(registration.registration_id))
.SetScopeURL(registration.scope.spec())
.SetIsDeleted(registration.delete_flag ==
ServiceWorkerRegistrationInfo::IS_DELETED)
.Build());
}
frontend_->WorkerRegistrationUpdated(std::move(result));
}
void ServiceWorkerHandler::OnWorkerVersionUpdated(
const std::vector<ServiceWorkerVersionInfo>& versions) {
using Version = ServiceWorker::ServiceWorkerVersion;
auto result = std::make_unique<protocol::Array<Version>>();
for (const auto& version : versions) {
base::flat_set<std::string> client_set;
for (const auto& client : version.clients) {
if (std::holds_alternative<GlobalRenderFrameHostId>(client.second)) {
WebContents* web_contents = WebContentsImpl::FromRenderFrameHostID(
std::get<GlobalRenderFrameHostId>(client.second));
// There is a possibility that the frame is already deleted
// because of the thread hopping.
if (!web_contents)
continue;
client_set.insert(
DevToolsAgentHost::GetOrCreateFor(web_contents)->GetId());
}
}
auto clients = std::make_unique<protocol::Array<std::string>>();
for (std::string& client : client_set)
clients->emplace_back(std::move(client));
std::unique_ptr<Version> version_value =
Version::Create()
.SetVersionId(base::NumberToString(version.version_id))
.SetRegistrationId(base::NumberToString(version.registration_id))
.SetScriptURL(version.script_url.spec())
.SetRunningStatus(
GetVersionRunningStatusString(version.running_status))
.SetStatus(GetVersionStatusString(version.status))
.SetScriptLastModified(
version.script_last_modified.InSecondsFSinceUnixEpoch())
.SetScriptResponseTime(
version.script_response_time.InSecondsFSinceUnixEpoch())
.SetControlledClients(std::move(clients))
.Build();
scoped_refptr<DevToolsAgentHostImpl> host(
ServiceWorkerDevToolsManager::GetInstance()
->GetDevToolsAgentHostForWorker(
version.process_id,
version.devtools_agent_route_id));
if (host) {
version_value->SetTargetId(host->GetId());
}
if (version.router_rules) {
version_value->SetRouterRules(*version.router_rules);
}
result->emplace_back(std::move(version_value));
}
frontend_->WorkerVersionUpdated(std::move(result));
}
void ServiceWorkerHandler::OnErrorReported(
int64_t registration_id,
int64_t version_id,
const ServiceWorkerContextObserver::ErrorInfo& info) {
frontend_->WorkerErrorReported(
ServiceWorker::ServiceWorkerErrorMessage::Create()
.SetErrorMessage(base::UTF16ToUTF8(info.error_message))
.SetRegistrationId(base::NumberToString(registration_id))
.SetVersionId(base::NumberToString(version_id))
.SetSourceURL(info.source_url.spec())
.SetLineNumber(info.line_number)
.SetColumnNumber(info.column_number)
.Build());
}
void ServiceWorkerHandler::ClearForceUpdate() {
if (context_)
context_->SetForceUpdateOnPageLoad(false);
}
} // namespace protocol
} // namespace content
|