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