Commit 90e3be50 authored by Aran Gilman's avatar Aran Gilman Committed by Commit Bot

Allow removing distillability observers.

Bug: 1017341
Change-Id: I719f13dd45a75783dc919a0f74be996445951be6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1875481Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Commit-Queue: Aran Gilman <gilmanmh@google.com>
Cr-Commit-Position: refs/heads/master@{#713868}
parent 4ca60ab5
...@@ -249,4 +249,14 @@ IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAllArticles, ...@@ -249,4 +249,14 @@ IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAllArticles,
Optional(AllOf(Not(IsDistillable()), IsLast(), Not(IsMobileFriendly())))); Optional(AllOf(Not(IsDistillable()), IsLast(), Not(IsMobileFriendly()))));
} }
IN_PROC_BROWSER_TEST_F(DistillablePageUtilsBrowserTestAllArticles,
ObserverNotCalledAfterRemoval) {
RemoveObserver(web_contents_, &holder_);
EXPECT_CALL(holder_, OnResult(_)).Times(0);
NavigateAndWait(kSimpleArticlePath, kWaitNoExpectedCall);
EXPECT_THAT(
GetLatestResult(web_contents_),
Optional(AllOf(IsDistillable(), IsLast(), Not(IsMobileFriendly()))));
}
} // namespace dom_distiller } // namespace dom_distiller
...@@ -63,12 +63,6 @@ void DistillabilityDriver::CreateDistillabilityService( ...@@ -63,12 +63,6 @@ void DistillabilityDriver::CreateDistillabilityService(
std::move(receiver)); std::move(receiver));
} }
void DistillabilityDriver::AddObserver(DistillabilityObserver* observer) {
if (!observers_.HasObserver(observer)) {
observers_.AddObserver(observer);
}
}
void DistillabilityDriver::OnDistillability( void DistillabilityDriver::OnDistillability(
const DistillabilityResult& result) { const DistillabilityResult& result) {
latest_result_ = result; latest_result_ = result;
......
...@@ -28,7 +28,9 @@ class DistillabilityDriver ...@@ -28,7 +28,9 @@ class DistillabilityDriver
void CreateDistillabilityService( void CreateDistillabilityService(
mojo::PendingReceiver<mojom::DistillabilityService> receiver); mojo::PendingReceiver<mojom::DistillabilityService> receiver);
void AddObserver(DistillabilityObserver* observer); base::ObserverList<DistillabilityObserver>* GetObserverList() {
return &observers_;
}
base::Optional<DistillabilityResult> GetLatestResult() const { base::Optional<DistillabilityResult> GetLatestResult() const {
return latest_result_; return latest_result_;
} }
......
...@@ -59,13 +59,34 @@ std::ostream& operator<<(std::ostream& os, const DistillabilityResult& result) { ...@@ -59,13 +59,34 @@ std::ostream& operator<<(std::ostream& os, const DistillabilityResult& result) {
void AddObserver(content::WebContents* web_contents, void AddObserver(content::WebContents* web_contents,
DistillabilityObserver* observer) { DistillabilityObserver* observer) {
DCHECK(observer);
CHECK(web_contents); CHECK(web_contents);
DistillabilityDriver::CreateForWebContents(web_contents); DistillabilityDriver::CreateForWebContents(web_contents);
DistillabilityDriver* driver = DistillabilityDriver* driver =
DistillabilityDriver::FromWebContents(web_contents); DistillabilityDriver::FromWebContents(web_contents);
CHECK(driver); CHECK(driver);
driver->AddObserver(observer); base::ObserverList<DistillabilityObserver>* observer_list =
driver->GetObserverList();
if (!observer_list->HasObserver(observer)) {
observer_list->AddObserver(observer);
}
}
void RemoveObserver(content::WebContents* web_contents,
DistillabilityObserver* observer) {
DCHECK(observer);
CHECK(web_contents);
DistillabilityDriver::CreateForWebContents(web_contents);
DistillabilityDriver* driver =
DistillabilityDriver::FromWebContents(web_contents);
CHECK(driver);
base::ObserverList<DistillabilityObserver>* observer_list =
driver->GetObserverList();
if (observer_list->HasObserver(observer)) {
observer_list->RemoveObserver(observer);
}
} }
base::Optional<DistillabilityResult> GetLatestResult( base::Optional<DistillabilityResult> GetLatestResult(
......
...@@ -40,11 +40,14 @@ class DistillabilityObserver : public base::CheckedObserver { ...@@ -40,11 +40,14 @@ class DistillabilityObserver : public base::CheckedObserver {
virtual void OnResult(const DistillabilityResult& result) = 0; virtual void OnResult(const DistillabilityResult& result) = 0;
}; };
// Set the delegate to receive the result of whether the page is distillable. // Add/remove objects to the list of observers to notify when the distillability
// service returns a result.
// //
// |web_contents| must be non-null. // |web_contents| and |observer| must both be non-null.
void AddObserver(content::WebContents* web_contents, void AddObserver(content::WebContents* web_contents,
DistillabilityObserver* observer); DistillabilityObserver* observer);
void RemoveObserver(content::WebContents* web_contents,
DistillabilityObserver* observer);
base::Optional<DistillabilityResult> GetLatestResult( base::Optional<DistillabilityResult> GetLatestResult(
content::WebContents* web_contents); content::WebContents* web_contents);
......
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