Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/web_applications/web_app.h" | 5 #include "chrome/browser/web_applications/web_app.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/i18n/file_util_icu.h" | 10 #include "base/i18n/file_util_icu.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "chrome/browser/browser_process.h" | |
| 16 #include "chrome/browser/extensions/extension_service.h" | |
| 15 #include "chrome/browser/extensions/image_loader.h" | 17 #include "chrome/browser/extensions/image_loader.h" |
| 16 #include "chrome/browser/extensions/tab_helper.h" | 18 #include "chrome/browser/extensions/tab_helper.h" |
| 17 #include "chrome/browser/favicon/favicon_tab_helper.h" | 19 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | |
| 19 #include "chrome/common/chrome_constants.h" | 22 #include "chrome/common/chrome_constants.h" |
| 20 #include "chrome/common/chrome_version_info.h" | 23 #include "chrome/common/chrome_version_info.h" |
| 21 #include "chrome/common/extensions/api/file_handlers/file_handlers_parser.h" | 24 #include "chrome/common/extensions/api/file_handlers/file_handlers_parser.h" |
| 22 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 25 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 23 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 24 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
| 25 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "extensions/browser/extension_registry.h" | |
| 30 #include "extensions/browser/extension_system.h" | |
| 26 #include "extensions/common/constants.h" | 31 #include "extensions/common/constants.h" |
| 27 #include "extensions/common/extension.h" | 32 #include "extensions/common/extension.h" |
| 33 #include "extensions/common/extension_set.h" | |
| 28 #include "extensions/common/manifest_handlers/icons_handler.h" | 34 #include "extensions/common/manifest_handlers/icons_handler.h" |
| 29 #include "grit/theme_resources.h" | 35 #include "grit/theme_resources.h" |
| 30 #include "skia/ext/image_operations.h" | 36 #include "skia/ext/image_operations.h" |
| 31 #include "third_party/skia/include/core/SkBitmap.h" | 37 #include "third_party/skia/include/core/SkBitmap.h" |
| 32 #include "ui/base/resource/resource_bundle.h" | 38 #include "ui/base/resource/resource_bundle.h" |
| 33 #include "ui/gfx/image/image.h" | 39 #include "ui/gfx/image/image.h" |
| 34 #include "ui/gfx/image/image_family.h" | 40 #include "ui/gfx/image/image_family.h" |
| 35 #include "ui/gfx/image/image_skia.h" | 41 #include "ui/gfx/image/image_skia.h" |
| 36 | 42 |
| 37 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 62 #endif | 68 #endif |
| 63 | 69 |
| 64 #if defined(TOOLKIT_VIEWS) | 70 #if defined(TOOLKIT_VIEWS) |
| 65 // Predicator for sorting images from largest to smallest. | 71 // Predicator for sorting images from largest to smallest. |
| 66 bool IconPrecedes(const WebApplicationInfo::IconInfo& left, | 72 bool IconPrecedes(const WebApplicationInfo::IconInfo& left, |
| 67 const WebApplicationInfo::IconInfo& right) { | 73 const WebApplicationInfo::IconInfo& right) { |
| 68 return left.width < right.width; | 74 return left.width < right.width; |
| 69 } | 75 } |
| 70 #endif | 76 #endif |
| 71 | 77 |
| 78 bool ShouldCreateShortcutFor(const extensions::Extension* extension) { | |
| 79 return extension->is_platform_app() && | |
| 80 extension->location() != extensions::Manifest::COMPONENT && | |
| 81 extension->ShouldDisplayInAppLauncher(); | |
| 82 } | |
| 83 | |
| 72 bool CreateShortcutsWithInfoOnFileThread( | 84 bool CreateShortcutsWithInfoOnFileThread( |
| 73 web_app::ShortcutCreationReason reason, | 85 web_app::ShortcutCreationReason reason, |
| 74 const web_app::ShortcutLocations& locations, | 86 const web_app::ShortcutLocations& locations, |
| 75 const web_app::ShortcutInfo& shortcut_info, | 87 const web_app::ShortcutInfo& shortcut_info, |
| 76 const extensions::FileHandlersInfo& file_handlers_info) { | 88 const extensions::FileHandlersInfo& file_handlers_info) { |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 78 | 90 |
| 79 base::FilePath shortcut_data_dir = | 91 base::FilePath shortcut_data_dir = |
| 80 web_app::GetWebAppDataDirectory(shortcut_info.profile_path, | 92 web_app::GetWebAppDataDirectory(shortcut_info.profile_path, |
| 81 shortcut_info.extension_id, | 93 shortcut_info.extension_id, |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 // The following string is used to build the directory name for | 239 // The following string is used to build the directory name for |
| 228 // shortcuts to chrome applications (the kind which are installed | 240 // shortcuts to chrome applications (the kind which are installed |
| 229 // from a CRX). Application shortcuts to URLs use the {host}_{path} | 241 // from a CRX). Application shortcuts to URLs use the {host}_{path} |
| 230 // for the name of this directory. Hosts can't include an underscore. | 242 // for the name of this directory. Hosts can't include an underscore. |
| 231 // By starting this string with an underscore, we ensure that there | 243 // By starting this string with an underscore, we ensure that there |
| 232 // are no naming conflicts. | 244 // are no naming conflicts. |
| 233 static const char* kCrxAppPrefix = "_crx_"; | 245 static const char* kCrxAppPrefix = "_crx_"; |
| 234 | 246 |
| 235 namespace internals { | 247 namespace internals { |
| 236 | 248 |
| 249 void CallForProfileAndAppId( | |
| 250 const base::FilePath& profile_path, | |
| 251 const std::string app_id, | |
| 252 const ShortcutOperationCallback& callback) { | |
| 253 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 254 Profile* profile = profile_manager->GetProfileByPath(profile_path); | |
| 255 if (!profile || !profile_manager->IsValidProfile(profile)) | |
| 256 return; | |
| 257 | |
| 258 extensions::ExtensionRegistry* registry = | |
| 259 extensions::ExtensionRegistry::Get(profile); | |
| 260 const extensions::Extension* extension = registry->GetExtensionById( | |
| 261 app_id, extensions::ExtensionRegistry::ENABLED); | |
| 262 if (!extension || !extension->is_platform_app()) | |
| 263 return; | |
| 264 | |
| 265 callback.Run(profile, extension); | |
| 266 } | |
| 267 | |
| 237 base::FilePath GetSanitizedFileName(const base::string16& name) { | 268 base::FilePath GetSanitizedFileName(const base::string16& name) { |
| 238 #if defined(OS_WIN) | 269 #if defined(OS_WIN) |
| 239 base::string16 file_name = name; | 270 base::string16 file_name = name; |
| 240 #else | 271 #else |
| 241 std::string file_name = base::UTF16ToUTF8(name); | 272 std::string file_name = base::UTF16ToUTF8(name); |
| 242 #endif | 273 #endif |
| 243 file_util::ReplaceIllegalCharactersInPath(&file_name, '_'); | 274 file_util::ReplaceIllegalCharactersInPath(&file_name, '_'); |
| 244 return base::FilePath(file_name); | 275 return base::FilePath(file_name); |
| 245 } | 276 } |
| 246 | 277 |
| 247 bool CreateShortcutsOnFileThread( | 278 bool CreateShortcutsOnFileThread( |
| 248 ShortcutCreationReason reason, | 279 ShortcutCreationReason reason, |
| 249 const web_app::ShortcutLocations& locations, | 280 const web_app::ShortcutLocations& locations, |
| 250 const web_app::ShortcutInfo& shortcut_info) { | 281 const web_app::ShortcutInfo& shortcut_info) { |
| 251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 252 | 283 |
| 253 return CreateShortcutsWithInfoOnFileThread( | 284 return CreateShortcutsWithInfoOnFileThread( |
| 254 reason, locations, shortcut_info, extensions::FileHandlersInfo()); | 285 reason, locations, shortcut_info, extensions::FileHandlersInfo()); |
| 255 } | 286 } |
| 256 | 287 |
| 288 void UpdateShortcutsForAllAppsForProfile(const base::FilePath& profile_path, | |
| 289 const std::set<std::string>& app_ids) { | |
| 290 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
|
calamity
2014/05/07 06:00:19
Why does this need to be on the FILE thread? Can y
jackhou1
2014/05/22 05:26:47
This is where we'll scan for shortcuts and decide
| |
| 291 web_app::ShortcutLocations creation_locations; | |
| 292 // Create the shortcut in the Chrome Apps subdir. | |
| 293 creation_locations.applications_menu_location = | |
| 294 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; | |
| 295 | |
| 296 for (std::set<std::string>::const_iterator it = app_ids.begin(); | |
| 297 it != app_ids.end(); | |
| 298 ++it) { | |
| 299 BrowserThread::PostTask( | |
| 300 BrowserThread::UI, | |
| 301 FROM_HERE, | |
| 302 base::Bind(&web_app::internals::CallForProfileAndAppId, | |
| 303 profile_path, | |
| 304 *it, | |
| 305 base::Bind(&web_app::CreateShortcuts, | |
| 306 web_app::SHORTCUT_CREATION_AUTOMATED, | |
| 307 creation_locations))); | |
| 308 } | |
| 309 } | |
| 310 | |
| 257 } // namespace internals | 311 } // namespace internals |
| 258 | 312 |
| 259 web_app::ShortcutInfo::ShortcutInfo() | 313 web_app::ShortcutInfo::ShortcutInfo() |
| 260 : is_platform_app(false) { | 314 : is_platform_app(false) { |
| 261 } | 315 } |
| 262 | 316 |
| 263 web_app::ShortcutInfo::~ShortcutInfo() {} | 317 web_app::ShortcutInfo::~ShortcutInfo() {} |
| 264 | 318 |
| 265 web_app::ShortcutLocations::ShortcutLocations() | 319 web_app::ShortcutLocations::ShortcutLocations() |
| 266 : on_desktop(false), | 320 : on_desktop(false), |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 reason, locations, shortcut_info)); | 459 reason, locations, shortcut_info)); |
| 406 } | 460 } |
| 407 | 461 |
| 408 void CreateShortcuts( | 462 void CreateShortcuts( |
| 409 ShortcutCreationReason reason, | 463 ShortcutCreationReason reason, |
| 410 const web_app::ShortcutLocations& locations, | 464 const web_app::ShortcutLocations& locations, |
| 411 Profile* profile, | 465 Profile* profile, |
| 412 const extensions::Extension* app) { | 466 const extensions::Extension* app) { |
| 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 414 | 468 |
| 469 if (!ShouldCreateShortcutFor(app)) | |
| 470 return; | |
| 471 | |
| 415 GetInfoForApp(app, | 472 GetInfoForApp(app, |
| 416 profile, | 473 profile, |
| 417 base::Bind(&CreateShortcutsWithInfo, reason, locations)); | 474 base::Bind(&CreateShortcutsWithInfo, reason, locations)); |
| 418 } | 475 } |
| 419 | 476 |
| 420 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { | 477 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { |
| 421 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 422 | 479 |
| 423 BrowserThread::PostTask( | 480 BrowserThread::PostTask( |
| 424 BrowserThread::FILE, | 481 BrowserThread::FILE, |
| 425 FROM_HERE, | 482 FROM_HERE, |
| 426 base::Bind(&DeleteShortcutsOnFileThread, | 483 base::Bind(&DeleteShortcutsOnFileThread, |
| 427 web_app::ShortcutInfoForExtensionAndProfile(app, profile))); | 484 web_app::ShortcutInfoForExtensionAndProfile(app, profile))); |
| 428 } | 485 } |
| 429 | 486 |
| 430 void UpdateAllShortcuts(const base::string16& old_app_title, | 487 void UpdateAllShortcuts(const base::string16& old_app_title, |
| 431 Profile* profile, | 488 Profile* profile, |
| 432 const extensions::Extension* app) { | 489 const extensions::Extension* app) { |
| 433 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 434 | 491 |
| 435 GetInfoForApp(app, | 492 GetInfoForApp(app, |
| 436 profile, | 493 profile, |
| 437 base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title)); | 494 base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title)); |
| 438 } | 495 } |
| 439 | 496 |
| 497 void UpdateShortcutsForAllApps(Profile* profile) { | |
| 498 // Check if extension system/service are available. They might not be in | |
| 499 // tests. | |
| 500 extensions::ExtensionSystem* extension_system; | |
| 501 ExtensionServiceInterface* extension_service; | |
| 502 if (!(extension_system = extensions::ExtensionSystem::Get(profile)) || | |
| 503 !(extension_service = extension_system->extension_service())) | |
| 504 return; | |
| 505 | |
| 506 // Get a set of app ids. | |
| 507 const extensions::ExtensionSet* apps = extension_service->extensions(); | |
| 508 std::set<std::string> app_ids; | |
| 509 for (extensions::ExtensionSet::const_iterator it = apps->begin(); | |
| 510 it != apps->end(); ++it) { | |
| 511 if (ShouldCreateShortcutFor(it->get())) | |
| 512 app_ids.insert((*it)->id()); | |
| 513 } | |
| 514 | |
| 515 BrowserThread::PostTask( | |
| 516 BrowserThread::FILE, | |
| 517 FROM_HERE, | |
| 518 base::Bind( | |
| 519 &web_app::internals::UpdateShortcutsForAllAppsForProfile, | |
| 520 profile->GetPath(), app_ids)); | |
| 521 } | |
| 522 | |
| 440 bool IsValidUrl(const GURL& url) { | 523 bool IsValidUrl(const GURL& url) { |
| 441 static const char* const kValidUrlSchemes[] = { | 524 static const char* const kValidUrlSchemes[] = { |
| 442 content::kFileScheme, | 525 content::kFileScheme, |
| 443 content::kFileSystemScheme, | 526 content::kFileSystemScheme, |
| 444 content::kFtpScheme, | 527 content::kFtpScheme, |
| 445 content::kHttpScheme, | 528 content::kHttpScheme, |
| 446 content::kHttpsScheme, | 529 content::kHttpsScheme, |
| 447 extensions::kExtensionScheme, | 530 extensions::kExtensionScheme, |
| 448 }; | 531 }; |
| 449 | 532 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 474 | 557 |
| 475 #if defined(OS_LINUX) | 558 #if defined(OS_LINUX) |
| 476 std::string GetWMClassFromAppName(std::string app_name) { | 559 std::string GetWMClassFromAppName(std::string app_name) { |
| 477 file_util::ReplaceIllegalCharactersInPath(&app_name, '_'); | 560 file_util::ReplaceIllegalCharactersInPath(&app_name, '_'); |
| 478 base::TrimString(app_name, "_", &app_name); | 561 base::TrimString(app_name, "_", &app_name); |
| 479 return app_name; | 562 return app_name; |
| 480 } | 563 } |
| 481 #endif | 564 #endif |
| 482 | 565 |
| 483 } // namespace web_app | 566 } // namespace web_app |
| OLD | NEW |