Commit d2c3c8fb authored by sangwoo.ko's avatar sangwoo.ko Committed by Commit Bot

Use TabStripModelObserver's new API in chrome/browser/resource_coordinator

Replace old API with new API. This CL is a refactor
ans has no intended behavior change.

Bug: 842194
Change-Id: Iac5ac3946c8590d5983c93d14a725d7de670311a
Reviewed-on: https://chromium-review.googlesource.com/1179120Reviewed-by: default avatarChris Hamilton <chrisha@chromium.org>
Commit-Queue: Sang Woo Ko <sangwoo108@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584967}
parent 21a01d66
...@@ -424,36 +424,45 @@ void TabActivityWatcher::OnBrowserSetLastActive(Browser* browser) { ...@@ -424,36 +424,45 @@ void TabActivityWatcher::OnBrowserSetLastActive(Browser* browser) {
web_contents_data->TabWindowActivated(); web_contents_data->TabWindowActivated();
} }
void TabActivityWatcher::TabInsertedAt(TabStripModel* tab_strip_model, void TabActivityWatcher::OnTabStripModelChanged(
content::WebContents* contents, TabStripModel* tab_strip_model,
int index, const TabStripModelChange& change,
bool foreground) { const TabStripSelectionChange& selection) {
// Ensure the WebContentsData is created to observe this WebContents since it switch (change.type()) {
// may represent a newly created tab. case TabStripModelChange::kInserted: {
WebContentsData::CreateForWebContents(contents); for (const auto& delta : change.deltas()) {
WebContentsData::FromWebContents(contents)->TabInserted(foreground); // Ensure the WebContentsData is created to observe this WebContents
} // since it may represent a newly created tab.
WebContentsData::CreateForWebContents(delta.insert.contents);
void TabActivityWatcher::TabDetachedAt(content::WebContents* contents, WebContentsData::FromWebContents(delta.insert.contents)
int index, ->TabInserted(selection.new_contents == delta.insert.contents);
bool was_active) { }
WebContentsData::FromWebContents(contents)->TabDetached(); break;
} }
case TabStripModelChange::kRemoved: {
void TabActivityWatcher::TabReplacedAt(TabStripModel* tab_strip_model, for (const auto& delta : change.deltas())
content::WebContents* old_contents, WebContentsData::FromWebContents(delta.remove.contents)->TabDetached();
content::WebContents* new_contents, break;
int index) { }
WebContentsData* old_web_contents_data = case TabStripModelChange::kReplaced: {
WebContentsData::FromWebContents(old_contents); for (const auto& delta : change.deltas()) {
old_web_contents_data->WasReplaced(); WebContentsData* old_web_contents_data =
WebContentsData::FromWebContents(delta.replace.old_contents);
// Ensure the WebContentsData is created to observe this WebContents since it old_web_contents_data->WasReplaced();
// likely hasn't been inserted into a tabstrip before.
WebContentsData::CreateForWebContents(new_contents); // Ensure the WebContentsData is created to observe this WebContents
// since it likely hasn't been inserted into a tabstrip before.
WebContentsData::FromWebContents(new_contents) WebContentsData::CreateForWebContents(delta.replace.new_contents);
->DidReplace(*old_web_contents_data);
WebContentsData::FromWebContents(delta.replace.new_contents)
->DidReplace(*old_web_contents_data);
}
break;
}
case TabStripModelChange::kMoved:
case TabStripModelChange::kSelectionOnly:
break;
}
} }
void TabActivityWatcher::TabPinnedStateChanged(TabStripModel* tab_strip_model, void TabActivityWatcher::TabPinnedStateChanged(TabStripModel* tab_strip_model,
......
...@@ -54,17 +54,10 @@ class TabActivityWatcher : public BrowserListObserver, ...@@ -54,17 +54,10 @@ class TabActivityWatcher : public BrowserListObserver,
void OnBrowserSetLastActive(Browser* browser) override; void OnBrowserSetLastActive(Browser* browser) override;
// TabStripModelObserver: // TabStripModelObserver:
void TabInsertedAt(TabStripModel* tab_strip_model, void OnTabStripModelChanged(
content::WebContents* contents, TabStripModel* tab_strip_model,
int index, const TabStripModelChange& change,
bool foreground) override; const TabStripSelectionChange& selection) override;
void TabDetachedAt(content::WebContents* contents,
int index,
bool was_active) override;
void TabReplacedAt(TabStripModel* tab_strip_model,
content::WebContents* old_contents,
content::WebContents* new_contents,
int index) override;
void TabPinnedStateChanged(TabStripModel* tab_strip_model, void TabPinnedStateChanged(TabStripModel* tab_strip_model,
content::WebContents* contents, content::WebContents* contents,
int index) override; int index) override;
......
...@@ -154,8 +154,15 @@ void TabLifecycleUnitSource::UpdateFocusedTab() { ...@@ -154,8 +154,15 @@ void TabLifecycleUnitSource::UpdateFocusedTab() {
TabLifecycleUnit* focused_lifecycle_unit = TabLifecycleUnit* focused_lifecycle_unit =
focused_web_contents ? GetTabLifecycleUnit(focused_web_contents) focused_web_contents ? GetTabLifecycleUnit(focused_web_contents)
: nullptr; : nullptr;
DCHECK(!focused_web_contents || focused_lifecycle_unit);
UpdateFocusedTabTo(focused_lifecycle_unit); // TODO(sangwoo.ko) We are refactoring TabStripModel API and this is
// workaround to avoid DCHECK failure on Chromium os. This DCHECK is supposing
// that OnTabInserted() is always called before OnBrowserSetLastActive is
// called but it's not. After replacing old API use in BrowserView,
// restore this to DCHECK(!focused_web_contents || focused_lifecycle_unit);
// else case will be handled by following OnTabInserted().
if (!focused_web_contents || focused_lifecycle_unit)
UpdateFocusedTabTo(focused_lifecycle_unit);
} }
void TabLifecycleUnitSource::UpdateFocusedTabTo( void TabLifecycleUnitSource::UpdateFocusedTabTo(
...@@ -169,9 +176,8 @@ void TabLifecycleUnitSource::UpdateFocusedTabTo( ...@@ -169,9 +176,8 @@ void TabLifecycleUnitSource::UpdateFocusedTabTo(
focused_lifecycle_unit_ = new_focused_lifecycle_unit; focused_lifecycle_unit_ = new_focused_lifecycle_unit;
} }
void TabLifecycleUnitSource::TabInsertedAt(TabStripModel* tab_strip_model, void TabLifecycleUnitSource::OnTabInserted(TabStripModel* tab_strip_model,
content::WebContents* contents, content::WebContents* contents,
int index,
bool foreground) { bool foreground) {
TabLifecycleUnit* lifecycle_unit = GetTabLifecycleUnit(contents); TabLifecycleUnit* lifecycle_unit = GetTabLifecycleUnit(contents);
if (lifecycle_unit) { if (lifecycle_unit) {
...@@ -197,9 +203,7 @@ void TabLifecycleUnitSource::TabInsertedAt(TabStripModel* tab_strip_model, ...@@ -197,9 +203,7 @@ void TabLifecycleUnitSource::TabInsertedAt(TabStripModel* tab_strip_model,
} }
} }
void TabLifecycleUnitSource::TabDetachedAt(content::WebContents* contents, void TabLifecycleUnitSource::OnTabDetached(content::WebContents* contents) {
int index,
bool was_active) {
TabLifecycleUnit* lifecycle_unit = GetTabLifecycleUnit(contents); TabLifecycleUnit* lifecycle_unit = GetTabLifecycleUnit(contents);
DCHECK(lifecycle_unit); DCHECK(lifecycle_unit);
if (focused_lifecycle_unit_ == lifecycle_unit) if (focused_lifecycle_unit_ == lifecycle_unit)
...@@ -207,18 +211,8 @@ void TabLifecycleUnitSource::TabDetachedAt(content::WebContents* contents, ...@@ -207,18 +211,8 @@ void TabLifecycleUnitSource::TabDetachedAt(content::WebContents* contents,
lifecycle_unit->SetTabStripModel(nullptr); lifecycle_unit->SetTabStripModel(nullptr);
} }
void TabLifecycleUnitSource::ActiveTabChanged( void TabLifecycleUnitSource::OnTabReplaced(content::WebContents* old_contents,
content::WebContents* old_contents, content::WebContents* new_contents) {
content::WebContents* new_contents,
int index,
int reason) {
UpdateFocusedTab();
}
void TabLifecycleUnitSource::TabReplacedAt(TabStripModel* tab_strip_model,
content::WebContents* old_contents,
content::WebContents* new_contents,
int index) {
auto* old_contents_holder = auto* old_contents_holder =
TabLifecycleUnitHolder::FromWebContents(old_contents); TabLifecycleUnitHolder::FromWebContents(old_contents);
DCHECK(old_contents_holder); DCHECK(old_contents_holder);
...@@ -233,6 +227,37 @@ void TabLifecycleUnitSource::TabReplacedAt(TabStripModel* tab_strip_model, ...@@ -233,6 +227,37 @@ void TabLifecycleUnitSource::TabReplacedAt(TabStripModel* tab_strip_model,
new_contents_holder->lifecycle_unit()->SetWebContents(new_contents); new_contents_holder->lifecycle_unit()->SetWebContents(new_contents);
} }
void TabLifecycleUnitSource::OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) {
switch (change.type()) {
case TabStripModelChange::kInserted: {
for (const auto& delta : change.deltas()) {
OnTabInserted(tab_strip_model, delta.insert.contents,
selection.new_contents == delta.insert.contents);
}
break;
}
case TabStripModelChange::kRemoved: {
for (const auto& delta : change.deltas())
OnTabDetached(delta.remove.contents);
break;
}
case TabStripModelChange::kReplaced: {
for (const auto& delta : change.deltas())
OnTabReplaced(delta.replace.old_contents, delta.replace.new_contents);
break;
}
case TabStripModelChange::kMoved:
case TabStripModelChange::kSelectionOnly:
break;
}
if (selection.active_tab_changed() && !tab_strip_model->empty())
UpdateFocusedTab();
}
void TabLifecycleUnitSource::TabChangedAt(content::WebContents* contents, void TabLifecycleUnitSource::TabChangedAt(content::WebContents* contents,
int index, int index,
TabChangeType change_type) { TabChangeType change_type) {
......
...@@ -111,22 +111,20 @@ class TabLifecycleUnitSource : public BrowserListObserver, ...@@ -111,22 +111,20 @@ class TabLifecycleUnitSource : public BrowserListObserver,
// TabInsertedAt() is called. // TabInsertedAt() is called.
void UpdateFocusedTabTo(TabLifecycleUnit* new_focused_lifecycle_unit); void UpdateFocusedTabTo(TabLifecycleUnit* new_focused_lifecycle_unit);
// TabStripModelObserver: // Methods called by OnTabStripModelChanged()
void TabInsertedAt(TabStripModel* tab_strip_model, void OnTabInserted(TabStripModel* tab_strip_model,
content::WebContents* contents, content::WebContents* contents,
int index, bool foreground);
bool foreground) override; void OnTabDetached(content::WebContents* contents);
void TabDetachedAt(content::WebContents* contents, void OnTabReplaced(content::WebContents* old_contents,
int index, content::WebContents* new_contents);
bool was_active) override;
void ActiveTabChanged(content::WebContents* old_contents, // TabStripModelObserver:
content::WebContents* new_contents, void OnTabStripModelChanged(
int index, TabStripModel* tab_strip_model,
int reason) override; const TabStripModelChange& change,
void TabReplacedAt(TabStripModel* tab_strip_model, const TabStripSelectionChange& selection) override;
content::WebContents* old_contents,
content::WebContents* new_contents,
int index) override;
void TabChangedAt(content::WebContents* contents, void TabChangedAt(content::WebContents* contents,
int index, int index,
TabChangeType change_type) override; TabChangeType change_type) override;
......
...@@ -546,10 +546,8 @@ void TabManager::OnMemoryPressure( ...@@ -546,10 +546,8 @@ void TabManager::OnMemoryPressure(
// calling PurgeBrowserMemory() before CRITICAL is reached. // calling PurgeBrowserMemory() before CRITICAL is reached.
} }
void TabManager::ActiveTabChanged(content::WebContents* old_contents, void TabManager::OnActiveTabChanged(content::WebContents* old_contents,
content::WebContents* new_contents, content::WebContents* new_contents) {
int index,
int reason) {
// An active tab is not purged. // An active tab is not purged.
// Calling GetWebContentsData() early ensures that the WebContentsData is // Calling GetWebContentsData() early ensures that the WebContentsData is
// created for |new_contents|, which |stats_collector_| expects. // created for |new_contents|, which |stats_collector_| expects.
...@@ -571,12 +569,10 @@ void TabManager::ActiveTabChanged(content::WebContents* old_contents, ...@@ -571,12 +569,10 @@ void TabManager::ActiveTabChanged(content::WebContents* old_contents,
ResumeTabNavigationIfNeeded(new_contents); ResumeTabNavigationIfNeeded(new_contents);
} }
void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, void TabManager::OnTabInserted(content::WebContents* contents,
content::WebContents* contents,
int index,
bool foreground) { bool foreground) {
// Only interested in background tabs, as foreground tabs get taken care of by // Only interested in background tabs, as foreground tabs get taken care of by
// ActiveTabChanged. // OnActiveTabChanged.
if (foreground) if (foreground)
return; return;
...@@ -588,11 +584,24 @@ void TabManager::TabInsertedAt(TabStripModel* tab_strip_model, ...@@ -588,11 +584,24 @@ void TabManager::TabInsertedAt(TabStripModel* tab_strip_model,
GetTimeToPurge(min_time_to_purge_, max_time_to_purge_)); GetTimeToPurge(min_time_to_purge_, max_time_to_purge_));
} }
void TabManager::TabReplacedAt(TabStripModel* tab_strip_model, void TabManager::OnTabStripModelChanged(
content::WebContents* old_contents, TabStripModel* tab_strip_model,
content::WebContents* new_contents, const TabStripModelChange& change,
int index) { const TabStripSelectionChange& selection) {
WebContentsData::CopyState(old_contents, new_contents); if (change.type() == TabStripModelChange::kInserted) {
for (const auto& delta : change.deltas()) {
OnTabInserted(delta.insert.contents,
delta.insert.contents == selection.new_contents);
}
} else if (change.type() == TabStripModelChange::kReplaced) {
for (const auto& delta : change.deltas()) {
WebContentsData::CopyState(delta.replace.old_contents,
delta.replace.new_contents);
}
}
if (selection.active_tab_changed() && !tab_strip_model->empty())
OnActiveTabChanged(selection.old_contents, selection.new_contents);
} }
void TabManager::OnStartTracking(content::WebContents* web_contents, void TabManager::OnStartTracking(content::WebContents* web_contents,
......
...@@ -311,19 +311,16 @@ class TabManager : public LifecycleUnitObserver, ...@@ -311,19 +311,16 @@ class TabManager : public LifecycleUnitObserver,
void OnMemoryPressure( void OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
// Methods called by OnTabStripModelChanged()
void OnActiveTabChanged(content::WebContents* old_contents,
content::WebContents* new_contents);
void OnTabInserted(content::WebContents* contents, bool foreground);
// TabStripModelObserver: // TabStripModelObserver:
void ActiveTabChanged(content::WebContents* old_contents, void OnTabStripModelChanged(
content::WebContents* new_contents, TabStripModel* tab_strip_model,
int index, const TabStripModelChange& change,
int reason) override; const TabStripSelectionChange& selection) override;
void TabInsertedAt(TabStripModel* tab_strip_model,
content::WebContents* contents,
int index,
bool foreground) override;
void TabReplacedAt(TabStripModel* tab_strip_model,
content::WebContents* old_contents,
content::WebContents* new_contents,
int index) override;
// TabLoadTracker::Observer: // TabLoadTracker::Observer:
void OnStartTracking(content::WebContents* web_contents, void OnStartTracking(content::WebContents* web_contents,
......
...@@ -632,9 +632,7 @@ TEST_F(TabManagerTest, OnDelayedTabSelected) { ...@@ -632,9 +632,7 @@ TEST_F(TabManagerTest, OnDelayedTabSelected) {
EXPECT_TRUE(tab_manager_->IsNavigationDelayedForTest(nav_handle3_.get())); EXPECT_TRUE(tab_manager_->IsNavigationDelayedForTest(nav_handle3_.get()));
// Simulate selecting tab 3, which should start loading immediately. // Simulate selecting tab 3, which should start loading immediately.
tab_manager_->ActiveTabChanged( tab_manager_->OnActiveTabChanged(contents1_.get(), contents3_.get());
contents1_.get(), contents3_.get(), 2,
TabStripModelObserver::CHANGE_REASON_USER_GESTURE);
EXPECT_TRUE(tab_manager_->IsTabLoadingForTest(contents1_.get())); EXPECT_TRUE(tab_manager_->IsTabLoadingForTest(contents1_.get()));
EXPECT_TRUE(tab_manager_->IsTabLoadingForTest(contents3_.get())); EXPECT_TRUE(tab_manager_->IsTabLoadingForTest(contents3_.get()));
......
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