Commit a7c39cdc authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Allow WebStateObserver to observe N WebStates [9/N].

Convert WebFaviconDriver to directly track registration with
the observed WebState instead of relying on the deprecated
code in WebStateObserver.

Bug: 775684
Change-Id: I7f7fcec4a57c1da79012069d971123c9e4144799
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-eg
Reviewed-on: https://chromium-review.googlesource.com/758863
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#515951}
parent f75e20be
...@@ -67,6 +67,7 @@ class WebFaviconDriver : public web::WebStateObserver, ...@@ -67,6 +67,7 @@ class WebFaviconDriver : public web::WebStateObserver,
void FaviconUrlUpdated( void FaviconUrlUpdated(
web::WebState* web_state, web::WebState* web_state,
const std::vector<web::FaviconURL>& candidates) override; const std::vector<web::FaviconURL>& candidates) override;
void WebStateDestroyed(web::WebState* web_state) override;
// Invoked when new favicon URL candidates are received. // Invoked when new favicon URL candidates are received.
void FaviconUrlUpdatedInternal( void FaviconUrlUpdatedInternal(
...@@ -78,6 +79,10 @@ class WebFaviconDriver : public web::WebStateObserver, ...@@ -78,6 +79,10 @@ class WebFaviconDriver : public web::WebStateObserver,
// Caches the favicon URLs candidates for same-document navigations. // Caches the favicon URLs candidates for same-document navigations.
std::vector<favicon::FaviconURL> candidates_; std::vector<favicon::FaviconURL> candidates_;
// The WebState this instance is observing. Will be null after
// WebStateDestroyed has been called.
web::WebState* web_state_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(WebFaviconDriver); DISALLOW_COPY_AND_ASSIGN(WebFaviconDriver);
}; };
......
...@@ -51,19 +51,19 @@ void WebFaviconDriver::CreateForWebState( ...@@ -51,19 +51,19 @@ void WebFaviconDriver::CreateForWebState(
gfx::Image WebFaviconDriver::GetFavicon() const { gfx::Image WebFaviconDriver::GetFavicon() const {
web::NavigationItem* item = web::NavigationItem* item =
web_state()->GetNavigationManager()->GetLastCommittedItem(); web_state_->GetNavigationManager()->GetLastCommittedItem();
return item ? item->GetFavicon().image : gfx::Image(); return item ? item->GetFavicon().image : gfx::Image();
} }
bool WebFaviconDriver::FaviconIsValid() const { bool WebFaviconDriver::FaviconIsValid() const {
web::NavigationItem* item = web::NavigationItem* item =
web_state()->GetNavigationManager()->GetLastCommittedItem(); web_state_->GetNavigationManager()->GetLastCommittedItem();
return item ? item->GetFavicon().valid : false; return item ? item->GetFavicon().valid : false;
} }
GURL WebFaviconDriver::GetActiveURL() { GURL WebFaviconDriver::GetActiveURL() {
web::NavigationItem* item = web::NavigationItem* item =
web_state()->GetNavigationManager()->GetVisibleItem(); web_state_->GetNavigationManager()->GetVisibleItem();
return item ? item->GetURL() : GURL(); return item ? item->GetURL() : GURL();
} }
...@@ -103,8 +103,8 @@ void WebFaviconDriver::DownloadManifest(const GURL& url, ...@@ -103,8 +103,8 @@ void WebFaviconDriver::DownloadManifest(const GURL& url,
} }
bool WebFaviconDriver::IsOffTheRecord() { bool WebFaviconDriver::IsOffTheRecord() {
DCHECK(web_state()); DCHECK(web_state_);
return web_state()->GetBrowserState()->IsOffTheRecord(); return web_state_->GetBrowserState()->IsOffTheRecord();
} }
void WebFaviconDriver::OnFaviconUpdated( void WebFaviconDriver::OnFaviconUpdated(
...@@ -122,7 +122,7 @@ void WebFaviconDriver::OnFaviconUpdated( ...@@ -122,7 +122,7 @@ void WebFaviconDriver::OnFaviconUpdated(
} }
web::NavigationItem* item = web::NavigationItem* item =
web_state()->GetNavigationManager()->GetVisibleItem(); web_state_->GetNavigationManager()->GetVisibleItem();
DCHECK(item); DCHECK(item);
web::FaviconStatus& favicon_status = item->GetFavicon(); web::FaviconStatus& favicon_status = item->GetFavicon();
...@@ -155,16 +155,23 @@ void WebFaviconDriver::OnFaviconDeleted( ...@@ -155,16 +155,23 @@ void WebFaviconDriver::OnFaviconDeleted(
WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, WebFaviconDriver::WebFaviconDriver(web::WebState* web_state,
FaviconService* favicon_service, FaviconService* favicon_service,
history::HistoryService* history_service) history::HistoryService* history_service)
: web::WebStateObserver(web_state), : FaviconDriverImpl(favicon_service, history_service),
FaviconDriverImpl(favicon_service, history_service), image_fetcher_(web_state->GetBrowserState()->GetRequestContext()),
image_fetcher_(web_state->GetBrowserState()->GetRequestContext()) {} web_state_(web_state) {
web_state_->AddObserver(this);
}
WebFaviconDriver::~WebFaviconDriver() { WebFaviconDriver::~WebFaviconDriver() {
// WebFaviconDriver is owned by WebState (as it is a WebStateUserData), so
// the WebStateDestroyed will be called before the destructor and the member
// field reset to null.
DCHECK(!web_state_);
} }
void WebFaviconDriver::DidStartNavigation( void WebFaviconDriver::DidStartNavigation(
web::WebState* web_state, web::WebState* web_state,
web::NavigationContext* navigation_context) { web::NavigationContext* navigation_context) {
DCHECK_EQ(web_state_, web_state);
SetFaviconOutOfDateForPage(navigation_context->GetUrl(), SetFaviconOutOfDateForPage(navigation_context->GetUrl(),
/*force_reload=*/false); /*force_reload=*/false);
} }
...@@ -172,6 +179,7 @@ void WebFaviconDriver::DidStartNavigation( ...@@ -172,6 +179,7 @@ void WebFaviconDriver::DidStartNavigation(
void WebFaviconDriver::DidFinishNavigation( void WebFaviconDriver::DidFinishNavigation(
web::WebState* web_state, web::WebState* web_state,
web::NavigationContext* navigation_context) { web::NavigationContext* navigation_context) {
DCHECK_EQ(web_state_, web_state);
if (navigation_context->GetError()) if (navigation_context->GetError())
return; return;
...@@ -191,11 +199,18 @@ void WebFaviconDriver::DidFinishNavigation( ...@@ -191,11 +199,18 @@ void WebFaviconDriver::DidFinishNavigation(
void WebFaviconDriver::FaviconUrlUpdated( void WebFaviconDriver::FaviconUrlUpdated(
web::WebState* web_state, web::WebState* web_state,
const std::vector<web::FaviconURL>& candidates) { const std::vector<web::FaviconURL>& candidates) {
DCHECK_EQ(web_state_, web_state);
DCHECK(!candidates.empty()); DCHECK(!candidates.empty());
candidates_ = FaviconURLsFromWebFaviconURLs(candidates); candidates_ = FaviconURLsFromWebFaviconURLs(candidates);
FaviconUrlUpdatedInternal(candidates_); FaviconUrlUpdatedInternal(candidates_);
} }
void WebFaviconDriver::WebStateDestroyed(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
web_state_->RemoveObserver(this);
web_state_ = nullptr;
}
void WebFaviconDriver::FaviconUrlUpdatedInternal( void WebFaviconDriver::FaviconUrlUpdatedInternal(
const std::vector<favicon::FaviconURL>& candidates) { const std::vector<favicon::FaviconURL>& candidates) {
OnUpdateCandidates(GetActiveURL(), candidates, GURL()); OnUpdateCandidates(GetActiveURL(), candidates, GURL());
......
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