426ef6e02d91cef539b665a8e374fcf473c944ff
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / launcher / browser_status_monitor.h
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_UI_ASH_LAUNCHER_BROWSER_STATUS_MONITOR_H_
6 #define CHROME_BROWSER_UI_ASH_LAUNCHER_BROWSER_STATUS_MONITOR_H_
7
8 #include <map>
9 #include <string>
10
11 #include "ash/shelf/scoped_observer_with_duplicated_sources.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/scoped_observer.h"
15 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
16 #include "chrome/browser/ui/browser_list_observer.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
18 #include "ui/aura/window_observer.h"
19 #include "ui/gfx/display_observer.h"
20 #include "ui/wm/public/activation_change_observer.h"
21
22 namespace aura {
23 class Window;
24
25 namespace client {
26 class ActivationClient;
27 }
28 }  // namespace aura
29
30 class Browser;
31
32 // BrowserStatusMonitor monitors creation/deletion of Browser and its
33 // TabStripModel to keep the launcher representation up to date as the
34 // active tab changes.
35 class BrowserStatusMonitor : public aura::client::ActivationChangeObserver,
36                              public aura::WindowObserver,
37                              public chrome::BrowserListObserver,
38                              public gfx::DisplayObserver,
39                              public TabStripModelObserver {
40  public:
41   explicit BrowserStatusMonitor(ChromeLauncherController* launcher_controller);
42   virtual ~BrowserStatusMonitor();
43
44   // A function which gets called when the current user has changed.
45   // Note that this function is called by the ChromeLauncherController to be
46   // able to do the activation in a proper order - rather then setting an
47   // observer.
48   virtual void ActiveUserChanged(const std::string& user_email) {}
49
50   // A shortcut to call the ChromeLauncherController's UpdateAppState().
51   void UpdateAppItemState(content::WebContents* contents,
52                           ChromeLauncherController::AppState app_state);
53
54   // A shortcut to call the BrowserShortcutLauncherItemController's
55   // UpdateBrowserItemState().
56   void UpdateBrowserItemState();
57
58   // aura::client::ActivationChangeObserver overrides:
59   virtual void OnWindowActivated(aura::Window* gained_active,
60                                  aura::Window* lost_active) OVERRIDE;
61
62   // aura::WindowObserver overrides:
63   virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
64
65   // chrome::BrowserListObserver overrides:
66   virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
67   virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
68
69   // gfx::DisplayObserver overrides:
70   virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE;
71   virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE;
72   virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE;
73
74   // TabStripModelObserver overrides:
75   virtual void ActiveTabChanged(content::WebContents* old_contents,
76                                 content::WebContents* new_contents,
77                                 int index,
78                                 int reason) OVERRIDE;
79   virtual void TabReplacedAt(TabStripModel* tab_strip_model,
80                              content::WebContents* old_contents,
81                              content::WebContents* new_contents,
82                              int index) OVERRIDE;
83   virtual void TabInsertedAt(content::WebContents* contents,
84                              int index,
85                              bool foreground) OVERRIDE;
86   virtual void TabClosingAt(TabStripModel* tab_strip_mode,
87                             content::WebContents* contents,
88                             int index) OVERRIDE;
89
90   // Called from our own |LocalWebContentsObserver| when web contents did go
91   // away without any other notification. This might happen in case of
92   // application uninstalls, page crashes, ...).
93   void WebContentsDestroyed(content::WebContents* web_contents);
94
95  protected:
96   // Add a V1 application to the shelf. This can get overwritten for multi
97   // profile implementations.
98   virtual void AddV1AppToShelf(Browser* browser);
99
100   // Remove a V1 application from the shelf. This can get overwritten for multi
101   // profile implementations.
102   virtual void RemoveV1AppFromShelf(Browser* browser);
103
104   // Check if V1 application is currently in the shelf.
105   bool IsV1AppInShelf(Browser* browser);
106
107  private:
108   class LocalWebContentsObserver;
109   class SettingsWindowObserver;
110
111   typedef std::map<Browser*, std::string> BrowserToAppIDMap;
112   typedef std::map<content::WebContents*, LocalWebContentsObserver*>
113       WebContentsToObserverMap;
114
115   // Create LocalWebContentsObserver for |contents|.
116   void AddWebContentsObserver(content::WebContents* contents);
117
118   // Remove LocalWebContentsObserver for |contents|.
119   void RemoveWebContentsObserver(content::WebContents* contents);
120
121   // Returns the ShelfID for |contents|.
122   ash::ShelfID GetShelfIDForWebContents(content::WebContents* contents);
123
124   // Sets the shelf id for browsers represented by the browser shortcut item.
125   void SetShelfIDForBrowserWindowContents(Browser* browser,
126                                           content::WebContents* web_contents);
127
128   ChromeLauncherController* launcher_controller_;
129
130   // Hold all observed activation clients.
131   ScopedObserverWithDuplicatedSources<aura::client::ActivationClient,
132       aura::client::ActivationChangeObserver> observed_activation_clients_;
133
134   // Hold all observed root windows.
135   ScopedObserver<aura::Window, aura::WindowObserver> observed_root_windows_;
136
137   BrowserToAppIDMap browser_to_app_id_map_;
138   WebContentsToObserverMap webcontents_to_observer_map_;
139   scoped_ptr<SettingsWindowObserver> settings_window_observer_;
140
141   DISALLOW_COPY_AND_ASSIGN(BrowserStatusMonitor);
142 };
143
144 #endif  // CHROME_BROWSER_UI_ASH_LAUNCHER_BROWSER_STATUS_MONITOR_H_