- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / search / webstore / webstore_result.cc
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.
4
5 #include "chrome/browser/ui/app_list/search/webstore/webstore_result.h"
6
7 #include <vector>
8
9 #include "base/bind.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"
34
35 using extensions::WebstoreEphemeralInstaller;
36
37 namespace {
38
39 const int kIconSize = 32;
40 const int kLaunchEphemeralAppAction = 1;
41
42 // BadgedImageSource adds a webstore badge to a webstore app icon.
43 class BadgedIconSource : public gfx::CanvasImageSource {
44  public:
45   explicit BadgedIconSource(const gfx::ImageSkia& icon)
46       : CanvasImageSource(gfx::Size(kIconSize, kIconSize), false),
47         icon_(icon) {
48   }
49
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);
54     canvas->DrawImageInt(
55         badge, icon_.width() - badge.width(), icon_.height() - badge.height());
56   }
57
58  private:
59   gfx::ImageSkia icon_;
60
61   DISALLOW_COPY_AND_ASSIGN(BadgedIconSource);
62 };
63
64 }  // namespace
65
66 namespace app_list {
67
68 WebstoreResult::WebstoreResult(Profile* profile,
69                                const std::string& app_id,
70                                const std::string& localized_name,
71                                const GURL& icon_url,
72                                AppListControllerDelegate* controller)
73     : profile_(profile),
74       app_id_(app_id),
75       localized_name_(localized_name),
76       icon_url_(icon_url),
77       weak_factory_(this),
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?
82
83   set_title(UTF8ToUTF16(localized_name_));
84   SetDefaultDetails();
85
86   UpdateActions();
87
88   icon_ = gfx::ImageSkia(
89       new UrlIconSource(base::Bind(&WebstoreResult::OnIconLoaded,
90                                    weak_factory_.GetWeakPtr()),
91                         profile_->GetRequestContext(),
92                         icon_url_,
93                         kIconSize,
94                         IDR_WEBSTORE_ICON_32),
95       gfx::Size(kIconSize, kIconSize));
96   SetIcon(icon_);
97
98   StartObservingInstall();
99 }
100
101 WebstoreResult::~WebstoreResult() {
102   StopObservingInstall();
103 }
104
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);
110
111   chrome::NavigateParams params(profile_,
112                                 store_url,
113                                 content::PAGE_TRANSITION_LINK);
114   params.disposition = ui::DispositionFromEventFlags(event_flags);
115   chrome::Navigate(&params);
116 }
117
118 void WebstoreResult::InvokeAction(int action_index, int event_flags) {
119   StartInstall(action_index == kLaunchEphemeralAppAction);
120 }
121
122 scoped_ptr<ChromeSearchResult> WebstoreResult::Duplicate() {
123   return scoped_ptr<ChromeSearchResult>(new WebstoreResult(
124       profile_, app_id_, localized_name_, icon_url_, controller_)).Pass();
125 }
126
127 void WebstoreResult::UpdateActions() {
128   Actions actions;
129
130   const bool is_otr = profile_->IsOffTheRecord();
131   const bool is_installed = !!extensions::ExtensionSystem::Get(profile_)->
132       extension_service()->GetInstalledExtension(app_id_);
133
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)));
144     } else {
145       actions.push_back(Action(
146           l10n_util::GetStringUTF16(IDS_EXTENSION_INLINE_INSTALL_PROMPT_TITLE),
147           base::string16()));
148     }
149   }
150
151   SetActions(actions);
152 }
153
154 void WebstoreResult::SetDefaultDetails() {
155   const base::string16 details =
156       l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE);
157   Tags details_tags;
158   details_tags.push_back(Tag(SearchResult::Tag::DIM, 0, details.length()));
159
160   set_details(details);
161   set_details_tags(details_tags);
162 }
163
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());
170
171   icon_ = gfx::ImageSkia(new BadgedIconSource(icon_),
172                          gfx::Size(kIconSize, kIconSize));
173
174   SetIcon(icon_);
175 }
176
177 void WebstoreResult::StartInstall(bool launch_ephemeral_app) {
178   SetPercentDownloaded(0);
179   SetIsInstalling(true);
180
181   if (launch_ephemeral_app) {
182     scoped_refptr<WebstoreEphemeralInstaller> installer =
183         WebstoreEphemeralInstaller::CreateForLauncher(
184             app_id_,
185             profile_,
186             controller_->GetAppListWindow(),
187             base::Bind(&WebstoreResult::InstallCallback,
188                        weak_factory_.GetWeakPtr()));
189     installer->BeginInstall();
190     return;
191   }
192
193   scoped_refptr<WebstoreInstaller> installer =
194       new WebstoreInstaller(
195           app_id_,
196           profile_,
197           controller_->GetAppListWindow(),
198           base::Bind(&WebstoreResult::InstallCallback,
199                      weak_factory_.GetWeakPtr()));
200   installer->BeginInstall();
201 }
202
203 void WebstoreResult::InstallCallback(bool success, const std::string& error) {
204   if (!success) {
205     LOG(ERROR) << "Failed to install app, error=" << error;
206     SetIsInstalling(false);
207     return;
208   }
209
210   // Success handling is continued in OnExtensionInstalled.
211   SetPercentDownloaded(100);
212 }
213
214 void WebstoreResult::StartObservingInstall() {
215   DCHECK(!install_tracker_);
216
217   install_tracker_ = extensions::InstallTrackerFactory::GetForProfile(profile_);
218   install_tracker_->AddObserver(this);
219 }
220
221 void WebstoreResult::StopObservingInstall() {
222   if (install_tracker_)
223     install_tracker_->RemoveObserver(this);
224
225   install_tracker_ = NULL;
226 }
227
228 void WebstoreResult::OnBeginExtensionInstall(
229     const std::string& extension_id,
230     const std::string& extension_name,
231     const gfx::ImageSkia& installing_icon,
232     bool is_app,
233     bool is_platform_app) {}
234
235 void WebstoreResult::OnDownloadProgress(const std::string& extension_id,
236                                         int percent_downloaded) {
237   if (extension_id != app_id_ || percent_downloaded < 0)
238     return;
239
240   SetPercentDownloaded(percent_downloaded);
241 }
242
243 void WebstoreResult::OnInstallFailure(const std::string& extension_id) {}
244
245 void WebstoreResult::OnExtensionInstalled(
246     const extensions::Extension* extension) {
247   if (extension->id() != app_id_)
248     return;
249
250   SetIsInstalling(false);
251   UpdateActions();
252   NotifyItemInstalled();
253 }
254
255 void WebstoreResult::OnExtensionLoaded(
256     const extensions::Extension* extension) {}
257
258 void WebstoreResult::OnExtensionUnloaded(
259     const extensions::Extension* extension) {}
260
261 void WebstoreResult::OnExtensionUninstalled(
262     const extensions::Extension* extension) {}
263
264 void WebstoreResult::OnAppsReordered() {}
265
266 void WebstoreResult::OnAppInstalledToAppList(const std::string& extension_id) {}
267
268 void WebstoreResult::OnShutdown() {
269   StopObservingInstall();
270 }
271
272 ChromeSearchResultType WebstoreResult::GetType() {
273   return SEARCH_WEBSTORE_SEARCH_RESULT;
274 }
275
276 }  // namespace app_list