Commit 1b396618 authored by Sigurdur Asgeirsson's avatar Sigurdur Asgeirsson Committed by Commit Bot

PM: Small readability improvements in worker code.

Change-Id: I6fbf2bdfcba2445997dd55e102dac491b2f981d0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2517622
Commit-Queue: Sigurður Ásgeirsson <siggi@chromium.org>
Reviewed-by: default avatarPatrick Monette <pmonette@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823670}
parent 22c2cd98
...@@ -19,10 +19,12 @@ class ServiceWorkerContextAdapter::RunningServiceWorker ...@@ -19,10 +19,12 @@ class ServiceWorkerContextAdapter::RunningServiceWorker
: content::RenderProcessHostObserver { : content::RenderProcessHostObserver {
public: public:
RunningServiceWorker(int64_t version_id, RunningServiceWorker(int64_t version_id,
content::RenderProcessHost* worker_process_host,
ServiceWorkerContextAdapter* adapter); ServiceWorkerContextAdapter* adapter);
~RunningServiceWorker() override; ~RunningServiceWorker() override;
void Subscribe(content::RenderProcessHost* worker_process_host);
void Unsubscribe();
void RenderProcessExited( void RenderProcessExited(
content::RenderProcessHost* host, content::RenderProcessHost* host,
const content::ChildProcessTerminationInfo& info) override; const content::ChildProcessTerminationInfo& info) override;
...@@ -30,31 +32,43 @@ class ServiceWorkerContextAdapter::RunningServiceWorker ...@@ -30,31 +32,43 @@ class ServiceWorkerContextAdapter::RunningServiceWorker
private: private:
// The version ID of the service worker. // The version ID of the service worker.
int version_id_; int const version_id_;
// The adapter that owns |this|. Notified when RenderProcessExited() is // The adapter that owns |this|. Notified when RenderProcessExited() is
// called. // called.
ServiceWorkerContextAdapter* const adapter_; ServiceWorkerContextAdapter* const adapter_;
// The render process host this instance is observing.
ScopedObserver<content::RenderProcessHost, content::RenderProcessHostObserver> content::RenderProcessHost* observing_ = nullptr;
scoped_render_process_host_observer_{this};
}; };
ServiceWorkerContextAdapter::RunningServiceWorker::RunningServiceWorker( ServiceWorkerContextAdapter::RunningServiceWorker::RunningServiceWorker(
int64_t version_id, int64_t version_id,
content::RenderProcessHost* worker_process_host,
ServiceWorkerContextAdapter* adapter) ServiceWorkerContextAdapter* adapter)
: version_id_(version_id), adapter_(adapter) { : version_id_(version_id), adapter_(adapter) {}
scoped_render_process_host_observer_.Add(worker_process_host);
ServiceWorkerContextAdapter::RunningServiceWorker::~RunningServiceWorker() {
DCHECK_EQ(observing_, nullptr);
}
void ServiceWorkerContextAdapter::RunningServiceWorker::Subscribe(
content::RenderProcessHost* worker_process_host) {
DCHECK_EQ(observing_, nullptr);
worker_process_host->AddObserver(this);
observing_ = worker_process_host;
} }
ServiceWorkerContextAdapter::RunningServiceWorker::~RunningServiceWorker() = void ServiceWorkerContextAdapter::RunningServiceWorker::Unsubscribe() {
default; DCHECK_NE(observing_, nullptr);
observing_->RemoveObserver(this);
observing_ = nullptr;
}
void ServiceWorkerContextAdapter::RunningServiceWorker::RenderProcessExited( void ServiceWorkerContextAdapter::RunningServiceWorker::RenderProcessExited(
content::RenderProcessHost* host, content::RenderProcessHost* host,
const content::ChildProcessTerminationInfo& info) { const content::ChildProcessTerminationInfo& info) {
adapter_->OnRenderProcessExited(version_id_); adapter_->OnRenderProcessExited(version_id_);
/* This object is deleted inside the above, don't touch "this". */
} }
void ServiceWorkerContextAdapter::RunningServiceWorker:: void ServiceWorkerContextAdapter::RunningServiceWorker::
...@@ -69,7 +83,12 @@ ServiceWorkerContextAdapter::ServiceWorkerContextAdapter( ...@@ -69,7 +83,12 @@ ServiceWorkerContextAdapter::ServiceWorkerContextAdapter(
scoped_underlying_context_observer_.Add(underlying_context); scoped_underlying_context_observer_.Add(underlying_context);
} }
ServiceWorkerContextAdapter::~ServiceWorkerContextAdapter() = default; ServiceWorkerContextAdapter::~ServiceWorkerContextAdapter() {
// Clean up any outstanding running service worker process subscriptions.
for (const auto& item : running_service_workers_)
item.second->Unsubscribe();
running_service_workers_.clear();
}
void ServiceWorkerContextAdapter::AddObserver( void ServiceWorkerContextAdapter::AddObserver(
content::ServiceWorkerContextObserver* observer) { content::ServiceWorkerContextObserver* observer) {
...@@ -226,7 +245,7 @@ void ServiceWorkerContextAdapter::OnVersionRedundant(int64_t version_id, ...@@ -226,7 +245,7 @@ void ServiceWorkerContextAdapter::OnVersionRedundant(int64_t version_id,
void ServiceWorkerContextAdapter::OnVersionStartedRunning( void ServiceWorkerContextAdapter::OnVersionStartedRunning(
int64_t version_id, int64_t version_id,
const content::ServiceWorkerRunningInfo& running_info) { const content::ServiceWorkerRunningInfo& running_info) {
auto* worker_process_host = content::RenderProcessHost* worker_process_host =
content::RenderProcessHost::FromID(running_info.render_process_id); content::RenderProcessHost::FromID(running_info.render_process_id);
// It's possible that the renderer is already gone since the notification // It's possible that the renderer is already gone since the notification
...@@ -240,19 +259,13 @@ void ServiceWorkerContextAdapter::OnVersionStartedRunning( ...@@ -240,19 +259,13 @@ void ServiceWorkerContextAdapter::OnVersionStartedRunning(
return; return;
} }
bool inserted = AddRunningServiceWorker(version_id, worker_process_host);
running_service_workers_
.emplace(version_id, std::make_unique<RunningServiceWorker>(
version_id, worker_process_host, this))
.second;
DCHECK(inserted);
for (auto& observer : observer_list_) for (auto& observer : observer_list_)
observer.OnVersionStartedRunning(version_id, running_info); observer.OnVersionStartedRunning(version_id, running_info);
} }
void ServiceWorkerContextAdapter::OnVersionStoppedRunning(int64_t version_id) { void ServiceWorkerContextAdapter::OnVersionStoppedRunning(int64_t version_id) {
size_t removed = running_service_workers_.erase(version_id); bool removed = MaybeRemoveRunningServiceWorker(version_id);
if (!removed) { if (!removed) {
#if DCHECK_IS_ON() #if DCHECK_IS_ON()
// If this service worker could not be found, then it must be because its // If this service worker could not be found, then it must be because its
...@@ -352,8 +365,8 @@ void ServiceWorkerContextAdapter::OnDestruct(ServiceWorkerContext* context) { ...@@ -352,8 +365,8 @@ void ServiceWorkerContextAdapter::OnDestruct(ServiceWorkerContext* context) {
} }
void ServiceWorkerContextAdapter::OnRenderProcessExited(int64_t version_id) { void ServiceWorkerContextAdapter::OnRenderProcessExited(int64_t version_id) {
size_t removed = running_service_workers_.erase(version_id); bool removed = MaybeRemoveRunningServiceWorker(version_id);
DCHECK_EQ(removed, 1u); DCHECK(removed);
for (auto& observer : observer_list_) for (auto& observer : observer_list_)
observer.OnVersionStoppedRunning(version_id); observer.OnVersionStoppedRunning(version_id);
...@@ -366,4 +379,30 @@ void ServiceWorkerContextAdapter::OnRenderProcessExited(int64_t version_id) { ...@@ -366,4 +379,30 @@ void ServiceWorkerContextAdapter::OnRenderProcessExited(int64_t version_id) {
#endif // DCHECK_IS_ON() #endif // DCHECK_IS_ON()
} }
void ServiceWorkerContextAdapter::AddRunningServiceWorker(
int64_t version_id,
content::RenderProcessHost* worker_process_host) {
std::unique_ptr<ServiceWorkerContextAdapter::RunningServiceWorker>
running_service_worker =
std::make_unique<RunningServiceWorker>(version_id, this);
running_service_worker->Subscribe(worker_process_host);
bool inserted = running_service_workers_
.emplace(version_id, std::move(running_service_worker))
.second;
DCHECK(inserted);
}
bool ServiceWorkerContextAdapter::MaybeRemoveRunningServiceWorker(
int64_t version_id) {
auto it = running_service_workers_.find(version_id);
if (it == running_service_workers_.end())
return false;
it->second->Unsubscribe();
running_service_workers_.erase(it);
return true;
}
} // namespace performance_manager } // namespace performance_manager
...@@ -123,6 +123,15 @@ class ServiceWorkerContextAdapter ...@@ -123,6 +123,15 @@ class ServiceWorkerContextAdapter
// has exited. // has exited.
void OnRenderProcessExited(int64_t version_id); void OnRenderProcessExited(int64_t version_id);
// Adds a registration to |worker_process_host| that will result in
// |OnRenderProcessExited| with |version_id| when it exits.
void AddRunningServiceWorker(int64_t version_id,
content::RenderProcessHost* worker_process_host);
// Removes a registration made by |AddRunningServiceWorker| if one exists,
// returns true if a registration existed, false otherwise.
bool MaybeRemoveRunningServiceWorker(int64_t version_id);
ScopedObserver<content::ServiceWorkerContext, ScopedObserver<content::ServiceWorkerContext,
content::ServiceWorkerContextObserver> content::ServiceWorkerContextObserver>
scoped_underlying_context_observer_{this}; scoped_underlying_context_observer_{this};
......
...@@ -28,8 +28,8 @@ void RecordWorkerClientFound(bool found) { ...@@ -28,8 +28,8 @@ void RecordWorkerClientFound(bool found) {
// Helper function to add |client_frame_node| as a client of |worker_node| on // Helper function to add |client_frame_node| as a client of |worker_node| on
// the PM sequence. // the PM sequence.
void ConnectClientOnGraph(WorkerNodeImpl* worker_node, void ConnectClientFrameOnGraph(WorkerNodeImpl* worker_node,
FrameNodeImpl* client_frame_node) { FrameNodeImpl* client_frame_node) {
PerformanceManagerImpl::CallOnGraphImpl( PerformanceManagerImpl::CallOnGraphImpl(
FROM_HERE, FROM_HERE,
base::BindOnce(&WorkerNodeImpl::AddClientFrame, base::BindOnce(&WorkerNodeImpl::AddClientFrame,
...@@ -38,8 +38,8 @@ void ConnectClientOnGraph(WorkerNodeImpl* worker_node, ...@@ -38,8 +38,8 @@ void ConnectClientOnGraph(WorkerNodeImpl* worker_node,
// Helper function to remove |client_frame_node| as a client of |worker_node| // Helper function to remove |client_frame_node| as a client of |worker_node|
// on the PM sequence. // on the PM sequence.
void DisconnectClientOnGraph(WorkerNodeImpl* worker_node, void DisconnectClientFrameOnGraph(WorkerNodeImpl* worker_node,
FrameNodeImpl* client_frame_node) { FrameNodeImpl* client_frame_node) {
PerformanceManagerImpl::CallOnGraphImpl( PerformanceManagerImpl::CallOnGraphImpl(
FROM_HERE, FROM_HERE,
base::BindOnce(&WorkerNodeImpl::RemoveClientFrame, base::BindOnce(&WorkerNodeImpl::RemoveClientFrame,
...@@ -48,8 +48,8 @@ void DisconnectClientOnGraph(WorkerNodeImpl* worker_node, ...@@ -48,8 +48,8 @@ void DisconnectClientOnGraph(WorkerNodeImpl* worker_node,
// Helper function to add |client_worker_node| as a client of |worker_node| on // Helper function to add |client_worker_node| as a client of |worker_node| on
// the PM sequence. // the PM sequence.
void ConnectClientOnGraph(WorkerNodeImpl* worker_node, void ConnectClientWorkerOnGraph(WorkerNodeImpl* worker_node,
WorkerNodeImpl* client_worker_node) { WorkerNodeImpl* client_worker_node) {
PerformanceManagerImpl::CallOnGraphImpl( PerformanceManagerImpl::CallOnGraphImpl(
FROM_HERE, FROM_HERE,
base::BindOnce(&WorkerNodeImpl::AddClientWorker, base::BindOnce(&WorkerNodeImpl::AddClientWorker,
...@@ -58,8 +58,8 @@ void ConnectClientOnGraph(WorkerNodeImpl* worker_node, ...@@ -58,8 +58,8 @@ void ConnectClientOnGraph(WorkerNodeImpl* worker_node,
// Helper function to remove |client_worker_node| as a client of |worker_node| // Helper function to remove |client_worker_node| as a client of |worker_node|
// on the PM sequence. // on the PM sequence.
void DisconnectClientOnGraph(WorkerNodeImpl* worker_node, void DisconnectClientWorkerOnGraph(WorkerNodeImpl* worker_node,
WorkerNodeImpl* client_worker_node) { WorkerNodeImpl* client_worker_node) {
PerformanceManagerImpl::CallOnGraphImpl( PerformanceManagerImpl::CallOnGraphImpl(
FROM_HERE, FROM_HERE,
base::BindOnce(&WorkerNodeImpl::RemoveClientWorker, base::BindOnce(&WorkerNodeImpl::RemoveClientWorker,
...@@ -505,7 +505,7 @@ void WorkerWatcher::ConnectFrameClient( ...@@ -505,7 +505,7 @@ void WorkerWatcher::ConnectFrameClient(
RecordWorkerClientFound(true); RecordWorkerClientFound(true);
ConnectClientOnGraph(worker_node, frame_node); ConnectClientFrameOnGraph(worker_node, frame_node);
// Keep track of the workers that this frame is a client to. // Keep track of the workers that this frame is a client to.
if (AddChildWorker(client_render_frame_host_id, worker_node)) { if (AddChildWorker(client_render_frame_host_id, worker_node)) {
...@@ -550,7 +550,7 @@ void WorkerWatcher::DisconnectFrameClient( ...@@ -550,7 +550,7 @@ void WorkerWatcher::DisconnectFrameClient(
return; return;
} }
DisconnectClientOnGraph(worker_node, frame_node); DisconnectClientFrameOnGraph(worker_node, frame_node);
// Remove |worker_node| from the set of workers that this frame is a client // Remove |worker_node| from the set of workers that this frame is a client
// of. // of.
...@@ -563,8 +563,8 @@ void WorkerWatcher::ConnectDedicatedWorkerClient( ...@@ -563,8 +563,8 @@ void WorkerWatcher::ConnectDedicatedWorkerClient(
blink::DedicatedWorkerToken client_dedicated_worker_token) { blink::DedicatedWorkerToken client_dedicated_worker_token) {
DCHECK(worker_node); DCHECK(worker_node);
ConnectClientOnGraph(worker_node, ConnectClientWorkerOnGraph(
GetDedicatedWorkerNode(client_dedicated_worker_token)); worker_node, GetDedicatedWorkerNode(client_dedicated_worker_token));
// Remember that |worker_node| is a child worker of this dedicated worker. // Remember that |worker_node| is a child worker of this dedicated worker.
bool inserted = dedicated_worker_child_workers_[client_dedicated_worker_token] bool inserted = dedicated_worker_child_workers_[client_dedicated_worker_token]
...@@ -590,7 +590,7 @@ void WorkerWatcher::DisconnectDedicatedWorkerClient( ...@@ -590,7 +590,7 @@ void WorkerWatcher::DisconnectDedicatedWorkerClient(
if (child_workers.empty()) if (child_workers.empty())
dedicated_worker_child_workers_.erase(it); dedicated_worker_child_workers_.erase(it);
DisconnectClientOnGraph( DisconnectClientWorkerOnGraph(
worker_node, GetDedicatedWorkerNode(client_dedicated_worker_token)); worker_node, GetDedicatedWorkerNode(client_dedicated_worker_token));
} }
...@@ -599,8 +599,8 @@ void WorkerWatcher::ConnectSharedWorkerClient( ...@@ -599,8 +599,8 @@ void WorkerWatcher::ConnectSharedWorkerClient(
blink::SharedWorkerToken client_shared_worker_token) { blink::SharedWorkerToken client_shared_worker_token) {
DCHECK(worker_node); DCHECK(worker_node);
ConnectClientOnGraph(worker_node, ConnectClientWorkerOnGraph(worker_node,
GetSharedWorkerNode(client_shared_worker_token)); GetSharedWorkerNode(client_shared_worker_token));
// Remember that |worker_node| is a child worker of this shared worker. // Remember that |worker_node| is a child worker of this shared worker.
bool inserted = shared_worker_child_workers_[client_shared_worker_token] bool inserted = shared_worker_child_workers_[client_shared_worker_token]
...@@ -625,8 +625,8 @@ void WorkerWatcher::DisconnectSharedWorkerClient( ...@@ -625,8 +625,8 @@ void WorkerWatcher::DisconnectSharedWorkerClient(
if (child_workers.empty()) if (child_workers.empty())
shared_worker_child_workers_.erase(it); shared_worker_child_workers_.erase(it);
DisconnectClientOnGraph(worker_node, DisconnectClientWorkerOnGraph(
GetSharedWorkerNode(client_shared_worker_token)); worker_node, GetSharedWorkerNode(client_shared_worker_token));
} }
void WorkerWatcher::ConnectAllServiceWorkerClients( void WorkerWatcher::ConnectAllServiceWorkerClients(
......
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