Revert of Don't install OEM default apps for enterprise users (patchset #1 id:1 of...
[chromium-blink-merge.git] / chrome / browser / extensions / bookmark_app_helper.cc
blob9c030a068f16bd23819f2ba7556f01590084758b
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_->Start(
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);
262 void FinishInstallation() {
263 std::map<int, BookmarkAppHelper::BitmapAndSource> size_map =
264 BookmarkAppHelper::ResizeIconsAndGenerateMissing(downloaded_bitmaps_,
265 &web_app_info_);
266 BookmarkAppHelper::UpdateWebAppIconsWithoutChangingLinks(size_map,
267 &web_app_info_);
268 scoped_refptr<extensions::CrxInstaller> installer(
269 extensions::CrxInstaller::CreateSilent(service_));
270 installer->set_error_on_unsupported_requirements(true);
271 installer->InstallWebApp(web_app_info_);
274 ExtensionService* service_;
275 WebApplicationInfo web_app_info_;
277 scoped_ptr<chrome::BitmapFetcher> bitmap_fetcher_;
278 std::vector<GURL> urls_to_download_;
279 std::vector<BookmarkAppHelper::BitmapAndSource> downloaded_bitmaps_;
282 } // namespace
284 namespace extensions {
286 // static
287 void BookmarkAppHelper::UpdateWebAppInfoFromManifest(
288 const content::Manifest& manifest,
289 WebApplicationInfo* web_app_info) {
290 if (!manifest.short_name.is_null())
291 web_app_info->title = manifest.short_name.string();
293 // Give the full length name priority.
294 if (!manifest.name.is_null())
295 web_app_info->title = manifest.name.string();
297 // Set the url based on the manifest value, if any.
298 if (manifest.start_url.is_valid())
299 web_app_info->app_url = manifest.start_url;
301 // If any icons are specified in the manifest, they take precedence over any
302 // we picked up from the web_app stuff.
303 if (!manifest.icons.empty()) {
304 web_app_info->icons.clear();
305 for (const auto& icon : manifest.icons) {
306 // TODO(benwells): Take the declared icon density and sizes into account.
307 WebApplicationInfo::IconInfo info;
308 info.url = icon.src;
309 web_app_info->icons.push_back(info);
314 // static
315 std::map<int, BookmarkAppHelper::BitmapAndSource>
316 BookmarkAppHelper::ConstrainBitmapsToSizes(
317 const std::vector<BookmarkAppHelper::BitmapAndSource>& bitmaps,
318 const std::set<int>& sizes) {
319 std::map<int, BitmapAndSource> output_bitmaps;
320 std::map<int, BitmapAndSource> ordered_bitmaps;
321 for (std::vector<BitmapAndSource>::const_iterator it = bitmaps.begin();
322 it != bitmaps.end(); ++it) {
323 DCHECK(it->bitmap.width() == it->bitmap.height());
324 ordered_bitmaps[it->bitmap.width()] = *it;
327 std::set<int>::const_iterator sizes_it = sizes.begin();
328 std::map<int, BitmapAndSource>::const_iterator bitmaps_it =
329 ordered_bitmaps.begin();
330 while (sizes_it != sizes.end() && bitmaps_it != ordered_bitmaps.end()) {
331 int size = *sizes_it;
332 // Find the closest not-smaller bitmap.
333 bitmaps_it = ordered_bitmaps.lower_bound(size);
334 ++sizes_it;
335 // Ensure the bitmap is valid and smaller than the next allowed size.
336 if (bitmaps_it != ordered_bitmaps.end() &&
337 (sizes_it == sizes.end() ||
338 bitmaps_it->second.bitmap.width() < *sizes_it)) {
339 output_bitmaps[size] = bitmaps_it->second;
340 // Resize the bitmap if it does not exactly match the desired size.
341 if (output_bitmaps[size].bitmap.width() != size) {
342 output_bitmaps[size].bitmap = skia::ImageOperations::Resize(
343 output_bitmaps[size].bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
344 size, size);
348 return output_bitmaps;
351 // static
352 void BookmarkAppHelper::GenerateIcon(
353 std::map<int, BookmarkAppHelper::BitmapAndSource>* bitmaps,
354 int output_size,
355 SkColor color,
356 char letter) {
357 // Do nothing if there is already an icon of |output_size|.
358 if (bitmaps->count(output_size))
359 return;
361 gfx::ImageSkia icon_image(
362 new GeneratedIconImageSource(letter, color, output_size),
363 gfx::Size(output_size, output_size));
364 icon_image.bitmap()->deepCopyTo(&(*bitmaps)[output_size].bitmap);
367 // static
368 std::map<int, BookmarkAppHelper::BitmapAndSource>
369 BookmarkAppHelper::ResizeIconsAndGenerateMissing(
370 std::vector<BookmarkAppHelper::BitmapAndSource> icons,
371 WebApplicationInfo* web_app_info) {
372 // Add the downloaded icons. Extensions only allow certain icon sizes. First
373 // populate icons that match the allowed sizes exactly and then downscale
374 // remaining icons to the closest allowed size that doesn't yet have an icon.
375 std::set<int> allowed_sizes(extension_misc::kExtensionIconSizes,
376 extension_misc::kExtensionIconSizes +
377 extension_misc::kNumExtensionIconSizes);
379 // If there are icons that don't match the accepted icon sizes, find the
380 // closest bigger icon to the accepted sizes and resize the icon to it. An
381 // icon will be resized and used for at most one size.
382 std::map<int, BitmapAndSource> resized_bitmaps(
383 ConstrainBitmapsToSizes(icons, allowed_sizes));
385 // Determine the color that will be used for the icon's background. For this
386 // the dominant color of the first icon found is used.
387 if (resized_bitmaps.size()) {
388 color_utils::GridSampler sampler;
389 web_app_info->generated_icon_color =
390 color_utils::CalculateKMeanColorOfBitmap(
391 resized_bitmaps.begin()->second.bitmap);
394 std::set<int> generate_sizes;
395 for (int size : SizesToGenerate()) {
396 if (resized_bitmaps.find(size) == resized_bitmaps.end())
397 generate_sizes.insert(size);
399 GenerateIcons(generate_sizes, web_app_info->app_url,
400 web_app_info->generated_icon_color, &resized_bitmaps);
402 return resized_bitmaps;
405 // static
406 void BookmarkAppHelper::UpdateWebAppIconsWithoutChangingLinks(
407 std::map<int, BookmarkAppHelper::BitmapAndSource> bitmap_map,
408 WebApplicationInfo* web_app_info) {
409 // First add in the icon data that have urls with the url / size data from the
410 // original web app info, and the data from the new icons (if any).
411 for (auto& icon : web_app_info->icons) {
412 if (!icon.url.is_empty() && icon.data.empty()) {
413 const auto& it = bitmap_map.find(icon.width);
414 if (it != bitmap_map.end() && it->second.source_url == icon.url)
415 icon.data = it->second.bitmap;
419 // Now add in any icons from the updated list that don't have URLs.
420 for (const auto& pair : bitmap_map) {
421 if (pair.second.source_url.is_empty()) {
422 WebApplicationInfo::IconInfo icon_info;
423 icon_info.data = pair.second.bitmap;
424 icon_info.width = pair.first;
425 icon_info.height = pair.first;
426 web_app_info->icons.push_back(icon_info);
431 BookmarkAppHelper::BitmapAndSource::BitmapAndSource() {
434 BookmarkAppHelper::BitmapAndSource::BitmapAndSource(const GURL& source_url_p,
435 const SkBitmap& bitmap_p)
436 : source_url(source_url_p),
437 bitmap(bitmap_p) {
440 BookmarkAppHelper::BitmapAndSource::~BitmapAndSource() {
443 BookmarkAppHelper::BookmarkAppHelper(Profile* profile,
444 WebApplicationInfo web_app_info,
445 content::WebContents* contents)
446 : profile_(profile),
447 contents_(contents),
448 web_app_info_(web_app_info),
449 crx_installer_(extensions::CrxInstaller::CreateSilent(
450 ExtensionSystem::Get(profile)->extension_service())) {
451 web_app_info_.open_as_window =
452 profile_->GetPrefs()->GetInteger(
453 extensions::pref_names::kBookmarkAppCreationLaunchType) ==
454 extensions::LAUNCH_TYPE_WINDOW;
456 // The default app title is the page title, which can be quite long. Limit the
457 // default name used to something sensible.
458 const int kMaxDefaultTitle = 40;
459 if (web_app_info_.title.length() > kMaxDefaultTitle) {
460 web_app_info_.title = web_app_info_.title.substr(0, kMaxDefaultTitle - 3) +
461 base::UTF8ToUTF16("...");
464 registrar_.Add(this,
465 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
466 content::Source<CrxInstaller>(crx_installer_.get()));
468 registrar_.Add(this,
469 extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR,
470 content::Source<CrxInstaller>(crx_installer_.get()));
472 crx_installer_->set_error_on_unsupported_requirements(true);
475 BookmarkAppHelper::~BookmarkAppHelper() {}
477 void BookmarkAppHelper::Create(const CreateBookmarkAppCallback& callback) {
478 callback_ = callback;
480 if (contents_) {
481 contents_->GetManifest(base::Bind(&BookmarkAppHelper::OnDidGetManifest,
482 base::Unretained(this)));
483 } else {
484 OnIconsDownloaded(true, std::map<GURL, std::vector<SkBitmap> >());
488 void BookmarkAppHelper::OnDidGetManifest(const content::Manifest& manifest) {
489 if (contents_->IsBeingDestroyed())
490 return;
492 UpdateWebAppInfoFromManifest(manifest, &web_app_info_);
494 // Add urls from the WebApplicationInfo.
495 std::vector<GURL> web_app_info_icon_urls;
496 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it =
497 web_app_info_.icons.begin();
498 it != web_app_info_.icons.end();
499 ++it) {
500 if (it->url.is_valid())
501 web_app_info_icon_urls.push_back(it->url);
504 favicon_downloader_.reset(
505 new FaviconDownloader(contents_,
506 web_app_info_icon_urls,
507 base::Bind(&BookmarkAppHelper::OnIconsDownloaded,
508 base::Unretained(this))));
509 favicon_downloader_->Start();
512 void BookmarkAppHelper::OnIconsDownloaded(
513 bool success,
514 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) {
515 // The tab has navigated away during the icon download. Cancel the bookmark
516 // app creation.
517 if (!success) {
518 favicon_downloader_.reset();
519 callback_.Run(nullptr, web_app_info_);
520 return;
523 std::vector<BitmapAndSource> downloaded_icons;
524 for (FaviconDownloader::FaviconMap::const_iterator map_it = bitmaps.begin();
525 map_it != bitmaps.end();
526 ++map_it) {
527 for (std::vector<SkBitmap>::const_iterator bitmap_it =
528 map_it->second.begin();
529 bitmap_it != map_it->second.end();
530 ++bitmap_it) {
531 if (bitmap_it->empty() || bitmap_it->width() != bitmap_it->height())
532 continue;
534 downloaded_icons.push_back(BitmapAndSource(map_it->first, *bitmap_it));
538 // Add all existing icons from WebApplicationInfo.
539 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it =
540 web_app_info_.icons.begin();
541 it != web_app_info_.icons.end();
542 ++it) {
543 const SkBitmap& icon = it->data;
544 if (!icon.drawsNothing() && icon.width() == icon.height()) {
545 downloaded_icons.push_back(BitmapAndSource(it->url, icon));
549 web_app_info_.generated_icon_color = SK_ColorTRANSPARENT;
550 std::map<int, BitmapAndSource> size_to_icons =
551 ResizeIconsAndGenerateMissing(downloaded_icons, &web_app_info_);
552 ReplaceWebAppIcons(size_to_icons, &web_app_info_);
553 favicon_downloader_.reset();
555 if (!contents_) {
556 // The web contents can be null in tests.
557 OnBubbleCompleted(true, web_app_info_);
558 return;
561 Browser* browser = chrome::FindBrowserWithWebContents(contents_);
562 if (!browser) {
563 // The browser can be null in tests.
564 OnBubbleCompleted(true, web_app_info_);
565 return;
567 browser->window()->ShowBookmarkAppBubble(
568 web_app_info_, base::Bind(&BookmarkAppHelper::OnBubbleCompleted,
569 base::Unretained(this)));
572 void BookmarkAppHelper::OnBubbleCompleted(
573 bool user_accepted,
574 const WebApplicationInfo& web_app_info) {
575 if (user_accepted) {
576 web_app_info_ = web_app_info;
577 crx_installer_->InstallWebApp(web_app_info_);
578 } else {
579 callback_.Run(nullptr, web_app_info_);
583 void BookmarkAppHelper::FinishInstallation(const Extension* extension) {
584 // Set the default 'open as' preference for use next time the dialog is
585 // shown.
586 extensions::LaunchType launch_type = web_app_info_.open_as_window
587 ? extensions::LAUNCH_TYPE_WINDOW
588 : extensions::LAUNCH_TYPE_REGULAR;
589 profile_->GetPrefs()->SetInteger(
590 extensions::pref_names::kBookmarkAppCreationLaunchType, launch_type);
592 // Set the launcher type for the app.
593 extensions::SetLaunchType(profile_, extension->id(), launch_type);
595 if (!contents_) {
596 // The web contents can be null in tests.
597 callback_.Run(extension, web_app_info_);
598 return;
601 Browser* browser = chrome::FindBrowserWithWebContents(contents_);
602 if (!browser) {
603 // The browser can be null in tests.
604 callback_.Run(extension, web_app_info_);
605 return;
608 // Pin the app to the relevant launcher depending on the OS.
609 Profile* current_profile = profile_->GetOriginalProfile();
611 // On Mac, shortcuts are automatically created for hosted apps when they are
612 // installed, so there is no need to create them again.
613 #if !defined(OS_MACOSX)
614 chrome::HostDesktopType desktop = browser->host_desktop_type();
615 if (desktop != chrome::HOST_DESKTOP_TYPE_ASH) {
616 web_app::ShortcutLocations creation_locations;
617 #if defined(OS_LINUX)
618 creation_locations.on_desktop = true;
619 #else
620 creation_locations.on_desktop = false;
621 #endif
622 creation_locations.applications_menu_location =
623 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
624 web_app::CreateShortcuts(web_app::SHORTCUT_CREATION_BY_USER,
625 creation_locations, current_profile, extension);
626 // Creating shortcuts in the start menu fails when the language is set
627 // to certain languages (e.g. Hindi). To work around this, the taskbar /
628 // quick launch icon is created separately to ensure it doesn't fail
629 // due to the start menu shortcut creation failing.
630 // See http://crbug.com/477297 and http://crbug.com/484577.
631 creation_locations.on_desktop = false;
632 creation_locations.applications_menu_location =
633 web_app::APP_MENU_LOCATION_NONE;
634 creation_locations.in_quick_launch_bar = true;
635 web_app::CreateShortcuts(web_app::SHORTCUT_CREATION_BY_USER,
636 creation_locations, current_profile, extension);
637 #if defined(USE_ASH)
638 } else {
639 ChromeLauncherController::instance()->PinAppWithID(extension->id());
640 #endif
642 #endif
644 #if defined(OS_MACOSX)
645 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
646 switches::kDisableHostedAppShimCreation)) {
647 web_app::RevealAppShimInFinderForApp(current_profile, extension);
649 #endif
651 callback_.Run(extension, web_app_info_);
654 void BookmarkAppHelper::Observe(int type,
655 const content::NotificationSource& source,
656 const content::NotificationDetails& details) {
657 switch (type) {
658 case extensions::NOTIFICATION_CRX_INSTALLER_DONE: {
659 const Extension* extension =
660 content::Details<const Extension>(details).ptr();
661 DCHECK(extension);
662 DCHECK_EQ(AppLaunchInfo::GetLaunchWebURL(extension),
663 web_app_info_.app_url);
664 FinishInstallation(extension);
665 break;
667 case extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR:
668 callback_.Run(nullptr, web_app_info_);
669 break;
670 default:
671 NOTREACHED();
672 break;
676 void CreateOrUpdateBookmarkApp(ExtensionService* service,
677 WebApplicationInfo* web_app_info) {
678 scoped_refptr<BookmarkAppInstaller> installer(
679 new BookmarkAppInstaller(service, *web_app_info));
680 installer->Run();
683 void GetWebApplicationInfoFromApp(
684 content::BrowserContext* browser_context,
685 const extensions::Extension* extension,
686 const base::Callback<void(const WebApplicationInfo&)> callback) {
687 if (!extension->from_bookmark()) {
688 callback.Run(WebApplicationInfo());
689 return;
692 WebApplicationInfo web_app_info;
693 web_app_info.app_url = AppLaunchInfo::GetLaunchWebURL(extension);
694 web_app_info.title = base::UTF8ToUTF16(extension->non_localized_name());
695 web_app_info.description = base::UTF8ToUTF16(extension->description());
697 std::vector<extensions::ImageLoader::ImageRepresentation> info_list;
698 for (size_t i = 0; i < extension_misc::kNumExtensionIconSizes; ++i) {
699 int size = extension_misc::kExtensionIconSizes[i];
700 extensions::ExtensionResource resource =
701 extensions::IconsInfo::GetIconResource(
702 extension, size, ExtensionIconSet::MATCH_EXACTLY);
703 if (!resource.empty()) {
704 info_list.push_back(extensions::ImageLoader::ImageRepresentation(
705 resource,
706 extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE,
707 gfx::Size(size, size),
708 ui::SCALE_FACTOR_100P));
712 extensions::ImageLoader::Get(browser_context)->LoadImageFamilyAsync(
713 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback));
716 bool IsValidBookmarkAppUrl(const GURL& url) {
717 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes);
718 origin_only_pattern.SetMatchAllURLs(true);
719 return url.is_valid() && origin_only_pattern.MatchesURL(url);
722 } // namespace extensions