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
|
// Copyright (c) 2010 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 "chrome/browser/extensions/extension_pref_store.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_service.h"
ExtensionPrefStore::ExtensionPrefStore(Profile* profile,
PrefNotifier::PrefStoreType type)
: prefs_(new DictionaryValue()),
profile_(profile),
type_(type) {
RegisterObservers();
}
ExtensionPrefStore::~ExtensionPrefStore() {
STLDeleteElements(&extension_stack_);
notification_registrar_.RemoveAll();
}
void ExtensionPrefStore::InstallExtensionPref(Extension* extension,
const char* new_pref_path,
Value* new_pref_value) {
ExtensionStack::iterator i;
for (i = extension_stack_.begin(); i != extension_stack_.end(); ++i) {
if ((*i)->extension == extension)
break;
}
// If this extension is not already in the stack, add it. Otherwise, update
// or add the value of this preference, but don't change the extension's
// position in the stack. We store the extension even if this preference
// isn't registered with our PrefService, so that the ordering of extensions
// is consistent among all local-state and user ExtensionPrefStores.
PrefService* pref_service = GetPrefService();
// The pref_service may be NULL in unit testing.
bool is_registered_pref = (pref_service == NULL ||
pref_service->FindPreference(new_pref_path) != NULL);
PrefValueMap* pref_values;
if (i == extension_stack_.end()) {
pref_values = new PrefValueMap();
if (is_registered_pref)
(*pref_values)[new_pref_path] = new_pref_value;
ExtensionPrefs* extension_prefs = new ExtensionPrefs(extension,
pref_values);
extension_stack_.push_front(extension_prefs);
} else if (is_registered_pref) {
pref_values = (*i)->pref_values;
delete (*pref_values)[new_pref_path];
(*pref_values)[new_pref_path] = new_pref_value;
}
// Apply the preference to our local |prefs_| store.
UpdateOnePref(new_pref_path);
}
void ExtensionPrefStore::UninstallExtension(Extension* extension) {
// Remove this extension from the stack.
for (ExtensionStack::iterator i = extension_stack_.begin();
i != extension_stack_.end(); ++i) {
if ((*i)->extension == extension) {
scoped_ptr<ExtensionPrefs> to_be_deleted(*i);
extension_stack_.erase(i);
UpdatePrefs(to_be_deleted->pref_values);
return;
}
}
}
void ExtensionPrefStore::GetExtensionIDs(std::vector<std::string>* result) {
for (ExtensionStack::iterator i = extension_stack_.begin();
i != extension_stack_.end(); ++i) {
(*result).push_back((*i)->extension->id());
}
}
// This could be sped up by keeping track of which extension currently controls
// a given preference, among other optimizations. But probably fewer than 10
// installed extensions will be trying to control any preferences, so stick
// with this simpler algorithm until it causes a problem.
void ExtensionPrefStore::UpdateOnePref(const char* path) {
scoped_ptr<Value> old_value;
PrefService* pref_service = GetPrefService();
// There are at least two PrefServices, one for local state and one for
// user prefs. (See browser_main.cc.) Different preferences are registered
// in each; if this one doesn't have the desired pref registered, we ignore
// it and let the other one handle it.
// The pref_service may be NULL in unit testing.
if (pref_service) {
const PrefService::Preference* pref = pref_service->FindPreference(path);
if (!pref)
return;
old_value.reset(pref->GetValue()->DeepCopy());
}
// DictionaryValue::Set complains if a key is overwritten with the same
// value, so delete it first.
prefs_->Remove(path, NULL);
// Find the first extension that wants to set this pref and use its value.
for (ExtensionStack::iterator ext_iter = extension_stack_.begin();
ext_iter != extension_stack_.end(); ++ext_iter) {
PrefValueMap::iterator value_iter = (*ext_iter)->pref_values->find(path);
if (value_iter != (*ext_iter)->pref_values->end()) {
prefs_->Set(path, (*value_iter).second->DeepCopy());
break;
}
}
if (pref_service) {
pref_service->pref_notifier()->OnPreferenceSet(
path, type_, old_value.get());
}
}
void ExtensionPrefStore::UpdatePrefs(const PrefValueMap* pref_values) {
if (!pref_values)
return;
for (PrefValueMap::const_iterator i = pref_values->begin();
i != pref_values->end(); ++i) {
UpdateOnePref(i->first);
}
}
PrefService* ExtensionPrefStore::GetPrefService() {
if (profile_)
return profile_->GetPrefs();
return g_browser_process->local_state();
}
void ExtensionPrefStore::RegisterObservers() {
notification_registrar_.Add(this,
NotificationType::EXTENSION_PREF_CHANGED,
NotificationService::AllSources());
notification_registrar_.Add(this,
NotificationType::EXTENSION_UNLOADED,
NotificationService::AllSources());
}
void ExtensionPrefStore::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSION_PREF_CHANGED: {
Profile* extension_profile = Source<Profile>(source).ptr();
// The ExtensionPrefStore for the local state watches all profiles.
if (!profile_ || profile_ == extension_profile) {
ExtensionPrefStore::ExtensionPrefDetails* data =
Details<ExtensionPrefStore::ExtensionPrefDetails>(details).ptr();
InstallExtensionPref(data->first, data->second.first,
data->second.second);
}
break;
}
case NotificationType::EXTENSION_UNLOADED: {
Profile* extension_profile = Source<Profile>(source).ptr();
Extension* extension = Details<Extension>(details).ptr();
// The ExtensionPrefStore for the local state watches all profiles.
if (profile_ == NULL || profile_ == extension_profile)
UninstallExtension(extension);
break;
}
default: {
NOTREACHED();
}
}
}
ExtensionPrefStore::ExtensionPrefs::ExtensionPrefs(Extension* extension,
PrefValueMap* values) : extension(extension), pref_values(values) {}
ExtensionPrefStore::ExtensionPrefs::~ExtensionPrefs() {
STLDeleteValues(pref_values);
delete pref_values;
}
|