Upstream version 5.34.104.0
[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 "content/public/browser/web_contents_observer.h"
19 #include "ui/aura/client/activation_change_observer.h"
20 #include "ui/aura/window_observer.h"
21 #include "ui/gfx/display_observer.h"
22
23 namespace aura {
24 class Window;
25
26 namespace client {
27 class ActivationClient;
28 }
29 }  // namespace aura
30
31 class Browser;
32
33 // BrowserStatusMonitor monitors creation/deletion of Browser and its
34 // TabStripModel to keep the launcher representation up to date as the
35 // active tab changes.
36 class BrowserStatusMonitor : public aura::client::ActivationChangeObserver,
37                              public aura::WindowObserver,
38                              public chrome::BrowserListObserver,
39                              public gfx::DisplayObserver,
40                              public TabStripModelObserver {
41  public:
42   explicit BrowserStatusMonitor(ChromeLauncherController* launcher_controller);
43   virtual ~BrowserStatusMonitor();
44
45   // A function which gets called when the current user has changed.
46   // Note that this function is called by the ChromeLauncherController to be
47   // able to do the activation in a proper order - rather then setting an
48   // observer.
49   virtual void ActiveUserChanged(const std::string& user_email) {}
50
51   // A shortcut to call the ChromeLauncherController's UpdateAppState().
52   void UpdateAppItemState(content::WebContents* contents,
53                           ChromeLauncherController::AppState app_state);
54
55   // A shortcut to call the BrowserShortcutLauncherItemController's
56   // UpdateBrowserItemState().
57   void UpdateBrowserItemState();
58
59   // aura::client::ActivationChangeObserver overrides:
60   virtual void OnWindowActivated(aura::Window* gained_active,
61                                  aura::Window* lost_active) OVERRIDE;
62
63   // aura::WindowObserver overrides:
64   virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
65
66   // chrome::BrowserListObserver overrides:
67   virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
68   virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
69
70   // gfx::DisplayObserver overrides:
71   virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE;
72   virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE;
73   virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE;
74
75   // TabStripModelObserver overrides:
76   virtual void ActiveTabChanged(content::WebContents* old_contents,
77                                 content::WebContents* new_contents,
78                                 int index,
79                                 int reason) OVERRIDE;
80   virtual void TabReplacedAt(TabStripModel* tab_strip_model,
81                              content::WebContents* old_contents,
82                              content::WebContents* new_contents,
83                              int index) OVERRIDE;
84   virtual void TabInsertedAt(content::WebContents* contents,
85                              int index,
86                              bool foreground) OVERRIDE;
87   virtual void TabClosingAt(TabStripModel* tab_strip_mode,
88                             content::WebContents* contents,
89                             int index) OVERRIDE;
90
91  protected:
92   // Add a V1 application to the shelf. This can get overwritten for multi
93   // profile implementations.
94   virtual void AddV1AppToShelf(Browser* browser);
95
96   // Remove a V1 application from the shelf. This can get overwritten for multi
97   // profile implementations.
98   virtual void RemoveV1AppFromShelf(Browser* browser);
99
100   // Check if V1 application is currently in the shelf.
101   bool IsV1AppInShelf(Browser* browser);
102
103  private:
104   // This class monitors the WebContent of the all tab and notifies a navigation
105   // to the BrowserStatusMonitor.
106   class LocalWebContentsObserver : public content::WebContentsObserver {
107    public:
108     LocalWebContentsObserver(content::WebContents* contents,
109                              BrowserStatusMonitor* monitor);
110     virtual ~LocalWebContentsObserver();
111
112     // content::WebContentsObserver overrides:
113     virtual void DidNavigateMainFrame(
114         const content::LoadCommittedDetails& details,
115         const content::FrameNavigateParams& params) OVERRIDE;
116
117    private:
118     BrowserStatusMonitor* monitor_;
119
120     DISALLOW_COPY_AND_ASSIGN(LocalWebContentsObserver);
121   };
122
123   typedef std::map<Browser*, std::string> BrowserToAppIDMap;
124   typedef std::map<content::WebContents*, LocalWebContentsObserver*>
125       WebContentsToObserverMap;
126
127   // Create LocalWebContentsObserver for |contents|.
128   void AddWebContentsObserver(content::WebContents* contents);
129
130   // Remove LocalWebContentsObserver for |contents|.
131   void RemoveWebContentsObserver(content::WebContents* contents);
132
133   // Retruns the ShelfID for |contents|.
134   ash::ShelfID GetShelfIDForWebContents(content::WebContents* contents);
135
136   ChromeLauncherController* launcher_controller_;
137
138   // Hold all observed activation clients.
139   ScopedObserverWithDuplicatedSources<aura::client::ActivationClient,
140       aura::client::ActivationChangeObserver> observed_activation_clients_;
141
142   // Hold all observed root windows.
143   ScopedObserver<aura::Window, aura::WindowObserver> observed_root_windows_;
144
145   BrowserToAppIDMap browser_to_app_id_map_;
146
147   WebContentsToObserverMap webcontents_to_observer_map_;
148
149   DISALLOW_COPY_AND_ASSIGN(BrowserStatusMonitor);
150 };
151
152 #endif  // CHROME_BROWSER_UI_ASH_LAUNCHER_BROWSER_STATUS_MONITOR_H_