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
22namespace aura {
23class Window;
24
25namespace client {
26class ActivationClient;
27}
28}  // namespace aura
29
30class 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.
35class 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 OnDisplayAdded(const gfx::Display& new_display) OVERRIDE;
71  virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE;
72  virtual void OnDisplayMetricsChanged(const gfx::Display& display,
73                                       uint32_t metrics) 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  // Called from our own |LocalWebContentsObserver| when web contents did go
92  // away without any other notification. This might happen in case of
93  // application uninstalls, page crashes, ...).
94  void WebContentsDestroyed(content::WebContents* web_contents);
95
96 protected:
97  // Add a V1 application to the shelf. This can get overwritten for multi
98  // profile implementations.
99  virtual void AddV1AppToShelf(Browser* browser);
100
101  // Remove a V1 application from the shelf. This can get overwritten for multi
102  // profile implementations.
103  virtual void RemoveV1AppFromShelf(Browser* browser);
104
105  // Check if V1 application is currently in the shelf.
106  bool IsV1AppInShelf(Browser* browser);
107
108 private:
109  class LocalWebContentsObserver;
110  class SettingsWindowObserver;
111
112  typedef std::map<Browser*, std::string> BrowserToAppIDMap;
113  typedef std::map<content::WebContents*, LocalWebContentsObserver*>
114      WebContentsToObserverMap;
115
116  // Create LocalWebContentsObserver for |contents|.
117  void AddWebContentsObserver(content::WebContents* contents);
118
119  // Remove LocalWebContentsObserver for |contents|.
120  void RemoveWebContentsObserver(content::WebContents* contents);
121
122  // Returns the ShelfID for |contents|.
123  ash::ShelfID GetShelfIDForWebContents(content::WebContents* contents);
124
125  // Sets the shelf id for browsers represented by the browser shortcut item.
126  void SetShelfIDForBrowserWindowContents(Browser* browser,
127                                          content::WebContents* web_contents);
128
129  ChromeLauncherController* launcher_controller_;
130
131  // Hold all observed activation clients.
132  ScopedObserverWithDuplicatedSources<aura::client::ActivationClient,
133      aura::client::ActivationChangeObserver> observed_activation_clients_;
134
135  // Hold all observed root windows.
136  ScopedObserver<aura::Window, aura::WindowObserver> observed_root_windows_;
137
138  BrowserToAppIDMap browser_to_app_id_map_;
139  WebContentsToObserverMap webcontents_to_observer_map_;
140  scoped_ptr<SettingsWindowObserver> settings_window_observer_;
141
142  DISALLOW_COPY_AND_ASSIGN(BrowserStatusMonitor);
143};
144
145#endif  // CHROME_BROWSER_UI_ASH_LAUNCHER_BROWSER_STATUS_MONITOR_H_
146