summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/wm_overview_controller.h
blob: 386189a03258f5fc377ec2c1b0385c083e1732c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_WM_OVERVIEW_CONTROLLER_H_
#define CHROME_BROWSER_CHROMEOS_WM_OVERVIEW_CONTROLLER_H_
#pragma once

#include <vector>

#include "base/linked_ptr.h"
#include "base/singleton.h"
#include "base/timer.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/chromeos/wm_message_listener.h"
#include "chrome/common/notification_registrar.h"
#include "ui/gfx/rect.h"

namespace views {
class Widget;
}

class Browser;
class RenderWidgetHost;

namespace chromeos {

class BrowserListener;

// WmOverviewController is responsible for managing a list of objects
// that listen to the browsers (BrowserListeners, defined in the
// source file for this class) for changes, and keep a list of
// snapshot images in sync with the browser tab contents.
//
// As tabs are added/removed from the browsers, the number of snapshot
// windows changes to match.
//
// As obtaining and setting snapshots is expensive we delay setting
// the snapshot. The delay is controlled by delay_timer_. Once the
// timer fires another timer is started (configure_timer_). This timer
// invokes ConfigureNextUnconfiguredCell on the BrowserListener, which
// obtains and sets the snapshot of the next uncofigured
// cell. ConfigureNextUnconfiguredCell only configures one cell at a
// time until they are all configured.

class WmOverviewController : public BrowserList::Observer,
                             public WmMessageListener::Observer,
                             public NotificationObserver {
 public:
  // These are the possible layout modes that this controller can be
  // in.  The layout mode is controlled by the window manager.
  enum LayoutMode {
    // ACTIVE_MODE is the mode where chrome takes up the whole screen
    // and the user interacts with it, and this controller hides the
    // snapshots and stops refreshing them.
    ACTIVE_MODE,

    // OVERVIEW_MODE is the mode where the toplevel windows are hidden
    // and the user interacts with the snapshots.  This is when the
    // snapshot windows are shown and actively updated by this
    // controller.
    OVERVIEW_MODE,
  };

  // This class is a singleton.
  static WmOverviewController* GetInstance();

  // BrowserList::Observer methods
  void OnBrowserAdded(const Browser* browser) {}
  void OnBrowserRemoved(const Browser* browser);
  // End BrowserList::Observer methods

  // WmMessageListener::Observer methods
  // This is called immediately after a browser is added to the list.
  void ProcessWmMessage(const WmIpc::Message& message,
                        GdkWindow* window);
  // End WmMessageListener::Observer methods

  // NotificationObserver methods
  void Observe(NotificationType type,
               const NotificationSource& source,
               const NotificationDetails& details);
  // End NotificationObserver methods

  // Used by the BrowserListeners to configure their snapshots.
  const gfx::Rect& monitor_bounds() const { return monitor_bounds_; }

  // Start reloading snapshots if in overview mode.
  void UpdateSnapshots();

  // Starts the delay timer, and once the delay is over, configures
  // any unconfigured snapshots one at a time until none are left to
  // be configured.
  void StartDelayTimer();

  LayoutMode layout_mode() const { return layout_mode_; }

 private:
  friend struct DefaultSingletonTraits<WmOverviewController>;

  // This class is a singleton.
  WmOverviewController();
  ~WmOverviewController();

  // Restores tab selections on all browsers to what they were when
  // Show was last called.  Used when cancelling overview mode.
  void RestoreTabSelections();

  // Saves the currently selected tabs in the snapshots so that they
  // can be restored later with RestoreTabSelections.
  void SaveTabSelections();

  // Show the snapshot windows, saving current tab selections.
  void Show();

  // Hide the snapshot windows.  When |cancelled| is true, then the
  // tab selections that were saved when the snapshot windows were
  // shown are restored.
  void Hide(bool cancelled);

  // Add browser listeners for all existing browsers, reusing any that
  // were already there.
  void AddAllBrowsers();

  // Called when the thumbnail generator notifies us that the snapshot
  // image changed.  This determines which TabContents the given
  // renderer is attached to, and reloads that snapshot.
  void SnapshotImageChanged(RenderWidgetHost* renderer);

  // This is so we can register for notifications.
  NotificationRegistrar registrar_;

  // This is a vector of listeners that listen to all the browsers.
  typedef std::vector<linked_ptr<BrowserListener> > BrowserListenerVector;
  BrowserListenerVector listeners_;

  // This is the bounds of the monitor we're being displayed on. This
  // is used to adjust the size of snapshots so they'll fit.
  gfx::Rect monitor_bounds_;

  // See description above class for details.
  base::OneShotTimer<WmOverviewController> delay_timer_;

  // The current layout mode.
  LayoutMode layout_mode_;

  // This flag is set whenever there is a pending |AskForSnapshot| to Chrome;
  // This is used to prevent |UpdateSnapshots| from being called multiple times.
  bool updating_snapshots_;

  // These indices are used to track the last updated browser listener and tab
  // content, They are used to implement update tab contents in a round-robin
  // fashion.
  int browser_listener_index_;  // index of the last updated browser listener.
  int tab_contents_index_;      // index of the last updated tab contents.

  DISALLOW_COPY_AND_ASSIGN(WmOverviewController);
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_WM_OVERVIEW_CONTROLLER_H_