Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: chrome/browser/ui/ash/app_list/extension_app_item.cc

Issue 10871011: Move common app_list delegate code to chrome/browser/ui/app_list. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Unit test file only in chrome Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "chrome/browser/ui/ash/app_list/extension_app_item.h"
6
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_prefs.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
12 #include "chrome/browser/extensions/management_policy.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/ash/app_list/app_list_controller.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_navigator.h"
17 #include "chrome/browser/ui/browser_tabstrip.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/common/extensions/extension.h"
20 #include "chrome/common/extensions/extension_constants.h"
21 #include "chrome/common/extensions/extension_icon_set.h"
22 #include "grit/chromium_strings.h"
23 #include "grit/generated_resources.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/gfx/image/image.h"
26
27 using extensions::Extension;
28
29 namespace {
30
31 enum CommandId {
32 LAUNCH = 100,
33 TOGGLE_PIN,
34 OPTIONS,
35 UNINSTALL,
36 DETAILS,
37 // Order matters in LAUNCHER_TYPE_xxxx and must match LaunchType.
38 LAUNCH_TYPE_START = 200,
39 LAUNCH_TYPE_PINNED_TAB = LAUNCH_TYPE_START,
40 LAUNCH_TYPE_REGULAR_TAB,
41 LAUNCH_TYPE_FULLSCREEN,
42 LAUNCH_TYPE_WINDOW,
43 LAUNCH_TYPE_LAST,
44 };
45
46 // ExtensionUninstaller decouples ExtensionAppItem from the extension uninstall
47 // flow. It shows extension uninstall dialog and wait for user to confirm or
48 // cancel the uninstall.
49 class ExtensionUninstaller : public ExtensionUninstallDialog::Delegate {
50 public:
51 ExtensionUninstaller(Profile* profile,
52 const std::string& extension_id)
53 : profile_(profile),
54 extension_id_(extension_id) {
55 }
56
57 void Run() {
58 const Extension* extension =
59 profile_->GetExtensionService()->GetExtensionById(extension_id_, true);
60 if (!extension) {
61 CleanUp();
62 return;
63 }
64
65 ExtensionUninstallDialog* dialog =
66 ExtensionUninstallDialog::Create(NULL, this);
67 dialog->ConfirmUninstall(extension);
68 }
69
70 private:
71 // Overridden from ExtensionUninstallDialog::Delegate:
72 virtual void ExtensionUninstallAccepted() OVERRIDE {
73 ExtensionService* service = profile_->GetExtensionService();
74 const Extension* extension = service->GetExtensionById(extension_id_, true);
75 if (extension) {
76 service->UninstallExtension(extension_id_,
77 false, /* external_uninstall*/
78 NULL);
79 }
80
81 CleanUp();
82 }
83
84 virtual void ExtensionUninstallCanceled() OVERRIDE {
85 CleanUp();
86 }
87
88 void CleanUp() {
89 delete this;
90 }
91
92 Profile* profile_;
93 std::string extension_id_;
94
95 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstaller);
96 };
97
98 extensions::ExtensionPrefs::LaunchType GetExtensionLaunchType(
99 Profile* profile,
100 const std::string& extension_id) {
101 return profile->GetExtensionService()->extension_prefs()->GetLaunchType(
102 extension_id, extensions::ExtensionPrefs::LAUNCH_DEFAULT);
103 }
104
105 void SetExtensionLaunchType(
106 Profile* profile,
107 const std::string& extension_id,
108 extensions::ExtensionPrefs::LaunchType launch_type) {
109 profile->GetExtensionService()->extension_prefs()->SetLaunchType(
110 extension_id, launch_type);
111 }
112
113 bool IsExtensionEnabled(Profile* profile, const std::string& extension_id) {
114 ExtensionService* service = profile->GetExtensionService();
115 return service->IsExtensionEnabled(extension_id) &&
116 !service->GetTerminatedExtension(extension_id);
117 }
118
119 } // namespace
120
121 ExtensionAppItem::ExtensionAppItem(Profile* profile,
122 const Extension* extension,
123 AppListController* controller)
124 : ChromeAppListItem(TYPE_APP),
125 profile_(profile),
126 extension_id_(extension->id()),
127 controller_(controller) {
128 SetTitle(extension->name());
129 LoadImage(extension);
130 }
131
132 ExtensionAppItem::~ExtensionAppItem() {
133 }
134
135 const Extension* ExtensionAppItem::GetExtension() const {
136 const Extension* extension =
137 profile_->GetExtensionService()->GetInstalledExtension(extension_id_);
138 return extension;
139 }
140
141 void ExtensionAppItem::LoadImage(const Extension* extension) {
142 tracker_.reset(new ImageLoadingTracker(this));
143 tracker_->LoadImage(extension,
144 extension->GetIconResource(
145 extension_misc::EXTENSION_ICON_LARGE,
146 ExtensionIconSet::MATCH_BIGGER),
147 gfx::Size(extension_misc::EXTENSION_ICON_LARGE,
148 extension_misc::EXTENSION_ICON_LARGE),
149 ImageLoadingTracker::DONT_CACHE);
150 }
151
152 void ExtensionAppItem::ShowExtensionOptions() {
153 const Extension* extension = GetExtension();
154 if (!extension)
155 return;
156
157 chrome::NavigateParams params(profile_,
158 extension->options_url(),
159 content::PAGE_TRANSITION_LINK);
160 chrome::Navigate(&params);
161 }
162
163 void ExtensionAppItem::ShowExtensionDetails() {
164 const Extension* extension = GetExtension();
165 if (!extension)
166 return;
167
168 chrome::NavigateParams params(profile_,
169 extension->details_url(),
170 content::PAGE_TRANSITION_LINK);
171 chrome::Navigate(&params);
172 }
173
174 void ExtensionAppItem::StartExtensionUninstall() {
175 // ExtensionUninstall deletes itself when done or aborted.
176 ExtensionUninstaller* uninstaller = new ExtensionUninstaller(profile_,
177 extension_id_);
178 uninstaller->Run();
179 }
180
181 void ExtensionAppItem::OnImageLoaded(const gfx::Image& image,
182 const std::string& extension_id,
183 int tracker_index) {
184 if (!image.IsEmpty())
185 SetIcon(*image.ToImageSkia());
186 else
187 SetIcon(Extension::GetDefaultIcon(true /* is_app */));
188 }
189
190 bool ExtensionAppItem::IsItemForCommandIdDynamic(int command_id) const {
191 return command_id == TOGGLE_PIN;
192 }
193
194 string16 ExtensionAppItem::GetLabelForCommandId(int command_id) const {
195 if (command_id == TOGGLE_PIN) {
196 return controller_->IsAppPinned(extension_id_) ?
197 l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_UNPIN) :
198 l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_PIN);
199 } else {
200 NOTREACHED();
201 return string16();
202 }
203 }
204
205 bool ExtensionAppItem::IsCommandIdChecked(int command_id) const {
206 if (command_id >= LAUNCH_TYPE_START && command_id < LAUNCH_TYPE_LAST) {
207 return static_cast<int>(GetExtensionLaunchType(profile_, extension_id_)) +
208 LAUNCH_TYPE_START == command_id;
209 }
210 return false;
211 }
212
213 bool ExtensionAppItem::IsCommandIdEnabled(int command_id) const {
214 if (command_id == TOGGLE_PIN) {
215 return controller_->CanPin();
216 } else if (command_id == OPTIONS) {
217 const Extension* extension = GetExtension();
218 return IsExtensionEnabled(profile_, extension_id_) && extension &&
219 !extension->options_url().is_empty();
220 } else if (command_id == UNINSTALL) {
221 const Extension* extension = GetExtension();
222 const extensions::ManagementPolicy* policy =
223 extensions::ExtensionSystem::Get(profile_)->management_policy();
224 return extension &&
225 policy->UserMayModifySettings(extension, NULL);
226 } else if (command_id == DETAILS) {
227 const Extension* extension = GetExtension();
228 return extension && extension->from_webstore();
229 }
230 return true;
231 }
232
233 bool ExtensionAppItem::GetAcceleratorForCommandId(
234 int command_id,
235 ui::Accelerator* acclelrator) {
236 return false;
237 }
238
239 void ExtensionAppItem::ExecuteCommand(int command_id) {
240 if (command_id == LAUNCH) {
241 Activate(0);
242 } else if (command_id == TOGGLE_PIN && controller_->CanPin()) {
243 if (controller_->IsAppPinned(extension_id_))
244 controller_->UnpinApp(extension_id_);
245 else
246 controller_->PinApp(extension_id_);
247 } else if (command_id >= LAUNCH_TYPE_START &&
248 command_id < LAUNCH_TYPE_LAST) {
249 SetExtensionLaunchType(profile_,
250 extension_id_,
251 static_cast<extensions::ExtensionPrefs::LaunchType>(
252 command_id - LAUNCH_TYPE_START));
253 } else if (command_id == OPTIONS) {
254 ShowExtensionOptions();
255 } else if (command_id == UNINSTALL) {
256 StartExtensionUninstall();
257 } else if (command_id == DETAILS) {
258 ShowExtensionDetails();
259 }
260 }
261
262 void ExtensionAppItem::Activate(int event_flags) {
263 const Extension* extension = GetExtension();
264 if (!extension)
265 return;
266
267 controller_->ActivateApp(profile_, extension->id(), event_flags);
268 }
269
270 ui::MenuModel* ExtensionAppItem::GetContextMenuModel() {
271 // No context menu for Chrome app.
272 if (extension_id_ == extension_misc::kChromeAppId)
273 return NULL;
274
275 if (!context_menu_model_.get()) {
276 context_menu_model_.reset(new ui::SimpleMenuModel(this));
277 context_menu_model_->AddItem(LAUNCH, UTF8ToUTF16(title()));
278 context_menu_model_->AddSeparator();
279 context_menu_model_->AddItemWithStringId(
280 TOGGLE_PIN,
281 controller_->IsAppPinned(extension_id_) ?
282 IDS_APP_LIST_CONTEXT_MENU_UNPIN :
283 IDS_APP_LIST_CONTEXT_MENU_PIN);
284 context_menu_model_->AddSeparator();
285 context_menu_model_->AddCheckItemWithStringId(
286 LAUNCH_TYPE_REGULAR_TAB,
287 IDS_APP_CONTEXT_MENU_OPEN_REGULAR);
288 context_menu_model_->AddCheckItemWithStringId(
289 LAUNCH_TYPE_PINNED_TAB,
290 IDS_APP_CONTEXT_MENU_OPEN_PINNED);
291 context_menu_model_->AddCheckItemWithStringId(
292 LAUNCH_TYPE_WINDOW,
293 IDS_APP_CONTEXT_MENU_OPEN_WINDOW);
294 // Even though the launch type is Full Screen it is more accurately
295 // described as Maximized in Ash.
296 context_menu_model_->AddCheckItemWithStringId(
297 LAUNCH_TYPE_FULLSCREEN,
298 IDS_APP_CONTEXT_MENU_OPEN_MAXIMIZED);
299 context_menu_model_->AddSeparator();
300 context_menu_model_->AddItemWithStringId(OPTIONS, IDS_NEW_TAB_APP_OPTIONS);
301 context_menu_model_->AddItemWithStringId(DETAILS, IDS_NEW_TAB_APP_DETAILS);
302 context_menu_model_->AddItemWithStringId(UNINSTALL,
303 IDS_EXTENSIONS_UNINSTALL);
304 }
305
306 return context_menu_model_.get();
307 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/app_list/extension_app_item.h ('k') | chrome/browser/ui/ash/app_list/search_builder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698