| 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_ui_util.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/manifest_handlers/app_launch_info.h" | 24 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 22 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
| 24 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 28 #include "extensions/browser/extension_registry.h" |
| 25 #include "extensions/common/constants.h" | 29 #include "extensions/common/constants.h" |
| 26 #include "extensions/common/extension.h" | 30 #include "extensions/common/extension.h" |
| 31 #include "extensions/common/extension_set.h" |
| 27 #include "extensions/common/manifest_handlers/icons_handler.h" | 32 #include "extensions/common/manifest_handlers/icons_handler.h" |
| 28 #include "grit/theme_resources.h" | 33 #include "grit/theme_resources.h" |
| 29 #include "skia/ext/image_operations.h" | 34 #include "skia/ext/image_operations.h" |
| 30 #include "third_party/skia/include/core/SkBitmap.h" | 35 #include "third_party/skia/include/core/SkBitmap.h" |
| 31 #include "ui/base/resource/resource_bundle.h" | 36 #include "ui/base/resource/resource_bundle.h" |
| 32 #include "ui/gfx/image/image.h" | 37 #include "ui/gfx/image/image.h" |
| 33 #include "ui/gfx/image/image_family.h" | 38 #include "ui/gfx/image/image_family.h" |
| 34 #include "ui/gfx/image/image_skia.h" | 39 #include "ui/gfx/image/image_skia.h" |
| 35 | 40 |
| 36 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 67 return left.width < right.width; | 72 return left.width < right.width; |
| 68 } | 73 } |
| 69 #endif | 74 #endif |
| 70 | 75 |
| 71 base::FilePath GetShortcutDataDir(const web_app::ShortcutInfo& shortcut_info) { | 76 base::FilePath GetShortcutDataDir(const web_app::ShortcutInfo& shortcut_info) { |
| 72 return web_app::GetWebAppDataDirectory(shortcut_info.profile_path, | 77 return web_app::GetWebAppDataDirectory(shortcut_info.profile_path, |
| 73 shortcut_info.extension_id, | 78 shortcut_info.extension_id, |
| 74 shortcut_info.url); | 79 shortcut_info.url); |
| 75 } | 80 } |
| 76 | 81 |
| 82 bool ShouldCreateShortcutFor(Profile* profile, |
| 83 const extensions::Extension* extension) { |
| 84 return extension->is_platform_app() && |
| 85 extension->location() != extensions::Manifest::COMPONENT && |
| 86 extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile); |
| 87 } |
| 88 |
| 77 void CreateShortcutsWithInfo( | 89 void CreateShortcutsWithInfo( |
| 78 web_app::ShortcutCreationReason reason, | 90 web_app::ShortcutCreationReason reason, |
| 79 const web_app::ShortcutLocations& locations, | 91 const web_app::ShortcutLocations& locations, |
| 80 const web_app::ShortcutInfo& shortcut_info, | 92 const web_app::ShortcutInfo& shortcut_info, |
| 81 const extensions::FileHandlersInfo& file_handlers_info) { | 93 const extensions::FileHandlersInfo& file_handlers_info) { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 83 | 95 |
| 84 BrowserThread::PostTask( | 96 BrowserThread::PostTask( |
| 85 BrowserThread::FILE, | 97 BrowserThread::FILE, |
| 86 FROM_HERE, | 98 FROM_HERE, |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 // The following string is used to build the directory name for | 208 // The following string is used to build the directory name for |
| 197 // shortcuts to chrome applications (the kind which are installed | 209 // shortcuts to chrome applications (the kind which are installed |
| 198 // from a CRX). Application shortcuts to URLs use the {host}_{path} | 210 // from a CRX). Application shortcuts to URLs use the {host}_{path} |
| 199 // for the name of this directory. Hosts can't include an underscore. | 211 // for the name of this directory. Hosts can't include an underscore. |
| 200 // By starting this string with an underscore, we ensure that there | 212 // By starting this string with an underscore, we ensure that there |
| 201 // are no naming conflicts. | 213 // are no naming conflicts. |
| 202 static const char* kCrxAppPrefix = "_crx_"; | 214 static const char* kCrxAppPrefix = "_crx_"; |
| 203 | 215 |
| 204 namespace internals { | 216 namespace internals { |
| 205 | 217 |
| 218 void CallForProfileAndAppId( |
| 219 const base::FilePath& profile_path, |
| 220 const std::string app_id, |
| 221 const ShortcutOperationCallback& callback) { |
| 222 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 223 Profile* profile = profile_manager->GetProfileByPath(profile_path); |
| 224 if (!profile || !profile_manager->IsValidProfile(profile)) |
| 225 return; |
| 226 |
| 227 extensions::ExtensionRegistry* registry = |
| 228 extensions::ExtensionRegistry::Get(profile); |
| 229 const extensions::Extension* extension = registry->GetExtensionById( |
| 230 app_id, extensions::ExtensionRegistry::EVERYTHING); |
| 231 if (!extension || !extension->is_platform_app()) |
| 232 return; |
| 233 |
| 234 callback.Run(profile, extension); |
| 235 } |
| 236 |
| 206 base::FilePath GetSanitizedFileName(const base::string16& name) { | 237 base::FilePath GetSanitizedFileName(const base::string16& name) { |
| 207 #if defined(OS_WIN) | 238 #if defined(OS_WIN) |
| 208 base::string16 file_name = name; | 239 base::string16 file_name = name; |
| 209 #else | 240 #else |
| 210 std::string file_name = base::UTF16ToUTF8(name); | 241 std::string file_name = base::UTF16ToUTF8(name); |
| 211 #endif | 242 #endif |
| 212 file_util::ReplaceIllegalCharactersInPath(&file_name, '_'); | 243 file_util::ReplaceIllegalCharactersInPath(&file_name, '_'); |
| 213 return base::FilePath(file_name); | 244 return base::FilePath(file_name); |
| 214 } | 245 } |
| 215 | 246 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 base::IgnoreResult(&web_app::internals::CreateShortcutsOnFileThread), | 398 base::IgnoreResult(&web_app::internals::CreateShortcutsOnFileThread), |
| 368 reason, locations, shortcut_info)); | 399 reason, locations, shortcut_info)); |
| 369 } | 400 } |
| 370 | 401 |
| 371 void CreateShortcuts(ShortcutCreationReason reason, | 402 void CreateShortcuts(ShortcutCreationReason reason, |
| 372 const ShortcutLocations& locations, | 403 const ShortcutLocations& locations, |
| 373 Profile* profile, | 404 Profile* profile, |
| 374 const extensions::Extension* app) { | 405 const extensions::Extension* app) { |
| 375 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 376 | 407 |
| 408 if (!ShouldCreateShortcutFor(profile, app)) |
| 409 return; |
| 410 |
| 377 GetInfoForApp(app, | 411 GetInfoForApp(app, |
| 378 profile, | 412 profile, |
| 379 base::Bind(&CreateShortcutsWithInfo, reason, locations)); | 413 base::Bind(&CreateShortcutsWithInfo, reason, locations)); |
| 380 } | 414 } |
| 381 | 415 |
| 382 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { | 416 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { |
| 383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 384 | 418 |
| 385 ShortcutInfo shortcut_info = | 419 ShortcutInfo shortcut_info = |
| 386 ShortcutInfoForExtensionAndProfile(app, profile); | 420 ShortcutInfoForExtensionAndProfile(app, profile); |
| 387 BrowserThread::PostTask( | 421 BrowserThread::PostTask( |
| 388 BrowserThread::FILE, | 422 BrowserThread::FILE, |
| 389 FROM_HERE, | 423 FROM_HERE, |
| 390 base::Bind(&web_app::internals::DeletePlatformShortcuts, | 424 base::Bind(&web_app::internals::DeletePlatformShortcuts, |
| 391 GetShortcutDataDir(shortcut_info), shortcut_info)); | 425 GetShortcutDataDir(shortcut_info), shortcut_info)); |
| 392 } | 426 } |
| 393 | 427 |
| 394 void UpdateAllShortcuts(const base::string16& old_app_title, | 428 void UpdateAllShortcuts(const base::string16& old_app_title, |
| 395 Profile* profile, | 429 Profile* profile, |
| 396 const extensions::Extension* app) { | 430 const extensions::Extension* app) { |
| 397 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 398 | 432 |
| 399 GetInfoForApp(app, | 433 GetInfoForApp(app, |
| 400 profile, | 434 profile, |
| 401 base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title)); | 435 base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title)); |
| 402 } | 436 } |
| 403 | 437 |
| 438 void UpdateShortcutsForAllApps(Profile* profile) { |
| 439 extensions::ExtensionRegistry* registry = |
| 440 extensions::ExtensionRegistry::Get(profile); |
| 441 if (!registry) |
| 442 return; |
| 443 |
| 444 // Get a set of app ids. |
| 445 scoped_ptr<extensions::ExtensionSet> everything = |
| 446 registry->GenerateInstalledExtensionsSet(); |
| 447 std::set<std::string> app_ids; |
| 448 for (extensions::ExtensionSet::const_iterator it = everything->begin(); |
| 449 it != everything->end(); ++it) { |
| 450 if (ShouldCreateShortcutFor(profile, it->get())) |
| 451 app_ids.insert((*it)->id()); |
| 452 } |
| 453 |
| 454 BrowserThread::PostTask( |
| 455 BrowserThread::FILE, |
| 456 FROM_HERE, |
| 457 base::Bind( |
| 458 &web_app::internals::UpdateShortcutsForAllAppsForProfile, |
| 459 profile->GetPath(), app_ids)); |
| 460 } |
| 461 |
| 404 bool IsValidUrl(const GURL& url) { | 462 bool IsValidUrl(const GURL& url) { |
| 405 static const char* const kValidUrlSchemes[] = { | 463 static const char* const kValidUrlSchemes[] = { |
| 406 content::kFileScheme, | 464 content::kFileScheme, |
| 407 content::kFileSystemScheme, | 465 content::kFileSystemScheme, |
| 408 content::kFtpScheme, | 466 content::kFtpScheme, |
| 409 url::kHttpScheme, | 467 url::kHttpScheme, |
| 410 url::kHttpsScheme, | 468 url::kHttpsScheme, |
| 411 extensions::kExtensionScheme, | 469 extensions::kExtensionScheme, |
| 412 }; | 470 }; |
| 413 | 471 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 438 | 496 |
| 439 #if defined(OS_LINUX) | 497 #if defined(OS_LINUX) |
| 440 std::string GetWMClassFromAppName(std::string app_name) { | 498 std::string GetWMClassFromAppName(std::string app_name) { |
| 441 file_util::ReplaceIllegalCharactersInPath(&app_name, '_'); | 499 file_util::ReplaceIllegalCharactersInPath(&app_name, '_'); |
| 442 base::TrimString(app_name, "_", &app_name); | 500 base::TrimString(app_name, "_", &app_name); |
| 443 return app_name; | 501 return app_name; |
| 444 } | 502 } |
| 445 #endif | 503 #endif |
| 446 | 504 |
| 447 } // namespace web_app | 505 } // namespace web_app |
| OLD | NEW |