Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / extensions / bookmark_app_helper.cc
blob57ec926a46cde2975a8bcc5268f6706803697109
1 // Copyright 2014 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/extensions/bookmark_app_helper.h"
7 #include <cctype>
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
12 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_delegate.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/extensions/crx_installer.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/favicon_downloader.h"
17 #include "chrome/browser/extensions/launch_util.h"
18 #include "chrome/browser/extensions/tab_helper.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/app_list/app_list_service.h"
21 #include "chrome/browser/ui/app_list/app_list_util.h"
22 #include "chrome/browser/ui/browser_finder.h"
23 #include "chrome/browser/ui/browser_window.h"
24 #include "chrome/browser/ui/host_desktop.h"
25 #include "chrome/browser/web_applications/web_app.h"
26 #include "chrome/common/extensions/extension_constants.h"
27 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
28 #include "chrome/common/url_constants.h"
29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/notification_source.h"
31 #include "content/public/browser/web_contents.h"
32 #include "extensions/browser/extension_system.h"
33 #include "extensions/browser/image_loader.h"
34 #include "extensions/browser/notification_types.h"
35 #include "extensions/browser/pref_names.h"
36 #include "extensions/common/constants.h"
37 #include "extensions/common/extension.h"
38 #include "extensions/common/manifest_handlers/icons_handler.h"
39 #include "extensions/common/url_pattern.h"
40 #include "grit/platform_locale_settings.h"
41 #include "net/base/load_flags.h"
42 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
43 #include "net/url_request/url_request.h"
44 #include "skia/ext/image_operations.h"
45 #include "skia/ext/platform_canvas.h"
46 #include "third_party/skia/include/core/SkBitmap.h"
47 #include "ui/base/l10n/l10n_util.h"
48 #include "ui/gfx/canvas.h"
49 #include "ui/gfx/color_analysis.h"
50 #include "ui/gfx/color_utils.h"
51 #include "ui/gfx/font.h"
52 #include "ui/gfx/font_list.h"
53 #include "ui/gfx/geometry/rect.h"
54 #include "ui/gfx/image/canvas_image_source.h"
55 #include "ui/gfx/image/image.h"
56 #include "ui/gfx/image/image_family.h"
58 #if defined(OS_MACOSX)
59 #include "base/command_line.h"
60 #include "chrome/browser/web_applications/web_app_mac.h"
61 #include "chrome/common/chrome_switches.h"
62 #endif
64 #if defined(USE_ASH)
65 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
66 #endif
68 namespace {
70 using extensions::BookmarkAppHelper;
72 // Overlays a shortcut icon over the bottom left corner of a given image.
73 class GeneratedIconImageSource : public gfx::CanvasImageSource {
74 public:
75 explicit GeneratedIconImageSource(char letter, SkColor color, int output_size)
76 : gfx::CanvasImageSource(gfx::Size(output_size, output_size), false),
77 letter_(letter),
78 color_(color),
79 output_size_(output_size) {}
80 ~GeneratedIconImageSource() override {}
82 private:
83 // gfx::CanvasImageSource overrides:
84 void Draw(gfx::Canvas* canvas) override {
85 const unsigned char kLuminanceThreshold = 190;
86 const int icon_size = output_size_ * 3 / 4;
87 const int icon_inset = output_size_ / 8;
88 const size_t border_radius = output_size_ / 16;
89 const size_t font_size = output_size_ * 7 / 16;
91 std::string font_name =
92 l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY);
93 #if defined(OS_CHROMEOS)
94 const std::string kChromeOSFontFamily = "Noto Sans";
95 font_name = kChromeOSFontFamily;
96 #endif
98 // Draw a rounded rect of the given |color|.
99 SkPaint background_paint;
100 background_paint.setFlags(SkPaint::kAntiAlias_Flag);
101 background_paint.setColor(color_);
103 gfx::Rect icon_rect(icon_inset, icon_inset, icon_size, icon_size);
104 canvas->DrawRoundRect(icon_rect, border_radius, background_paint);
106 // The text rect's size needs to be odd to center the text correctly.
107 gfx::Rect text_rect(icon_inset, icon_inset, icon_size + 1, icon_size + 1);
108 // Draw the letter onto the rounded rect. The letter's color depends on the
109 // luminance of |color|.
110 unsigned char luminance = color_utils::GetLuminanceForColor(color_);
111 canvas->DrawStringRectWithFlags(
112 base::string16(1, std::toupper(letter_)),
113 gfx::FontList(gfx::Font(font_name, font_size)),
114 luminance > kLuminanceThreshold ? SK_ColorBLACK : SK_ColorWHITE,
115 text_rect,
116 gfx::Canvas::TEXT_ALIGN_CENTER);
119 char letter_;
121 SkColor color_;
123 int output_size_;
125 DISALLOW_COPY_AND_ASSIGN(GeneratedIconImageSource);
128 void OnIconsLoaded(
129 WebApplicationInfo web_app_info,
130 const base::Callback<void(const WebApplicationInfo&)> callback,
131 const gfx::ImageFamily& image_family) {
132 for (gfx::ImageFamily::const_iterator it = image_family.begin();
133 it != image_family.end();
134 ++it) {
135 WebApplicationInfo::IconInfo icon_info;
136 icon_info.data = *it->ToSkBitmap();
137 icon_info.width = icon_info.data.width();
138 icon_info.height = icon_info.data.height();
139 web_app_info.icons.push_back(icon_info);
141 callback.Run(web_app_info);
144 std::set<int> SizesToGenerate() {
145 // Generate container icons from smaller icons.
146 const int kIconSizesToGenerate[] = {
147 extension_misc::EXTENSION_ICON_SMALL,
148 extension_misc::EXTENSION_ICON_MEDIUM,
149 extension_misc::EXTENSION_ICON_LARGE,
151 return std::set<int>(kIconSizesToGenerate,
152 kIconSizesToGenerate + arraysize(kIconSizesToGenerate));
155 void GenerateIcons(
156 std::set<int> generate_sizes,
157 const GURL& app_url,
158 SkColor generated_icon_color,
159 std::map<int, BookmarkAppHelper::BitmapAndSource>* bitmap_map) {
160 // The letter that will be painted on the generated icon.
161 char icon_letter = ' ';
162 std::string domain_and_registry(
163 net::registry_controlled_domains::GetDomainAndRegistry(
164 app_url,
165 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
166 if (!domain_and_registry.empty()) {
167 icon_letter = domain_and_registry[0];
168 } else if (!app_url.host().empty()) {
169 icon_letter = app_url.host()[0];
172 // If no color has been specified, use a dark gray so it will stand out on the
173 // black shelf.
174 if (generated_icon_color == SK_ColorTRANSPARENT)
175 generated_icon_color = SK_ColorDKGRAY;
177 for (std::set<int>::const_iterator it = generate_sizes.begin();
178 it != generate_sizes.end(); ++it) {
179 extensions::BookmarkAppHelper::GenerateIcon(
180 bitmap_map, *it, generated_icon_color, icon_letter);
181 // Also generate the 2x resource for this size.
182 extensions::BookmarkAppHelper::GenerateIcon(
183 bitmap_map, *it * 2, generated_icon_color, icon_letter);
187 void ReplaceWebAppIcons(
188 std::map<int, BookmarkAppHelper::BitmapAndSource> bitmap_map,
189 WebApplicationInfo* web_app_info) {
190 web_app_info->icons.clear();
192 // Populate the icon data into the WebApplicationInfo we are using to
193 // install the bookmark app.
194 for (const auto& pair : bitmap_map) {
195 WebApplicationInfo::IconInfo icon_info;
196 icon_info.data = pair.second.bitmap;
197 icon_info.url = pair.second.source_url;
198 icon_info.width = icon_info.data.width();
199 icon_info.height = icon_info.data.height();
200 web_app_info->icons.push_back(icon_info);
204 // Class to handle installing a bookmark app. Handles downloading and decoding
205 // the icons.
206 class BookmarkAppInstaller : public base::RefCounted<BookmarkAppInstaller>,
207 public chrome::BitmapFetcherDelegate {
208 public:
209 BookmarkAppInstaller(ExtensionService* service,
210 const WebApplicationInfo& web_app_info)
211 : service_(service), web_app_info_(web_app_info) {}
213 void Run() {
214 for (const auto& icon : web_app_info_.icons) {
215 if (icon.url.is_valid())
216 urls_to_download_.push_back(icon.url);
219 if (urls_to_download_.size()) {
220 DownloadNextImage();
222 // Matched in OnFetchComplete.
223 AddRef();
224 return;
227 FinishInstallation();
230 private:
231 friend class base::RefCounted<BookmarkAppInstaller>;
232 ~BookmarkAppInstaller() override {}
234 // BitmapFetcherDelegate:
235 void OnFetchComplete(const GURL& url, const SkBitmap* bitmap) override {
236 if (bitmap && !bitmap->empty() && bitmap->width() == bitmap->height()) {
237 downloaded_bitmaps_.push_back(
238 BookmarkAppHelper::BitmapAndSource(url, *bitmap));
241 if (urls_to_download_.size()) {
242 DownloadNextImage();
243 return;
246 FinishInstallation();
247 Release();
250 void DownloadNextImage() {
251 DCHECK(urls_to_download_.size());
253 bitmap_fetcher_.reset(
254 new chrome::BitmapFetcher(urls_to_download_.back(), this));
255 urls_to_download_.pop_back();
256 bitmap_fetcher_->Init(
257 service_->profile()->GetRequestContext(), std::string(),
258 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
259 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES);
260 bitmap_fetcher_->Start();
263 void FinishInstallation() {
264 std::map<int, BookmarkAppHelper::BitmapAndSource> size_map =
265 BookmarkAppHelper::ResizeIconsAndGenerateMissing(downloaded_bitmaps_,
266 SizesToGenerate(),
267 &web_app_info_);
268 BookmarkAppHelper::UpdateWebAppIconsWithoutChangingLinks(size_map,
269 &web_app_info_);
270 scoped_refptr<extensions::CrxInstaller> installer(
271 extensions::CrxInstaller::CreateSilent(service_));
272 installer->set_error_on_unsupported_requirements(true);
273 installer->InstallWebApp(web_app_info_);
276 ExtensionService* service_;
277 WebApplicationInfo web_app_info_;
279 scoped_ptr<chrome::BitmapFetcher> bitmap_fetcher_;
280 std::vector<GURL> urls_to_download_;
281 std::vector<BookmarkAppHelper::BitmapAndSource> downloaded_bitmaps_;
284 } // namespace
286 namespace extensions {
288 // static
289 void BookmarkAppHelper::UpdateWebAppInfoFromManifest(
290 const content::Manifest& manifest,
291 WebApplicationInfo* web_app_info) {
292 if (!manifest.short_name.is_null())
293 web_app_info->title = manifest.short_name.string();
295 // Give the full length name priority.
296 if (!manifest.name.is_null())
297 web_app_info->title = manifest.name.string();
299 // Set the url based on the manifest value, if any.
300 if (manifest.start_url.is_valid())
301 web_app_info->app_url = manifest.start_url;
303 // If any icons are specified in the manifest, they take precedence over any
304 // we picked up from the web_app stuff.
305 if (!manifest.icons.empty()) {
306 web_app_info->icons.clear();
307 for (const auto& icon : manifest.icons) {
308 // TODO(benwells): Take the declared icon density and sizes into account.
309 WebApplicationInfo::IconInfo info;
310 info.url = icon.src;
311 web_app_info->icons.push_back(info);
316 // static
317 std::map<int, BookmarkAppHelper::BitmapAndSource>
318 BookmarkAppHelper::ConstrainBitmapsToSizes(
319 const std::vector<BookmarkAppHelper::BitmapAndSource>& bitmaps,
320 const std::set<int>& sizes) {
321 std::map<int, BitmapAndSource> output_bitmaps;
322 std::map<int, BitmapAndSource> ordered_bitmaps;
323 for (std::vector<BitmapAndSource>::const_iterator it = bitmaps.begin();
324 it != bitmaps.end(); ++it) {
325 DCHECK(it->bitmap.width() == it->bitmap.height());
326 ordered_bitmaps[it->bitmap.width()] = *it;
329 std::set<int>::const_iterator sizes_it = sizes.begin();
330 std::map<int, BitmapAndSource>::const_iterator bitmaps_it =
331 ordered_bitmaps.begin();
332 while (sizes_it != sizes.end() && bitmaps_it != ordered_bitmaps.end()) {
333 int size = *sizes_it;
334 // Find the closest not-smaller bitmap.
335 bitmaps_it = ordered_bitmaps.lower_bound(size);
336 ++sizes_it;
337 // Ensure the bitmap is valid and smaller than the next allowed size.
338 if (bitmaps_it != ordered_bitmaps.end() &&
339 (sizes_it == sizes.end() ||
340 bitmaps_it->second.bitmap.width() < *sizes_it)) {
341 output_bitmaps[size] = bitmaps_it->second;
342 // Resize the bitmap if it does not exactly match the desired size.
343 if (output_bitmaps[size].bitmap.width() != size) {
344 output_bitmaps[size].bitmap = skia::ImageOperations::Resize(
345 output_bitmaps[size].bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
346 size, size);
350 return output_bitmaps;
353 // static
354 void BookmarkAppHelper::GenerateIcon(
355 std::map<int, BookmarkAppHelper::BitmapAndSource>* bitmaps,
356 int output_size,
357 SkColor color,
358 char letter) {
359 // Do nothing if there is already an icon of |output_size|.
360 if (bitmaps->count(output_size))
361 return;
363 gfx::ImageSkia icon_image(
364 new GeneratedIconImageSource(letter, color, output_size),
365 gfx::Size(output_size, output_size));
366 icon_image.bitmap()->deepCopyTo(&(*bitmaps)[output_size].bitmap);
369 // static
370 std::map<int, BookmarkAppHelper::BitmapAndSource>
371 BookmarkAppHelper::ResizeIconsAndGenerateMissing(
372 std::vector<BookmarkAppHelper::BitmapAndSource> icons,
373 std::set<int> sizes_to_generate,
374 WebApplicationInfo* web_app_info) {
375 // Add the downloaded icons. Extensions only allow certain icon sizes. First
376 // populate icons that match the allowed sizes exactly and then downscale
377 // remaining icons to the closest allowed size that doesn't yet have an icon.
378 std::set<int> allowed_sizes(extension_misc::kExtensionIconSizes,
379 extension_misc::kExtensionIconSizes +
380 extension_misc::kNumExtensionIconSizes);
382 // If there are icons that don't match the accepted icon sizes, find the
383 // closest bigger icon to the accepted sizes and resize the icon to it. An
384 // icon will be resized and used for at most one size.
385 std::map<int, BitmapAndSource> resized_bitmaps(
386 ConstrainBitmapsToSizes(icons, allowed_sizes));
388 // Determine the color that will be used for the icon's background. For this
389 // the dominant color of the first icon found is used.
390 if (resized_bitmaps.size()) {
391 color_utils::GridSampler sampler;
392 web_app_info->generated_icon_color =
393 color_utils::CalculateKMeanColorOfBitmap(
394 resized_bitmaps.begin()->second.bitmap);
397 // Work out what icons we need to generate here. Icons are only generated if:
398 // a. there is no icon in the required size, AND
399 // b. there is no icon LARGER than the required size.
400 // Larger icons will be scaled down and used at display time.
401 std::set<int> generate_sizes;
402 for (int size : sizes_to_generate) {
403 if (resized_bitmaps.lower_bound(size) == resized_bitmaps.end())
404 generate_sizes.insert(size);
406 GenerateIcons(generate_sizes, web_app_info->app_url,
407 web_app_info->generated_icon_color, &resized_bitmaps);
409 return resized_bitmaps;
412 // static
413 void BookmarkAppHelper::UpdateWebAppIconsWithoutChangingLinks(
414 std::map<int, BookmarkAppHelper::BitmapAndSource> bitmap_map,
415 WebApplicationInfo* web_app_info) {
416 // First add in the icon data that have urls with the url / size data from the
417 // original web app info, and the data from the new icons (if any).
418 for (auto& icon : web_app_info->icons) {
419 if (!icon.url.is_empty() && icon.data.empty()) {
420 const auto& it = bitmap_map.find(icon.width);
421 if (it != bitmap_map.end() && it->second.source_url == icon.url)
422 icon.data = it->second.bitmap;
426 // Now add in any icons from the updated list that don't have URLs.
427 for (const auto& pair : bitmap_map) {
428 if (pair.second.source_url.is_empty()) {
429 WebApplicationInfo::IconInfo icon_info;
430 icon_info.data = pair.second.bitmap;
431 icon_info.width = pair.first;
432 icon_info.height = pair.first;
433 web_app_info->icons.push_back(icon_info);
438 BookmarkAppHelper::BitmapAndSource::BitmapAndSource() {
441 BookmarkAppHelper::BitmapAndSource::BitmapAndSource(const GURL& source_url_p,
442 const SkBitmap& bitmap_p)
443 : source_url(source_url_p),
444 bitmap(bitmap_p) {
447 BookmarkAppHelper::BitmapAndSource::~BitmapAndSource() {
450 BookmarkAppHelper::BookmarkAppHelper(Profile* profile,
451 WebApplicationInfo web_app_info,
452 content::WebContents* contents)
453 : profile_(profile),
454 contents_(contents),
455 web_app_info_(web_app_info),
456 crx_installer_(extensions::CrxInstaller::CreateSilent(
457 ExtensionSystem::Get(profile)->extension_service())) {
458 web_app_info_.open_as_window =
459 profile_->GetPrefs()->GetInteger(
460 extensions::pref_names::kBookmarkAppCreationLaunchType) ==
461 extensions::LAUNCH_TYPE_WINDOW;
463 // The default app title is the page title, which can be quite long. Limit the
464 // default name used to something sensible.
465 const int kMaxDefaultTitle = 40;
466 if (web_app_info_.title.length() > kMaxDefaultTitle) {
467 web_app_info_.title = web_app_info_.title.substr(0, kMaxDefaultTitle - 3) +
468 base::UTF8ToUTF16("...");
471 registrar_.Add(this,
472 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
473 content::Source<CrxInstaller>(crx_installer_.get()));
475 registrar_.Add(this,
476 extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR,
477 content::Source<CrxInstaller>(crx_installer_.get()));
479 crx_installer_->set_error_on_unsupported_requirements(true);
482 BookmarkAppHelper::~BookmarkAppHelper() {}
484 void BookmarkAppHelper::Create(const CreateBookmarkAppCallback& callback) {
485 callback_ = callback;
487 if (contents_) {
488 contents_->GetManifest(base::Bind(&BookmarkAppHelper::OnDidGetManifest,
489 base::Unretained(this)));
490 } else {
491 OnIconsDownloaded(true, std::map<GURL, std::vector<SkBitmap> >());
495 void BookmarkAppHelper::CreateFromAppBanner(
496 const CreateBookmarkAppCallback& callback,
497 const content::Manifest& manifest) {
498 DCHECK(!manifest.short_name.is_null() || !manifest.name.is_null());
499 DCHECK(manifest.start_url.is_valid());
501 callback_ = callback;
502 OnDidGetManifest(manifest);
505 void BookmarkAppHelper::OnDidGetManifest(const content::Manifest& manifest) {
506 if (contents_->IsBeingDestroyed())
507 return;
509 UpdateWebAppInfoFromManifest(manifest, &web_app_info_);
511 // Add urls from the WebApplicationInfo.
512 std::vector<GURL> web_app_info_icon_urls;
513 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it =
514 web_app_info_.icons.begin();
515 it != web_app_info_.icons.end();
516 ++it) {
517 if (it->url.is_valid())
518 web_app_info_icon_urls.push_back(it->url);
521 favicon_downloader_.reset(
522 new FaviconDownloader(contents_,
523 web_app_info_icon_urls,
524 base::Bind(&BookmarkAppHelper::OnIconsDownloaded,
525 base::Unretained(this))));
526 favicon_downloader_->Start();
529 void BookmarkAppHelper::OnIconsDownloaded(
530 bool success,
531 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) {
532 // The tab has navigated away during the icon download. Cancel the bookmark
533 // app creation.
534 if (!success) {
535 favicon_downloader_.reset();
536 callback_.Run(nullptr, web_app_info_);
537 return;
540 std::vector<BitmapAndSource> downloaded_icons;
541 for (FaviconDownloader::FaviconMap::const_iterator map_it = bitmaps.begin();
542 map_it != bitmaps.end();
543 ++map_it) {
544 for (std::vector<SkBitmap>::const_iterator bitmap_it =
545 map_it->second.begin();
546 bitmap_it != map_it->second.end();
547 ++bitmap_it) {
548 if (bitmap_it->empty() || bitmap_it->width() != bitmap_it->height())
549 continue;
551 downloaded_icons.push_back(BitmapAndSource(map_it->first, *bitmap_it));
555 // Add all existing icons from WebApplicationInfo.
556 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it =
557 web_app_info_.icons.begin();
558 it != web_app_info_.icons.end();
559 ++it) {
560 const SkBitmap& icon = it->data;
561 if (!icon.drawsNothing() && icon.width() == icon.height()) {
562 downloaded_icons.push_back(BitmapAndSource(it->url, icon));
566 web_app_info_.generated_icon_color = SK_ColorTRANSPARENT;
567 std::map<int, BitmapAndSource> size_to_icons =
568 ResizeIconsAndGenerateMissing(downloaded_icons, SizesToGenerate(),
569 &web_app_info_);
570 ReplaceWebAppIcons(size_to_icons, &web_app_info_);
571 favicon_downloader_.reset();
573 if (!contents_) {
574 // The web contents can be null in tests.
575 OnBubbleCompleted(true, web_app_info_);
576 return;
579 Browser* browser = chrome::FindBrowserWithWebContents(contents_);
580 if (!browser) {
581 // The browser can be null in tests.
582 OnBubbleCompleted(true, web_app_info_);
583 return;
585 browser->window()->ShowBookmarkAppBubble(
586 web_app_info_, base::Bind(&BookmarkAppHelper::OnBubbleCompleted,
587 base::Unretained(this)));
590 void BookmarkAppHelper::OnBubbleCompleted(
591 bool user_accepted,
592 const WebApplicationInfo& web_app_info) {
593 if (user_accepted) {
594 web_app_info_ = web_app_info;
595 crx_installer_->InstallWebApp(web_app_info_);
596 } else {
597 callback_.Run(nullptr, web_app_info_);
601 void BookmarkAppHelper::FinishInstallation(const Extension* extension) {
602 // Set the default 'open as' preference for use next time the dialog is
603 // shown.
604 extensions::LaunchType launch_type = web_app_info_.open_as_window
605 ? extensions::LAUNCH_TYPE_WINDOW
606 : extensions::LAUNCH_TYPE_REGULAR;
607 profile_->GetPrefs()->SetInteger(
608 extensions::pref_names::kBookmarkAppCreationLaunchType, launch_type);
610 // Set the launcher type for the app.
611 extensions::SetLaunchType(profile_, extension->id(), launch_type);
613 if (!contents_) {
614 // The web contents can be null in tests.
615 callback_.Run(extension, web_app_info_);
616 return;
619 Browser* browser = chrome::FindBrowserWithWebContents(contents_);
620 if (!browser) {
621 // The browser can be null in tests.
622 callback_.Run(extension, web_app_info_);
623 return;
626 // Pin the app to the relevant launcher depending on the OS.
627 Profile* current_profile = profile_->GetOriginalProfile();
629 // On Mac, shortcuts are automatically created for hosted apps when they are
630 // installed, so there is no need to create them again.
631 #if !defined(OS_MACOSX)
632 chrome::HostDesktopType desktop = browser->host_desktop_type();
633 if (desktop != chrome::HOST_DESKTOP_TYPE_ASH) {
634 web_app::ShortcutLocations creation_locations;
635 #if defined(OS_LINUX)
636 creation_locations.on_desktop = true;
637 #else
638 creation_locations.on_desktop = false;
639 #endif
640 creation_locations.applications_menu_location =
641 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
642 web_app::CreateShortcuts(web_app::SHORTCUT_CREATION_BY_USER,
643 creation_locations, current_profile, extension);
644 // Creating shortcuts in the start menu fails when the language is set
645 // to certain languages (e.g. Hindi). To work around this, the taskbar /
646 // quick launch icon is created separately to ensure it doesn't fail
647 // due to the start menu shortcut creation failing.
648 // See http://crbug.com/477297 and http://crbug.com/484577.
649 creation_locations.on_desktop = false;
650 creation_locations.applications_menu_location =
651 web_app::APP_MENU_LOCATION_NONE;
652 creation_locations.in_quick_launch_bar = true;
653 web_app::CreateShortcuts(web_app::SHORTCUT_CREATION_BY_USER,
654 creation_locations, current_profile, extension);
655 #if defined(USE_ASH)
656 } else {
657 ChromeLauncherController::instance()->PinAppWithID(extension->id());
658 #endif
660 #endif
662 #if defined(OS_MACOSX)
663 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
664 switches::kDisableHostedAppShimCreation)) {
665 web_app::RevealAppShimInFinderForApp(current_profile, extension);
667 #endif
669 callback_.Run(extension, web_app_info_);
672 void BookmarkAppHelper::Observe(int type,
673 const content::NotificationSource& source,
674 const content::NotificationDetails& details) {
675 switch (type) {
676 case extensions::NOTIFICATION_CRX_INSTALLER_DONE: {
677 const Extension* extension =
678 content::Details<const Extension>(details).ptr();
679 DCHECK(extension);
680 DCHECK_EQ(AppLaunchInfo::GetLaunchWebURL(extension),
681 web_app_info_.app_url);
682 FinishInstallation(extension);
683 break;
685 case extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR:
686 callback_.Run(nullptr, web_app_info_);
687 break;
688 default:
689 NOTREACHED();
690 break;
694 void CreateOrUpdateBookmarkApp(ExtensionService* service,
695 WebApplicationInfo* web_app_info) {
696 scoped_refptr<BookmarkAppInstaller> installer(
697 new BookmarkAppInstaller(service, *web_app_info));
698 installer->Run();
701 void GetWebApplicationInfoFromApp(
702 content::BrowserContext* browser_context,
703 const extensions::Extension* extension,
704 const base::Callback<void(const WebApplicationInfo&)> callback) {
705 if (!extension->from_bookmark()) {
706 callback.Run(WebApplicationInfo());
707 return;
710 WebApplicationInfo web_app_info;
711 web_app_info.app_url = AppLaunchInfo::GetLaunchWebURL(extension);
712 web_app_info.title = base::UTF8ToUTF16(extension->non_localized_name());
713 web_app_info.description = base::UTF8ToUTF16(extension->description());
715 std::vector<extensions::ImageLoader::ImageRepresentation> info_list;
716 for (size_t i = 0; i < extension_misc::kNumExtensionIconSizes; ++i) {
717 int size = extension_misc::kExtensionIconSizes[i];
718 extensions::ExtensionResource resource =
719 extensions::IconsInfo::GetIconResource(
720 extension, size, ExtensionIconSet::MATCH_EXACTLY);
721 if (!resource.empty()) {
722 info_list.push_back(extensions::ImageLoader::ImageRepresentation(
723 resource,
724 extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE,
725 gfx::Size(size, size),
726 ui::SCALE_FACTOR_100P));
730 extensions::ImageLoader::Get(browser_context)->LoadImageFamilyAsync(
731 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback));
734 bool IsValidBookmarkAppUrl(const GURL& url) {
735 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes);
736 origin_only_pattern.SetMatchAllURLs(true);
737 return url.is_valid() && origin_only_pattern.MatchesURL(url);
740 } // namespace extensions