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/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_system.h"
15 #include "chrome/browser/extensions/install_tracker.h"
16 #include "chrome/browser/extensions/install_tracker_factory.h"
17 #include "chrome/browser/extensions/webstore_ephemeral_installer.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
20 #include "chrome/browser/ui/app_list/search/common/url_icon_source.h"
21 #include "chrome/browser/ui/app_list/search/webstore/webstore_installer.h"
22 #include "chrome/browser/ui/browser_navigator.h"
23 #include "chrome/browser/ui/extensions/application_launch.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/extensions/extension.h"
26 #include "grit/chromium_strings.h"
27 #include "grit/generated_resources.h"
28 #include "grit/theme_resources.h"
29 #include "net/base/url_util.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/canvas.h"
33 #include "ui/gfx/image/canvas_image_source.h"
35 using extensions::WebstoreEphemeralInstaller;
39 const int kIconSize = 32;
40 const int kLaunchEphemeralAppAction = 1;
42 // BadgedImageSource adds a webstore badge to a webstore app icon.
43 class BadgedIconSource : public gfx::CanvasImageSource {
45 explicit BadgedIconSource(const gfx::ImageSkia& icon)
46 : CanvasImageSource(gfx::Size(kIconSize, kIconSize), false),
50 virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
51 canvas->DrawImageInt(icon_, 0, 0);
52 const gfx::ImageSkia& badge = *ui::ResourceBundle::GetSharedInstance().
53 GetImageSkiaNamed(IDR_WEBSTORE_ICON_16);
55 badge, icon_.width() - badge.width(), icon_.height() - badge.height());
61 DISALLOW_COPY_AND_ASSIGN(BadgedIconSource);
68 WebstoreResult::WebstoreResult(Profile* profile,
69 const std::string& app_id,
70 const std::string& localized_name,
72 AppListControllerDelegate* controller)
75 localized_name_(localized_name),
78 controller_(controller),
79 install_tracker_(NULL) {
80 set_id(extensions::Extension::GetBaseURLFromExtensionId(app_id_).spec());
81 set_relevance(0.0); // What is the right value to use?
83 set_title(UTF8ToUTF16(localized_name_));
88 icon_ = gfx::ImageSkia(
89 new UrlIconSource(base::Bind(&WebstoreResult::OnIconLoaded,
90 weak_factory_.GetWeakPtr()),
91 profile_->GetRequestContext(),
94 IDR_WEBSTORE_ICON_32),
95 gfx::Size(kIconSize, kIconSize));
98 StartObservingInstall();
101 WebstoreResult::~WebstoreResult() {
102 StopObservingInstall();
105 void WebstoreResult::Open(int event_flags) {
106 const GURL store_url = net::AppendQueryParameter(
107 GURL(extension_urls::GetWebstoreItemDetailURLPrefix() + app_id_),
108 extension_urls::kWebstoreSourceField,
109 extension_urls::kLaunchSourceAppListSearch);
111 chrome::NavigateParams params(profile_,
113 content::PAGE_TRANSITION_LINK);
114 params.disposition = ui::DispositionFromEventFlags(event_flags);
115 chrome::Navigate(¶ms);
118 void WebstoreResult::InvokeAction(int action_index, int event_flags) {
119 StartInstall(action_index == kLaunchEphemeralAppAction);
122 scoped_ptr<ChromeSearchResult> WebstoreResult::Duplicate() {
123 return scoped_ptr<ChromeSearchResult>(new WebstoreResult(
124 profile_, app_id_, localized_name_, icon_url_, controller_)).Pass();
127 void WebstoreResult::UpdateActions() {
130 const bool is_otr = profile_->IsOffTheRecord();
131 const bool is_installed = !!extensions::ExtensionSystem::Get(profile_)->
132 extension_service()->GetInstalledExtension(app_id_);
134 if (!is_otr && !is_installed && !is_installing()) {
135 if (CommandLine::ForCurrentProcess()->HasSwitch(
136 switches::kEnableEphemeralApps)) {
137 actions.push_back(Action(
138 l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_INSTALL),
139 l10n_util::GetStringUTF16(
140 IDS_EXTENSION_INLINE_INSTALL_PROMPT_TITLE)));
141 actions.push_back(Action(
142 l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_LAUNCH),
143 l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_LAUNCH_APP_TOOLTIP)));
145 actions.push_back(Action(
146 l10n_util::GetStringUTF16(IDS_EXTENSION_INLINE_INSTALL_PROMPT_TITLE),
154 void WebstoreResult::SetDefaultDetails() {
155 const base::string16 details =
156 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE);
158 details_tags.push_back(Tag(SearchResult::Tag::DIM, 0, details.length()));
160 set_details(details);
161 set_details_tags(details_tags);
164 void WebstoreResult::OnIconLoaded() {
165 // Remove the existing image reps since the icon data is loaded and they
166 // need to be re-created.
167 const std::vector<gfx::ImageSkiaRep>& image_reps = icon_.image_reps();
168 for (size_t i = 0; i < image_reps.size(); ++i)
169 icon_.RemoveRepresentation(image_reps[i].scale());
171 icon_ = gfx::ImageSkia(new BadgedIconSource(icon_),
172 gfx::Size(kIconSize, kIconSize));
177 void WebstoreResult::StartInstall(bool launch_ephemeral_app) {
178 SetPercentDownloaded(0);
179 SetIsInstalling(true);
181 if (launch_ephemeral_app) {
182 scoped_refptr<WebstoreEphemeralInstaller> installer =
183 WebstoreEphemeralInstaller::CreateForLauncher(
186 controller_->GetAppListWindow(),
187 base::Bind(&WebstoreResult::InstallCallback,
188 weak_factory_.GetWeakPtr()));
189 installer->BeginInstall();
193 scoped_refptr<WebstoreInstaller> installer =
194 new WebstoreInstaller(
197 controller_->GetAppListWindow(),
198 base::Bind(&WebstoreResult::InstallCallback,
199 weak_factory_.GetWeakPtr()));
200 installer->BeginInstall();
203 void WebstoreResult::InstallCallback(bool success, const std::string& error) {
205 LOG(ERROR) << "Failed to install app, error=" << error;
206 SetIsInstalling(false);
210 // Success handling is continued in OnExtensionInstalled.
211 SetPercentDownloaded(100);
214 void WebstoreResult::StartObservingInstall() {
215 DCHECK(!install_tracker_);
217 install_tracker_ = extensions::InstallTrackerFactory::GetForProfile(profile_);
218 install_tracker_->AddObserver(this);
221 void WebstoreResult::StopObservingInstall() {
222 if (install_tracker_)
223 install_tracker_->RemoveObserver(this);
225 install_tracker_ = NULL;
228 void WebstoreResult::OnBeginExtensionInstall(
229 const std::string& extension_id,
230 const std::string& extension_name,
231 const gfx::ImageSkia& installing_icon,
233 bool is_platform_app) {}
235 void WebstoreResult::OnDownloadProgress(const std::string& extension_id,
236 int percent_downloaded) {
237 if (extension_id != app_id_ || percent_downloaded < 0)
240 SetPercentDownloaded(percent_downloaded);
243 void WebstoreResult::OnInstallFailure(const std::string& extension_id) {}
245 void WebstoreResult::OnExtensionInstalled(
246 const extensions::Extension* extension) {
247 if (extension->id() != app_id_)
250 SetIsInstalling(false);
252 NotifyItemInstalled();
255 void WebstoreResult::OnExtensionLoaded(
256 const extensions::Extension* extension) {}
258 void WebstoreResult::OnExtensionUnloaded(
259 const extensions::Extension* extension) {}
261 void WebstoreResult::OnExtensionUninstalled(
262 const extensions::Extension* extension) {}
264 void WebstoreResult::OnAppsReordered() {}
266 void WebstoreResult::OnAppInstalledToAppList(const std::string& extension_id) {}
268 void WebstoreResult::OnShutdown() {
269 StopObservingInstall();
272 ChromeSearchResultType WebstoreResult::GetType() {
273 return SEARCH_WEBSTORE_SEARCH_RESULT;
276 } // namespace app_list