1 // Copyright 2013 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/search/webstore/webstore_result.h"
10 #include "base/command_line.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/apps/ephemeral_app_launcher.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_util.h"
17 #include "chrome/browser/extensions/install_tracker.h"
18 #include "chrome/browser/extensions/install_tracker_factory.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
21 #include "chrome/browser/ui/app_list/search/common/url_icon_source.h"
22 #include "chrome/browser/ui/app_list/search/webstore/webstore_installer.h"
23 #include "chrome/browser/ui/browser_navigator.h"
24 #include "chrome/browser/ui/extensions/application_launch.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "extensions/common/extension.h"
27 #include "grit/chromium_strings.h"
28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "net/base/url_util.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/base/resource/resource_bundle.h"
33 #include "ui/gfx/canvas.h"
34 #include "ui/gfx/image/canvas_image_source.h"
38 const int kIconSize = 32;
39 const int kLaunchEphemeralAppAction = 1;
41 // BadgedImageSource adds a webstore badge to a webstore app icon.
42 class BadgedIconSource : public gfx::CanvasImageSource {
44 explicit BadgedIconSource(const gfx::ImageSkia& icon)
45 : CanvasImageSource(gfx::Size(kIconSize, kIconSize), false),
49 virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
50 canvas->DrawImageInt(icon_, 0, 0);
51 const gfx::ImageSkia& badge = *ui::ResourceBundle::GetSharedInstance().
52 GetImageSkiaNamed(IDR_WEBSTORE_ICON_16);
54 badge, icon_.width() - badge.width(), icon_.height() - badge.height());
60 DISALLOW_COPY_AND_ASSIGN(BadgedIconSource);
67 WebstoreResult::WebstoreResult(Profile* profile,
68 const std::string& app_id,
69 const std::string& localized_name,
71 AppListControllerDelegate* controller)
74 localized_name_(localized_name),
77 controller_(controller),
78 install_tracker_(NULL) {
79 set_id(extensions::Extension::GetBaseURLFromExtensionId(app_id_).spec());
80 set_relevance(0.0); // What is the right value to use?
82 set_title(base::UTF8ToUTF16(localized_name_));
87 icon_ = gfx::ImageSkia(
88 new UrlIconSource(base::Bind(&WebstoreResult::OnIconLoaded,
89 weak_factory_.GetWeakPtr()),
90 profile_->GetRequestContext(),
93 IDR_WEBSTORE_ICON_32),
94 gfx::Size(kIconSize, kIconSize));
97 StartObservingInstall();
100 WebstoreResult::~WebstoreResult() {
101 StopObservingInstall();
104 void WebstoreResult::Open(int event_flags) {
105 const GURL store_url = net::AppendQueryParameter(
106 GURL(extension_urls::GetWebstoreItemDetailURLPrefix() + app_id_),
107 extension_urls::kWebstoreSourceField,
108 extension_urls::kLaunchSourceAppListSearch);
110 chrome::NavigateParams params(profile_,
112 content::PAGE_TRANSITION_LINK);
113 params.disposition = ui::DispositionFromEventFlags(event_flags);
114 chrome::Navigate(¶ms);
117 void WebstoreResult::InvokeAction(int action_index, int event_flags) {
118 StartInstall(action_index == kLaunchEphemeralAppAction);
121 scoped_ptr<ChromeSearchResult> WebstoreResult::Duplicate() {
122 return scoped_ptr<ChromeSearchResult>(new WebstoreResult(
123 profile_, app_id_, localized_name_, icon_url_, controller_)).Pass();
126 void WebstoreResult::UpdateActions() {
129 const bool is_otr = profile_->IsOffTheRecord();
130 const bool is_installed =
131 extensions::util::IsExtensionInstalledPermanently(app_id_, profile_);
133 if (!is_otr && !is_installed && !is_installing()) {
134 if (CommandLine::ForCurrentProcess()->HasSwitch(
135 switches::kEnableEphemeralApps)) {
136 actions.push_back(Action(
137 l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_INSTALL),
138 l10n_util::GetStringUTF16(
139 IDS_EXTENSION_INLINE_INSTALL_PROMPT_TITLE)));
140 actions.push_back(Action(
141 l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_LAUNCH),
142 l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_LAUNCH_APP_TOOLTIP)));
144 actions.push_back(Action(
145 l10n_util::GetStringUTF16(IDS_EXTENSION_INLINE_INSTALL_PROMPT_TITLE),
153 void WebstoreResult::SetDefaultDetails() {
154 const base::string16 details =
155 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE);
157 details_tags.push_back(Tag(SearchResult::Tag::DIM, 0, details.length()));
159 set_details(details);
160 set_details_tags(details_tags);
163 void WebstoreResult::OnIconLoaded() {
164 // Remove the existing image reps since the icon data is loaded and they
165 // need to be re-created.
166 const std::vector<gfx::ImageSkiaRep>& image_reps = icon_.image_reps();
167 for (size_t i = 0; i < image_reps.size(); ++i)
168 icon_.RemoveRepresentation(image_reps[i].scale());
170 icon_ = gfx::ImageSkia(new BadgedIconSource(icon_),
171 gfx::Size(kIconSize, kIconSize));
176 void WebstoreResult::StartInstall(bool launch_ephemeral_app) {
177 SetPercentDownloaded(0);
178 SetIsInstalling(true);
180 if (launch_ephemeral_app) {
181 scoped_refptr<EphemeralAppLauncher> installer =
182 EphemeralAppLauncher::CreateForLauncher(
185 controller_->GetAppListWindow(),
186 base::Bind(&WebstoreResult::InstallCallback,
187 weak_factory_.GetWeakPtr()));
192 scoped_refptr<WebstoreInstaller> installer =
193 new WebstoreInstaller(
196 controller_->GetAppListWindow(),
197 base::Bind(&WebstoreResult::InstallCallback,
198 weak_factory_.GetWeakPtr()));
199 installer->BeginInstall();
202 void WebstoreResult::InstallCallback(bool success, const std::string& error) {
204 LOG(ERROR) << "Failed to install app, error=" << error;
205 SetIsInstalling(false);
209 // Success handling is continued in OnExtensionInstalled.
210 SetPercentDownloaded(100);
213 void WebstoreResult::StartObservingInstall() {
214 DCHECK(!install_tracker_);
216 install_tracker_ = extensions::InstallTrackerFactory::GetForProfile(profile_);
217 install_tracker_->AddObserver(this);
220 void WebstoreResult::StopObservingInstall() {
221 if (install_tracker_)
222 install_tracker_->RemoveObserver(this);
224 install_tracker_ = NULL;
227 void WebstoreResult::OnBeginExtensionInstall(
228 const ExtensionInstallParams& params) {}
230 void WebstoreResult::OnDownloadProgress(const std::string& extension_id,
231 int percent_downloaded) {
232 if (extension_id != app_id_ || percent_downloaded < 0)
235 SetPercentDownloaded(percent_downloaded);
238 void WebstoreResult::OnInstallFailure(const std::string& extension_id) {}
240 void WebstoreResult::OnExtensionInstalled(
241 const extensions::Extension* extension) {
242 if (extension->id() != app_id_)
245 SetIsInstalling(false);
247 NotifyItemInstalled();
250 void WebstoreResult::OnExtensionLoaded(
251 const extensions::Extension* extension) {}
253 void WebstoreResult::OnExtensionUnloaded(
254 const extensions::Extension* extension) {}
256 void WebstoreResult::OnExtensionUninstalled(
257 const extensions::Extension* extension) {}
259 void WebstoreResult::OnAppsReordered() {}
261 void WebstoreResult::OnAppInstalledToAppList(const std::string& extension_id) {}
263 void WebstoreResult::OnShutdown() {
264 StopObservingInstall();
267 ChromeSearchResultType WebstoreResult::GetType() {
268 return SEARCH_WEBSTORE_SEARCH_RESULT;
271 } // namespace app_list