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

Side by Side Diff: chrome/browser/ui/views/aura/app_list/extension_item.cc

Issue 8890049: [Aura] Implement views-based applist. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased and address comments in #2 Created 9 years 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) 2011 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/views/aura/app_list/extension_item.h"
6
7 #include "chrome/browser/event_disposition.h"
8 #include "chrome/browser/extensions/extension_prefs.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_icon_set.h"
14 #include "chrome/common/extensions/extension_resource.h"
15 #include "grit/component_extension_resources_map.h"
16 #include "grit/theme_resources.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/aura_shell/app_list/app_list_item_view.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/gfx/codec/png_codec.h"
21 #include "ui/gfx/image/image.h"
22
23 namespace {
24
25 const ExtensionPrefs* GetExtensionPrefs(Profile* profile) {
26 return profile->GetExtensionService()->extension_prefs();
27 }
28
29 // Gets page ordinal of given |extension| in its app launch page.
30 StringOrdinal GetExtensionPageOrdinal(Profile* profile,
31 const Extension* extension) {
32 const ExtensionPrefs* prefs = GetExtensionPrefs(profile);
33 StringOrdinal page_ordinal = prefs->GetPageOrdinal(extension->id());
34 if (!page_ordinal.IsValid()) {
35 page_ordinal = extension->id() == extension_misc::kWebStoreAppId ?
36 prefs->CreateFirstAppPageOrdinal() : prefs->GetNaturalAppPageOrdinal();
37 }
38
39 return page_ordinal;
40 }
41
42 // Gets page index from page ordinal.
43 int GetExtensionPageIndex(Profile* profile,
44 const Extension* extension) {
45 return std::max(0, GetExtensionPrefs(profile)->PageStringOrdinalAsInteger(
46 GetExtensionPageOrdinal(profile, extension)));
47 }
48
49 // Gets app launch ordinal of given extension.
50 StringOrdinal GetExtensionLaunchOrdinal(Profile* profile,
51 const Extension* extension) {
52 const ExtensionPrefs* prefs = GetExtensionPrefs(profile);
53 StringOrdinal app_launch_ordinal =
54 prefs->GetAppLaunchOrdinal(extension->id());
55 if (!app_launch_ordinal.IsValid()) {
56 StringOrdinal page_ordinal = GetExtensionPageOrdinal(profile, extension);
57 app_launch_ordinal = extension->id() == extension_misc::kWebStoreAppId ?
58 prefs->CreateFirstAppLaunchOrdinal(page_ordinal) :
59 prefs->CreateNextAppLaunchOrdinal(page_ordinal);
60 }
61 return app_launch_ordinal;
62 }
63
64 }
65
66 ExtensionItem::ExtensionItem(Profile* profile,
67 const Extension* extension)
68 : profile_(profile),
69 extension_id_(extension->id()),
70 page_index_(GetExtensionPageIndex(profile, extension)),
71 launch_ordinal_(GetExtensionLaunchOrdinal(profile, extension)) {
72 SetTitle(extension->name());
73 LoadImage(extension);
74 }
75
76 ExtensionItem::~ExtensionItem() {
77 }
78
79 const Extension* ExtensionItem::GetExtension() const {
80 const Extension* extension =
81 profile_->GetExtensionService()->GetInstalledExtension(extension_id_);
82 return extension;
83 }
84
85 void ExtensionItem::LoadImage(const Extension* extension) {
86 ExtensionResource icon = extension->GetIconResource(
87 aura_shell::AppListItemView::kIconSize,
88 ExtensionIconSet::MATCH_BIGGER);
89 if (icon.relative_path().empty()) {
90 LoadDefaultImage();
91 return;
92 }
93
94 if (extension->location() == Extension::COMPONENT) {
95 FilePath directory_path = extension->path();
96 FilePath relative_path = directory_path.BaseName().Append(
97 icon.relative_path());
98 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) {
99 FilePath bm_resource_path =
100 FilePath().AppendASCII(kComponentExtensionResources[i].name);
101 #if defined(OS_WIN)
102 bm_resource_path = bm_resource_path.NormalizeWindowsPathSeparators();
103 #endif
104 if (relative_path == bm_resource_path) {
105 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
106 int resource = kComponentExtensionResources[i].value;
107
108 base::StringPiece contents = rb.GetRawDataResource(resource);
109 SkBitmap icon;
110 if (gfx::PNGCodec::Decode(
111 reinterpret_cast<const unsigned char*>(contents.data()),
112 contents.size(), &icon)) {
113 SetIcon(icon);
114 return;
115 } else {
116 NOTREACHED() << "Unable to decode image resource " << resource;
117 }
118 }
119 }
120 }
121
122 tracker_.reset(new ImageLoadingTracker(this));
123 tracker_->LoadImage(extension,
124 icon,
125 gfx::Size(aura_shell::AppListItemView::kIconSize,
126 aura_shell::AppListItemView::kIconSize),
127 ImageLoadingTracker::DONT_CACHE);
128 }
129
130 void ExtensionItem::LoadDefaultImage() {
131 const Extension* extension = GetExtension();
132 int resource = IDR_APP_DEFAULT_ICON;
133 if (extension && extension->id() == extension_misc::kWebStoreAppId)
134 resource = IDR_WEBSTORE_ICON;
135
136 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
137 SetIcon(*rb.GetImageNamed(resource).ToSkBitmap());
138 }
139
140 void ExtensionItem::OnImageLoaded(SkBitmap* image,
141 const ExtensionResource& resource,
142 int tracker_index) {
143 if (image && !image->empty())
144 SetIcon(*image);
145 else
146 LoadDefaultImage();
147 }
148
149 void ExtensionItem::Activate(int event_flags) {
150 const Extension* extension = GetExtension();
151 if (!extension)
152 return;
153
154 WindowOpenDisposition disposition =
155 browser::DispositionFromEventFlags(event_flags);
156
157 GURL url;
158 if (extension_id_ == extension_misc::kWebStoreAppId)
159 url = extension->GetFullLaunchURL();
160
161 if (disposition == NEW_FOREGROUND_TAB || disposition == NEW_BACKGROUND_TAB) {
162 // Opens in a tab.
163 Browser::OpenApplication(
164 profile_, extension, extension_misc::LAUNCH_TAB, url, disposition);
165 } else if (disposition == NEW_WINDOW) {
166 // Force a new window open.
167 Browser::OpenApplication(
168 profile_, extension, extension_misc::LAUNCH_WINDOW, url,
169 disposition);
170 } else {
171 // Look at preference to find the right launch container. If no preference
172 // is set, launch as a regular tab.
173 extension_misc::LaunchContainer launch_container =
174 profile_->GetExtensionService()->extension_prefs()->GetLaunchContainer(
175 extension, ExtensionPrefs::LAUNCH_REGULAR);
176
177 Browser::OpenApplication(
178 profile_, extension, launch_container, GURL(url),
179 NEW_FOREGROUND_TAB);
180 }
181 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698