Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / launcher / browser_status_monitor.cc
index 0b1c506..93f6ef1 100644 (file)
 #include "chrome/browser/ui/browser_finder.h"
 #include "chrome/browser/ui/browser_list.h"
 #include "chrome/browser/ui/browser_window.h"
+#include "chrome/browser/ui/settings_window_manager.h"
+#include "chrome/browser/ui/settings_window_manager_observer.h"
 #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 "ui/aura/client/activation_client.h"
-#include "ui/aura/root_window.h"
+#include "content/public/browser/web_contents_observer.h"
+#include "grit/ash_resources.h"
+#include "grit/generated_resources.h"
 #include "ui/aura/window.h"
+#include "ui/aura/window_event_dispatcher.h"
+#include "ui/base/l10n/l10n_util.h"
 #include "ui/gfx/screen.h"
+#include "ui/wm/public/activation_client.h"
+
+// This class monitors the WebContent of the all tab and notifies a navigation
+// to the BrowserStatusMonitor.
+class BrowserStatusMonitor::LocalWebContentsObserver
+    : public content::WebContentsObserver {
+ public:
+  LocalWebContentsObserver(content::WebContents* contents,
+                           BrowserStatusMonitor* monitor)
+      : content::WebContentsObserver(contents),
+        monitor_(monitor) {}
+
+  virtual ~LocalWebContentsObserver() {}
+
+  // content::WebContentsObserver
+  virtual void DidNavigateMainFrame(
+      const content::LoadCommittedDetails& details,
+      const content::FrameNavigateParams& params) OVERRIDE {
+    Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
+    ChromeLauncherController::AppState state =
+        ChromeLauncherController::APP_STATE_INACTIVE;
+    if (browser->window()->IsActive() &&
+        browser->tab_strip_model()->GetActiveWebContents() == web_contents())
+      state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE;
+    else if (browser->window()->IsActive())
+      state = ChromeLauncherController::APP_STATE_ACTIVE;
+
+    monitor_->UpdateAppItemState(web_contents(), state);
+    monitor_->UpdateBrowserItemState();
+
+    // Navigating may change the ShelfID associated with the WebContents.
+    if (browser->tab_strip_model()->GetActiveWebContents() == web_contents())
+      monitor_->SetShelfIDForBrowserWindowContents(browser, web_contents());
+  }
 
-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_INACTIVE;
-  if (browser->window()->IsActive() &&
-      browser->tab_strip_model()->GetActiveWebContents() == web_contents())
-    state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE;
-  else if (browser->window()->IsActive())
-    state = ChromeLauncherController::APP_STATE_ACTIVE;
-
-  monitor_->UpdateAppItemState(web_contents(), state);
-  monitor_->UpdateBrowserItemState();
+  virtual void WebContentsDestroyed() OVERRIDE {
+    // We can only come here when there was a non standard termination like
+    // an app got un-installed while running, etc.
+    monitor_->WebContentsDestroyed(web_contents());
+    // |this| is gone now.
+  }
 
-  // Navigating may change the LauncherID associated with the WebContents.
-  if (browser->tab_strip_model()->GetActiveWebContents() == web_contents()) {
-    ash::SetLauncherIDForWindow(
-        monitor_->GetLauncherIDForWebContents(web_contents()),
-        browser->window()->GetNativeWindow());
+ private:
+  BrowserStatusMonitor* monitor_;
+
+  DISALLOW_COPY_AND_ASSIGN(LocalWebContentsObserver);
+};
+
+// Observes any new settings windows and sets their shelf icon (since they
+// are excluded from BrowserShortcutLauncherItem).
+class BrowserStatusMonitor::SettingsWindowObserver
+    : public chrome::SettingsWindowManagerObserver {
+ public:
+  SettingsWindowObserver() {}
+  virtual ~SettingsWindowObserver() {}
+
+  // SettingsWindowManagerObserver
+  virtual void OnNewSettingsWindow(Browser* settings_browser) OVERRIDE {
+    ash::SetShelfItemDetailsForDialogWindow(
+        settings_browser->window()->GetNativeWindow(),
+        IDR_ASH_SHELF_ICON_SETTINGS,
+        l10n_util::GetStringUTF16(IDS_SETTINGS_TITLE));
   }
-}
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(SettingsWindowObserver);
+};
 
 BrowserStatusMonitor::BrowserStatusMonitor(
     ChromeLauncherController* launcher_controller)
     : launcher_controller_(launcher_controller),
       observed_activation_clients_(this),
