1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
// Copyright 2017 The Chromium Authors
// 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 <optional>
#include "base/observer_list.h"
#include "base/version.h"
#include "content/public/browser/browser_context.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_prefs.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)
: browser_context_(browser_context) {
extension_registry_observation_.Observe(
ExtensionRegistry::Get(browser_context_));
}
LazyEventDispatchUtil::~LazyEventDispatchUtil() = default;
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::Value::Dict* info = prefs->ReadPrefAsDict(
extension_id, kPrefPendingOnInstalledEventDispatchInfo);
if (!info) {
return false;
}
const std::string* previous_version_string =
info->FindString(kPrefPreviousVersion);
// |previous_version_string| can be empty.
*previous_version = base::Version(
previous_version_string ? *previous_version_string : std::string());
return true;
}
void LazyEventDispatchUtil::RemovePendingOnInstallInfoFromPref(
const ExtensionId& extension_id) {
ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_);
DCHECK(prefs);
prefs->UpdateExtensionPref(
extension_id, kPrefPendingOnInstalledEventDispatchInfo, std::nullopt);
}
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.
base::Value::Dict pending_on_install_info;
base::Version previous_version = ExtensionRegistry::Get(browser_context_)
->GetStoredVersion(extension->id());
pending_on_install_info.Set(kPrefPreviousVersion,
previous_version.IsValid()
? previous_version.GetString()
: std::string());
prefs->UpdateExtensionPref(extension->id(),
kPrefPendingOnInstalledEventDispatchInfo,
base::Value(std::move(pending_on_install_info)));
}
} // namespace extensions
|