OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/bookmark_app_helper.h" | 5 #include "chrome/browser/extensions/bookmark_app_helper.h" |
6 | 6 |
7 #include <cctype> | 7 #include <cctype> |
8 | 8 |
9 #include "base/prefs/pref_service.h" | |
9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/chrome_notification_types.h" | |
10 #include "chrome/browser/extensions/crx_installer.h" | 12 #include "chrome/browser/extensions/crx_installer.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/extensions/favicon_downloader.h" | 14 #include "chrome/browser/extensions/favicon_downloader.h" |
15 #include "chrome/browser/extensions/launch_util.h" | |
13 #include "chrome/browser/extensions/tab_helper.h" | 16 #include "chrome/browser/extensions/tab_helper.h" |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/ui/app_list/app_list_service.h" | |
19 #include "chrome/browser/ui/app_list/app_list_util.h" | |
20 #include "chrome/browser/ui/browser_finder.h" | |
21 #include "chrome/browser/ui/browser_window.h" | |
22 #include "chrome/browser/ui/host_desktop.h" | |
14 #include "chrome/common/extensions/extension_constants.h" | 23 #include "chrome/common/extensions/extension_constants.h" |
15 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 24 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
25 #include "chrome/common/url_constants.h" | |
16 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
17 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
18 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
29 #include "extensions/browser/extension_system.h" | |
19 #include "extensions/browser/image_loader.h" | 30 #include "extensions/browser/image_loader.h" |
20 #include "extensions/browser/notification_types.h" | 31 #include "extensions/browser/notification_types.h" |
32 #include "extensions/browser/pref_names.h" | |
21 #include "extensions/common/constants.h" | 33 #include "extensions/common/constants.h" |
22 #include "extensions/common/extension.h" | 34 #include "extensions/common/extension.h" |
23 #include "extensions/common/manifest_handlers/icons_handler.h" | 35 #include "extensions/common/manifest_handlers/icons_handler.h" |
24 #include "extensions/common/url_pattern.h" | 36 #include "extensions/common/url_pattern.h" |
25 #include "grit/platform_locale_settings.h" | 37 #include "grit/platform_locale_settings.h" |
26 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 38 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
27 #include "skia/ext/image_operations.h" | 39 #include "skia/ext/image_operations.h" |
28 #include "skia/ext/platform_canvas.h" | 40 #include "skia/ext/platform_canvas.h" |
29 #include "third_party/skia/include/core/SkBitmap.h" | 41 #include "third_party/skia/include/core/SkBitmap.h" |
30 #include "ui/base/l10n/l10n_util.h" | 42 #include "ui/base/l10n/l10n_util.h" |
31 #include "ui/gfx/canvas.h" | 43 #include "ui/gfx/canvas.h" |
32 #include "ui/gfx/color_analysis.h" | 44 #include "ui/gfx/color_analysis.h" |
33 #include "ui/gfx/color_utils.h" | 45 #include "ui/gfx/color_utils.h" |
34 #include "ui/gfx/font.h" | 46 #include "ui/gfx/font.h" |
35 #include "ui/gfx/font_list.h" | 47 #include "ui/gfx/font_list.h" |
36 #include "ui/gfx/geometry/rect.h" | 48 #include "ui/gfx/geometry/rect.h" |
37 #include "ui/gfx/image/canvas_image_source.h" | 49 #include "ui/gfx/image/canvas_image_source.h" |
38 #include "ui/gfx/image/image.h" | 50 #include "ui/gfx/image/image.h" |
39 #include "ui/gfx/image/image_family.h" | 51 #include "ui/gfx/image/image_family.h" |
40 | 52 |
53 #if defined(OS_MACOSX) | |
54 #include "base/command_line.h" | |
55 #include "chrome/browser/web_applications/web_app_mac.h" | |
56 #include "chrome/common/chrome_switches.h" | |
57 #endif | |
58 | |
59 #if defined(USE_ASH) | |
60 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | |
61 #endif | |
62 | |
41 namespace { | 63 namespace { |
42 | 64 |
43 // Overlays a shortcut icon over the bottom left corner of a given image. | 65 // Overlays a shortcut icon over the bottom left corner of a given image. |
44 class GeneratedIconImageSource : public gfx::CanvasImageSource { | 66 class GeneratedIconImageSource : public gfx::CanvasImageSource { |
45 public: | 67 public: |
46 explicit GeneratedIconImageSource(char letter, SkColor color, int output_size) | 68 explicit GeneratedIconImageSource(char letter, SkColor color, int output_size) |
47 : gfx::CanvasImageSource(gfx::Size(output_size, output_size), false), | 69 : gfx::CanvasImageSource(gfx::Size(output_size, output_size), false), |
48 letter_(letter), | 70 letter_(letter), |
49 color_(color), | 71 color_(color), |
50 output_size_(output_size) {} | 72 output_size_(output_size) {} |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 // Do nothing if there is already an icon of |output_size|. | 233 // Do nothing if there is already an icon of |output_size|. |
212 if (bitmaps->count(output_size)) | 234 if (bitmaps->count(output_size)) |
213 return; | 235 return; |
214 | 236 |
215 gfx::ImageSkia icon_image( | 237 gfx::ImageSkia icon_image( |
216 new GeneratedIconImageSource(letter, color, output_size), | 238 new GeneratedIconImageSource(letter, color, output_size), |
217 gfx::Size(output_size, output_size)); | 239 gfx::Size(output_size, output_size)); |
218 icon_image.bitmap()->deepCopyTo(&(*bitmaps)[output_size]); | 240 icon_image.bitmap()->deepCopyTo(&(*bitmaps)[output_size]); |
219 } | 241 } |
220 | 242 |
221 BookmarkAppHelper::BookmarkAppHelper(ExtensionService* service, | 243 BookmarkAppHelper::BookmarkAppHelper(Profile* profile, |
222 WebApplicationInfo web_app_info, | 244 WebApplicationInfo web_app_info, |
223 content::WebContents* contents) | 245 content::WebContents* contents) |
224 : contents_(contents), | 246 : profile_(profile), |
247 contents_(contents), | |
225 web_app_info_(web_app_info), | 248 web_app_info_(web_app_info), |
226 crx_installer_(extensions::CrxInstaller::CreateSilent(service)) { | 249 crx_installer_(extensions::CrxInstaller::CreateSilent( |
250 ExtensionSystem::Get(profile)->extension_service())) { | |
227 registrar_.Add(this, | 251 registrar_.Add(this, |
228 extensions::NOTIFICATION_CRX_INSTALLER_DONE, | 252 extensions::NOTIFICATION_CRX_INSTALLER_DONE, |
229 content::Source<CrxInstaller>(crx_installer_.get())); | 253 content::Source<CrxInstaller>(crx_installer_.get())); |
230 | 254 |
231 registrar_.Add(this, | 255 registrar_.Add(this, |
232 extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR, | 256 extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
233 content::Source<CrxInstaller>(crx_installer_.get())); | 257 content::Source<CrxInstaller>(crx_installer_.get())); |
234 | 258 |
235 crx_installer_->set_error_on_unsupported_requirements(true); | 259 crx_installer_->set_error_on_unsupported_requirements(true); |
236 } | 260 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 favicon_downloader_->Start(); | 296 favicon_downloader_->Start(); |
273 } | 297 } |
274 | 298 |
275 void BookmarkAppHelper::OnIconsDownloaded( | 299 void BookmarkAppHelper::OnIconsDownloaded( |
276 bool success, | 300 bool success, |
277 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) { | 301 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) { |
278 // The tab has navigated away during the icon download. Cancel the bookmark | 302 // The tab has navigated away during the icon download. Cancel the bookmark |
279 // app creation. | 303 // app creation. |
280 if (!success) { | 304 if (!success) { |
281 favicon_downloader_.reset(); | 305 favicon_downloader_.reset(); |
282 callback_.Run(NULL, web_app_info_); | 306 callback_.Run(nullptr, web_app_info_); |
283 return; | 307 return; |
284 } | 308 } |
285 | 309 |
286 std::vector<SkBitmap> downloaded_icons; | 310 std::vector<SkBitmap> downloaded_icons; |
287 for (FaviconDownloader::FaviconMap::const_iterator map_it = bitmaps.begin(); | 311 for (FaviconDownloader::FaviconMap::const_iterator map_it = bitmaps.begin(); |
288 map_it != bitmaps.end(); | 312 map_it != bitmaps.end(); |
289 ++map_it) { | 313 ++map_it) { |
290 for (std::vector<SkBitmap>::const_iterator bitmap_it = | 314 for (std::vector<SkBitmap>::const_iterator bitmap_it = |
291 map_it->second.begin(); | 315 map_it->second.begin(); |
292 bitmap_it != map_it->second.end(); | 316 bitmap_it != map_it->second.end(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
328 | 352 |
329 std::map<int, SkBitmap> generated_icons; | 353 std::map<int, SkBitmap> generated_icons; |
330 // Icons are always generated, replacing the icons that were downloaded. This | 354 // Icons are always generated, replacing the icons that were downloaded. This |
331 // is done so that the icons are consistent across machines. | 355 // is done so that the icons are consistent across machines. |
332 // TODO(benwells): Use blob sync once it is available to sync the downloaded | 356 // TODO(benwells): Use blob sync once it is available to sync the downloaded |
333 // icons, and then only generate when there are required sizes missing. | 357 // icons, and then only generate when there are required sizes missing. |
334 GenerateIcons(generate_sizes, web_app_info_.app_url, | 358 GenerateIcons(generate_sizes, web_app_info_.app_url, |
335 web_app_info_.generated_icon_color, &generated_icons); | 359 web_app_info_.generated_icon_color, &generated_icons); |
336 | 360 |
337 ReplaceWebAppIcons(generated_icons, &web_app_info_); | 361 ReplaceWebAppIcons(generated_icons, &web_app_info_); |
362 favicon_downloader_.reset(); | |
338 | 363 |
339 // Install the app. | 364 if (!contents_) { |
340 crx_installer_->InstallWebApp(web_app_info_); | 365 // The web contents can be null in tests. |
341 favicon_downloader_.reset(); | 366 OnBubbleCompleted(true, web_app_info_); |
367 return; | |
368 } | |
369 | |
370 Browser* browser = chrome::FindBrowserWithWebContents(contents_); | |
371 if (!browser) { | |
372 // The browser can be null in tests. | |
373 OnBubbleCompleted(true, web_app_info_); | |
374 return; | |
375 } | |
376 browser->window()->ShowBookmarkAppBubble( | |
377 web_app_info_, base::Bind(&BookmarkAppHelper::OnBubbleCompleted, | |
378 base::Unretained(this))); | |
379 } | |
380 | |
381 void BookmarkAppHelper::OnBubbleCompleted( | |
382 bool user_accepted, | |
383 const WebApplicationInfo& web_app_info) { | |
384 if (user_accepted) { | |
385 web_app_info_ = web_app_info; | |
386 crx_installer_->InstallWebApp(web_app_info_); | |
387 } else { | |
388 callback_.Run(nullptr, web_app_info_); | |
389 } | |
390 } | |
391 | |
392 void BookmarkAppHelper::FinishInstallation(const Extension* extension) { | |
393 extensions::LaunchType launch_type = web_app_info_.open_as_window | |
calamity
2015/02/03 07:29:49
Comment about what these lines do.
benwells
2015/02/03 08:54:13
Done.
| |
394 ? extensions::LAUNCH_TYPE_WINDOW | |
395 : extensions::LAUNCH_TYPE_REGULAR; | |
396 profile_->GetPrefs()->SetInteger( | |
397 extensions::pref_names::kBookmarkAppCreationLaunchType, launch_type); | |
398 extensions::SetLaunchType(profile_, extension->id(), launch_type); | |
399 | |
400 if (!contents_) { | |
401 // The web contents can be null in tests. | |
402 callback_.Run(extension, web_app_info_); | |
403 return; | |
404 } | |
405 | |
406 Browser* browser = chrome::FindBrowserWithWebContents(contents_); | |
407 if (!browser) { | |
408 // The browser can be null in tests. | |
409 callback_.Run(extension, web_app_info_); | |
410 return; | |
411 } | |
412 | |
413 chrome::HostDesktopType desktop = browser->host_desktop_type(); | |
414 #if defined(USE_ASH) | |
415 if (desktop == chrome::HOST_DESKTOP_TYPE_ASH) | |
416 ChromeLauncherController::instance()->PinAppWithID(extension->id()); | |
417 #endif | |
418 | |
419 // Show the newly installed app in the app launcher or chrome://apps. | |
420 Profile* current_profile = profile_->GetOriginalProfile(); | |
421 if (IsAppLauncherEnabled()) { | |
422 AppListService::Get(desktop) | |
423 ->ShowForAppInstall(current_profile, extension->id(), false); | |
424 #if defined(OS_MACOSX) | |
425 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
426 switches::kEnableHostedAppShimCreation)) { | |
427 web_app::RevealAppShimInFinderForApp(profile_, extension); | |
428 #endif | |
429 } else { | |
430 chrome::NavigateParams params(current_profile, | |
431 GURL(chrome::kChromeUIAppsURL), | |
432 ui::PAGE_TRANSITION_LINK); | |
433 params.disposition = SINGLETON_TAB; | |
434 chrome::Navigate(¶ms); | |
435 | |
436 content::NotificationService::current()->Notify( | |
437 chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, | |
438 content::Source<content::WebContents>(params.target_contents), | |
439 content::Details<const std::string>(&extension->id())); | |
440 } | |
441 | |
442 callback_.Run(extension, web_app_info_); | |
342 } | 443 } |
343 | 444 |
344 void BookmarkAppHelper::Observe(int type, | 445 void BookmarkAppHelper::Observe(int type, |
345 const content::NotificationSource& source, | 446 const content::NotificationSource& source, |
346 const content::NotificationDetails& details) { | 447 const content::NotificationDetails& details) { |
347 switch (type) { | 448 switch (type) { |
348 case extensions::NOTIFICATION_CRX_INSTALLER_DONE: { | 449 case extensions::NOTIFICATION_CRX_INSTALLER_DONE: { |
349 const Extension* extension = | 450 const Extension* extension = |
350 content::Details<const Extension>(details).ptr(); | 451 content::Details<const Extension>(details).ptr(); |
351 DCHECK(extension); | 452 DCHECK(extension); |
352 DCHECK_EQ(AppLaunchInfo::GetLaunchWebURL(extension), | 453 DCHECK_EQ(AppLaunchInfo::GetLaunchWebURL(extension), |
353 web_app_info_.app_url); | 454 web_app_info_.app_url); |
354 callback_.Run(extension, web_app_info_); | 455 FinishInstallation(extension); |
355 break; | 456 break; |
356 } | 457 } |
357 case extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR: | 458 case extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR: |
358 callback_.Run(NULL, web_app_info_); | 459 callback_.Run(nullptr, web_app_info_); |
359 break; | 460 break; |
360 default: | 461 default: |
361 NOTREACHED(); | 462 NOTREACHED(); |
362 break; | 463 break; |
363 } | 464 } |
364 } | 465 } |
365 | 466 |
366 void CreateOrUpdateBookmarkApp(ExtensionService* service, | 467 void CreateOrUpdateBookmarkApp(ExtensionService* service, |
367 WebApplicationInfo* web_app_info) { | 468 WebApplicationInfo* web_app_info) { |
368 scoped_refptr<extensions::CrxInstaller> installer( | 469 scoped_refptr<extensions::CrxInstaller> installer( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
411 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback)); | 512 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback)); |
412 } | 513 } |
413 | 514 |
414 bool IsValidBookmarkAppUrl(const GURL& url) { | 515 bool IsValidBookmarkAppUrl(const GURL& url) { |
415 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes); | 516 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes); |
416 origin_only_pattern.SetMatchAllURLs(true); | 517 origin_only_pattern.SetMatchAllURLs(true); |
417 return url.is_valid() && origin_only_pattern.MatchesURL(url); | 518 return url.is_valid() && origin_only_pattern.MatchesURL(url); |
418 } | 519 } |
419 | 520 |
420 } // namespace extensions | 521 } // namespace extensions |
OLD | NEW |