Commit 20c44b29 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

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

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

Bug: 775684
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I0b3f3b63e599ad2327df155a75549248b8db9b1b
Reviewed-on: https://chromium-review.googlesource.com/766749
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#516292}
parent e67a222f
...@@ -52,10 +52,15 @@ class HistoryTabHelper : public history::Context, ...@@ -52,10 +52,15 @@ class HistoryTabHelper : public history::Context,
void DidFinishNavigation(web::WebState* web_state, void DidFinishNavigation(web::WebState* web_state,
web::NavigationContext* navigation_context) override; web::NavigationContext* navigation_context) override;
void TitleWasSet(web::WebState* web_state) override; void TitleWasSet(web::WebState* web_state) override;
void WebStateDestroyed(web::WebState* web_state) override;
// Helper function to return the history service. May return null. // Helper function to return the history service. May return null.
history::HistoryService* GetHistoryService(); history::HistoryService* GetHistoryService();
// The WebState this instance is observing. Will be null after
// WebStateDestroyed has been called.
web::WebState* web_state_ = nullptr;
// Hold navigation entries that need to be added to the history database. // Hold navigation entries that need to be added to the history database.
// Pre-rendered WebStates do not write navigation data to the history DB // Pre-rendered WebStates do not write navigation data to the history DB
// immediately, instead they are cached in this vector and added when it // immediately, instead they are cached in this vector and added when it
......
...@@ -24,7 +24,9 @@ ...@@ -24,7 +24,9 @@
DEFINE_WEB_STATE_USER_DATA_KEY(HistoryTabHelper); DEFINE_WEB_STATE_USER_DATA_KEY(HistoryTabHelper);
HistoryTabHelper::~HistoryTabHelper() {} HistoryTabHelper::~HistoryTabHelper() {
DCHECK(!web_state_);
}
void HistoryTabHelper::UpdateHistoryPageTitle(const web::NavigationItem& item) { void HistoryTabHelper::UpdateHistoryPageTitle(const web::NavigationItem& item) {
DCHECK(!delay_notification_); DCHECK(!delay_notification_);
...@@ -60,19 +62,22 @@ void HistoryTabHelper::SetDelayHistoryServiceNotification( ...@@ -60,19 +62,22 @@ void HistoryTabHelper::SetDelayHistoryServiceNotification(
std::swap(recorded_navigations_, empty_vector); std::swap(recorded_navigations_, empty_vector);
web::NavigationItem* last_committed_item = web::NavigationItem* last_committed_item =
web_state()->GetNavigationManager()->GetLastCommittedItem(); web_state_->GetNavigationManager()->GetLastCommittedItem();
if (last_committed_item) { if (last_committed_item) {
UpdateHistoryPageTitle(*last_committed_item); UpdateHistoryPageTitle(*last_committed_item);
} }
} }
HistoryTabHelper::HistoryTabHelper(web::WebState* web_state) HistoryTabHelper::HistoryTabHelper(web::WebState* web_state)
: web::WebStateObserver(web_state) {} : web_state_(web_state) {
web_state_->AddObserver(this);
}
void HistoryTabHelper::DidFinishNavigation( void HistoryTabHelper::DidFinishNavigation(
web::WebState* web_state, web::WebState* web_state,
web::NavigationContext* navigation_context) { web::NavigationContext* navigation_context) {
if (web_state->GetBrowserState()->IsOffTheRecord()) { DCHECK_EQ(web_state_, web_state);
if (web_state_->GetBrowserState()->IsOffTheRecord()) {
return; return;
} }
...@@ -87,9 +92,9 @@ void HistoryTabHelper::DidFinishNavigation( ...@@ -87,9 +92,9 @@ void HistoryTabHelper::DidFinishNavigation(
return; return;
} }
DCHECK(web_state->GetNavigationManager()->GetVisibleItem()); DCHECK(web_state_->GetNavigationManager()->GetVisibleItem());
web::NavigationItem* visible_item = web::NavigationItem* visible_item =
web_state->GetNavigationManager()->GetVisibleItem(); web_state_->GetNavigationManager()->GetVisibleItem();
DCHECK(!visible_item->GetTimestamp().is_null()); DCHECK(!visible_item->GetTimestamp().is_null());
// Do not update the history database for back/forward navigations. // Do not update the history database for back/forward navigations.
...@@ -152,21 +157,28 @@ void HistoryTabHelper::DidFinishNavigation( ...@@ -152,21 +157,28 @@ void HistoryTabHelper::DidFinishNavigation(
} }
void HistoryTabHelper::TitleWasSet(web::WebState* web_state) { void HistoryTabHelper::TitleWasSet(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
if (delay_notification_) { if (delay_notification_) {
return; return;
} }
web::NavigationItem* last_committed_item = web::NavigationItem* last_committed_item =
web_state->GetNavigationManager()->GetLastCommittedItem(); web_state_->GetNavigationManager()->GetLastCommittedItem();
if (last_committed_item) { if (last_committed_item) {
UpdateHistoryPageTitle(*last_committed_item); UpdateHistoryPageTitle(*last_committed_item);
} }
} }
void HistoryTabHelper::WebStateDestroyed(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
web_state_->RemoveObserver(this);
web_state_ = nullptr;
}
history::HistoryService* HistoryTabHelper::GetHistoryService() { history::HistoryService* HistoryTabHelper::GetHistoryService() {
ios::ChromeBrowserState* browser_state = ios::ChromeBrowserState* browser_state =
ios::ChromeBrowserState::FromBrowserState(web_state()->GetBrowserState()); ios::ChromeBrowserState::FromBrowserState(web_state_->GetBrowserState());
if (browser_state->IsOffTheRecord()) if (browser_state->IsOffTheRecord())
return nullptr; return nullptr;
......
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