Commit 5970f41e authored by Chris Hamilton's avatar Chris Hamilton Committed by Commit Bot

Add ServiceWorkerToken to service worker code.

This adorns content::ServiceWorkerVersion, blink::ServiceWorkerThread
and blink::ServiceWorkerGlobalScope with a ServiceWorkerToken. The token
is generated in the browser process and communicated to the renderer as
the worker is started.

BUG=1085129, 1096617

Change-Id: I24972cf9be48dd0208042abaa3607807e0f6afed
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2330450Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Commit-Queue: Chris Hamilton <chrisha@chromium.org>
Cr-Commit-Position: refs/heads/master@{#795636}
parent a05c9ecd
......@@ -256,11 +256,10 @@ void WorkerWatcher::OnVersionStartedRunning(
const content::ServiceWorkerRunningInfo& running_info) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// TODO(chrisha): Plumb in the ServiceWorkerToken.
auto worker_node = PerformanceManagerImpl::CreateWorkerNode(
browser_context_id_, WorkerNode::WorkerType::kService,
process_node_source_->GetProcessNode(running_info.render_process_id),
WorkerToken::Create());
WorkerToken(running_info.token.value()));
bool inserted =
service_worker_nodes_.emplace(version_id, std::move(worker_node)).second;
DCHECK(inserted);
......
......@@ -345,8 +345,10 @@ int64_t TestServiceWorkerContext::StartServiceWorker(int worker_process_id) {
// Notify observers.
for (auto& observer : observer_list_) {
observer.OnVersionStartedRunning(version_id,
{worker_url, GURL(), worker_process_id});
observer.OnVersionStartedRunning(
version_id,
content::ServiceWorkerRunningInfo(worker_url, GURL(), worker_process_id,
blink::ServiceWorkerToken::Create()));
}
return version_id;
......
......@@ -864,6 +864,7 @@ void EmbeddedWorkerInstance::Start(
status_ = EmbeddedWorkerStatus::STARTING;
starting_phase_ = ALLOCATING_PROCESS;
network_accessed_for_script_ = false;
token_ = blink::ServiceWorkerToken::Create();
for (auto& observer : listener_list_)
observer.OnStarting();
......@@ -873,6 +874,7 @@ void EmbeddedWorkerInstance::Start(
params->wait_for_debugger = false;
params->subresource_loader_updater =
subresource_loader_updater_.BindNewPipeAndPassReceiver();
params->service_worker_token = token_.value();
// TODO(https://crbug.com/978694): Consider a reset flow since new mojo types
// check is_bound strictly.
......@@ -1373,6 +1375,7 @@ void EmbeddedWorkerInstance::ReleaseProcess() {
status_ = EmbeddedWorkerStatus::STOPPED;
starting_phase_ = NOT_STARTING;
thread_id_ = ServiceWorkerConsts::kInvalidEmbeddedWorkerThreadId;
token_ = base::nullopt;
}
void EmbeddedWorkerInstance::OnSetupFailed(
......
......@@ -17,6 +17,7 @@
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/optional.h"
#include "base/strings/string16.h"
#include "base/threading/sequence_bound.h"
#include "base/time/time.h"
......@@ -31,6 +32,7 @@
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/cache_storage/cache_storage.mojom.h"
#include "third_party/blink/public/mojom/service_worker/controller_service_worker.mojom.h"
#include "third_party/blink/public/mojom/service_worker/embedded_worker.mojom.h"
......@@ -247,6 +249,13 @@ class CONTENT_EXPORT EmbeddedWorkerInstance
std::unique_ptr<blink::PendingURLLoaderFactoryBundle> subresouce_bundle)>;
void CreateFactoryBundles(CreateFactoryBundlesCallback callback);
// Returns the unique token that has been generated to identify this worker
// instance, and its corresponding GlobalScope in the renderer process. If the
// service worker is not currently running, this is base::nullopt.
const base::Optional<blink::ServiceWorkerToken>& token() const {
return token_;
}
private:
typedef base::ObserverList<Listener>::Unchecked ListenerList;
class ScopedLifetimeTracker;
......@@ -399,6 +408,12 @@ class CONTENT_EXPORT EmbeddedWorkerInstance
mojo::Remote<network::mojom::CrossOriginEmbedderPolicyReporter>
coep_reporter_;
// A unique identifier for this service worker instance. This is unique across
// the browser process, but not persistent across service worker restarts.
// This token is set every time the worker starts, and is plumbed through to
// the corresponding ServiceWorkerGlobalScope in the renderer process.
base::Optional<blink::ServiceWorkerToken> token_;
base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
......
......@@ -959,7 +959,8 @@ void ServiceWorkerContextCore::OnRunningStateChanged(
observer_list_->Notify(
FROM_HERE, &ServiceWorkerContextCoreObserver::OnStarted,
version->version_id(), version->scope(),
version->embedded_worker()->process_id(), version->script_url());
version->embedded_worker()->process_id(), version->script_url(),
version->embedded_worker()->token().value());
break;
case EmbeddedWorkerStatus::STOPPING:
observer_list_->Notify(FROM_HERE,
......
......@@ -15,6 +15,7 @@
#include "content/browser/service_worker/service_worker_version.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/service_worker_context_observer.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_container_type.mojom.h"
#include "url/gurl.h"
......@@ -33,7 +34,8 @@ class ServiceWorkerContextCoreObserver {
virtual void OnStarted(int64_t version_id,
const GURL& scope,
int process_id,
const GURL& script_url) {}
const GURL& script_url,
const blink::ServiceWorkerToken& token) {}
virtual void OnStopping(int64_t version_id) {}
virtual void OnStopped(int64_t version_id) {}
// Called when the context core is about to be deleted. After this is called,
......
......@@ -237,10 +237,12 @@ void ServiceWorkerContextWatcher::OnStarting(int64_t version_id) {
OnRunningStateChanged(version_id, EmbeddedWorkerStatus::STARTING);
}
void ServiceWorkerContextWatcher::OnStarted(int64_t version_id,
void ServiceWorkerContextWatcher::OnStarted(
int64_t version_id,
const GURL& scope,
int process_id,
const GURL& script_url) {
const GURL& script_url,
const blink::ServiceWorkerToken& token) {
OnRunningStateChanged(version_id, EmbeddedWorkerStatus::RUNNING);
}
......
......@@ -89,7 +89,8 @@ class CONTENT_EXPORT ServiceWorkerContextWatcher
void OnStarted(int64_t version_id,
const GURL& scope,
int process_id,
const GURL& script_url) override;
const GURL& script_url,
const blink::ServiceWorkerToken& token) override;
void OnStopping(int64_t version_id) override;
void OnStopped(int64_t version_id) override;
void OnVersionStateChanged(int64_t version_id,
......
......@@ -423,14 +423,17 @@ void ServiceWorkerContextWrapper::OnControlleeNavigationCommitted(
render_frame_host_id);
}
void ServiceWorkerContextWrapper::OnStarted(int64_t version_id,
void ServiceWorkerContextWrapper::OnStarted(
int64_t version_id,
const GURL& scope,
int process_id,
const GURL& script_url) {
const GURL& script_url,
const blink::ServiceWorkerToken& token) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto insertion_result = running_service_workers_.insert(std::make_pair(
version_id, ServiceWorkerRunningInfo(script_url, scope, process_id)));
version_id,
ServiceWorkerRunningInfo(script_url, scope, process_id, token)));
DCHECK(insertion_result.second);
const auto& running_info = insertion_result.first->second;
......
......@@ -147,7 +147,8 @@ class CONTENT_EXPORT ServiceWorkerContextWrapper
void OnStarted(int64_t version_id,
const GURL& scope,
int process_id,
const GURL& script_url) override;
const GURL& script_url,
const blink::ServiceWorkerToken& token) override;
void OnStopped(int64_t version_id) override;
void OnDeleteAndStartOver() override;
void OnVersionStateChanged(int64_t version_id,
......
......@@ -283,7 +283,8 @@ class ServiceWorkerInternalsUI::PartitionObserver
void OnStarted(int64_t version_id,
const GURL& scope,
int process_id,
const GURL& script_url) override {
const GURL& script_url,
const blink::ServiceWorkerToken& token) override {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
web_ui_->CallJavascriptFunctionUnsafe(
"serviceworker.onRunningStateChanged");
......
......@@ -6,12 +6,15 @@
namespace content {
ServiceWorkerRunningInfo::ServiceWorkerRunningInfo(const GURL& script_url,
ServiceWorkerRunningInfo::ServiceWorkerRunningInfo(
const GURL& script_url,
const GURL& scope,
int64_t render_process_id)
int64_t render_process_id,
const blink::ServiceWorkerToken& token)
: script_url(script_url),
scope(scope),
render_process_id(render_process_id) {}
render_process_id(render_process_id),
token(token) {}
ServiceWorkerRunningInfo::ServiceWorkerRunningInfo(
ServiceWorkerRunningInfo&& other) noexcept = default;
......
......@@ -7,6 +7,7 @@
#include "content/common/content_export.h"
#include "content/public/common/child_process_host.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_object.mojom.h"
#include "url/gurl.h"
......@@ -16,7 +17,8 @@ namespace content {
struct CONTENT_EXPORT ServiceWorkerRunningInfo {
ServiceWorkerRunningInfo(const GURL& script_url,
const GURL& scope,
int64_t render_process_id);
int64_t render_process_id,
const blink::ServiceWorkerToken& token);
ServiceWorkerRunningInfo(ServiceWorkerRunningInfo&& other) noexcept;
ServiceWorkerRunningInfo& operator=(
ServiceWorkerRunningInfo&& other) noexcept;
......@@ -30,6 +32,9 @@ struct CONTENT_EXPORT ServiceWorkerRunningInfo {
// The ID of the render process on which this service worker lives.
int render_process_id = content::ChildProcessHost::kInvalidUniqueID;
// The token that uniquely identifies this worker.
blink::ServiceWorkerToken token;
};
} // namespace content
......
......@@ -176,6 +176,7 @@ EmbeddedWorkerInstanceClientImpl::BuildStartData(
? blink::WebEmbeddedWorkerStartData::kWaitForDebugger
: blink::WebEmbeddedWorkerStartData::kDontWaitForDebugger;
start_data->devtools_worker_token = params.devtools_worker_token;
start_data->service_worker_token = params.service_worker_token;
return start_data;
}
......
......@@ -22,6 +22,7 @@ import "third_party/blink/public/mojom/service_worker/service_worker_installed_s
import "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom";
import "third_party/blink/public/mojom/service_worker/service_worker_provider.mojom";
import "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom";
import "third_party/blink/public/mojom/tokens/tokens.mojom";
import "third_party/blink/public/mojom/user_agent/user_agent_metadata.mojom";
import "third_party/blink/public/mojom/worker/worker_content_settings_proxy.mojom";
import "third_party/blink/public/mojom/web_feature/web_feature.mojom";
......@@ -114,6 +115,12 @@ struct EmbeddedWorkerStartParams {
// Used for updating subresource loaders after NetworkService crash etc.
pending_receiver<SubresourceLoaderUpdater> subresource_loader_updater;
// A unique token identifying this ServiceWorker. In can be used to identify
// this particular ServiceWorker in subsequent renderer -> browser messages
// that aren't on the EmbeddedWorkerInstanceHost interface. This is not
// persistent across worker restarts.
ServiceWorkerToken service_worker_token;
};
// Holds timing information about the start worker sequence for UMA.
......
......@@ -33,6 +33,7 @@
#include "base/unguessable_token.h"
#include "services/network/public/mojom/ip_address_space.mojom-shared.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom-shared.h"
#include "third_party/blink/public/platform/web_fetch_client_settings_object.h"
......@@ -59,6 +60,10 @@ struct WebEmbeddedWorkerStartData {
WebFetchClientSettingsObject outside_fetch_client_settings_object;
// Unique token that identifies this worker across the browser and renderer
// processes. This is not persistent across worker restarts.
blink::ServiceWorkerToken service_worker_token;
explicit WebEmbeddedWorkerStartData(
WebFetchClientSettingsObject outside_fetch_client_settings_object)
: wait_for_debugger_mode(kDontWaitForDebugger),
......
......@@ -231,7 +231,7 @@ void WebEmbeddedWorkerImpl::StartWorkerThread(
std::make_unique<ServiceWorkerGlobalScopeProxy>(
*this, *worker_context_client_, initiator_thread_task_runner),
std::move(installed_scripts_manager), std::move(cache_storage_remote),
initiator_thread_task_runner);
initiator_thread_task_runner, worker_start_data->service_worker_token);
auto devtools_params = std::make_unique<WorkerDevToolsParams>();
devtools_params->devtools_worker_token =
......
......@@ -193,7 +193,8 @@ ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::Create(
std::unique_ptr<ServiceWorkerInstalledScriptsManager>
installed_scripts_manager,
mojo::PendingRemote<mojom::blink::CacheStorage> cache_storage_remote,
base::TimeTicks time_origin) {
base::TimeTicks time_origin,
const ServiceWorkerToken& service_worker_token) {
#if DCHECK_IS_ON()
// If the script is being loaded via script streaming, the script is not yet
// loaded.
......@@ -210,7 +211,7 @@ ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::Create(
return MakeGarbageCollected<ServiceWorkerGlobalScope>(
std::move(creation_params), thread, std::move(installed_scripts_manager),
std::move(cache_storage_remote), time_origin);
std::move(cache_storage_remote), time_origin, service_worker_token);
}
ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(
......@@ -219,13 +220,15 @@ ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(
std::unique_ptr<ServiceWorkerInstalledScriptsManager>
installed_scripts_manager,
mojo::PendingRemote<mojom::blink::CacheStorage> cache_storage_remote,
base::TimeTicks time_origin)
base::TimeTicks time_origin,
const ServiceWorkerToken& service_worker_token)
: WorkerGlobalScope(std::move(creation_params),
thread,
time_origin,
ukm::kInvalidSourceId),
installed_scripts_manager_(std::move(installed_scripts_manager)),
cache_storage_remote_(std::move(cache_storage_remote)) {
cache_storage_remote_(std::move(cache_storage_remote)),
token_(service_worker_token) {
// Create the event queue. At this point its timer is not started. It will be
// started by DidEvaluateScript().
//
......
......@@ -38,6 +38,7 @@
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/network/public/mojom/network_context.mojom-blink-forward.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/cache_storage/cache_storage.mojom-blink-forward.h"
#include "third_party/blink/public/mojom/service_worker/controller_service_worker.mojom-blink.h"
#include "third_party/blink/public/mojom/service_worker/service_worker.mojom-blink.h"
......@@ -95,14 +96,16 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
std::unique_ptr<GlobalScopeCreationParams>,
std::unique_ptr<ServiceWorkerInstalledScriptsManager>,
mojo::PendingRemote<mojom::blink::CacheStorage>,
base::TimeTicks time_origin);
base::TimeTicks time_origin,
const ServiceWorkerToken& service_worker_token);
ServiceWorkerGlobalScope(
std::unique_ptr<GlobalScopeCreationParams>,
ServiceWorkerThread*,
std::unique_ptr<ServiceWorkerInstalledScriptsManager>,
mojo::PendingRemote<mojom::blink::CacheStorage>,
base::TimeTicks time_origin);
base::TimeTicks time_origin,
const ServiceWorkerToken& service_worker_token);
~ServiceWorkerGlobalScope() override;
// ExecutionContext overrides:
......@@ -325,6 +328,10 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
ResourceLoadScheduler::ThrottleOptionOverride GetThrottleOptionOverride()
const override;
// TODO(chrisha): Lift this up to WorkerGlobalScope once all worker types
// have tokens.
const ServiceWorkerToken& token() const { return token_; }
private:
void importScripts(const Vector<String>& urls, ExceptionState&) override;
SingleCachedMetadataHandler* CreateWorkerScriptCachedMetadataHandler(
......@@ -710,6 +717,11 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
HeapMojoWrapperMode::kWithoutContextObserver,
std::unique_ptr<CrossOriginResourcePolicyChecker>>
controller_receivers_{this, this};
// Token that uniquely identifies this service worker. Corresponds to the
// same value in the browser representation of this object. This is not
// persistent across worker restarts.
const ServiceWorkerToken token_;
};
template <>
......
......@@ -47,14 +47,16 @@ ServiceWorkerThread::ServiceWorkerThread(
installed_scripts_manager,
mojo::PendingRemote<mojom::blink::CacheStorage> cache_storage_remote,
scoped_refptr<base::SingleThreadTaskRunner>
parent_thread_default_task_runner)
parent_thread_default_task_runner,
const blink::ServiceWorkerToken& service_worker_token)
: WorkerThread(*global_scope_proxy,
std::move(parent_thread_default_task_runner)),
global_scope_proxy_(std::move(global_scope_proxy)),
worker_backing_thread_(std::make_unique<WorkerBackingThread>(
ThreadCreationParams(GetThreadType()))),
installed_scripts_manager_(std::move(installed_scripts_manager)),
cache_storage_remote_(std::move(cache_storage_remote)) {}
cache_storage_remote_(std::move(cache_storage_remote)),
service_worker_token_(service_worker_token) {}
ServiceWorkerThread::~ServiceWorkerThread() {
global_scope_proxy_->Detach();
......@@ -73,7 +75,7 @@ WorkerOrWorkletGlobalScope* ServiceWorkerThread::CreateWorkerGlobalScope(
std::unique_ptr<GlobalScopeCreationParams> creation_params) {
return ServiceWorkerGlobalScope::Create(
this, std::move(creation_params), std::move(installed_scripts_manager_),
std::move(cache_storage_remote_), time_origin_);
std::move(cache_storage_remote_), time_origin_, service_worker_token_);
}
} // namespace blink
......@@ -32,6 +32,7 @@
#include <memory>
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/cache_storage/cache_storage.mojom-blink-forward.h"
#include "third_party/blink/renderer/core/workers/worker_thread.h"
#include "third_party/blink/renderer/modules/modules_export.h"
......@@ -53,7 +54,8 @@ class MODULES_EXPORT ServiceWorkerThread final : public WorkerThread {
std::unique_ptr<ServiceWorkerInstalledScriptsManager>,
mojo::PendingRemote<mojom::blink::CacheStorage> cache_storage_remote,
scoped_refptr<base::SingleThreadTaskRunner>
parent_thread_default_task_runner);
parent_thread_default_task_runner,
const ServiceWorkerToken& service_worker_token);
~ServiceWorkerThread() override;
WorkerBackingThread& GetWorkerBackingThread() override {
......@@ -77,6 +79,8 @@ class MODULES_EXPORT ServiceWorkerThread final : public WorkerThread {
std::unique_ptr<ServiceWorkerInstalledScriptsManager>
installed_scripts_manager_;
mojo::PendingRemote<mojom::blink::CacheStorage> cache_storage_remote_;
const ServiceWorkerToken service_worker_token_;
};
} // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment