Remove NOTIFICATION_PROFILE_DESTROYED from LazyDomDistillerService.
The lifetime is tied to that of the profile, so hang it off the
profile as a piece of UserData.
Bug: 268984
Change-Id: Id897dd7d09281df943722709499a9f769841e270
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1832650
Reviewed-by: Tommy Nyquist <[email protected]>
Commit-Queue: Evan Stade <[email protected]>
Cr-Commit-Position: refs/heads/master@{#702435}
diff --git a/chrome/browser/dom_distiller/lazy_dom_distiller_service.cc b/chrome/browser/dom_distiller/lazy_dom_distiller_service.cc
index 8ee1b0a..4c9b577 100644
--- a/chrome/browser/dom_distiller/lazy_dom_distiller_service.cc
+++ b/chrome/browser/dom_distiller/lazy_dom_distiller_service.cc
@@ -6,78 +6,79 @@
#include <utility>
-#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/dom_distiller/core/distilled_page_prefs.h"
#include "components/dom_distiller/core/distiller_page.h"
#include "components/dom_distiller/core/dom_distiller_service.h"
-#include "content/public/browser/notification_source.h"
namespace dom_distiller {
-LazyDomDistillerService::LazyDomDistillerService(
- Profile* profile,
- const DomDistillerServiceFactory* service_factory)
- : profile_(profile), service_factory_(service_factory) {
- registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
- content::Source<Profile>(profile));
+namespace {
+
+// Identifies the user data on the profile.
+const char kLazyDomDistillerServiceKey[] = "kLazyDomDistillerServiceKey";
+
+} // namespace
+
+// static
+LazyDomDistillerService* LazyDomDistillerService::Create(Profile* profile) {
+ // We can't use make_unique because the constructor is private.
+ LazyDomDistillerService* instance = new LazyDomDistillerService(profile);
+ profile->SetUserData(&kLazyDomDistillerServiceKey,
+ base::WrapUnique(instance));
+ return instance;
}
-LazyDomDistillerService::~LazyDomDistillerService() {}
-
-// This will create an object and schedule work the first time it's called
-// and just return an existing object after that.
-DomDistillerServiceInterface* LazyDomDistillerService::instance() const {
- return service_factory_->GetForBrowserContext(profile_);
-}
-
-void LazyDomDistillerService::Observe(
- int type,
- const content::NotificationSource& source,
- const content::NotificationDetails& details) {
- DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type);
- DCHECK_EQ(profile_, content::Source<Profile>(source).ptr());
- delete this;
-}
+LazyDomDistillerService::~LazyDomDistillerService() = default;
bool LazyDomDistillerService::HasEntry(const std::string& entry_id) {
- return instance()->HasEntry(entry_id);
+ return GetImpl()->HasEntry(entry_id);
}
std::string LazyDomDistillerService::GetUrlForEntry(
const std::string& entry_id) {
- return instance()->GetUrlForEntry(entry_id);
+ return GetImpl()->GetUrlForEntry(entry_id);
}
std::unique_ptr<ViewerHandle> LazyDomDistillerService::ViewEntry(
ViewRequestDelegate* delegate,
std::unique_ptr<DistillerPage> distiller_page,
const std::string& entry_id) {
- return instance()->ViewEntry(delegate, std::move(distiller_page), entry_id);
+ return GetImpl()->ViewEntry(delegate, std::move(distiller_page), entry_id);
}
std::unique_ptr<ViewerHandle> LazyDomDistillerService::ViewUrl(
ViewRequestDelegate* delegate,
std::unique_ptr<DistillerPage> distiller_page,
const GURL& url) {
- return instance()->ViewUrl(delegate, std::move(distiller_page), url);
+ return GetImpl()->ViewUrl(delegate, std::move(distiller_page), url);
}
std::unique_ptr<DistillerPage>
LazyDomDistillerService::CreateDefaultDistillerPage(
const gfx::Size& render_view_size) {
- return instance()->CreateDefaultDistillerPage(render_view_size);
+ return GetImpl()->CreateDefaultDistillerPage(render_view_size);
}
std::unique_ptr<DistillerPage>
LazyDomDistillerService::CreateDefaultDistillerPageWithHandle(
std::unique_ptr<SourcePageHandle> handle) {
- return instance()->CreateDefaultDistillerPageWithHandle(std::move(handle));
+ return GetImpl()->CreateDefaultDistillerPageWithHandle(std::move(handle));
}
DistilledPagePrefs* LazyDomDistillerService::GetDistilledPagePrefs() {
- return instance()->GetDistilledPagePrefs();
+ return GetImpl()->GetDistilledPagePrefs();
+}
+
+LazyDomDistillerService::LazyDomDistillerService(Profile* profile)
+ : profile_(profile) {}
+
+// This will create an object and schedule work the first time it's called
+// and just return an existing object after that.
+DomDistillerServiceInterface* LazyDomDistillerService::GetImpl() const {
+ return DomDistillerServiceFactory::GetInstance()->GetForBrowserContext(
+ profile_);
}
} // namespace dom_distiller
diff --git a/chrome/browser/dom_distiller/lazy_dom_distiller_service.h b/chrome/browser/dom_distiller/lazy_dom_distiller_service.h
index a7adba3..9c15165 100644
--- a/chrome/browser/dom_distiller/lazy_dom_distiller_service.h
+++ b/chrome/browser/dom_distiller/lazy_dom_distiller_service.h
@@ -8,33 +8,25 @@
#include <memory>
#include "base/macros.h"
+#include "base/supports_user_data.h"
#include "components/dom_distiller/core/dom_distiller_service.h"
#include "components/dom_distiller/core/task_tracker.h"
-#include "content/public/browser/notification_observer.h"
-#include "content/public/browser/notification_registrar.h"
-
-namespace content {
-class NotificationSource;
-class NotificationDetails;
-} // namespace content
class Profile;
namespace dom_distiller {
-class DomDistillerServiceFactory;
-
// A class which helps with lazy instantiation of the DomDistillerService, using
-// the BrowserContextKeyedServiceFactory for it. This class will delete itself
-// when the profile is destroyed.
+// the BrowserContextKeyedServiceFactory for it. This class is owned by Profile.
class LazyDomDistillerService : public DomDistillerServiceInterface,
- public content::NotificationObserver {
+ public base::SupportsUserData::Data {
public:
- LazyDomDistillerService(Profile* profile,
- const DomDistillerServiceFactory* service_factory);
+ // Creates and returns an instance for |profile|. This does not pass ownership
+ // of the returned pointer.
+ static LazyDomDistillerService* Create(Profile* profile);
+
~LazyDomDistillerService() override;
- public:
// DomDistillerServiceInterface implementation:
bool HasEntry(const std::string& entry_id) override;
std::string GetUrlForEntry(const std::string& entry_id) override;
@@ -53,24 +45,15 @@
DistilledPagePrefs* GetDistilledPagePrefs() override;
private:
- // Accessor method for the backing service instance.
- DomDistillerServiceInterface* instance() const;
+ explicit LazyDomDistillerService(Profile* profile);
- // content::NotificationObserver implementation:
- void Observe(int type,
- const content::NotificationSource& source,
- const content::NotificationDetails& details) override;
+ // Accessor method for the backing service instance.
+ DomDistillerServiceInterface* GetImpl() const;
// The Profile to use when retrieving the DomDistillerService and also the
// profile to listen for destruction of.
Profile* profile_;
- // A BrowserContextKeyedServiceFactory for the DomDistillerService.
- const DomDistillerServiceFactory* service_factory_;
-
- // Used to track when the profile is shut down.
- content::NotificationRegistrar registrar_;
-
DISALLOW_COPY_AND_ASSIGN(LazyDomDistillerService);
};
diff --git a/chrome/browser/dom_distiller/profile_utils.cc b/chrome/browser/dom_distiller/profile_utils.cc
index f05228c..59230bb 100644
--- a/chrome/browser/dom_distiller/profile_utils.cc
+++ b/chrome/browser/dom_distiller/profile_utils.cc
@@ -8,7 +8,6 @@
#include <utility>
#include "base/command_line.h"
-#include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
#include "chrome/browser/dom_distiller/lazy_dom_distiller_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_isolated_world_ids.h"
@@ -29,11 +28,8 @@
if (!dom_distiller::IsDomDistillerEnabled())
return;
- DomDistillerServiceFactory* dom_distiller_service_factory =
- DomDistillerServiceFactory::GetInstance();
- // The LazyDomDistillerService deletes itself when the profile is destroyed.
LazyDomDistillerService* lazy_service =
- new LazyDomDistillerService(profile, dom_distiller_service_factory);
+ LazyDomDistillerService::Create(profile);
std::unique_ptr<DistillerUIHandle> ui_handle;
#if defined(OS_ANDROID)