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

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

Issue 8198003: Convert app_launch_index and page_index from int to StringOrdinal. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 2 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_number_conversions.h" 7 #include "base/string_number_conversions.h"
8 #include "base/string_util.h" 8 #include "base/string_util.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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 86
87 // A preference set by the web store to indicate login information for 87 // A preference set by the web store to indicate login information for
88 // purchased apps. 88 // purchased apps.
89 const char kWebStoreLogin[] = "extensions.webstore_login"; 89 const char kWebStoreLogin[] = "extensions.webstore_login";
90 90
91 // A preference set by the the NTP to persist the desired launch container type 91 // A preference set by the the NTP to persist the desired launch container type
92 // used for apps. 92 // used for apps.
93 const char kPrefLaunchType[] = "launchType"; 93 const char kPrefLaunchType[] = "launchType";
94 94
95 // A preference determining the order of which the apps appear on the NTP. 95 // A preference determining the order of which the apps appear on the NTP.
96 const char kPrefAppLaunchIndex[] = "app_launcher_index"; 96 const char kPrefAppLaunchIndexDepreciated[] = "app_launcher_index";
Mihai Parparita -not on Chrome 2011/10/07 23:15:12 Typo (use "Deprecated" instead of "Depreciated").
csharp 2011/11/11 18:13:32 Done.
97 const char kPrefAppLaunchIndex[] = "app_launcher_index_str";
97 98
98 // A preference determining the page on which an app appears in the NTP. 99 // A preference determining the page on which an app appears in the NTP.
99 const char kPrefPageIndex[] = "page_index"; 100 const char kPrefPageIndexDepreciated[] = "page_index";
101 const char kPrefPageIndex[] = "page_index_str";
100 102
101 // A preference specifying if the user dragged the app on the NTP. 103 // A preference specifying if the user dragged the app on the NTP.
102 const char kPrefUserDraggedApp[] = "user_dragged_app_ntp"; 104 const char kPrefUserDraggedApp[] = "user_dragged_app_ntp";
103 105
104 // A preference for storing extra data sent in update checks for an extension. 106 // A preference for storing extra data sent in update checks for an extension.
105 const char kUpdateUrlData[] = "update_url_data"; 107 const char kUpdateUrlData[] = "update_url_data";
106 108
107 // Whether the browser action is visible in the toolbar. 109 // Whether the browser action is visible in the toolbar.
108 const char kBrowserActionVisible[] = "browser_action_visible"; 110 const char kBrowserActionVisible[] = "browser_action_visible";
109 111
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 const DictionaryValue* ext = GetExtensionPref(extension_id); 393 const DictionaryValue* ext = GetExtensionPref(extension_id);
392 ListValue* out = NULL; 394 ListValue* out = NULL;
393 if (!ext || !ext->GetList(pref_key, &out)) 395 if (!ext || !ext->GetList(pref_key, &out))
394 return false; 396 return false;
395 if (out_value) 397 if (out_value)
396 *out_value = out; 398 *out_value = out;
397 399
398 return true; 400 return true;
399 } 401 }
400 402
403 bool ExtensionPrefs::ReadExtensionPrefString(
404 const std::string& extension_id, const std::string& pref_key,
405 std::string* out_value) {
406 const DictionaryValue* ext = GetExtensionPref(extension_id);
407 if (!ext || !ext->GetString(pref_key, out_value))
408 return false;
409
410 return true;
411 }
412
401 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet( 413 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet(
402 const std::string& extension_id, 414 const std::string& extension_id,
403 const std::string& pref_key, 415 const std::string& pref_key,
404 URLPatternSet* result, 416 URLPatternSet* result,
405 int valid_schemes) { 417 int valid_schemes) {
406 const ListValue* value = NULL; 418 const ListValue* value = NULL;
407 if (!ReadExtensionPrefList(extension_id, pref_key, &value)) 419 if (!ReadExtensionPrefList(extension_id, pref_key, &value))
408 return false; 420 return false;
409 421
410 result->ClearPatterns(); 422 result->ClearPatterns();
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) { 763 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) {
752 UpdateExtensionPref( 764 UpdateExtensionPref(
753 *ext_id, explicit_hosts, hosts->DeepCopy()); 765 *ext_id, explicit_hosts, hosts->DeepCopy());
754 766
755 // We can get rid of the old one by setting it to an empty list. 767 // We can get rid of the old one by setting it to an empty list.
756 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue()); 768 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue());
757 } 769 }
758 } 770 }
759 } 771 }
760 772
773 void ExtensionPrefs::MigrateAppIndex(const ExtensionIdSet& extension_ids) {
774 for (ExtensionIdSet::const_iterator ext_id = extension_ids.begin();
775 ext_id != extension_ids.end(); ++ext_id) {
776 std::string index;
777 if (ReadExtensionPrefString(*ext_id, kPrefPageIndex, &index)) {
Mihai Parparita -not on Chrome 2011/10/07 23:15:12 I'm confused about this migration code, since this
csharp 2011/11/11 18:13:32 Done.
778 int old_index;
779 if (ReadExtensionPrefInteger(*ext_id,
780 kPrefPageIndexDepreciated,
781 &old_index)) {
782 SetPageIndex(*ext_id, base::IntToString(old_index));
783 }
784 }
785
786 if (ReadExtensionPrefString(*ext_id, kPrefAppLaunchIndex, &index)) {
787 int old_index;
788 if (ReadExtensionPrefInteger(*ext_id,
789 kPrefPageIndexDepreciated,
790 &old_index)) {
791 SetAppLaunchIndex(*ext_id, base::IntToString(old_index));
792 }
793 }
794 }
795 }
796
761 ExtensionPermissionSet* ExtensionPrefs::GetGrantedPermissions( 797 ExtensionPermissionSet* ExtensionPrefs::GetGrantedPermissions(
762 const std::string& extension_id) { 798 const std::string& extension_id) {
763 CHECK(Extension::IdIsValid(extension_id)); 799 CHECK(Extension::IdIsValid(extension_id));
764 return ReadExtensionPrefPermissionSet(extension_id, kPrefGrantedPermissions); 800 return ReadExtensionPrefPermissionSet(extension_id, kPrefGrantedPermissions);
765 } 801 }
766 802
767 void ExtensionPrefs::AddGrantedPermissions( 803 void ExtensionPrefs::AddGrantedPermissions(
768 const std::string& extension_id, 804 const std::string& extension_id,
769 const ExtensionPermissionSet* permissions) { 805 const ExtensionPermissionSet* permissions) {
770 CHECK(Extension::IdIsValid(extension_id)); 806 CHECK(Extension::IdIsValid(extension_id));
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 iter != extension_ids.end(); ++iter) { 986 iter != extension_ids.end(); ++iter) {
951 toolbar_order->Append(new StringValue(*iter)); 987 toolbar_order->Append(new StringValue(*iter));
952 } 988 }
953 SavePrefs(); 989 SavePrefs();
954 } 990 }
955 991
956 void ExtensionPrefs::OnExtensionInstalled( 992 void ExtensionPrefs::OnExtensionInstalled(
957 const Extension* extension, 993 const Extension* extension,
958 Extension::State initial_state, 994 Extension::State initial_state,
959 bool from_webstore, 995 bool from_webstore,
960 int page_index) { 996 std::string page_index) {
961 const std::string& id = extension->id(); 997 const std::string& id = extension->id();
962 CHECK(Extension::IdIsValid(id)); 998 CHECK(Extension::IdIsValid(id));
963 ScopedExtensionPrefUpdate update(prefs_, id); 999 ScopedExtensionPrefUpdate update(prefs_, id);
964 DictionaryValue* extension_dict = update.Get(); 1000 DictionaryValue* extension_dict = update.Get();
965 const base::Time install_time = GetCurrentTime(); 1001 const base::Time install_time = GetCurrentTime();
966 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state)); 1002 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state));
967 extension_dict->Set(kPrefLocation, 1003 extension_dict->Set(kPrefLocation,
968 Value::CreateIntegerValue(extension->location())); 1004 Value::CreateIntegerValue(extension->location()));
969 extension_dict->Set(kPrefFromWebStore, 1005 extension_dict->Set(kPrefFromWebStore,
970 Value::CreateBooleanValue(from_webstore)); 1006 Value::CreateBooleanValue(from_webstore));
(...skipping 11 matching lines...) Expand all
982 extension->path()); 1018 extension->path());
983 extension_dict->Set(kPrefPath, Value::CreateStringValue(path)); 1019 extension_dict->Set(kPrefPath, Value::CreateStringValue(path));
984 // We store prefs about LOAD extensions, but don't cache their manifest 1020 // We store prefs about LOAD extensions, but don't cache their manifest
985 // since it may change on disk. 1021 // since it may change on disk.
986 if (extension->location() != Extension::LOAD) { 1022 if (extension->location() != Extension::LOAD) {
987 extension_dict->Set(kPrefManifest, 1023 extension_dict->Set(kPrefManifest,
988 extension->manifest_value()->DeepCopy()); 1024 extension->manifest_value()->DeepCopy());
989 } 1025 }
990 1026
991 if (extension->is_app()) { 1027 if (extension->is_app()) {
992 if (page_index == -1) 1028 if (page_index == extension_misc::kUnsetIndex)
993 page_index = GetNaturalAppPageIndex(); 1029 page_index = GetNaturalAppPageIndex();
994 extension_dict->Set(kPrefPageIndex, 1030 extension_dict->Set(kPrefPageIndex,
995 Value::CreateIntegerValue(page_index)); 1031 Value::CreateStringValue(page_index));
996 extension_dict->Set(kPrefAppLaunchIndex, 1032 extension_dict->Set(kPrefAppLaunchIndex,
997 Value::CreateIntegerValue(GetNextAppLaunchIndex(page_index))); 1033 Value::CreateStringValue(GetNextAppLaunchIndex(page_index)));
998 } 1034 }
999 extension_pref_value_map_->RegisterExtension( 1035 extension_pref_value_map_->RegisterExtension(
1000 id, install_time, initial_state == Extension::ENABLED); 1036 id, install_time, initial_state == Extension::ENABLED);
1001 content_settings_store_->RegisterExtension( 1037 content_settings_store_->RegisterExtension(
1002 id, install_time, initial_state == Extension::ENABLED); 1038 id, install_time, initial_state == Extension::ENABLED);
1003 } 1039 }
1004 1040
1005 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, 1041 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id,
1006 const Extension::Location& location, 1042 const Extension::Location& location,
1007 bool external_uninstall) { 1043 bool external_uninstall) {
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
1320 return true; 1356 return true;
1321 } 1357 }
1322 return false; 1358 return false;
1323 } 1359 }
1324 1360
1325 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) { 1361 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) {
1326 prefs_->SetString(kWebStoreLogin, login); 1362 prefs_->SetString(kWebStoreLogin, login);
1327 SavePrefs(); 1363 SavePrefs();
1328 } 1364 }
1329 1365
1330 int ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) { 1366 std::string ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) {
1331 int value; 1367 std::string value;
1332 if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value)) 1368 if (ReadExtensionPrefString(extension_id, kPrefAppLaunchIndex, &value))
1333 return value; 1369 return value;
1334 1370
1335 return -1; 1371 return extension_misc::kUnsetIndex;
1336 } 1372 }
1337 1373
1338 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, 1374 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id,
1339 int index) { 1375 const std::string& index) {
1340 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, 1376 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex,
1341 Value::CreateIntegerValue(index)); 1377 Value::CreateStringValue(index));
1342 } 1378 }
1343 1379
1344 int ExtensionPrefs::GetNextAppLaunchIndex(int on_page) { 1380 std::string ExtensionPrefs::GetNextAppLaunchIndex(std::string on_page) {
1345 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); 1381 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1346 if (!extensions) 1382 if (!extensions)
1347 return 0; 1383 return "0";
1348 1384
1349 int max_value = -1; 1385 std::string max_value = extension_misc::kUnsetIndex;
1350 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); 1386 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys();
1351 extension_id != extensions->end_keys(); ++extension_id) { 1387 extension_id != extensions->end_keys(); ++extension_id) {
1352 int value = GetAppLaunchIndex(*extension_id); 1388 std::string value = GetAppLaunchIndex(*extension_id);
1353 int page = GetPageIndex(*extension_id); 1389 std::string page = GetPageIndex(*extension_id);
1354 if (page == on_page && value > max_value) 1390
1391 int max_value_int, value_int;
1392 base::StringToInt(max_value, &max_value_int);
1393 base::StringToInt(value, &value_int);
1394 if (page == on_page && max_value_int < value_int)
1355 max_value = value; 1395 max_value = value;
1356 } 1396 }
1357 return max_value + 1; 1397
1398 int new_value;
1399 base::StringToInt(max_value, &new_value);
akalin 2011/10/07 17:31:16 So all I'm seeing are conversions from int->string
1400 ++new_value;
1401
1402 return base::IntToString(new_value);
1358 } 1403 }
1359 1404
1360 int ExtensionPrefs::GetNaturalAppPageIndex() { 1405 std::string ExtensionPrefs::GetNaturalAppPageIndex() {
1361 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); 1406 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1362 if (!extensions) 1407 if (!extensions)
1363 return 0; 1408 return "0";
1364 1409
1365 std::map<int, int> page_counts; 1410 std::map<std::string, int> page_counts;
1366 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); 1411 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys();
1367 extension_id != extensions->end_keys(); ++extension_id) { 1412 extension_id != extensions->end_keys(); ++extension_id) {
1368 int page_index = GetPageIndex(*extension_id); 1413 std::string page_index = GetPageIndex(*extension_id);
1369 if (page_index >= 0) 1414 page_counts[page_index] = page_counts[page_index] + 1;
1370 page_counts[page_index] = page_counts[page_index] + 1;
1371 } 1415 }
1372 for (int i = 0; ; i++) { 1416
1373 std::map<int, int>::const_iterator it = page_counts.find(i); 1417 std::map<std::string, int>::const_iterator it = page_counts.begin();
1374 if (it == page_counts.end() || it->second < kNaturalAppPageSize) 1418 for (int i = 0; it != page_counts.end(); ++it, ++i) {
1375 return i; 1419 if (it->second < kNaturalAppPageSize) {
1420 return base::IntToString(i);
1421 }
1376 } 1422 }
1423
1424 // Add a new page
1425 return base::IntToString(page_counts.size());
1377 } 1426 }
1378 1427
1379 void ExtensionPrefs::SetAppLauncherOrder( 1428 void ExtensionPrefs::SetAppLauncherOrder(
1380 const std::vector<std::string>& extension_ids) { 1429 const std::vector<std::string>& extension_ids) {
1381 for (size_t i = 0; i < extension_ids.size(); ++i) 1430 for (size_t i = 0; i < extension_ids.size(); ++i) {
1382 SetAppLaunchIndex(extension_ids.at(i), i); 1431 SetAppLaunchIndex(extension_ids.at(i), base::IntToString(i));
1432 }
1383 1433
1384 NotificationService::current()->Notify( 1434 NotificationService::current()->Notify(
1385 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, 1435 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED,
1386 Source<ExtensionPrefs>(this), 1436 Source<ExtensionPrefs>(this),
1387 NotificationService::NoDetails()); 1437 NotificationService::NoDetails());
1388 } 1438 }
1389 1439
1390 int ExtensionPrefs::GetPageIndex(const std::string& extension_id) { 1440 std::string ExtensionPrefs::GetPageIndex(const std::string& extension_id) {
1391 int value = -1; 1441 std::string value;
1392 ReadExtensionPrefInteger(extension_id, kPrefPageIndex, &value); 1442 if (ReadExtensionPrefString(extension_id, kPrefPageIndex, &value)) {
1393 return value; 1443 return value;
1444 }
1445
1446 return extension_misc::kUnsetIndex;
1394 } 1447 }
1395 1448
1396 void ExtensionPrefs::SetPageIndex(const std::string& extension_id, int index) { 1449 void ExtensionPrefs::SetPageIndex(const std::string& extension_id,
1450 const std::string& page_index) {
1397 UpdateExtensionPref(extension_id, kPrefPageIndex, 1451 UpdateExtensionPref(extension_id, kPrefPageIndex,
1398 Value::CreateIntegerValue(index)); 1452 Value::CreateStringValue(page_index));
1399 } 1453 }
1400 1454
1401 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { 1455 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) {
1402 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); 1456 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp);
1403 } 1457 }
1404 1458
1405 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { 1459 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) {
1406 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, 1460 UpdateExtensionPref(extension_id, kPrefUserDraggedApp,
1407 Value::CreateBooleanValue(true)); 1461 Value::CreateBooleanValue(true));
1408 } 1462 }
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1536 // are pruned when persisting the preferences to disk). 1590 // are pruned when persisting the preferences to disk).
1537 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1591 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1538 ext_id != extension_ids.end(); ++ext_id) { 1592 ext_id != extension_ids.end(); ++ext_id) {
1539 ScopedExtensionPrefUpdate update(prefs_, *ext_id); 1593 ScopedExtensionPrefUpdate update(prefs_, *ext_id);
1540 // This creates an empty dictionary if none is stored. 1594 // This creates an empty dictionary if none is stored.
1541 update.Get(); 1595 update.Get();
1542 } 1596 }
1543 1597
1544 FixMissingPrefs(extension_ids); 1598 FixMissingPrefs(extension_ids);
1545 MigratePermissions(extension_ids); 1599 MigratePermissions(extension_ids);
1600 MigrateAppIndex(extension_ids);
1546 1601
1547 // Store extension controlled preference values in the 1602 // Store extension controlled preference values in the
1548 // |extension_pref_value_map_|, which then informs the subscribers 1603 // |extension_pref_value_map_|, which then informs the subscribers
1549 // (ExtensionPrefStores) about the winning values. 1604 // (ExtensionPrefStores) about the winning values.
1550 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1605 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1551 ext_id != extension_ids.end(); ++ext_id) { 1606 ext_id != extension_ids.end(); ++ext_id) {
1552 extension_pref_value_map_->RegisterExtension( 1607 extension_pref_value_map_->RegisterExtension(
1553 *ext_id, 1608 *ext_id,
1554 GetInstallTime(*ext_id), 1609 GetInstallTime(*ext_id),
1555 !IsExtensionDisabled(*ext_id)); 1610 !IsExtensionDisabled(*ext_id));
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1710 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, 1765 prefs->RegisterListPref(prefs::kExtensionInstallAllowList,
1711 PrefService::UNSYNCABLE_PREF); 1766 PrefService::UNSYNCABLE_PREF);
1712 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, 1767 prefs->RegisterListPref(prefs::kExtensionInstallDenyList,
1713 PrefService::UNSYNCABLE_PREF); 1768 PrefService::UNSYNCABLE_PREF);
1714 prefs->RegisterListPref(prefs::kExtensionInstallForceList, 1769 prefs->RegisterListPref(prefs::kExtensionInstallForceList,
1715 PrefService::UNSYNCABLE_PREF); 1770 PrefService::UNSYNCABLE_PREF);
1716 prefs->RegisterStringPref(kWebStoreLogin, 1771 prefs->RegisterStringPref(kWebStoreLogin,
1717 std::string() /* default_value */, 1772 std::string() /* default_value */,
1718 PrefService::UNSYNCABLE_PREF); 1773 PrefService::UNSYNCABLE_PREF);
1719 } 1774 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_prefs.h ('k') | chrome/browser/extensions/extension_prefs_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698