-      observed_root_windows_(this) {
+      observed_root_windows_(this),
+      settings_window_observer_(new SettingsWindowObserver) {
   DCHECK(launcher_controller_);
   BrowserList::AddObserver(this);
+  chrome::SettingsWindowManager::GetInstance()->AddObserver(
+      settings_window_observer_.get());
 
   // This check needs for win7_aura. Without this, all tests in
   // ChromeLauncherController will fail in win7_aura.
@@ -106,9 +148,11 @@ void BrowserStatusMonitor::UpdateAppItemState(
     ChromeLauncherController::AppState app_state) {
   DCHECK(contents);
   // It is possible to come here from Browser::SwapTabContent where the contents
-  // cannot be associated with a browser.
+  // cannot be associated with a browser. A removal however should be properly
+  // processed.
   Browser* browser = chrome::FindBrowserWithWebContents(contents);
-  if (browser && launcher_controller_->IsBrowserFromActiveUser(browser))
+  if (app_state == ChromeLauncherController::APP_STATE_REMOVED ||
+      (browser && launcher_controller_->IsBrowserFromActiveUser(browser)))
     launcher_controller_->UpdateAppState(contents, app_state);
 }
 
@@ -183,11 +227,6 @@ void BrowserStatusMonitor::OnBrowserRemoved(Browser* browser) {
   UpdateBrowserItemState();
 }
 
-void BrowserStatusMonitor::OnDisplayBoundsChanged(
-    const gfx::Display& display) {
-  // Do nothing here.
-}
-
 void BrowserStatusMonitor::OnDisplayAdded(const gfx::Display& new_display) {
   // Add a new RootWindow and its ActivationClient to observed list.
   aura::Window* root_window = ash::Shell::GetInstance()->
@@ -208,6 +247,11 @@ void BrowserStatusMonitor::OnDisplayRemoved(const gfx::Display& old_display) {
   // Do nothing here.
 }
 
+void BrowserStatusMonitor::OnDisplayMetricsChanged(const gfx::Display&,
+                                                   uint32_t) {
+  // Do nothing here.
+}
+
 void BrowserStatusMonitor::ActiveTabChanged(content::WebContents* old_contents,
                                             content::WebContents* new_contents,
                                             int index,
@@ -235,9 +279,7 @@ void BrowserStatusMonitor::ActiveTabChanged(content::WebContents* old_contents,
         ChromeLauncherController::APP_STATE_ACTIVE;
     UpdateAppItemState(new_contents, state);
     UpdateBrowserItemState();
-    ash::SetLauncherIDForWindow(
-        GetLauncherIDForWebContents(new_contents),
-        browser->window()->GetNativeWindow());
+    SetShelfIDForBrowserWindowContents(browser, new_contents);
   }
 }
 
@@ -263,11 +305,8 @@ void BrowserStatusMonitor::TabReplacedAt(TabStripModel* tab_strip_model,
   UpdateAppItemState(new_contents, state);
   UpdateBrowserItemState();
 
-  if (tab_strip_model->GetActiveWebContents() == new_contents) {
-    ash::SetLauncherIDForWindow(
-        GetLauncherIDForWebContents(new_contents),
-        browser->window()->GetNativeWindow());
-  }
+  if (tab_strip_model->GetActiveWebContents() == new_contents)
+    SetShelfIDForBrowserWindowContents(browser, new_contents);
 
   AddWebContentsObserver(new_contents);
 }
@@ -290,6 +329,12 @@ void BrowserStatusMonitor::TabClosingAt(TabStripModel* tab_strip_mode,
   RemoveWebContentsObserver(contents);
 }
 
+void BrowserStatusMonitor::WebContentsDestroyed(
+    content::WebContents* contents) {
+  UpdateAppItemState(contents, ChromeLauncherController::APP_STATE_REMOVED);
+  RemoveWebContentsObserver(contents);
+}
+
 void BrowserStatusMonitor::AddV1AppToShelf(Browser* browser) {
   DCHECK(browser->is_type_popup() && browser->is_app());
 
@@ -335,7 +380,14 @@ void BrowserStatusMonitor::RemoveWebContentsObserver(
   webcontents_to_observer_map_.erase(contents);
 }
 
-ash::LauncherID BrowserStatusMonitor::GetLauncherIDForWebContents(
+ash::ShelfID BrowserStatusMonitor::GetShelfIDForWebContents(
     content::WebContents* contents) {
-  return launcher_controller_->GetLauncherIDForWebContents(contents);
+  return launcher_controller_->GetShelfIDForWebContents(contents);
+}
+
+void BrowserStatusMonitor::SetShelfIDForBrowserWindowContents(
+    Browser* browser,
+    content::WebContents* web_contents) {
+  launcher_controller_->GetBrowserShortcutLauncherItemController()->
+      SetShelfIDForBrowserWindowContents(browser, web_contents);
 }