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.
5 #include "chrome/browser/ui/app_list/extension_app_item.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_util.h"
11 #include "chrome/browser/extensions/launch_util.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/app_list/app_context_menu.h"
14 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
15 #include "chrome/browser/ui/app_list/app_list_service.h"
16 #include "chrome/browser/ui/extensions/extension_enable_flow.h"
17 #include "chrome/browser/ui/host_desktop.h"
18 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/extensions/extension_constants.h"
21 #include "chrome/common/extensions/manifest_url_handler.h"
22 #include "content/public/browser/user_metrics.h"
23 #include "extensions/browser/app_sorting.h"
24 #include "extensions/browser/extension_prefs.h"
25 #include "extensions/browser/extension_system.h"
26 #include "extensions/common/extension.h"
27 #include "extensions/common/extension_icon_set.h"
28 #include "extensions/common/manifest_handlers/icons_handler.h"
29 #include "grit/theme_resources.h"
30 #include "sync/api/string_ordinal.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/canvas.h"
33 #include "ui/gfx/color_utils.h"
34 #include "ui/gfx/image/canvas_image_source.h"
35 #include "ui/gfx/image/image_skia_operations.h"
36 #include "ui/gfx/rect.h"
38 using extensions::Extension;
42 // Overlays a shortcut icon over the bottom left corner of a given image.
43 class ShortcutOverlayImageSource : public gfx::CanvasImageSource {
45 explicit ShortcutOverlayImageSource(const gfx::ImageSkia& icon)
46 : gfx::CanvasImageSource(icon.size(), false),
49 virtual ~ShortcutOverlayImageSource() {}
52 // gfx::CanvasImageSource overrides:
53 virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
54 canvas->DrawImageInt(icon_, 0, 0);
56 // Draw the overlay in the bottom left corner of the icon.
57 const gfx::ImageSkia& overlay = *ui::ResourceBundle::GetSharedInstance().
58 GetImageSkiaNamed(IDR_APP_LIST_TAB_OVERLAY);
59 canvas->DrawImageInt(overlay, 0, icon_.height() - overlay.height());
64 DISALLOW_COPY_AND_ASSIGN(ShortcutOverlayImageSource);
67 // Rounds the corners of a given image.
68 class RoundedCornersImageSource : public gfx::CanvasImageSource {
70 explicit RoundedCornersImageSource(const gfx::ImageSkia& icon)
71 : gfx::CanvasImageSource(icon.size(), false),
74 virtual ~RoundedCornersImageSource() {}
77 // gfx::CanvasImageSource overrides:
78 virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
79 // The radius used to round the app icon.
80 const size_t kRoundingRadius = 2;
82 canvas->DrawImageInt(icon_, 0, 0);
84 scoped_ptr<gfx::Canvas> masking_canvas(
85 new gfx::Canvas(gfx::Size(icon_.width(), icon_.height()), 1.0f, false));
86 DCHECK(masking_canvas);
89 opaque_paint.setColor(SK_ColorWHITE);
90 opaque_paint.setFlags(SkPaint::kAntiAlias_Flag);
91 masking_canvas->DrawRoundRect(
92 gfx::Rect(icon_.width(), icon_.height()),
93 kRoundingRadius, opaque_paint);
95 SkPaint masking_paint;
96 masking_paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
98 gfx::ImageSkia(masking_canvas->ExtractImageRep()), 0, 0, masking_paint);
101 gfx::ImageSkia icon_;
103 DISALLOW_COPY_AND_ASSIGN(RoundedCornersImageSource);
106 extensions::AppSorting* GetAppSorting(Profile* profile) {
107 return extensions::ExtensionPrefs::Get(profile)->app_sorting();
110 const color_utils::HSL shift = {-1, 0, 0.6};
114 ExtensionAppItem::ExtensionAppItem(
116 const app_list::AppListSyncableService::SyncItem* sync_item,
117 const std::string& extension_id,
118 const std::string& extension_name,
119 const gfx::ImageSkia& installing_icon,
120 bool is_platform_app)
121 : app_list::AppListItem(extension_id),
123 extension_id_(extension_id),
124 extension_enable_flow_controller_(NULL),
125 extension_name_(extension_name),
127 gfx::ImageSkiaOperations::CreateHSLShiftedImage(installing_icon,
129 is_platform_app_(is_platform_app),
130 has_overlay_(false) {
132 if (sync_item && sync_item->item_ordinal.IsValid()) {
133 // An existing synced position exists, use that.
134 set_position(sync_item->item_ordinal);
135 // Only set the name from the sync item if it is empty.
137 SetName(sync_item->item_name);
140 GetAppSorting(profile_)->EnsureValidOrdinals(extension_id_,
141 syncer::StringOrdinal());
142 UpdatePositionFromExtensionOrdering();
145 ExtensionAppItem::~ExtensionAppItem() {
148 bool ExtensionAppItem::NeedsOverlay() const {
149 // The overlay icon is disabled for hosted apps in windowed mode with
150 // streamlined hosted apps.
151 bool streamlined_hosted_apps = CommandLine::ForCurrentProcess()->
152 HasSwitch(switches::kEnableStreamlinedHostedApps);
153 #if defined(OS_CHROMEOS)
154 if (!streamlined_hosted_apps)
157 extensions::LaunchType launch_type =
159 ? extensions::GetLaunchType(extensions::ExtensionPrefs::Get(profile_),
161 : extensions::LAUNCH_TYPE_WINDOW;
163 return !is_platform_app_ && extension_id_ != extension_misc::kChromeAppId &&
164 (!streamlined_hosted_apps ||
165 launch_type != extensions::LAUNCH_TYPE_WINDOW);
168 void ExtensionAppItem::Reload() {
169 const Extension* extension = GetExtension();
170 bool is_installing = !extension;
171 SetIsInstalling(is_installing);
173 SetName(extension_name_);
177 SetNameAndShortName(extension->name(), extension->short_name());
178 LoadImage(extension);
181 void ExtensionAppItem::UpdateIcon() {
182 gfx::ImageSkia icon = installing_icon_;
184 // Use the app icon if the app exists. Turn the image greyscale if the app is
186 if (GetExtension()) {
187 icon = icon_->image_skia();
188 const bool enabled = extensions::util::IsAppLaunchable(extension_id_,
191 const color_utils::HSL shift = {-1, 0, 0.6};
192 icon = gfx::ImageSkiaOperations::CreateHSLShiftedImage(icon, shift);
195 if (GetExtension()->from_bookmark())
196 icon = gfx::ImageSkia(new RoundedCornersImageSource(icon), icon.size());
198 // Paint the shortcut overlay if necessary.
199 has_overlay_ = NeedsOverlay();
201 icon = gfx::ImageSkia(new ShortcutOverlayImageSource(icon), icon.size());
206 void ExtensionAppItem::Move(const ExtensionAppItem* prev,
207 const ExtensionAppItem* next) {
209 return; // No reordering necessary
211 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
212 extensions::AppSorting* sorting = GetAppSorting(profile_);
214 syncer::StringOrdinal page;
215 std::string prev_id, next_id;
217 next_id = next->extension_id();
218 page = sorting->GetPageOrdinal(next_id);
220 prev_id = prev->extension_id();
221 page = sorting->GetPageOrdinal(prev_id);
223 prev_id = prev->extension_id();
224 page = sorting->GetPageOrdinal(prev_id);
225 // Only set |next_id| if on the same page, otherwise just insert after prev.
226 if (page.Equals(sorting->GetPageOrdinal(next->extension_id())))
227 next_id = next->extension_id();
229 prefs->SetAppDraggedByUser(extension_id_);
230 sorting->SetPageOrdinal(extension_id_, page);
231 sorting->OnExtensionMoved(extension_id_, prev_id, next_id);
232 UpdatePositionFromExtensionOrdering();
235 const Extension* ExtensionAppItem::GetExtension() const {
236 const ExtensionService* service =
237 extensions::ExtensionSystem::Get(profile_)->extension_service();
238 const Extension* extension = service->GetInstalledExtension(extension_id_);
242 void ExtensionAppItem::LoadImage(const Extension* extension) {
243 icon_.reset(new extensions::IconImage(
246 extensions::IconsInfo::GetIcons(extension),
247 extension_misc::EXTENSION_ICON_MEDIUM,
248 extensions::util::GetDefaultAppIcon(),
253 bool ExtensionAppItem::RunExtensionEnableFlow() {
254 if (extensions::util::IsAppLaunchableWithoutEnabling(extension_id_, profile_))
257 if (!extension_enable_flow_) {
258 extension_enable_flow_controller_ = GetController();
259 extension_enable_flow_controller_->OnShowChildDialog();
261 extension_enable_flow_.reset(new ExtensionEnableFlow(
262 profile_, extension_id_, this));
263 extension_enable_flow_->StartForNativeWindow(
264 extension_enable_flow_controller_->GetAppListWindow());
269 void ExtensionAppItem::Launch(int event_flags) {
270 // |extension| could be NULL when it is being unloaded for updating.
271 const Extension* extension = GetExtension();
275 // Don't auto-enable apps that cannot be launched.
276 if (!extensions::util::IsAppLaunchable(extension_id_, profile_))
279 if (RunExtensionEnableFlow())
282 GetController()->LaunchApp(profile_,
284 AppListControllerDelegate::LAUNCH_FROM_APP_LIST,
288 void ExtensionAppItem::OnExtensionIconImageChanged(
289 extensions::IconImage* image) {
290 DCHECK(icon_.get() == image);
294 void ExtensionAppItem::ExtensionEnableFlowFinished() {
295 extension_enable_flow_.reset();
296 extension_enable_flow_controller_->OnCloseChildDialog();
297 extension_enable_flow_controller_ = NULL;
299 // Automatically launch app after enabling.
303 void ExtensionAppItem::ExtensionEnableFlowAborted(bool user_initiated) {
304 extension_enable_flow_.reset();
305 extension_enable_flow_controller_->OnCloseChildDialog();
306 extension_enable_flow_controller_ = NULL;
309 void ExtensionAppItem::Activate(int event_flags) {
310 // |extension| could be NULL when it is being unloaded for updating.
311 const Extension* extension = GetExtension();
315 // Don't auto-enable apps that cannot be launched.
316 if (!extensions::util::IsAppLaunchable(extension_id_, profile_))
319 if (RunExtensionEnableFlow())
322 content::RecordAction(base::UserMetricsAction("AppList_ClickOnApp"));
323 CoreAppLauncherHandler::RecordAppListMainLaunch(extension);
324 GetController()->ActivateApp(profile_,
326 AppListControllerDelegate::LAUNCH_FROM_APP_LIST,
330 ui::MenuModel* ExtensionAppItem::GetContextMenuModel() {
331 context_menu_.reset(new app_list::AppContextMenu(
332 this, profile_, extension_id_, GetController()));
333 context_menu_->set_is_platform_app(is_platform_app_);
335 context_menu_->set_is_in_folder(true);
336 return context_menu_->GetMenuModel();
339 void ExtensionAppItem::OnExtensionPreferenceChanged() {
340 if (has_overlay_ != NeedsOverlay())
345 const char ExtensionAppItem::kItemType[] = "ExtensionAppItem";
347 const char* ExtensionAppItem::GetItemType() const {
348 return ExtensionAppItem::kItemType;
351 void ExtensionAppItem::ExecuteLaunchCommand(int event_flags) {
355 void ExtensionAppItem::UpdatePositionFromExtensionOrdering() {
356 const syncer::StringOrdinal& page =
357 GetAppSorting(profile_)->GetPageOrdinal(extension_id_);
358 const syncer::StringOrdinal& launch =
359 GetAppSorting(profile_)->GetAppLaunchOrdinal(extension_id_);
360 set_position(syncer::StringOrdinal(
361 page.ToInternalValue() + launch.ToInternalValue()));
364 AppListControllerDelegate* ExtensionAppItem::GetController() {
365 return AppListService::Get(chrome::GetActiveDesktop())->
366 GetControllerDelegate();