0

PM: Use base::ScopedObservation in WorkerWatcher.

Also move ScopedObservation into the base namespace. It's an
oversight that it wasn't there in the first place.

Change-Id: I854bc0c96af72dff096894cd3b82cd7710f6add3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2519271
Reviewed-by: Patrick Monette <pmonette@chromium.org>
Reviewed-by: François Doray <fdoray@chromium.org>
Commit-Queue: Sigurður Ásgeirsson <siggi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824123}
This commit is contained in:
Sigurdur Asgeirsson
2020-11-04 20:14:15 +00:00
committed by Commit Bot
parent f5e1a945a9
commit 80f6e8f595
2 changed files with 15 additions and 9 deletions

@ -9,6 +9,8 @@
#include "base/check_op.h"
namespace base {
// ScopedObservation is used to keep track of a single observation.
// When ScopedObservation is destroyed, it removes the registered observation,
// if any. Basic example (as a member variable):
@ -74,4 +76,6 @@ class ScopedObservation {
Source* source_ = nullptr;
};
} // namespace base
#endif // BASE_SCOPED_OBSERVATION_H_

@ -6,6 +6,7 @@
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/scoped_observation.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"
@ -37,8 +38,10 @@ class ServiceWorkerContextAdapter::RunningServiceWorker
// The adapter that owns |this|. Notified when RenderProcessExited() is
// called.
ServiceWorkerContextAdapter* const adapter_;
// The render process host this instance is observing.
content::RenderProcessHost* observing_ = nullptr;
base::ScopedObservation<content::RenderProcessHost,
content::RenderProcessHostObserver>
scoped_observation_{this};
};
ServiceWorkerContextAdapter::RunningServiceWorker::RunningServiceWorker(
@ -47,20 +50,19 @@ ServiceWorkerContextAdapter::RunningServiceWorker::RunningServiceWorker(
: version_id_(version_id), adapter_(adapter) {}
ServiceWorkerContextAdapter::RunningServiceWorker::~RunningServiceWorker() {
DCHECK_EQ(observing_, nullptr);
DCHECK(!scoped_observation_.IsObserving());
}
void ServiceWorkerContextAdapter::RunningServiceWorker::Subscribe(
content::RenderProcessHost* worker_process_host) {
DCHECK_EQ(observing_, nullptr);
worker_process_host->AddObserver(this);
observing_ = worker_process_host;
DCHECK(!scoped_observation_.IsObserving());
scoped_observation_.Observe(worker_process_host);
}
void ServiceWorkerContextAdapter::RunningServiceWorker::Unsubscribe() {
DCHECK_NE(observing_, nullptr);
observing_->RemoveObserver(this);
observing_ = nullptr;
DCHECK(scoped_observation_.IsObserving());
scoped_observation_.RemoveObservation();
}
void ServiceWorkerContextAdapter::RunningServiceWorker::RenderProcessExited(