Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: chrome/browser/extensions/extension_prefs.cc

Issue 5915004: Introduce incognito preference settings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 9 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_prefs.h" 5 #include "chrome/browser/extensions/extension_prefs.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/string_number_conversions.h" 8 #include "base/string_number_conversions.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/extensions/extension_pref_store.h" 10 #include "chrome/browser/extensions/extension_pref_store.h"
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 std::set<std::string>* result) { 134 std::set<std::string>* result) {
135 ExtensionExtent::PatternList patterns = host_extent.patterns(); 135 ExtensionExtent::PatternList patterns = host_extent.patterns();
136 ExtensionExtent::PatternList::const_iterator i; 136 ExtensionExtent::PatternList::const_iterator i;
137 137
138 for (i = patterns.begin(); i != patterns.end(); ++i) 138 for (i = patterns.begin(); i != patterns.end(); ++i)
139 result->insert(i->GetAsString()); 139 result->insert(i->GetAsString());
140 } 140 }
141 141
142 } // namespace 142 } // namespace
143 143
144 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, 144 ExtensionPrefs::ExtensionPrefs(
145 const FilePath& root_dir, 145 PrefService* prefs,
146 ExtensionPrefStore* pref_store) 146 PrefService* incognito_prefs,
147 const FilePath& root_dir,
148 ExtensionPrefStore* pref_store,
149 ExtensionPrefStore* incognito_pref_store)
147 : prefs_(prefs), 150 : prefs_(prefs),
151 incognito_prefs_(incognito_prefs),
148 install_directory_(root_dir), 152 install_directory_(root_dir),
149 pref_store_(pref_store) { 153 pref_store_(pref_store),
154 incognito_pref_store_(incognito_pref_store) {
150 // TODO(asargent) - Remove this in a couple of months. (See comment above 155 // TODO(asargent) - Remove this in a couple of months. (See comment above
151 // CleanupBadExtensionKeys). 156 // CleanupBadExtensionKeys).
152 CleanupBadExtensionKeys(prefs_); 157 CleanupBadExtensionKeys(prefs_);
153 158
154 MakePathsRelative(); 159 MakePathsRelative();
155 160
156 InitPrefStore(); 161 InitPrefStore();
157 } 162 }
158 163
159 ExtensionPrefs::~ExtensionPrefs() {} 164 ExtensionPrefs::~ExtensionPrefs() {}
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 Value* data_value) { 821 Value* data_value) {
817 if (!Extension::IdIsValid(extension_id)) { 822 if (!Extension::IdIsValid(extension_id)) {
818 NOTREACHED() << "Invalid extension_id " << extension_id; 823 NOTREACHED() << "Invalid extension_id " << extension_id;
819 return; 824 return;
820 } 825 }
821 DictionaryValue* extension = GetOrCreateExtensionPref(extension_id); 826 DictionaryValue* extension = GetOrCreateExtensionPref(extension_id);
822 extension->Set(key, data_value); 827 extension->Set(key, data_value);
823 } 828 }
824 829
825 void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { 830 void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) {
826 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); 831 for (int incognito = 0; incognito <= 1; ++incognito) {
danno 2010/12/22 10:48:21 Ouch. This fast-and-loose use of boolean/int conve
battre 2010/12/22 18:34:53 Done. - Though with "boolean incognito" instead of
827 if (dict->HasKey(extension_id)) { 832 PrefService* prefs = incognito ? incognito_prefs_ : prefs_;
828 dict->Remove(extension_id, NULL); 833 if (!prefs) // May be null for unit tests.
829 SavePrefsAndNotify(); 834 continue;
835 DictionaryValue* dict = prefs->GetMutableDictionary(kExtensionsPref);
836 if (dict->HasKey(extension_id)) {
837 dict->Remove(extension_id, NULL);
838 SavePrefsAndNotify();
839 }
830 } 840 }
831 } 841 }
832 842
833 DictionaryValue* ExtensionPrefs::GetOrCreateExtensionPref( 843 DictionaryValue* ExtensionPrefs::GetOrCreateExtensionPref(
834 const std::string& extension_id) { 844 const std::string& extension_id) {
835 DCHECK(Extension::IdIsValid(extension_id)); 845 DCHECK(Extension::IdIsValid(extension_id));
836 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); 846 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref);
837 DictionaryValue* extension = NULL; 847 DictionaryValue* extension = NULL;
838 if (!dict->GetDictionary(extension_id, &extension)) { 848 if (!dict->GetDictionary(extension_id, &extension)) {
839 // Extension pref does not exist, create it. 849 // Extension pref does not exist, create it.
840 extension = new DictionaryValue(); 850 extension = new DictionaryValue();
841 dict->Set(extension_id, extension); 851 dict->Set(extension_id, extension);
842 } 852 }
843 return extension; 853 return extension;
844 } 854 }
845 855
846 DictionaryValue* ExtensionPrefs::GetExtensionPref( 856 DictionaryValue* ExtensionPrefs::GetExtensionPref(
847 const std::string& extension_id) const { 857 const std::string& extension_id) const {
848 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); 858 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref);
849 if (!dict) 859 if (!dict)
850 return NULL; 860 return NULL;
851 DictionaryValue* extension = NULL; 861 DictionaryValue* extension = NULL;
852 dict->GetDictionary(extension_id, &extension); 862 dict->GetDictionary(extension_id, &extension);
853 return extension; 863 return extension;
854 } 864 }
855 865
856 DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( 866 DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs(
857 const std::string& extension_id) const { 867 const std::string& extension_id,
858 DictionaryValue* extension = GetExtensionPref(extension_id); 868 bool incognito) const {
859 if (!extension) { 869 PrefService* source = incognito ? incognito_prefs_ : prefs_;
860 NOTREACHED(); 870 if (source == NULL) // May be null for unit tests.
861 return NULL; 871 return NULL;
872 DictionaryValue* source_dict = source->GetMutableDictionary(kExtensionsPref);
873
874 // The user pref store of incognito_prefs_ should have a kExtensionsPref
875 // dictionary. The request must not fall through to the underlying
876 // PrefService.
877 if (incognito &&
878 source_dict == prefs_->GetMutableDictionary(kExtensionsPref)) {
879 DictionaryValue empty;
880 incognito_prefs_->Set(kExtensionsPref, empty);
881 source_dict = source->GetMutableDictionary(kExtensionsPref);
862 } 882 }
883 DCHECK(!incognito ||
884 source_dict != prefs_->GetMutableDictionary(kExtensionsPref));
885
863 DictionaryValue* preferences = NULL; 886 DictionaryValue* preferences = NULL;
864 extension->GetDictionary(kPrefPreferences, &preferences); 887 std::string key = extension_id + std::string(".") + kPrefPreferences;
888 if (!source_dict->GetDictionary(key, &preferences)) {
889 source_dict->Set(key, new DictionaryValue);
890 bool success = source_dict->GetDictionary(key, &preferences);
891 DCHECK(success);
892 }
865 return preferences; 893 return preferences;
866 } 894 }
867 895
868 // Helper function for GetInstalledExtensionsInfo. 896 // Helper function for GetInstalledExtensionsInfo.
869 static ExtensionInfo* GetInstalledExtensionInfoImpl( 897 static ExtensionInfo* GetInstalledExtensionInfoImpl(
870 DictionaryValue* extension_data, 898 DictionaryValue* extension_data,
871 DictionaryValue::key_iterator extension_id) { 899 DictionaryValue::key_iterator extension_id) {
872 DictionaryValue* ext; 900 DictionaryValue* ext;
873 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) { 901 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) {
874 LOG(WARNING) << "Invalid pref for extension " << *extension_id; 902 LOG(WARNING) << "Invalid pref for extension " << *extension_id;
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
1187 1215
1188 // Collect the unique extension controlled preference keys of all extensions. 1216 // Collect the unique extension controlled preference keys of all extensions.
1189 PrefKeySet ext_controlled_prefs; 1217 PrefKeySet ext_controlled_prefs;
1190 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1218 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1191 ext_id != extension_ids.end(); ++ext_id) { 1219 ext_id != extension_ids.end(); ++ext_id) {
1192 GetExtensionControlledPrefKeys(*ext_id, &ext_controlled_prefs); 1220 GetExtensionControlledPrefKeys(*ext_id, &ext_controlled_prefs);
1193 } 1221 }
1194 1222
1195 // Store winning preference for each extension controlled preference. 1223 // Store winning preference for each extension controlled preference.
1196 UpdatePrefStore(ext_controlled_prefs); 1224 UpdatePrefStore(ext_controlled_prefs);
1197 pref_store_->OnInitializationCompleted(); 1225 if (pref_store_)
1226 pref_store_->OnInitializationCompleted();
1198 } 1227 }
1199 1228
1200 const Value* ExtensionPrefs::GetWinningExtensionControlledPrefValue( 1229 const Value* ExtensionPrefs::GetWinningExtensionControlledPrefValue(
1201 const std::string& key) const { 1230 const std::string& key, bool incognito) const {
1202 Value *winner = NULL; 1231 Value *winner = NULL;
1203 base::Time winners_install_time = base::Time(); 1232 base::Time winners_install_time = base::Time();
1204 1233
1205 ExtensionIdSet extension_ids; 1234 ExtensionIdSet extension_ids;
1206 GetEnabledExtensions(&extension_ids); 1235 GetEnabledExtensions(&extension_ids);
1207 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1236 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1208 ext_id != extension_ids.end(); ++ext_id) { 1237 ext_id != extension_ids.end(); ++ext_id) {
1209 base::Time extension_install_time = GetInstallTime(*ext_id); 1238 base::Time extension_install_time = GetInstallTime(*ext_id);
1210 1239
1211 // We do not need to consider extensions that were installed before the 1240 // We do not need to consider extensions that were installed before the
1212 // most recent extension found that provides the requested preference. 1241 // most recent extension found that provides the requested preference.
1213 if (extension_install_time < winners_install_time) 1242 if (extension_install_time < winners_install_time)
1214 continue; 1243 continue;
1215 1244
1216 DictionaryValue* preferences = GetExtensionControlledPrefs(*ext_id); 1245 for (int i = 0; i <= (incognito ? 1 : 0); ++i) {
1217 Value *value = NULL; 1246 DictionaryValue* preferences = GetExtensionControlledPrefs(*ext_id, !!i);
danno 2010/12/22 10:48:21 whoa. !!!(likes(danno, this)). Is this really more
battre 2010/12/22 18:34:53 Philistine! This is art. ;-) Done.
1218 if (preferences && preferences->GetWithoutPathExpansion(key, &value)) { 1247 Value *value = NULL;
1219 // This extension is more recent than the last one providing this pref. 1248 if (preferences && preferences->GetWithoutPathExpansion(key, &value)) {
1220 winner = value; 1249 // This extension is more recent than the last one providing this pref.
1221 winners_install_time = extension_install_time; 1250 winner = value;
1251 winners_install_time = extension_install_time;
1252 }
1222 } 1253 }
1223 } 1254 }
1224 1255
1225 return winner; 1256 return winner;
1226 } 1257 }
1227 1258
1228 void ExtensionPrefs::UpdatePrefStore( 1259 void ExtensionPrefs::UpdatePrefStore(
1229 const ExtensionPrefs::PrefKeySet& pref_keys) { 1260 const ExtensionPrefs::PrefKeySet& pref_keys) {
1230 for (PrefKeySet::const_iterator i = pref_keys.begin(); 1261 for (PrefKeySet::const_iterator i = pref_keys.begin();
1231 i != pref_keys.end(); ++i) { 1262 i != pref_keys.end(); ++i) {
1232 UpdatePrefStore(*i); 1263 UpdatePrefStore(*i, false); // Regular PrefService.
1264 UpdatePrefStore(*i, true); // Incognito PrefService.
1233 } 1265 }
1234 } 1266 }
1235 1267
1236 void ExtensionPrefs::UpdatePrefStore(const std::string& pref_key) { 1268 void ExtensionPrefs::UpdatePrefStore(const std::string& pref_key,
1237 if (pref_store_ == NULL) 1269 bool incognito) {
1270 scoped_refptr<ExtensionPrefStore> destination =
1271 incognito ? incognito_pref_store_ : pref_store_;
1272 if (destination == NULL) // May be null for unit tests.
1238 return; 1273 return;
1239 const Value* winning_pref_value = 1274 const Value* winning_pref_value =
1240 GetWinningExtensionControlledPrefValue(pref_key); 1275 GetWinningExtensionControlledPrefValue(pref_key, incognito);
1241 1276
1242 if (winning_pref_value) 1277 if (winning_pref_value)
1243 pref_store_->SetExtensionPref(pref_key, winning_pref_value->DeepCopy()); 1278 destination->SetExtensionPref(pref_key, winning_pref_value->DeepCopy());
1244 else 1279 else
1245 pref_store_->RemoveExtensionPref(pref_key); 1280 destination->RemoveExtensionPref(pref_key);
1246 } 1281 }
1247 1282
1248 void ExtensionPrefs::SetExtensionControlledPref(const std::string& extension_id, 1283 void ExtensionPrefs::SetExtensionControlledPref(const std::string& extension_id,
1249 const std::string& pref_key, 1284 const std::string& pref_key,
1285 bool incognito,
1250 Value* value) { 1286 Value* value) {
1251 scoped_ptr<Value> scoped_value(value); 1287 scoped_ptr<Value> scoped_value(value);
1252 DCHECK(pref_service()->FindPreference(pref_key.c_str())) 1288 DCHECK(pref_service()->FindPreference(pref_key.c_str()))
1253 << "Extension controlled preference key " << pref_key 1289 << "Extension controlled preference key " << pref_key
1254 << " not registered."; 1290 << " not registered.";
1291
1292 if (incognito && !incognito_pref_store_) {
1293 LOG(WARNING) << "Ignoring SetExtensionControlledPref for incognito "
1294 << "preferences, missing an incognito_pref_store.";
1295 return;
1296 }
1297
1255 DictionaryValue* extension_preferences = 1298 DictionaryValue* extension_preferences =
1256 GetExtensionControlledPrefs(extension_id); 1299 GetExtensionControlledPrefs(extension_id, incognito);
1257
1258 if (extension_preferences == NULL) { // May be pruned when writing to disk.
1259 DictionaryValue* extension = GetExtensionPref(extension_id);
1260 if (extension == NULL) {
1261 LOG(ERROR) << "Extension preference for " << extension_id << " undefined";
1262 return;
1263 }
1264 extension_preferences = new DictionaryValue;
1265 extension->Set(kPrefPreferences, extension_preferences);
1266 }
1267 1300
1268 Value* oldValue = NULL; 1301 Value* oldValue = NULL;
1269 extension_preferences->GetWithoutPathExpansion(pref_key, &oldValue); 1302 extension_preferences->GetWithoutPathExpansion(pref_key, &oldValue);
1270 bool modified = !Value::Equals(oldValue, scoped_value.get()); 1303 bool modified = !Value::Equals(oldValue, scoped_value.get());
1271 if (!modified) 1304 if (!modified)
1272 return; 1305 return;
1273 1306
1274 if (scoped_value.get() == NULL) 1307 if (scoped_value.get() == NULL)
1275 extension_preferences->RemoveWithoutPathExpansion(pref_key, NULL); 1308 extension_preferences->RemoveWithoutPathExpansion(pref_key, NULL);
1276 else 1309 else
1277 extension_preferences->SetWithoutPathExpansion(pref_key, 1310 extension_preferences->SetWithoutPathExpansion(pref_key,
1278 scoped_value.release()); 1311 scoped_value.release());
1279 pref_service()->ScheduleSavePersistentPrefs(); 1312 if (!incognito)
1313 pref_service()->ScheduleSavePersistentPrefs();
1280 1314
1281 UpdatePrefStore(pref_key); 1315 if (!incognito)
1316 UpdatePrefStore(pref_key, false); // Update regular prefs.
1317 UpdatePrefStore(pref_key, true); // Update incognito prefs.
1282 } 1318 }
1283 1319
1284 void ExtensionPrefs::GetExtensionControlledPrefKeys( 1320 void ExtensionPrefs::GetExtensionControlledPrefKeys(
1285 const std::string& extension_id, PrefKeySet *out) const { 1321 const std::string& extension_id, PrefKeySet *out) const {
1286 DCHECK(out != NULL); 1322 DCHECK(out != NULL);
1287 DictionaryValue* ext_prefs = GetExtensionControlledPrefs(extension_id); 1323 for (int incognito = 0; incognito <= 1; ++incognito) {
1288 if (ext_prefs) { 1324 DictionaryValue* ext_prefs = GetExtensionControlledPrefs(extension_id,
1289 for (DictionaryValue::key_iterator i = ext_prefs->begin_keys(); 1325 !!incognito);
danno 2010/12/22 10:48:21 !!!(still_likes(danno, this))
battre 2010/12/22 18:34:53 Done.
1290 i != ext_prefs->end_keys(); ++i) { 1326 if (ext_prefs) {
1291 out->insert(*i); 1327 for (DictionaryValue::key_iterator i = ext_prefs->begin_keys();
1328 i != ext_prefs->end_keys(); ++i) {
1329 out->insert(*i);
1330 }
1292 } 1331 }
1293 } 1332 }
1294 } 1333 }
1295 1334
1296 // static 1335 // static
1297 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { 1336 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) {
1298 prefs->RegisterDictionaryPref(kExtensionsPref); 1337 prefs->RegisterDictionaryPref(kExtensionsPref);
1299 prefs->RegisterListPref(kExtensionToolbar); 1338 prefs->RegisterListPref(kExtensionToolbar);
1300 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); 1339 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1);
1301 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); 1340 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate);
1302 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); 1341 prefs->RegisterListPref(prefs::kExtensionInstallAllowList);
1303 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); 1342 prefs->RegisterListPref(prefs::kExtensionInstallDenyList);
1304 prefs->RegisterListPref(prefs::kExtensionInstallForceList); 1343 prefs->RegisterListPref(prefs::kExtensionInstallForceList);
1305 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); 1344 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */);
1306 } 1345 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698