Chromium Code Reviews| Index: extensions/browser/events/lazy_event_dispatch_util.cc |
| diff --git a/extensions/browser/events/lazy_event_dispatch_util.cc b/extensions/browser/events/lazy_event_dispatch_util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f20704a9889670233d7248a662c19ebb76c69221 |
| --- /dev/null |
| +++ b/extensions/browser/events/lazy_event_dispatch_util.cc |
| @@ -0,0 +1,121 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "extensions/browser/events/lazy_event_dispatch_util.h" |
| + |
| +#include "base/version.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "extensions/browser/event_router.h" |
| +#include "extensions/browser/extension_prefs.h" |
| +#include "extensions/browser/extension_registry.h" |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +// Previously installed version number. |
| +const char kPrefPreviousVersion[] = "previous_version"; |
| + |
| +// A preference key storing the information about an extension that was |
| +// installed but not loaded. We keep the pending info here so that we can send |
| +// chrome.runtime.onInstalled event during the extension load. |
| +const char kPrefPendingOnInstalledEventDispatchInfo[] = |
| + "pending_on_installed_event_dispatch_info"; |
| + |
| +} // namespace |
| + |
| +LazyEventDispatchUtil::LazyEventDispatchUtil( |
| + content::BrowserContext* browser_context, |
| + ExtensionRegistry* extension_registry) |
| + : browser_context_(browser_context), extension_registry_observer_(this) { |
| + extension_registry_observer_.Add(extension_registry); |
| +} |
| + |
| +LazyEventDispatchUtil::~LazyEventDispatchUtil() {} |
| + |
| +void LazyEventDispatchUtil::AddObserver(Observer* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void LazyEventDispatchUtil::RemoveObserver(Observer* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +void LazyEventDispatchUtil::OnExtensionLoaded( |
| + content::BrowserContext* browser_context, |
| + const Extension* extension) { |
| + base::Version previous_version; |
| + if (ReadPendingOnInstallInfoFromPref(extension->id(), &previous_version)) { |
| + for (auto& observer : observers_) { |
| + observer.OnExtensionInstalledAndLoaded(browser_context_, extension, |
| + previous_version); |
| + } |
| + RemovePendingOnInstallInfoFromPref(extension->id()); |
| + } |
| +} |
| + |
| +void LazyEventDispatchUtil::OnExtensionUninstalled( |
| + content::BrowserContext* browser_context, |
| + const Extension* extension, |
| + UninstallReason reason) { |
| + RemovePendingOnInstallInfoFromPref(extension->id()); |
| +} |
| + |
| +void LazyEventDispatchUtil::OnExtensionWillBeInstalled( |
| + content::BrowserContext* browser_context, |
| + const Extension* extension, |
| + bool is_update, |
| + const std::string& old_name) { |
| + StorePendingOnInstallInfoToPref(extension); |
| +} |
| + |
| +bool LazyEventDispatchUtil::ReadPendingOnInstallInfoFromPref( |
| + const ExtensionId& extension_id, |
| + base::Version* previous_version) { |
| + ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); |
| + DCHECK(prefs); |
| + |
| + const base::DictionaryValue* info = nullptr; |
| + if (!prefs->ReadPrefAsDictionary( |
| + extension_id, kPrefPendingOnInstalledEventDispatchInfo, &info)) { |
| + return false; |
| + } |
| + |
| + std::string previous_version_string; |
| + info->GetString(kPrefPreviousVersion, &previous_version_string); |
| + // |previous_version_string| can be empty. |
| + *previous_version = base::Version(previous_version_string); |
| + return true; |
| +} |
| + |
| +void LazyEventDispatchUtil::RemovePendingOnInstallInfoFromPref( |
| + const ExtensionId& extension_id) { |
| + ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); |
| + DCHECK(prefs); |
| + |
| + prefs->UpdateExtensionPref(extension_id, |
| + kPrefPendingOnInstalledEventDispatchInfo, nullptr); |
| +} |
| + |
| +void LazyEventDispatchUtil::StorePendingOnInstallInfoToPref( |
| + const Extension* extension) { |
| + ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); |
| + DCHECK(prefs); |
| + |
| + // |pending_on_install_info| currently only contains a version string. Instead |
| + // of making the pref hold a plain string, we store it as a dictionary value |
| + // so that we can add more stuff to it in the future if necessary. |
| + std::unique_ptr<base::DictionaryValue> pending_on_install_info( |
| + new base::DictionaryValue()); |
|
Devlin
2017/05/23 21:30:36
MakeUnique
lazyboy
2017/05/23 23:19:08
Done.
|
| + base::Version previous_version = ExtensionRegistry::Get(browser_context_) |
| + ->GetStoredVersion(extension->id()); |
| + pending_on_install_info->SetString( |
| + kPrefPreviousVersion, |
| + previous_version.IsValid() ? previous_version.GetString() : ""); |
| + prefs->UpdateExtensionPref(extension->id(), |
| + kPrefPendingOnInstalledEventDispatchInfo, |
| + std::move(pending_on_install_info)); |
| +} |
| + |
| +} // namespace extensions |