Index: chrome/browser/ui/ash/launcher/browser_status_monitor.cc |
diff --git a/chrome/browser/ui/ash/launcher/browser_status_monitor.cc b/chrome/browser/ui/ash/launcher/browser_status_monitor.cc |
index c9965ed1b419acd26631e71fa2b4b000c4a1f1a7..514fb1073175509e590e881ac058df104d0a0316 100644 |
--- a/chrome/browser/ui/ash/launcher/browser_status_monitor.cc |
+++ b/chrome/browser/ui/ash/launcher/browser_status_monitor.cc |
@@ -6,6 +6,7 @@ |
#include "ash/shell.h" |
#include "ash/wm/window_util.h" |
+#include "base/stl_util.h" |
#include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controller.h" |
#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
#include "chrome/browser/ui/browser.h" |
@@ -15,12 +16,35 @@ |
#include "chrome/browser/ui/tabs/tab_strip_model.h" |
#include "chrome/browser/web_applications/web_app.h" |
#include "content/public/browser/web_contents.h" |
-#include "content/public/browser/web_contents_view.h" |
#include "ui/aura/client/activation_client.h" |
#include "ui/aura/root_window.h" |
#include "ui/aura/window.h" |
#include "ui/gfx/screen.h" |
+BrowserStatusMonitor::LocalWebContentsObserver::LocalWebContentsObserver( |
+ content::WebContents* contents, |
+ BrowserStatusMonitor* monitor) |
+ : content::WebContentsObserver(contents), |
+ monitor_(monitor) { |
+} |
+ |
+BrowserStatusMonitor::LocalWebContentsObserver::~LocalWebContentsObserver() { |
+} |
+ |
+void BrowserStatusMonitor::LocalWebContentsObserver::DidNavigateMainFrame( |
+ const content::LoadCommittedDetails& details, |
+ const content::FrameNavigateParams& params) { |
+ Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
+ ChromeLauncherController::AppState state = |
+ ChromeLauncherController::APP_STATE_ACTIVE; |
+ if (browser->window()->IsActive() && |
+ browser->tab_strip_model()->GetActiveWebContents() == web_contents()) |
+ state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; |
+ |
+ monitor_->UpdateAppItemState(web_contents(), state); |
+ monitor_->UpdateBrowserItemState(); |
+} |
+ |
BrowserStatusMonitor::BrowserStatusMonitor( |
ChromeLauncherController* launcher_controller) |
: launcher_controller_(launcher_controller), |
@@ -62,21 +86,54 @@ BrowserStatusMonitor::~BrowserStatusMonitor() { |
i != browser_list->end(); ++i) { |
OnBrowserRemoved(*i); |
} |
+ |
+ STLDeleteContainerPairSecondPointers(webcontents_to_observer_map_.begin(), |
+ webcontents_to_observer_map_.end()); |
+} |
+ |
+void BrowserStatusMonitor::UpdateAppItemState( |
+ content::WebContents* contents, |
+ ChromeLauncherController::AppState app_state) { |
+ DCHECK(contents); |
+ launcher_controller_->UpdateAppState(contents, app_state); |
+} |
+ |
+void BrowserStatusMonitor::UpdateBrowserItemState() { |
+ launcher_controller_->GetBrowserShortcutLauncherItemController()-> |
+ UpdateBrowserItemState(); |
} |
void BrowserStatusMonitor::OnWindowActivated(aura::Window* gained_active, |
aura::Window* lost_active) { |
- Browser* browser = chrome::FindBrowserWithWindow(lost_active); |
- if (browser) { |
- UpdateAppAndBrowserState( |
- browser->tab_strip_model()->GetActiveWebContents()); |
+ Browser* browser = NULL; |
+ content::WebContents* contents_from_gained = NULL; |
+ content::WebContents* contents_from_lost = NULL; |
+ // Update active webcontents's app item state of |lost_active|, if existed. |
+ if (lost_active) { |
+ browser = chrome::FindBrowserWithWindow(lost_active); |
+ if (browser) |
+ contents_from_lost = browser->tab_strip_model()->GetActiveWebContents(); |
+ if (contents_from_lost) { |
+ UpdateAppItemState( |
+ contents_from_lost, |
+ ChromeLauncherController::APP_STATE_INACTIVE); |
+ } |
} |
- browser = chrome::FindBrowserWithWindow(gained_active); |
- if (browser) { |
- UpdateAppAndBrowserState( |
- browser->tab_strip_model()->GetActiveWebContents()); |
+ // Update active webcontents's app item state of |gained_active|, if existed. |
+ if (gained_active) { |
+ browser = chrome::FindBrowserWithWindow(gained_active); |
+ if (browser) |
+ contents_from_gained = browser->tab_strip_model()->GetActiveWebContents(); |
+ if (contents_from_gained) { |
+ UpdateAppItemState( |
+ contents_from_gained, |
+ ChromeLauncherController::APP_STATE_WINDOW_ACTIVE); |
+ } |
} |
+ |
+ if (contents_from_lost || contents_from_gained) |
+ UpdateBrowserItemState(); |
} |
void BrowserStatusMonitor::OnWindowDestroyed(aura::Window* window) { |
@@ -145,77 +202,85 @@ void BrowserStatusMonitor::ActiveTabChanged(content::WebContents* old_contents, |
int index, |
int reason) { |
Browser* browser = NULL; |
- if (old_contents) |
- browser = chrome::FindBrowserWithWebContents(old_contents); |
+ // Use |new_contents|. |old_contents| could be NULL. |
+ DCHECK(new_contents); |
+ browser = chrome::FindBrowserWithWebContents(new_contents); |
if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) |
return; |
+ ChromeLauncherController::AppState state = |
+ ChromeLauncherController::APP_STATE_INACTIVE; |
+ |
// Update immediately on a tab change. |
- if (browser && |
+ if (old_contents && |
(TabStripModel::kNoTab != |
- browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) { |
- launcher_controller_->UpdateAppState( |
- old_contents, |
- ChromeLauncherController::APP_STATE_INACTIVE); |
+ browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) |
+ UpdateAppItemState(old_contents, state); |
+ |
+ if (new_contents) { |
+ state = browser->window()->IsActive() ? |
+ ChromeLauncherController::APP_STATE_WINDOW_ACTIVE : |
+ ChromeLauncherController::APP_STATE_ACTIVE; |
+ UpdateAppItemState(new_contents, state); |
+ UpdateBrowserItemState(); |
} |
+} |
- UpdateAppAndBrowserState(new_contents); |
+void BrowserStatusMonitor::TabReplacedAt(TabStripModel* tab_strip_model, |
+ content::WebContents* old_contents, |
+ content::WebContents* new_contents, |
+ int index) { |
+ DCHECK(old_contents && new_contents); |
+ Browser* browser = chrome::FindBrowserWithWebContents(new_contents); |
+ |
+ if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) |
+ return; |
+ |
+ UpdateAppItemState(old_contents, |
+ ChromeLauncherController::APP_STATE_REMOVED); |
+ RemoveWebContentsObserver(old_contents); |
+ |
+ ChromeLauncherController::AppState state = |
+ ChromeLauncherController::APP_STATE_ACTIVE; |
+ if (browser->window()->IsActive() && |
+ (tab_strip_model->GetActiveWebContents() == new_contents)) |
+ state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; |
+ UpdateAppItemState(new_contents, state); |
+ AddWebContentsObserver(new_contents); |
} |
void BrowserStatusMonitor::TabInsertedAt(content::WebContents* contents, |
int index, |
bool foreground) { |
- UpdateAppAndBrowserState(contents); |
+ // An inserted tab is not active - ActiveTabChanged() will be called to |
+ // activate. We initialize therefore with |APP_STATE_INACTIVE|. |
+ UpdateAppItemState(contents, |
+ ChromeLauncherController::APP_STATE_INACTIVE); |
+ AddWebContentsObserver(contents); |
} |
-void BrowserStatusMonitor::TabDetachedAt(content::WebContents* contents, |
- int index) { |
- launcher_controller_->UpdateAppState( |
- contents, ChromeLauncherController::APP_STATE_REMOVED); |
- UpdateBrowserItemState(); |
-} |
- |
-void BrowserStatusMonitor::TabChangedAt( |
- content::WebContents* contents, |
- int index, |
- TabStripModelObserver::TabChangeType change_type) { |
- UpdateAppAndBrowserState(contents); |
+void BrowserStatusMonitor::TabClosingAt(TabStripModel* tab_strip_mode, |
+ content::WebContents* contents, |
+ int index) { |
+ UpdateAppItemState(contents, |
+ ChromeLauncherController::APP_STATE_REMOVED); |
+ RemoveWebContentsObserver(contents); |
} |
-void BrowserStatusMonitor::TabReplacedAt(TabStripModel* tab_strip_model, |
- content::WebContents* old_contents, |
- content::WebContents* new_contents, |
- int index) { |
- launcher_controller_->UpdateAppState( |
- old_contents, |
- ChromeLauncherController::APP_STATE_REMOVED); |
- UpdateAppAndBrowserState(new_contents); |
-} |
- |
-void BrowserStatusMonitor::UpdateAppAndBrowserState( |
+void BrowserStatusMonitor::AddWebContentsObserver( |
content::WebContents* contents) { |
- if (contents) { |
- ChromeLauncherController::AppState app_state = |
- ChromeLauncherController::APP_STATE_INACTIVE; |
- |
- Browser* browser = chrome::FindBrowserWithWebContents(contents); |
- DCHECK(browser); |
- if (browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) |
- return; |
- if (browser->tab_strip_model()->GetActiveWebContents() == contents) { |
- if (browser->window()->IsActive()) |
- app_state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; |
- else |
- app_state = ChromeLauncherController::APP_STATE_ACTIVE; |
- } |
- |
- launcher_controller_->UpdateAppState(contents, app_state); |
+ if (webcontents_to_observer_map_.find(contents) == |
+ webcontents_to_observer_map_.end()) { |
+ webcontents_to_observer_map_[contents] = |
+ new LocalWebContentsObserver(contents, this); |
} |
- UpdateBrowserItemState(); |
} |
-void BrowserStatusMonitor::UpdateBrowserItemState() { |
- launcher_controller_->GetBrowserShortcutLauncherItemController()-> |
- UpdateBrowserItemState(); |
+void BrowserStatusMonitor::RemoveWebContentsObserver( |
+ content::WebContents* contents) { |
+ DCHECK(webcontents_to_observer_map_.find(contents) != |
+ webcontents_to_observer_map_.end()); |
+ delete webcontents_to_observer_map_[contents]; |
+ webcontents_to_observer_map_.erase(contents); |
} |