[merge-m73] Do not bind a raw pointer in AppShortcutManager callbacks.

UpdateShortcutsForAllApps() spawns disk IO tasks to update shortcuts,
then executes a callback on the calling thread when they're all complete.
It's possible for a browser shutdown or profile deletion to occur in
the meantime, in which case we can no longer write to the profile
preferences. Updates will happen again on the next browser startup
if the profile still exists then.

The initial call to UpdateShortcutsForAllApps() is also delayed.

To fix, call these via the WeakPtr that already exists.
AppShortcutManager is a [Profile]KeyedService so will always be
destroyed before the Profile* it's keyed to, nerfing callbacks that have
bound the WeakPtr.

Note UpdateShortcutsForAllApps() writes to user home folders, so can't
be tested on the bots directly. (Things it calls are tested via mocks
bound to WebAppShortcutCreator).

TBR=tapted@chromium.org

(cherry picked from commit 0fce8e3240f217c0ff3cd446878a6d2525b31dd1)

Bug: 925194
Change-Id: If05c45178cda5e7b0322d30c155b79feecddf478
Reviewed-on: https://chromium-review.googlesource.com/c/1442035
Reviewed-by: ccameron <ccameron@chromium.org>
Commit-Queue: Trent Apted <tapted@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#627672}
Reviewed-on: https://chromium-review.googlesource.com/c/1451476
Reviewed-by: Trent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#150}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/chrome/browser/apps/platform_apps/shortcut_manager.cc b/chrome/browser/apps/platform_apps/shortcut_manager.cc
index 39b10d0..21379bb 100644
--- a/chrome/browser/apps/platform_apps/shortcut_manager.cc
+++ b/chrome/browser/apps/platform_apps/shortcut_manager.cc
@@ -57,10 +57,6 @@
                            creation_locations, profile, app);
 }
 
-void SetCurrentAppShortcutsVersion(PrefService* prefs) {
-  prefs->SetInteger(prefs::kAppShortcutsVersion, kCurrentAppShortcutsVersion);
-}
-
 }  // namespace
 
 // static
@@ -73,7 +69,6 @@
 AppShortcutManager::AppShortcutManager(Profile* profile)
     : profile_(profile),
       is_profile_attributes_storage_observer_(false),
-      prefs_(profile->GetPrefs()),
       extension_registry_observer_(this),
       weak_ptr_factory_(this) {
   // Use of g_browser_process requires that we are either on the UI thread, or
@@ -145,17 +140,32 @@
                      profile_path));
 }
 
+void AppShortcutManager::UpdateShortcutsForAllAppsNow() {
+  web_app::UpdateShortcutsForAllApps(
+      profile_,
+      base::BindOnce(&AppShortcutManager::SetCurrentAppShortcutsVersion,
+                     weak_ptr_factory_.GetWeakPtr()));
+}
+
+void AppShortcutManager::SetCurrentAppShortcutsVersion() {
+  profile_->GetPrefs()->SetInteger(prefs::kAppShortcutsVersion,
+                                   kCurrentAppShortcutsVersion);
+}
+
 void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() {
+  // Updating shortcuts writes to user home folders, which can not be done in
+  // tests without exploding disk space usage on the bots.
   if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
     return;
 
-  int last_version = prefs_->GetInteger(prefs::kAppShortcutsVersion);
+  int last_version =
+      profile_->GetPrefs()->GetInteger(prefs::kAppShortcutsVersion);
   if (last_version >= kCurrentAppShortcutsVersion)
     return;
 
   base::PostDelayedTaskWithTraits(
       FROM_HERE, {content::BrowserThread::UI},
-      base::BindOnce(&web_app::UpdateShortcutsForAllApps, profile_,
-                     base::Bind(&SetCurrentAppShortcutsVersion, prefs_)),
+      base::BindOnce(&AppShortcutManager::UpdateShortcutsForAllAppsNow,
+                     weak_ptr_factory_.GetWeakPtr()),
       base::TimeDelta::FromSeconds(kUpdateShortcutsForAllAppsDelay));
 }
diff --git a/chrome/browser/apps/platform_apps/shortcut_manager.h b/chrome/browser/apps/platform_apps/shortcut_manager.h
index 565b6639..dfb6458 100644
--- a/chrome/browser/apps/platform_apps/shortcut_manager.h
+++ b/chrome/browser/apps/platform_apps/shortcut_manager.h
@@ -13,7 +13,6 @@
 #include "extensions/browser/extension_registry_observer.h"
 #include "extensions/common/extension.h"
 
-class PrefService;
 class Profile;
 
 namespace extensions {
@@ -35,8 +34,8 @@
 
   ~AppShortcutManager() override;
 
-  // Updates all shortcuts if kAppShortcutsVersion in prefs is less than
-  // kCurrentAppShortcutsVersion.
+  // Schedules a call to UpdateShortcutsForAllAppsNow() if kAppShortcutsVersion
+  // in prefs is less than kCurrentAppShortcutsVersion.
   void UpdateShortcutsForAllAppsIfNeeded();
 
   // extensions::ExtensionRegistryObserver.
@@ -52,11 +51,12 @@
   void OnProfileWillBeRemoved(const base::FilePath& profile_path) override;
 
  private:
+  void UpdateShortcutsForAllAppsNow();
+  void SetCurrentAppShortcutsVersion();
   void DeleteApplicationShortcuts(const extensions::Extension* extension);
 
   Profile* profile_;
   bool is_profile_attributes_storage_observer_;
-  PrefService* prefs_;
 
   ScopedObserver<extensions::ExtensionRegistry,
                  extensions::ExtensionRegistryObserver>