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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
// Copyright (c) 2012 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_UI_VIEWS_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_
#define CHROME_BROWSER_UI_VIEWS_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_
#pragma once
#include <list>
#include <map>
#include <string>
#include "ash/launcher/launcher_delegate.h"
#include "ash/launcher/launcher_model_observer.h"
#include "ash/launcher/launcher_types.h"
#include "ash/wm/shelf_auto_hide_behavior.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/extension_prefs.h"
#include "chrome/browser/extensions/shell_window_registry.h"
#include "chrome/browser/prefs/pref_change_registrar.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "ui/aura/client/activation_change_observer.h"
#include "ui/aura/window_observer.h"
namespace ash {
class LauncherModel;
}
namespace aura {
class Window;
namespace client {
class ActivationClient;
}
}
class BrowserLauncherItemController;
class BrowserLauncherItemControllerTest;
class PrefService;
class Profile;
class TabContents;
// ChromeLauncherController manages the launcher items needed for tabbed
// browsers (BrowserLauncherItemController) and browser shortcuts.
class ChromeLauncherController : public ash::LauncherDelegate,
public ash::LauncherModelObserver,
public content::NotificationObserver,
public ShellWindowRegistry::Observer,
public aura::client::ActivationChangeObserver,
public aura::WindowObserver {
public:
// Indicates if a launcher item is incognito or not.
enum IncognitoState {
STATE_INCOGNITO,
STATE_NOT_INCOGNITO,
};
// Interface used to load app icons. This is in it's own class so that it can
// be mocked.
class AppIconLoader {
public:
virtual ~AppIconLoader() {}
// Returns the app id of the specified tab, or an empty string if there is
// no app.
virtual std::string GetAppID(TabContents* tab) = 0;
// Returns true if |id| is valid. Used during restore to ignore no longer
// valid extensions.
virtual bool IsValidID(const std::string& id) = 0;
// Fetches the image for the specified id. When done (which may be
// synchronous), this should invoke SetAppImage() on the LauncherUpdater.
virtual void FetchImage(const std::string& id) = 0;
};
ChromeLauncherController(Profile* profile, ash::LauncherModel* model);
virtual ~ChromeLauncherController();
// Initializes this ChromeLauncherController.
void Init();
// Returns the single ChromeLauncherController instnace.
static ChromeLauncherController* instance() { return instance_; }
// Creates a new tabbed item on the launcher for |controller|.
ash::LauncherID CreateTabbedLauncherItem(
BrowserLauncherItemController* controller,
IncognitoState is_incognito,
ash::LauncherItemStatus status);
// Creates a new app item on the launcher for |controller|.
ash::LauncherID CreateAppLauncherItem(
BrowserLauncherItemController* controller,
const std::string& app_id,
ash::LauncherItemStatus status);
// Updates the running status of an item.
void SetItemStatus(ash::LauncherID id, ash::LauncherItemStatus status);
// Invoked when the underlying browser/app is closed.
void LauncherItemClosed(ash::LauncherID id);
// Pins the specified id. Currently only supports platform apps.
void Pin(ash::LauncherID id);
// Unpins the specified id, closing if not running.
void Unpin(ash::LauncherID id);
// Returns true if the item identified by |id| is pinned.
bool IsPinned(ash::LauncherID id);
// Pins/unpins the specified id.
void TogglePinned(ash::LauncherID id);
// Returns true if the specified item can be pinned or unpinned. Only apps can
// be pinned.
bool IsPinnable(ash::LauncherID id) const;
// Opens the specified item. |event_flags| holds the flags of the
// event which triggered this command.
void Open(ash::LauncherID id, int event_flags);
// Closes the specified item.
void Close(ash::LauncherID id);
// Returns true if the specified item is open.
bool IsOpen(ash::LauncherID id);
// Returns the launch type of app for the specified id.
ExtensionPrefs::LaunchType GetLaunchType(ash::LauncherID id);
// Returns the id of the app for the specified tab.
std::string GetAppID(TabContents* tab);
// Sets the image for an app tab. This is intended to be invoked from the
// AppIconLoader.
void SetAppImage(const std::string& app_id, const SkBitmap* image);
// Returns true if a pinned launcher item with given |app_id| could be found.
bool IsAppPinned(const std::string& app_id);
// Pins an app with |app_id| to launcher. If there is a running instance in
// launcher, the running instance is pinned. If there is no running instance,
// a new launcher item is created and pinned.
void PinAppWithID(const std::string& app_id);
// Updates the launche type of the app for the specified id to |launch_type|.
void SetLaunchType(ash::LauncherID id,
ExtensionPrefs::LaunchType launch_type);
// Unpins any app items whose id is |app_id|.
void UnpinAppsWithID(const std::string& app_id);
// Returns true if the user is currently logged in as a guest.
bool IsLoggedInAsGuest();
// Invoked when the user clicks on button in the launcher to create a new
// incognito window.
void CreateNewIncognitoWindow();
// Checks whether the user is allowed to pin apps. Pinning may be disallowed
// by policy in case there is a pre-defined set of pinned apps.
bool CanPin() const;
ash::LauncherModel* model() { return model_; }
Profile* profile() { return profile_; }
void SetAutoHideBehavior(ash::ShelfAutoHideBehavior behavior);
// ash::LauncherDelegate overrides:
virtual void CreateNewTab() OVERRIDE;
virtual void CreateNewWindow() OVERRIDE;
virtual void ItemClicked(const ash::LauncherItem& item,
int event_flags) OVERRIDE;
virtual int GetBrowserShortcutResourceId() OVERRIDE;
virtual string16 GetTitle(const ash::LauncherItem& item) OVERRIDE;
virtual ui::MenuModel* CreateContextMenu(
const ash::LauncherItem& item) OVERRIDE;
virtual ui::MenuModel* CreateContextMenuForLauncher() OVERRIDE;
virtual ash::LauncherID GetIDByWindow(aura::Window* window) OVERRIDE;
virtual bool IsDraggable(const ash::LauncherItem& item) OVERRIDE;
// ash::LauncherModelObserver overrides:
virtual void LauncherItemAdded(int index) OVERRIDE;
virtual void LauncherItemRemoved(int index, ash::LauncherID id) OVERRIDE;
virtual void LauncherItemMoved(int start_index, int target_index) OVERRIDE;
virtual void LauncherItemChanged(int index,
const ash::LauncherItem& old_item) OVERRIDE;
// Overridden from content::NotificationObserver:
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
// Overridden from ShellWindowRegistry::Observer:
virtual void OnShellWindowAdded(ShellWindow* shell_window) OVERRIDE;
virtual void OnShellWindowRemoved(ShellWindow* shell_window) OVERRIDE;
// Overriden from client::ActivationChangeObserver:
virtual void OnWindowActivated(
aura::Window* active,
aura::Window* old_active) OVERRIDE;
// Overriden from aura::WindowObserver:
virtual void OnWindowRemovingFromRootWindow(aura::Window* window) OVERRIDE;
private:
friend class BrowserLauncherItemControllerTest;
friend class ChromeLauncherControllerTest;
enum ItemType {
TYPE_APP,
TYPE_TABBED_BROWSER
};
// Used to identity an item on the launcher.
struct Item {
Item();
~Item();
// Type of item.
ItemType item_type;
// ID of the app.
std::string app_id;
// The BrowserLauncherItemController this item came from. NULL if a
// shortcut.
BrowserLauncherItemController* controller;
};
typedef std::map<ash::LauncherID, Item> IDToItemMap;
typedef std::map<aura::Window*, ash::LauncherID> WindowToIDMap;
typedef std::list<aura::Window*> WindowList;
// Updates the pinned pref state. The pinned state consists of a list pref.
// Each item of the list is a dictionary. The key |kAppIDPath| gives the
// id of the app.
void PersistPinnedState();
// Sets the AppIconLoader, taking ownership of |loader|. This is intended for
// testing.
void SetAppIconLoaderForTest(AppIconLoader* loader);
// Returns the profile used for new windows.
Profile* GetProfileForNewWindows();
// Returns item status for given |id|.
ash::LauncherItemStatus GetItemStatus(ash::LauncherID id) const;
// Finds the launcher item that represents given |app_id| and updates the
// pending state.
void MarkAppPending(const std::string& app_id);
// Internal helpers for pinning and unpinning that handle both
// client-triggered and internal pinning operations.
void DoPinAppWithID(const std::string& app_id);
void DoUnpinAppsWithID(const std::string& app_id);
// Re-syncs launcher model with prefs::kPinnedLauncherApps.
void UpdateAppLaunchersFromPref();
// Creates an app launcher to insert at |index|. Note that |index| may be
// adjusted by the model to meet ordering constraints.
ash::LauncherID InsertAppLauncherItem(
BrowserLauncherItemController* controller,
const std::string& app_id,
ash::LauncherItemStatus status,
int index);
static ChromeLauncherController* instance_;
ash::LauncherModel* model_;
// Profile used for prefs and loading extensions. This is NOT necessarily the
// profile new windows are created with.
Profile* profile_;
IDToItemMap id_to_item_map_;
// Allows us to get from an aura::Window to the id of a launcher item.
// Currently only used for platform app windows.
WindowToIDMap window_to_id_map_;
// Maintains the activation order. The first element is most recent.
// Currently only used for platform app windows.
WindowList activation_order_;
// Used to load the image for an app tab.
scoped_ptr<AppIconLoader> app_icon_loader_;
content::NotificationRegistrar notification_registrar_;
PrefChangeRegistrar pref_change_registrar_;
aura::client::ActivationClient* activation_client_;
DISALLOW_COPY_AND_ASSIGN(ChromeLauncherController);
};
#endif // CHROME_BROWSER_UI_VIEWS_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_
|