OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/browser/events/lazy_event_dispatch_util.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/version.h" |
| 9 #include "content/public/browser/browser_context.h" |
| 10 #include "extensions/browser/event_router.h" |
| 11 #include "extensions/browser/extension_prefs.h" |
| 12 #include "extensions/browser/extension_registry.h" |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Previously installed version number. |
| 19 const char kPrefPreviousVersion[] = "previous_version"; |
| 20 |
| 21 // A preference key storing the information about an extension that was |
| 22 // installed but not loaded. We keep the pending info here so that we can send |
| 23 // chrome.runtime.onInstalled event during the extension load. |
| 24 const char kPrefPendingOnInstalledEventDispatchInfo[] = |
| 25 "pending_on_installed_event_dispatch_info"; |
| 26 |
| 27 } // namespace |
| 28 |
| 29 LazyEventDispatchUtil::LazyEventDispatchUtil( |
| 30 content::BrowserContext* browser_context) |
| 31 : browser_context_(browser_context), extension_registry_observer_(this) { |
| 32 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 33 } |
| 34 |
| 35 LazyEventDispatchUtil::~LazyEventDispatchUtil() {} |
| 36 |
| 37 void LazyEventDispatchUtil::AddObserver(Observer* observer) { |
| 38 observers_.AddObserver(observer); |
| 39 } |
| 40 |
| 41 void LazyEventDispatchUtil::RemoveObserver(Observer* observer) { |
| 42 observers_.RemoveObserver(observer); |
| 43 } |
| 44 |
| 45 void LazyEventDispatchUtil::OnExtensionLoaded( |
| 46 content::BrowserContext* browser_context, |
| 47 const Extension* extension) { |
| 48 base::Version previous_version; |
| 49 if (ReadPendingOnInstallInfoFromPref(extension->id(), &previous_version)) { |
| 50 for (auto& observer : observers_) { |
| 51 observer.OnExtensionInstalledAndLoaded(browser_context_, extension, |
| 52 previous_version); |
| 53 } |
| 54 RemovePendingOnInstallInfoFromPref(extension->id()); |
| 55 } |
| 56 } |
| 57 |
| 58 void LazyEventDispatchUtil::OnExtensionUninstalled( |
| 59 content::BrowserContext* browser_context, |
| 60 const Extension* extension, |
| 61 UninstallReason reason) { |
| 62 RemovePendingOnInstallInfoFromPref(extension->id()); |
| 63 } |
| 64 |
| 65 void LazyEventDispatchUtil::OnExtensionWillBeInstalled( |
| 66 content::BrowserContext* browser_context, |
| 67 const Extension* extension, |
| 68 bool is_update, |
| 69 const std::string& old_name) { |
| 70 StorePendingOnInstallInfoToPref(extension); |
| 71 } |
| 72 |
| 73 bool LazyEventDispatchUtil::ReadPendingOnInstallInfoFromPref( |
| 74 const ExtensionId& extension_id, |
| 75 base::Version* previous_version) { |
| 76 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); |
| 77 DCHECK(prefs); |
| 78 |
| 79 const base::DictionaryValue* info = nullptr; |
| 80 if (!prefs->ReadPrefAsDictionary( |
| 81 extension_id, kPrefPendingOnInstalledEventDispatchInfo, &info)) { |
| 82 return false; |
| 83 } |
| 84 |
| 85 std::string previous_version_string; |
| 86 info->GetString(kPrefPreviousVersion, &previous_version_string); |
| 87 // |previous_version_string| can be empty. |
| 88 *previous_version = base::Version(previous_version_string); |
| 89 return true; |
| 90 } |
| 91 |
| 92 void LazyEventDispatchUtil::RemovePendingOnInstallInfoFromPref( |
| 93 const ExtensionId& extension_id) { |
| 94 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); |
| 95 DCHECK(prefs); |
| 96 |
| 97 prefs->UpdateExtensionPref(extension_id, |
| 98 kPrefPendingOnInstalledEventDispatchInfo, nullptr); |
| 99 } |
| 100 |
| 101 void LazyEventDispatchUtil::StorePendingOnInstallInfoToPref( |
| 102 const Extension* extension) { |
| 103 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); |
| 104 DCHECK(prefs); |
| 105 |
| 106 // |pending_on_install_info| currently only contains a version string. Instead |
| 107 // of making the pref hold a plain string, we store it as a dictionary value |
| 108 // so that we can add more stuff to it in the future if necessary. |
| 109 auto pending_on_install_info = base::MakeUnique<base::DictionaryValue>(); |
| 110 base::Version previous_version = ExtensionRegistry::Get(browser_context_) |
| 111 ->GetStoredVersion(extension->id()); |
| 112 pending_on_install_info->SetString(kPrefPreviousVersion, |
| 113 previous_version.IsValid() |
| 114 ? previous_version.GetString() |
| 115 : std::string()); |
| 116 prefs->UpdateExtensionPref(extension->id(), |
| 117 kPrefPendingOnInstalledEventDispatchInfo, |
| 118 std::move(pending_on_install_info)); |
| 119 } |
| 120 |
| 121 } // namespace extensions |
OLD | NEW |