Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/browser/web_applications/web_app.cc

Issue 263403002: Replace OnceOffCreateShortcuts with UpdateShortcutsForAllAppsIfNeeded. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove once-off creation logic. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/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"
29 #include "extensions/browser/extension_system.h"
25 #include "extensions/common/constants.h" 30 #include "extensions/common/constants.h"
26 #include "extensions/common/extension.h" 31 #include "extensions/common/extension.h"
32 #include "extensions/common/extension_set.h"
27 #include "extensions/common/manifest_handlers/icons_handler.h" 33 #include "extensions/common/manifest_handlers/icons_handler.h"
28 #include "grit/theme_resources.h" 34 #include "grit/theme_resources.h"
29 #include "skia/ext/image_operations.h" 35 #include "skia/ext/image_operations.h"
30 #include "third_party/skia/include/core/SkBitmap.h" 36 #include "third_party/skia/include/core/SkBitmap.h"
31 #include "ui/base/resource/resource_bundle.h" 37 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/image/image.h" 38 #include "ui/gfx/image/image.h"
33 #include "ui/gfx/image/image_family.h" 39 #include "ui/gfx/image/image_family.h"
34 #include "ui/gfx/image/image_skia.h" 40 #include "ui/gfx/image/image_skia.h"
35 41
36 #if defined(OS_WIN) 42 #if defined(OS_WIN)
(...skipping 24 matching lines...) Expand all
61 #endif 67 #endif
62 68
63 #if defined(TOOLKIT_VIEWS) 69 #if defined(TOOLKIT_VIEWS)
64 // Predicator for sorting images from largest to smallest. 70 // Predicator for sorting images from largest to smallest.
65 bool IconPrecedes(const WebApplicationInfo::IconInfo& left, 71 bool IconPrecedes(const WebApplicationInfo::IconInfo& left,
66 const WebApplicationInfo::IconInfo& right) { 72 const WebApplicationInfo::IconInfo& right) {
67 return left.width < right.width; 73 return left.width < right.width;
68 } 74 }
69 #endif 75 #endif
70 76
77 bool ShouldCreateShortcutFor(const extensions::Extension* extension) {
78 return extension->is_platform_app() &&
79 extension->location() != extensions::Manifest::COMPONENT &&
80 extension->ShouldDisplayInAppLauncher();
81 }
82
71 bool CreateShortcutsWithInfoOnFileThread( 83 bool CreateShortcutsWithInfoOnFileThread(
72 web_app::ShortcutCreationReason reason, 84 web_app::ShortcutCreationReason reason,
73 const web_app::ShortcutLocations& locations, 85 const web_app::ShortcutLocations& locations,
74 const web_app::ShortcutInfo& shortcut_info, 86 const web_app::ShortcutInfo& shortcut_info,
75 const extensions::FileHandlersInfo& file_handlers_info) { 87 const extensions::FileHandlersInfo& file_handlers_info) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
77 89
78 base::FilePath shortcut_data_dir = 90 base::FilePath shortcut_data_dir =
79 web_app::GetWebAppDataDirectory(shortcut_info.profile_path, 91 web_app::GetWebAppDataDirectory(shortcut_info.profile_path,
80 shortcut_info.extension_id, 92 shortcut_info.extension_id,
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 // The following string is used to build the directory name for 237 // The following string is used to build the directory name for
226 // shortcuts to chrome applications (the kind which are installed 238 // shortcuts to chrome applications (the kind which are installed
227 // from a CRX). Application shortcuts to URLs use the {host}_{path} 239 // from a CRX). Application shortcuts to URLs use the {host}_{path}
228 // for the name of this directory. Hosts can't include an underscore. 240 // for the name of this directory. Hosts can't include an underscore.
229 // By starting this string with an underscore, we ensure that there 241 // By starting this string with an underscore, we ensure that there
230 // are no naming conflicts. 242 // are no naming conflicts.
231 static const char* kCrxAppPrefix = "_crx_"; 243 static const char* kCrxAppPrefix = "_crx_";
232 244
233 namespace internals { 245 namespace internals {
234 246
247 void CallForProfileAndAppId(
248 const base::FilePath& profile_path,
249 const std::string app_id,
250 const ShortcutOperationCallback& callback) {
251 ProfileManager* profile_manager = g_browser_process->profile_manager();
252 Profile* profile = profile_manager->GetProfileByPath(profile_path);
253 if (!profile || !profile_manager->IsValidProfile(profile))
254 return;
255
256 extensions::ExtensionRegistry* registry =
257 extensions::ExtensionRegistry::Get(profile);
258 const extensions::Extension* extension = registry->GetExtensionById(
259 app_id, extensions::ExtensionRegistry::ENABLED);
260 if (!extension || !extension->is_platform_app())
261 return;
262
263 callback.Run(profile, extension);
264 }
265
235 base::FilePath GetSanitizedFileName(const base::string16& name) { 266 base::FilePath GetSanitizedFileName(const base::string16& name) {
236 #if defined(OS_WIN) 267 #if defined(OS_WIN)
237 base::string16 file_name = name; 268 base::string16 file_name = name;
238 #else 269 #else
239 std::string file_name = base::UTF16ToUTF8(name); 270 std::string file_name = base::UTF16ToUTF8(name);
240 #endif 271 #endif
241 file_util::ReplaceIllegalCharactersInPath(&file_name, '_'); 272 file_util::ReplaceIllegalCharactersInPath(&file_name, '_');
242 return base::FilePath(file_name); 273 return base::FilePath(file_name);
243 } 274 }
244 275
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 base::IgnoreResult(&web_app::internals::CreateShortcutsOnFileThread), 427 base::IgnoreResult(&web_app::internals::CreateShortcutsOnFileThread),
397 reason, locations, shortcut_info)); 428 reason, locations, shortcut_info));
398 } 429 }
399 430
400 void CreateShortcuts(ShortcutCreationReason reason, 431 void CreateShortcuts(ShortcutCreationReason reason,
401 const ShortcutLocations& locations, 432 const ShortcutLocations& locations,
402 Profile* profile, 433 Profile* profile,
403 const extensions::Extension* app) { 434 const extensions::Extension* app) {
404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
405 436
437 if (!ShouldCreateShortcutFor(app))
438 return;
439
406 GetInfoForApp(app, 440 GetInfoForApp(app,
407 profile, 441 profile,
408 base::Bind(&CreateShortcutsWithInfo, reason, locations)); 442 base::Bind(&CreateShortcutsWithInfo, reason, locations));
409 } 443 }
410 444
411 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { 445 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) {
412 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 446 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
413 447
414 BrowserThread::PostTask( 448 BrowserThread::PostTask(
415 BrowserThread::FILE, 449 BrowserThread::FILE,
416 FROM_HERE, 450 FROM_HERE,
417 base::Bind(&DeleteShortcutsOnFileThread, 451 base::Bind(&DeleteShortcutsOnFileThread,
418 ShortcutInfoForExtensionAndProfile(app, profile))); 452 ShortcutInfoForExtensionAndProfile(app, profile)));
419 } 453 }
420 454
421 void UpdateAllShortcuts(const base::string16& old_app_title, 455 void UpdateAllShortcuts(const base::string16& old_app_title,
422 Profile* profile, 456 Profile* profile,
423 const extensions::Extension* app) { 457 const extensions::Extension* app) {
424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 458 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
425 459
426 GetInfoForApp(app, 460 GetInfoForApp(app,
427 profile, 461 profile,
428 base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title)); 462 base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title));
429 } 463 }
430 464
465 void UpdateShortcutsForAllApps(Profile* profile) {
466 // Check if extension system/service are available. They might not be in
467 // tests.
468 extensions::ExtensionSystem* extension_system;
469 ExtensionServiceInterface* extension_service;
470 if (!(extension_system = extensions::ExtensionSystem::Get(profile)) ||
Matt Giuca 2014/05/23 07:01:57 Uh, != ?
jackhou1 2014/05/26 07:57:37 Get() and extension_service() both can return NULL
Matt Giuca 2014/05/26 08:07:36 Ohh.. wow, I totally didn't see that it was = inst
jackhou1 2014/05/27 01:03:57 Ok, changed to something simpler.
471 !(extension_service = extension_system->extension_service()))
472 return;
473
474 // Get a set of app ids.
475 const extensions::ExtensionSet* apps = extension_service->extensions();
476 std::set<std::string> app_ids;
477 for (extensions::ExtensionSet::const_iterator it = apps->begin();
478 it != apps->end(); ++it) {
479 if (ShouldCreateShortcutFor(it->get()))
480 app_ids.insert((*it)->id());
481 }
482
483 BrowserThread::PostTask(
484 BrowserThread::FILE,
485 FROM_HERE,
486 base::Bind(
487 &web_app::internals::UpdateShortcutsForAllAppsForProfile,
488 profile->GetPath(), app_ids));
489 }
490
431 bool IsValidUrl(const GURL& url) { 491 bool IsValidUrl(const GURL& url) {
432 static const char* const kValidUrlSchemes[] = { 492 static const char* const kValidUrlSchemes[] = {
433 content::kFileScheme, 493 content::kFileScheme,
434 content::kFileSystemScheme, 494 content::kFileSystemScheme,
435 content::kFtpScheme, 495 content::kFtpScheme,
436 url::kHttpScheme, 496 url::kHttpScheme,
437 url::kHttpsScheme, 497 url::kHttpsScheme,
438 extensions::kExtensionScheme, 498 extensions::kExtensionScheme,
439 }; 499 };
440 500
(...skipping 24 matching lines...) Expand all
465 525
466 #if defined(OS_LINUX) 526 #if defined(OS_LINUX)
467 std::string GetWMClassFromAppName(std::string app_name) { 527 std::string GetWMClassFromAppName(std::string app_name) {
468 file_util::ReplaceIllegalCharactersInPath(&app_name, '_'); 528 file_util::ReplaceIllegalCharactersInPath(&app_name, '_');
469 base::TrimString(app_name, "_", &app_name); 529 base::TrimString(app_name, "_", &app_name);
470 return app_name; 530 return app_name;
471 } 531 }
472 #endif 532 #endif
473 533
474 } // namespace web_app 534 } // namespace web_app
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698