1// Copyright (c) 2011 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_CHROMEOS_WM_OVERVIEW_CONTROLLER_H_
6#define CHROME_BROWSER_CHROMEOS_WM_OVERVIEW_CONTROLLER_H_
7#pragma once
8
9#include <vector>
10
11#include "base/memory/linked_ptr.h"
12#include "base/memory/singleton.h"
13#include "base/timer.h"
14#include "chrome/browser/chromeos/wm_message_listener.h"
15#include "chrome/browser/ui/browser_list.h"
16#include "content/common/notification_registrar.h"
17#include "ui/gfx/rect.h"
18
19namespace views {
20class Widget;
21}
22
23class Browser;
24class RenderWidgetHost;
25
26namespace chromeos {
27
28class BrowserListener;
29
30// WmOverviewController is responsible for managing a list of objects
31// that listen to the browsers (BrowserListeners, defined in the
32// source file for this class) for changes, and keep a list of
33// snapshot images in sync with the browser tab contents.
34//
35// As tabs are added/removed from the browsers, the number of snapshot
36// windows changes to match.
37//
38// As obtaining and setting snapshots is expensive we delay setting
39// the snapshot. The delay is controlled by delay_timer_. Once the
40// timer fires another timer is started (configure_timer_). This timer
41// invokes ConfigureNextUnconfiguredCell on the BrowserListener, which
42// obtains and sets the snapshot of the next uncofigured
43// cell. ConfigureNextUnconfiguredCell only configures one cell at a
44// time until they are all configured.
45
46class WmOverviewController : public BrowserList::Observer,
47                             public WmMessageListener::Observer,
48                             public NotificationObserver {
49 public:
50  // These are the possible layout modes that this controller can be
51  // in.  The layout mode is controlled by the window manager.
52  enum LayoutMode {
53    // ACTIVE_MODE is the mode where chrome takes up the whole screen
54    // and the user interacts with it, and this controller hides the
55    // snapshots and stops refreshing them.
56    ACTIVE_MODE,
57
58    // OVERVIEW_MODE is the mode where the toplevel windows are hidden
59    // and the user interacts with the snapshots.  This is when the
60    // snapshot windows are shown and actively updated by this
61    // controller.
62    OVERVIEW_MODE,
63  };
64
65  // This class is a singleton.
66  static WmOverviewController* GetInstance();
67
68  // BrowserList::Observer methods
69  void OnBrowserAdded(const Browser* browser) {}
70  void OnBrowserRemoved(const Browser* browser);
71  // End BrowserList::Observer methods
72
73  // WmMessageListener::Observer methods
74  // This is called immediately after a browser is added to the list.
75  void ProcessWmMessage(const WmIpc::Message& message,
76                        GdkWindow* window);
77  // End WmMessageListener::Observer methods
78
79  // NotificationObserver methods
80  void Observe(NotificationType type,
81               const NotificationSource& source,
82               const NotificationDetails& details);
83  // End NotificationObserver methods
84
85  // Used by the BrowserListeners to configure their snapshots.
86  const gfx::Rect& monitor_bounds() const { return monitor_bounds_; }
87
88  // Start reloading snapshots if in overview mode.
89  void UpdateSnapshots();
90
91  // Starts the delay timer, and once the delay is over, configures
92  // any unconfigured snapshots one at a time until none are left to
93  // be configured.
94  void StartDelayTimer();
95
96  LayoutMode layout_mode() const { return layout_mode_; }
97
98 private:
99  friend struct DefaultSingletonTraits<WmOverviewController>;
100
101  // This class is a singleton.
102  WmOverviewController();
103  ~WmOverviewController();
104
105  // Restores tab selections on all browsers to what they were when
106  // Show was last called.  Used when cancelling overview mode.
107  void RestoreTabSelections();
108
109  // Saves the currently selected tabs in the snapshots so that they
110  // can be restored later with RestoreTabSelections.
111  void SaveTabSelections();
112
113  // Show the snapshot windows, saving current tab selections.
114  void Show();
115
116  // Hide the snapshot windows.  When |cancelled| is true, then the
117  // tab selections that were saved when the snapshot windows were
118  // shown are restored.
119  void Hide(bool cancelled);
120
121  // Add browser listeners for all existing browsers, reusing any that
122  // were already there.
123  void AddAllBrowsers();
124
125  // Called when the thumbnail generator notifies us that the snapshot
126  // image changed.  This determines which TabContents the given
127  // renderer is attached to, and reloads that snapshot.
128  void SnapshotImageChanged(RenderWidgetHost* renderer);
129
130  // This is so we can register for notifications.
131  NotificationRegistrar registrar_;
132
133  // This is a vector of listeners that listen to all the browsers.
134  typedef std::vector<linked_ptr<BrowserListener> > BrowserListenerVector;
135  BrowserListenerVector listeners_;
136
137  // This is the bounds of the monitor we're being displayed on. This
138  // is used to adjust the size of snapshots so they'll fit.
139  gfx::Rect monitor_bounds_;
140
141  // See description above class for details.
142  base::OneShotTimer<WmOverviewController> delay_timer_;
143
144  // The current layout mode.
145  LayoutMode layout_mode_;
146
147  // This flag is set whenever there is a pending |AskForSnapshot| to Chrome;
148  // This is used to prevent |UpdateSnapshots| from being called multiple times.
149  bool updating_snapshots_;
150
151  // These indices are used to track the last updated browser listener and tab
152  // content, They are used to implement update tab contents in a round-robin
153  // fashion.
154  int browser_listener_index_;  // index of the last updated browser listener.
155  int tab_contents_index_;      // index of the last updated tab contents.
156
157  DISALLOW_COPY_AND_ASSIGN(WmOverviewController);
158};
159
160}  // namespace chromeos
161
162#endif  // CHROME_BROWSER_CHROMEOS_WM_OVERVIEW_CONTROLLER_H_
163