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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
// Copyright 2014 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/extension_pref_value_map.h"
#include <utility>
#include "base/memory/ptr_util.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/prefs/pref_value_map.h"
#include "extensions/common/api/types.h"
#include "extensions/common/extension_id.h"
struct ExtensionPrefValueMap::ExtensionEntry {
// Installation time of the extension.
base::Time install_time;
// Whether extension is enabled in the profile.
bool enabled;
// Whether the extension has access to the incognito profile.
bool incognito_enabled;
// Extension controlled preferences for the regular profile.
PrefValueMap regular_profile_preferences;
// Extension controlled preferences that should *only* apply to the regular
// profile.
PrefValueMap regular_only_profile_preferences;
// Persistent extension controlled preferences for the incognito profile,
// empty for regular profile ExtensionPrefStore.
PrefValueMap incognito_profile_preferences_persistent;
// Session only extension controlled preferences for the incognito profile.
// These preferences are deleted when the incognito profile is destroyed.
PrefValueMap incognito_profile_preferences_session_only;
};
ExtensionPrefValueMap::ExtensionPrefValueMap() : destroyed_(false) {
}
ExtensionPrefValueMap::~ExtensionPrefValueMap() {
if (!destroyed_) {
NotifyOfDestruction();
destroyed_ = true;
}
}
void ExtensionPrefValueMap::Shutdown() {
NotifyOfDestruction();
destroyed_ = true;
}
void ExtensionPrefValueMap::SetExtensionPref(const std::string& ext_id,
const std::string& key,
ChromeSettingScope scope,
base::Value value) {
PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, scope);
if (prefs->SetValue(key, std::move(value))) {
NotifyPrefValueChanged(key);
}
}
void ExtensionPrefValueMap::RemoveExtensionPref(const std::string& ext_id,
const std::string& key,
ChromeSettingScope scope) {
PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, scope);
if (prefs->RemoveValue(key)) {
NotifyPrefValueChanged(key);
}
}
bool ExtensionPrefValueMap::CanExtensionControlPref(
const extensions::ExtensionId& extension_id,
const std::string& pref_key,
bool incognito) const {
auto ext = entries_.find(extension_id);
if (ext == entries_.end()) {
NOTREACHED() << "Extension " << extension_id
<< " is not registered but accesses pref " << pref_key
<< " (incognito: " << incognito << ")."
<< " http://crbug.com/454513";
}
if (incognito && !ext->second->incognito_enabled) {
return false;
}
auto winner = GetEffectivePrefValueController(pref_key, incognito, nullptr);
if (winner == entries_.end()) {
return true;
}
return winner->second->install_time <= ext->second->install_time;
}
void ExtensionPrefValueMap::ClearAllIncognitoSessionOnlyPreferences() {
typedef std::set<std::string> KeySet;
KeySet deleted_keys;
for (const auto& entry : entries_) {
PrefValueMap& inc_prefs =
entry.second->incognito_profile_preferences_session_only;
for (const auto& pref : inc_prefs)
deleted_keys.insert(pref.first);
inc_prefs.Clear();
}
for (const auto& key : deleted_keys)
NotifyPrefValueChanged(key);
}
bool ExtensionPrefValueMap::DoesExtensionControlPref(
const extensions::ExtensionId& extension_id,
const std::string& pref_key,
bool* from_incognito) const {
bool incognito = (from_incognito != nullptr);
auto winner =
GetEffectivePrefValueController(pref_key, incognito, from_incognito);
if (winner == entries_.end()) {
return false;
}
return winner->first == extension_id;
}
void ExtensionPrefValueMap::RegisterExtension(const std::string& ext_id,
const base::Time& install_time,
bool is_enabled,
bool is_incognito_enabled) {
auto& entry = entries_[ext_id];
if (!entry) {
entry = std::make_unique<ExtensionEntry>();
entry->install_time = install_time;
}
entry->enabled = is_enabled;
entry->incognito_enabled = is_incognito_enabled;
}
void ExtensionPrefValueMap::UnregisterExtension(const std::string& ext_id) {
auto i = entries_.find(ext_id);
if (i == entries_.end()) {
return;
}
std::set<std::string> keys; // keys set by this extension
GetExtensionControlledKeys(*(i->second.get()), &keys);
entries_.erase(i);
NotifyPrefValueChanged(keys);
}
void ExtensionPrefValueMap::SetExtensionState(const std::string& ext_id,
bool is_enabled) {
ExtensionEntryMap::const_iterator i = entries_.find(ext_id);
// This may happen when sync sets the extension state for an
// extension that is not installed.
if (i == entries_.end()) {
return;
}
if (i->second->enabled == is_enabled) {
return;
}
std::set<std::string> keys; // keys set by this extension
GetExtensionControlledKeys(*(i->second), &keys);
i->second->enabled = is_enabled;
NotifyPrefValueChanged(keys);
}
void ExtensionPrefValueMap::SetExtensionIncognitoState(
const std::string& ext_id,
bool is_incognito_enabled) {
ExtensionEntryMap::const_iterator i = entries_.find(ext_id);
// This may happen when sync sets the extension state for an
// extension that is not installed.
if (i == entries_.end()) {
return;
}
if (i->second->incognito_enabled == is_incognito_enabled) {
return;
}
std::set<std::string> keys; // keys set by this extension
GetExtensionControlledKeys(*(i->second), &keys);
i->second->incognito_enabled = is_incognito_enabled;
NotifyPrefValueChanged(keys);
}
PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap(
const std::string& ext_id,
ChromeSettingScope scope) {
ExtensionEntryMap::const_iterator i = entries_.find(ext_id);
CHECK(i != entries_.end());
switch (scope) {
case ChromeSettingScope::kRegular:
return &i->second->regular_profile_preferences;
case ChromeSettingScope::kRegularOnly:
return &i->second->regular_only_profile_preferences;
case ChromeSettingScope::kIncognitoPersistent:
return &i->second->incognito_profile_preferences_persistent;
case ChromeSettingScope::kIncognitoSessionOnly:
return &i->second->incognito_profile_preferences_session_only;
case ChromeSettingScope::kNone:
break;
}
NOTREACHED();
}
const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap(
const std::string& ext_id,
ChromeSettingScope scope) const {
auto i = entries_.find(ext_id);
CHECK(i != entries_.end());
switch (scope) {
case ChromeSettingScope::kRegular:
return &i->second->regular_profile_preferences;
case ChromeSettingScope::kRegularOnly:
return &i->second->regular_only_profile_preferences;
case ChromeSettingScope::kIncognitoPersistent:
return &i->second->incognito_profile_preferences_persistent;
case ChromeSettingScope::kIncognitoSessionOnly:
return &i->second->incognito_profile_preferences_session_only;
case ChromeSettingScope::kNone:
break;
}
NOTREACHED();
}
void ExtensionPrefValueMap::GetExtensionControlledKeys(
const ExtensionEntry& entry,
std::set<std::string>* out) const {
PrefValueMap::const_iterator i;
const PrefValueMap& regular_prefs = entry.regular_profile_preferences;
for (i = regular_prefs.begin(); i != regular_prefs.end(); ++i)
out->insert(i->first);
const PrefValueMap& regular_only_prefs =
entry.regular_only_profile_preferences;
for (i = regular_only_prefs.begin(); i != regular_only_prefs.end(); ++i)
out->insert(i->first);
const PrefValueMap& inc_prefs_pers =
entry.incognito_profile_preferences_persistent;
for (i = inc_prefs_pers.begin(); i != inc_prefs_pers.end(); ++i)
out->insert(i->first);
const PrefValueMap& inc_prefs_session =
entry.incognito_profile_preferences_session_only;
for (i = inc_prefs_session.begin(); i != inc_prefs_session.end(); ++i)
out->insert(i->first);
}
const base::Value* ExtensionPrefValueMap::GetEffectivePrefValue(
const std::string& key,
bool incognito,
bool* from_incognito) const {
auto winner = GetEffectivePrefValueController(key, incognito, from_incognito);
if (winner == entries_.end()) {
return nullptr;
}
const base::Value* value = nullptr;
const std::string& ext_id = winner->first;
// First search for incognito session only preferences.
if (incognito) {
DCHECK(winner->second->incognito_enabled);
const PrefValueMap* prefs = GetExtensionPrefValueMap(
ext_id, ChromeSettingScope::kIncognitoSessionOnly);
prefs->GetValue(key, &value);
if (value) {
return value;
}
// If no incognito session only preference exists, fall back to persistent
// incognito preference.
prefs = GetExtensionPrefValueMap(ext_id,
ChromeSettingScope::kIncognitoPersistent);
prefs->GetValue(key, &value);
if (value) {
return value;
}
} else {
// Regular-only preference.
const PrefValueMap* prefs =
GetExtensionPrefValueMap(ext_id, ChromeSettingScope::kRegularOnly);
prefs->GetValue(key, &value);
if (value) {
return value;
}
}
// Regular preference.
const PrefValueMap* prefs =
GetExtensionPrefValueMap(ext_id, ChromeSettingScope::kRegular);
prefs->GetValue(key, &value);
return value;
}
ExtensionPrefValueMap::ExtensionEntryMap::const_iterator
ExtensionPrefValueMap::GetEffectivePrefValueController(
const std::string& key,
bool incognito,
bool* from_incognito) const {
auto winner = entries_.cend();
base::Time winners_install_time;
for (auto i = entries_.cbegin(); i != entries_.cend(); ++i) {
const std::string& ext_id = i->first;
const base::Time& install_time = i->second->install_time;
const bool enabled = i->second->enabled;
const bool incognito_enabled = i->second->incognito_enabled;
if (!enabled) {
continue;
}
if (install_time < winners_install_time) {
continue;
}
if (incognito && !incognito_enabled) {
continue;
}
const base::Value* value = nullptr;
const PrefValueMap* prefs =
GetExtensionPrefValueMap(ext_id, ChromeSettingScope::kRegular);
if (prefs->GetValue(key, &value)) {
winner = i;
winners_install_time = install_time;
if (from_incognito) {
*from_incognito = false;
}
}
if (!incognito) {
prefs =
GetExtensionPrefValueMap(ext_id, ChromeSettingScope::kRegularOnly);
if (prefs->GetValue(key, &value)) {
winner = i;
winners_install_time = install_time;
if (from_incognito) {
*from_incognito = false;
}
}
// Ignore the following prefs, because they're incognito-only.
continue;
}
prefs = GetExtensionPrefValueMap(ext_id,
ChromeSettingScope::kIncognitoPersistent);
if (prefs->GetValue(key, &value)) {
winner = i;
winners_install_time = install_time;
if (from_incognito) {
*from_incognito = true;
}
}
prefs = GetExtensionPrefValueMap(ext_id,
ChromeSettingScope::kIncognitoSessionOnly);
if (prefs->GetValue(key, &value)) {
winner = i;
winners_install_time = install_time;
if (from_incognito) {
*from_incognito = true;
}
}
}
return winner;
}
void ExtensionPrefValueMap::AddObserver(
ExtensionPrefValueMap::Observer* observer) {
observers_.AddObserver(observer);
// Collect all currently used keys and notify the new observer.
std::set<std::string> keys;
ExtensionEntryMap::const_iterator i;
for (i = entries_.begin(); i != entries_.end(); ++i)
GetExtensionControlledKeys(*(i->second), &keys);
std::set<std::string>::const_iterator j;
for (j = keys.begin(); j != keys.end(); ++j)
observer->OnPrefValueChanged(*j);
}
void ExtensionPrefValueMap::RemoveObserver(
ExtensionPrefValueMap::Observer* observer) {
observers_.RemoveObserver(observer);
}
std::string ExtensionPrefValueMap::GetExtensionControllingPref(
const std::string& pref_key) const {
auto winner = GetEffectivePrefValueController(pref_key, false, nullptr);
if (winner == entries_.end()) {
return std::string();
}
return winner->first;
}
void ExtensionPrefValueMap::NotifyInitializationCompleted() {
for (auto& observer : observers_)
observer.OnInitializationCompleted();
}
void ExtensionPrefValueMap::NotifyPrefValueChanged(
const std::set<std::string>& keys) {
for (const auto& key : keys)
NotifyPrefValueChanged(key);
}
void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) {
for (auto& observer : observers_)
observer.OnPrefValueChanged(key);
}
void ExtensionPrefValueMap::NotifyOfDestruction() {
for (auto& observer : observers_)
observer.OnExtensionPrefValueMapDestruction();
}
|