OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_VIEWS_AURA_LAUNCHER_CHROME_LAUNCHER_DELEGATE_H_ | |
6 #define CHROME_BROWSER_UI_VIEWS_AURA_LAUNCHER_CHROME_LAUNCHER_DELEGATE_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 #include <string> | |
11 | |
12 #include "ash/launcher/launcher_delegate.h" | |
13 #include "ash/launcher/launcher_model_observer.h" | |
14 #include "ash/launcher/launcher_types.h" | |
15 #include "base/basictypes.h" | |
16 #include "base/compiler_specific.h" | |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "content/public/browser/notification_observer.h" | |
19 #include "content/public/browser/notification_registrar.h" | |
20 | |
21 namespace ash { | |
22 class LauncherModel; | |
23 } | |
24 | |
25 class LauncherUpdater; | |
26 class LauncherUpdaterTest; | |
27 class PrefService; | |
28 class Profile; | |
29 class TabContentsWrapper; | |
30 | |
31 // ChromeLauncherDelegate manages the launcher items needed for tabbed browsers | |
32 // and apps. It does this by way of LauncherUpdaters. | |
33 // TODO: rename this. ChromeLauncherDelegate is a poor name for what it actually | |
34 // does. | |
35 class ChromeLauncherDelegate : public ash::LauncherDelegate, | |
36 public ash::LauncherModelObserver, | |
37 public content::NotificationObserver { | |
38 public: | |
39 // Indicates what should happen when the app is launched. | |
40 enum AppType { | |
41 APP_TYPE_WINDOW, | |
42 APP_TYPE_TAB | |
43 }; | |
44 | |
45 // Interface used to load app icons. This is in it's own class so that it can | |
46 // be mocked. | |
47 class AppIconLoader { | |
48 public: | |
49 virtual ~AppIconLoader() {} | |
50 | |
51 // Returns the app id of the specified tab, or an empty string if there is | |
52 // no app. | |
53 virtual std::string GetAppID(TabContentsWrapper* tab) = 0; | |
54 | |
55 // Returns true if |id| is valid. Used during restore to ignore no longer | |
56 // valid extensions. | |
57 virtual bool IsValidID(const std::string& id) = 0; | |
58 | |
59 // Fetches the image for the specified id. When done (which may be | |
60 // synchronous), this should invoke SetAppImage() on the LauncherUpdater. | |
61 virtual void FetchImage(const std::string& id) = 0; | |
62 }; | |
63 | |
64 ChromeLauncherDelegate(Profile* profile, ash::LauncherModel* model); | |
65 virtual ~ChromeLauncherDelegate(); | |
66 | |
67 // Initializes this ChromeLauncherDelegate. | |
68 void Init(); | |
69 | |
70 // Returns the single ChromeLauncherDelegate instnace. | |
71 static ChromeLauncherDelegate* instance() { return instance_; } | |
72 | |
73 // Registers the prefs used by ChromeLauncherDelegate. | |
74 static void RegisterUserPrefs(PrefService* user_prefs); | |
75 | |
76 // Creates a new tabbed item on the launcher for |updater|. | |
77 ash::LauncherID CreateTabbedLauncherItem(LauncherUpdater* updater); | |
78 | |
79 // Creates a new app item on the launcher for |updater|. If there is an | |
80 // existing pinned app that isn't running on the launcher, its id is returned. | |
81 ash::LauncherID CreateAppLauncherItem(LauncherUpdater* updater, | |
82 const std::string& app_id, | |
83 AppType app_type); | |
84 | |
85 // Converts an app item to a tabbed item. | |
86 void ConvertAppToTabbed(ash::LauncherID id); | |
87 | |
88 // Converts a tabbed item to an app item. | |
89 void ConvertTabbedToApp(ash::LauncherID id, | |
90 const std::string& app_id, | |
91 AppType app_type); | |
92 | |
93 // Invoked when the underlying browser/app is closed. If the item isn't pinned | |
94 // it's removed, otherwise the item says around so that the next time the user | |
95 // launches the app it uses the existing item. | |
96 void LauncherItemClosed(ash::LauncherID id); | |
97 | |
98 // Invoked when the id of an app changes. | |
99 void AppIDChanged(ash::LauncherID id, const std::string& app_id); | |
100 | |
101 // Returns true if there is a closed item identified by the specified | |
102 // arguments.. | |
103 bool HasClosedAppItem(const std::string& app_id, AppType app_type); | |
104 | |
105 // Pins the specified id. | |
106 void Pin(ash::LauncherID id); | |
107 | |
108 // Unpins the specified id, closing if not running. | |
109 void Unpin(ash::LauncherID id); | |
110 | |
111 // Returns true if the item identified by |id| is pinned. | |
112 bool IsPinned(ash::LauncherID id); | |
113 | |
114 // Pins/unpins the specified id. | |
115 void TogglePinned(ash::LauncherID id); | |
116 | |
117 // Returns true if the specified item can be pinned or unpinned. Only apps can | |
118 // be pinned. | |
119 bool IsPinnable(ash::LauncherID id); | |
120 | |
121 // Opens the specified item. | |
122 void Open(ash::LauncherID id); | |
123 | |
124 // Closes the specified item. | |
125 void Close(ash::LauncherID id); | |
126 | |
127 // Returns true if the specified item is open. | |
128 bool IsOpen(ash::LauncherID id); | |
129 | |
130 // Returns the type of app for the specified id. | |
131 AppType GetAppType(ash::LauncherID id); | |
132 | |
133 // Returns the id of the app for the specified tab. | |
134 std::string GetAppID(TabContentsWrapper* tab); | |
135 | |
136 // Sets the image for an app tab. This is intended to be invoked from the | |
137 // AppIconLoader. | |
138 void SetAppImage(const std::string& app_id, SkBitmap* image); | |
139 | |
140 ash::LauncherModel* model() { return model_; } | |
141 | |
142 // ash::LauncherDelegate overrides: | |
143 virtual void CreateNewWindow() OVERRIDE; | |
144 virtual void ItemClicked(const ash::LauncherItem& item) OVERRIDE; | |
145 virtual int GetBrowserShortcutResourceId() OVERRIDE; | |
146 virtual string16 GetTitle(const ash::LauncherItem& item) OVERRIDE; | |
147 virtual ui::MenuModel* CreateContextMenu( | |
148 const ash::LauncherItem& item) OVERRIDE; | |
149 | |
150 // ash::LauncherModelObserver overrides: | |
151 virtual void LauncherItemAdded(int index) OVERRIDE; | |
152 virtual void LauncherItemRemoved(int index, ash::LauncherID id) OVERRIDE; | |
153 virtual void LauncherItemMoved(int start_index, int target_index) OVERRIDE; | |
154 virtual void LauncherItemChanged(int index, | |
155 const ash::LauncherItem& old_item) OVERRIDE; | |
156 virtual void LauncherItemWillChange(int index) OVERRIDE; | |
157 | |
158 // Overridden from content::NotificationObserver: | |
159 virtual void Observe(int type, | |
160 const content::NotificationSource& source, | |
161 const content::NotificationDetails& details) OVERRIDE; | |
162 private: | |
163 friend class LauncherUpdaterTest; | |
164 | |
165 enum ItemType { | |
166 TYPE_APP, | |
167 TYPE_TABBED_BROWSER | |
168 }; | |
169 | |
170 // Used to identity an item on the launcher. | |
171 struct Item { | |
172 Item(); | |
173 ~Item(); | |
174 | |
175 // Type of item. | |
176 ItemType item_type; | |
177 | |
178 // If |item_type| is |TYPE_APP|, this identifies how the app is launched. | |
179 AppType app_type; | |
180 | |
181 // ID of the app. | |
182 std::string app_id; | |
183 | |
184 // The LauncherUpdater this item came from. NULL if pinned and not open. | |
185 LauncherUpdater* updater; | |
186 | |
187 // Whether the item is pinned. | |
188 bool pinned; | |
189 }; | |
190 | |
191 typedef std::map<ash::LauncherID, Item> IDToItemMap; | |
192 | |
193 // Updates the pinned pref state. The pinned state consists of a list pref. | |
194 // Each item of the list is a dictionary. The key |kAppIDPath| gives the | |
195 // id of the app. |kAppTypePath| is one of |kAppTypeTab| or |kAppTypeWindow| | |
196 // and indicates how the app is opened. | |
197 void PersistPinnedState(); | |
198 | |
199 // Unpins any app items whose id is |app_id|. | |
200 void UnpinAppsWithID(const std::string& app_id); | |
201 | |
202 // Sets the AppIconLoader, taking ownership of |loader|. This is intended for | |
203 // testing. | |
204 void SetAppIconLoaderForTest(AppIconLoader* loader); | |
205 | |
206 // Returns the profile used for new windows. | |
207 Profile* GetProfileForNewWindows(); | |
208 | |
209 static ChromeLauncherDelegate* instance_; | |
210 | |
211 ash::LauncherModel* model_; | |
212 | |
213 // Profile used for prefs and loading extensions. This is NOT necessarily the | |
214 // profile new windows are created with. | |
215 Profile* profile_; | |
216 | |
217 IDToItemMap id_to_item_map_; | |
218 | |
219 // Used to load the image for an app tab. | |
220 scoped_ptr<AppIconLoader> app_icon_loader_; | |
221 | |
222 content::NotificationRegistrar registrar_; | |
223 | |
224 DISALLOW_COPY_AND_ASSIGN(ChromeLauncherDelegate); | |
225 }; | |
226 | |
227 #endif // CHROME_BROWSER_UI_VIEWS_AURA_LAUNCHER_CHROME_LAUNCHER_DELEGATE_H_ | |
OLD | NEW |