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

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: Adding constness and comments Created 9 years, 1 month 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 103
104 // A preference set by the web store to indicate login information for 104 // A preference set by the web store to indicate login information for
105 // purchased apps. 105 // purchased apps.
106 const char kWebStoreLogin[] = "extensions.webstore_login"; 106 const char kWebStoreLogin[] = "extensions.webstore_login";
107 107
108 // A preference set by the the NTP to persist the desired launch container type 108 // A preference set by the the NTP to persist the desired launch container type
109 // used for apps. 109 // used for apps.
110 const char kPrefLaunchType[] = "launchType"; 110 const char kPrefLaunchType[] = "launchType";
111 111
112 // A preference determining the order of which the apps appear on the NTP. 112 // A preference determining the order of which the apps appear on the NTP.
113 const char kPrefAppLaunchIndex[] = "app_launcher_index"; 113 const char kPrefAppLaunchIndexDeprecated[] = "app_launcher_index";
114 const char kPrefAppLaunchIndex[] = "app_launcher_index_string_ordinal";
114 115
115 // A preference determining the page on which an app appears in the NTP. 116 // A preference determining the page on which an app appears in the NTP.
116 const char kPrefPageIndex[] = "page_index"; 117 const char kPrefPageIndexDeprecated[] = "page_index";
118 const char kPrefPageIndex[] = "page_index_string_ordinal";
117 119
118 // A preference specifying if the user dragged the app on the NTP. 120 // A preference specifying if the user dragged the app on the NTP.
119 const char kPrefUserDraggedApp[] = "user_dragged_app_ntp"; 121 const char kPrefUserDraggedApp[] = "user_dragged_app_ntp";
120 122
121 // A preference for storing extra data sent in update checks for an extension. 123 // A preference for storing extra data sent in update checks for an extension.
122 const char kUpdateUrlData[] = "update_url_data"; 124 const char kUpdateUrlData[] = "update_url_data";
123 125
124 // Whether the browser action is visible in the toolbar. 126 // Whether the browser action is visible in the toolbar.
125 const char kBrowserActionVisible[] = "browser_action_visible"; 127 const char kBrowserActionVisible[] = "browser_action_visible";
126 128
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 const std::string extension_id_; 234 const std::string extension_id_;
233 const std::string incognito_or_regular_path_; 235 const std::string incognito_or_regular_path_;
234 236
235 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionControlledPrefUpdate); 237 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionControlledPrefUpdate);
236 }; 238 };
237 239
238 std::string JoinPrefs(std::string parent, const char* child) { 240 std::string JoinPrefs(std::string parent, const char* child) {
239 return parent + "." + child; 241 return parent + "." + child;
240 } 242 }
241 243
244 // A simple structure used when sorting extension based on the order they
Finnur 2011/11/17 14:59:25 extension -> apps?
csharp 2011/11/17 19:51:58 Done.
245 // appear on the NTP. This is used by the code that migrates the data
246 // from integers to StringOrdinals.
247 struct ExtensionNTPOldPosition {
248 const std::string* extension_id;
249 const int page_index;
250 const int app_launch_index;
251
252 ExtensionNTPOldPosition(const std::string& ext_id, int page, int app_launch)
253 : extension_id(&ext_id),
254 page_index(page),
255 app_launch_index(app_launch) {}
256 };
257
258 struct SortByNTPOldPosition {
259 bool operator() (const ExtensionNTPOldPosition& lhs,
260 const ExtensionNTPOldPosition& rhs) const {
261 if (lhs.page_index != rhs.page_index)
262 return lhs.page_index < rhs.page_index;
263
264 return lhs.app_launch_index < rhs.app_launch_index;
265 }
266 };
267
242 } // namespace 268 } // namespace
243 269
244 ExtensionPrefs::ExtensionPrefs( 270 ExtensionPrefs::ExtensionPrefs(
245 PrefService* prefs, 271 PrefService* prefs,
246 const FilePath& root_dir, 272 const FilePath& root_dir,
247 ExtensionPrefValueMap* extension_pref_value_map) 273 ExtensionPrefValueMap* extension_pref_value_map)
248 : prefs_(prefs), 274 : prefs_(prefs),
249 install_directory_(root_dir), 275 install_directory_(root_dir),
250 extension_pref_value_map_(extension_pref_value_map), 276 extension_pref_value_map_(extension_pref_value_map),
251 content_settings_store_(new ExtensionContentSettingsStore()) { 277 content_settings_store_(new ExtensionContentSettingsStore()) {
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 const DictionaryValue* ext = GetExtensionPref(extension_id); 434 const DictionaryValue* ext = GetExtensionPref(extension_id);
409 ListValue* out = NULL; 435 ListValue* out = NULL;
410 if (!ext || !ext->GetList(pref_key, &out)) 436 if (!ext || !ext->GetList(pref_key, &out))
411 return false; 437 return false;
412 if (out_value) 438 if (out_value)
413 *out_value = out; 439 *out_value = out;
414 440
415 return true; 441 return true;
416 } 442 }
417 443
444 bool ExtensionPrefs::ReadExtensionPrefString(
445 const std::string& extension_id, const std::string& pref_key,
446 std::string* out_value) const {
447 const DictionaryValue* ext = GetExtensionPref(extension_id);
448
449 if (!ext || !ext->GetString(pref_key, out_value))
450 return false;
451
452 return true;
453 }
454
418 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet( 455 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet(
419 const std::string& extension_id, 456 const std::string& extension_id,
420 const std::string& pref_key, 457 const std::string& pref_key,
421 URLPatternSet* result, 458 URLPatternSet* result,
422 int valid_schemes) { 459 int valid_schemes) {
423 const ListValue* value = NULL; 460 const ListValue* value = NULL;
424 if (!ReadExtensionPrefList(extension_id, pref_key, &value)) 461 if (!ReadExtensionPrefList(extension_id, pref_key, &value))
425 return false; 462 return false;
426 463
427 result->ClearPatterns(); 464 result->ClearPatterns();
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) { 888 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) {
852 UpdateExtensionPref( 889 UpdateExtensionPref(
853 *ext_id, explicit_hosts, hosts->DeepCopy()); 890 *ext_id, explicit_hosts, hosts->DeepCopy());
854 891
855 // We can get rid of the old one by setting it to an empty list. 892 // We can get rid of the old one by setting it to an empty list.
856 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue()); 893 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue());
857 } 894 }
858 } 895 }
859 } 896 }
860 897
898 void ExtensionPrefs::MigrateAppIndex(const ExtensionIdSet& extension_ids) {
899 if (extension_ids.begin() == extension_ids.end()) {
900 return;
901 }
Finnur 2011/11/17 14:59:25 No braces for single line if clauses.
csharp 2011/11/17 19:51:58 Done.
902
903 // Create a sorted set of all the extensions that need either their page
904 // or launch index converted.
905 std::set<ExtensionNTPOldPosition, SortByNTPOldPosition> apps_to_convert;
906 for (ExtensionIdSet::const_iterator ext_id = extension_ids.begin();
907 ext_id != extension_ids.end(); ++ext_id) {
908 int old_page_index(0);
909 int old_app_launch_index(0);
Finnur 2011/11/17 14:59:25 nit: Even though you can, we never initialize ints
csharp 2011/11/17 19:51:58 Done.
910
911 bool has_old_values = ReadExtensionPrefInteger(*ext_id,
912 kPrefPageIndexDeprecated,
913 &old_page_index);
914 has_old_values |= ReadExtensionPrefInteger(*ext_id,
915 kPrefAppLaunchIndexDeprecated,
916 &old_app_launch_index);
917
918 if (has_old_values) {
919 // Its ok to use both values, even if they weren't read, because this
920 // only affects the sorting, which can handle apps with the default
921 // integer values.
922 apps_to_convert.insert(ExtensionNTPOldPosition(
923 *ext_id, old_page_index, old_app_launch_index));
924 }
925 }
926
927 if (apps_to_convert.empty())
928 return;
929
930 // Create the new values and remove the old preferences.
931 for (std::set<ExtensionNTPOldPosition, SortByNTPOldPosition>::iterator ext =
932 apps_to_convert.begin(); ext != apps_to_convert.end(); ++ext) {
933 int old_page_index;
934 if (ReadExtensionPrefInteger(*(ext->extension_id),
935 kPrefPageIndexDeprecated,
936 &old_page_index)) {
937 SetPageIndex(*(ext->extension_id),
938 PageIndexAsStringOrdinal(old_page_index));
939 UpdateExtensionPref(*(ext->extension_id), kPrefPageIndexDeprecated, NULL);
940 }
941
942 int old_app_launch_index;
943 if (ReadExtensionPrefInteger(*(ext->extension_id),
944 kPrefAppLaunchIndexDeprecated,
945 &old_app_launch_index)) {
Finnur 2011/11/17 14:59:25 old_page_index is read and used above. old_app_lau
csharp 2011/11/17 19:51:58 I don't think the order can change, because set th
946 StringOrdinal page = GetPageIndex(*(ext->extension_id));
947 SetAppLaunchIndex(*(ext->extension_id),
948 GetNextAppLaunchIndex(page));
949 UpdateExtensionPref(
950 *(ext->extension_id), kPrefAppLaunchIndexDeprecated, NULL);
951 }
952 }
953 }
954
861 ExtensionPermissionSet* ExtensionPrefs::GetGrantedPermissions( 955 ExtensionPermissionSet* ExtensionPrefs::GetGrantedPermissions(
862 const std::string& extension_id) { 956 const std::string& extension_id) {
863 CHECK(Extension::IdIsValid(extension_id)); 957 CHECK(Extension::IdIsValid(extension_id));
864 return ReadExtensionPrefPermissionSet(extension_id, kPrefGrantedPermissions); 958 return ReadExtensionPrefPermissionSet(extension_id, kPrefGrantedPermissions);
865 } 959 }
866 960
867 void ExtensionPrefs::AddGrantedPermissions( 961 void ExtensionPrefs::AddGrantedPermissions(
868 const std::string& extension_id, 962 const std::string& extension_id,
869 const ExtensionPermissionSet* permissions) { 963 const ExtensionPermissionSet* permissions) {
870 CHECK(Extension::IdIsValid(extension_id)); 964 CHECK(Extension::IdIsValid(extension_id));
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 iter != extension_ids.end(); ++iter) { 1159 iter != extension_ids.end(); ++iter) {
1066 toolbar_order->Append(new StringValue(*iter)); 1160 toolbar_order->Append(new StringValue(*iter));
1067 } 1161 }
1068 SavePrefs(); 1162 SavePrefs();
1069 } 1163 }
1070 1164
1071 void ExtensionPrefs::OnExtensionInstalled( 1165 void ExtensionPrefs::OnExtensionInstalled(
1072 const Extension* extension, 1166 const Extension* extension,
1073 Extension::State initial_state, 1167 Extension::State initial_state,
1074 bool from_webstore, 1168 bool from_webstore,
1075 int page_index) { 1169 StringOrdinal page_index) {
1076 const std::string& id = extension->id(); 1170 const std::string& id = extension->id();
1077 CHECK(Extension::IdIsValid(id)); 1171 CHECK(Extension::IdIsValid(id));
1078 ScopedExtensionPrefUpdate update(prefs_, id); 1172 ScopedExtensionPrefUpdate update(prefs_, id);
1079 DictionaryValue* extension_dict = update.Get(); 1173 DictionaryValue* extension_dict = update.Get();
1080 const base::Time install_time = GetCurrentTime(); 1174 const base::Time install_time = GetCurrentTime();
1081 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state)); 1175 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state));
1082 extension_dict->Set(kPrefLocation, 1176 extension_dict->Set(kPrefLocation,
1083 Value::CreateIntegerValue(extension->location())); 1177 Value::CreateIntegerValue(extension->location()));
1084 extension_dict->Set(kPrefFromWebStore, 1178 extension_dict->Set(kPrefFromWebStore,
1085 Value::CreateBooleanValue(from_webstore)); 1179 Value::CreateBooleanValue(from_webstore));
(...skipping 11 matching lines...) Expand all
1097 extension->path()); 1191 extension->path());
1098 extension_dict->Set(kPrefPath, Value::CreateStringValue(path)); 1192 extension_dict->Set(kPrefPath, Value::CreateStringValue(path));
1099 // We store prefs about LOAD extensions, but don't cache their manifest 1193 // We store prefs about LOAD extensions, but don't cache their manifest
1100 // since it may change on disk. 1194 // since it may change on disk.
1101 if (extension->location() != Extension::LOAD) { 1195 if (extension->location() != Extension::LOAD) {
1102 extension_dict->Set(kPrefManifest, 1196 extension_dict->Set(kPrefManifest,
1103 extension->manifest_value()->DeepCopy()); 1197 extension->manifest_value()->DeepCopy());
1104 } 1198 }
1105 1199
1106 if (extension->is_app()) { 1200 if (extension->is_app()) {
1107 if (page_index == -1) 1201 if (!page_index.IsValid())
1108 page_index = GetNaturalAppPageIndex(); 1202 page_index = GetNaturalAppPageIndex();
1109 extension_dict->Set(kPrefPageIndex, 1203 extension_dict->Set(kPrefPageIndex,
1110 Value::CreateIntegerValue(page_index)); 1204 Value::CreateStringValue(page_index.ToString()));
1205 UpdatePageIndexMap(StringOrdinal(), page_index);
1206
1111 extension_dict->Set(kPrefAppLaunchIndex, 1207 extension_dict->Set(kPrefAppLaunchIndex,
1112 Value::CreateIntegerValue(GetNextAppLaunchIndex(page_index))); 1208 Value::CreateStringValue(
1209 GetNextAppLaunchIndex(page_index).ToString()));
1113 } 1210 }
1114 extension_pref_value_map_->RegisterExtension( 1211 extension_pref_value_map_->RegisterExtension(
1115 id, install_time, initial_state == Extension::ENABLED); 1212 id, install_time, initial_state == Extension::ENABLED);
1116 content_settings_store_->RegisterExtension( 1213 content_settings_store_->RegisterExtension(
1117 id, install_time, initial_state == Extension::ENABLED); 1214 id, install_time, initial_state == Extension::ENABLED);
1118 } 1215 }
1119 1216
1120 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, 1217 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id,
1121 const Extension::Location& location, 1218 const Extension::Location& location,
1122 bool external_uninstall) { 1219 bool external_uninstall) {
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
1435 return true; 1532 return true;
1436 } 1533 }
1437 return false; 1534 return false;
1438 } 1535 }
1439 1536
1440 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) { 1537 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) {
1441 prefs_->SetString(kWebStoreLogin, login); 1538 prefs_->SetString(kWebStoreLogin, login);
1442 SavePrefs(); 1539 SavePrefs();
1443 } 1540 }
1444 1541
1445 int ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) { 1542 void ExtensionPrefs::GetAllAppsOnPageSorted(
Finnur 2011/11/17 14:59:25 This doesn't really get all Apps on the page as mu
csharp 2011/11/17 19:51:58 Correct, function name updated On 2011/11/17 14:5
1446 int value; 1543 const StringOrdinal& target_page_index,
1447 if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value)) 1544 std::set<StringOrdinal, StringOrdinal::Comparsion>* indexes_on_page) const {
Finnur 2011/11/17 14:59:25 indexes -> indices?
csharp 2011/11/17 19:51:58 Done.
1448 return value; 1545 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1546 if (!extensions || !target_page_index.IsValid())
1547 return;
1449 1548
1450 return -1; 1549 for (DictionaryValue::key_iterator ext_it = extensions->begin_keys();
1550 ext_it != extensions->end_keys(); ++ext_it) {
1551 StringOrdinal page_index = GetPageIndex(*ext_it);
1552 if (page_index.IsValid() && page_index.Equal(target_page_index)) {
1553 StringOrdinal app_launch_index = GetAppLaunchIndex(*ext_it);
1554 if (app_launch_index.IsValid())
1555 indexes_on_page->insert(app_launch_index);
1556 }
1557 }
1558 }
1559
1560 StringOrdinal ExtensionPrefs::GetFirstAppPage() const {
1561 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1562 if (!extensions)
1563 return StringOrdinal();
1564
1565 if (page_index_map_.size() == 0)
1566 return StringOrdinal::CreateValidOrdinal();
1567
1568 return page_index_map_.begin()->first;
1569 }
1570
1571 StringOrdinal ExtensionPrefs::CreateFirstAppLaunchIndex(
1572 const StringOrdinal& page_index) const {
1573 std::set<StringOrdinal, StringOrdinal::Comparsion> apps_on_page;
1574 GetAllAppsOnPageSorted(page_index, &apps_on_page);
1575
1576 if (apps_on_page.empty()) {
1577 return StringOrdinal::CreateValidOrdinal();
1578 } else {
1579 return apps_on_page.begin()->CreateBefore();
1580 }
Finnur 2011/11/17 14:59:25 nit: no braces around single-line clauses.
csharp 2011/11/17 19:51:58 Done.
1581 }
1582
1583 StringOrdinal ExtensionPrefs::GetAppLaunchIndex(
1584 const std::string& extension_id) const {
1585 std::string raw_value;
1586 ReadExtensionPrefString(extension_id, kPrefAppLaunchIndex, &raw_value);
1587 return StringOrdinal(raw_value);
Finnur 2011/11/17 14:59:25 Is the idea here to return a blank ordinal if the
csharp 2011/11/17 19:51:58 Yes, which is an invalid ordinal which does repres
1451 } 1588 }
1452 1589
1453 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, 1590 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id,
1454 int index) { 1591 const StringOrdinal& index) {
1455 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, 1592 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex,
1456 Value::CreateIntegerValue(index)); 1593 Value::CreateStringValue(index.ToString()));
1457 } 1594 }
1458 1595
1459 int ExtensionPrefs::GetNextAppLaunchIndex(int on_page) { 1596 StringOrdinal ExtensionPrefs::GetNextAppLaunchIndex(StringOrdinal on_page)
1460 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); 1597 const {
1461 if (!extensions) 1598 std::set<StringOrdinal, StringOrdinal::Comparsion> apps_on_page;
1462 return 0; 1599 GetAllAppsOnPageSorted(on_page, &apps_on_page);
1463 1600
1464 int max_value = -1; 1601 if (apps_on_page.empty()) {
1465 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); 1602 return StringOrdinal::CreateValidOrdinal();
1466 extension_id != extensions->end_keys(); ++extension_id) { 1603 } else {
1467 int value = GetAppLaunchIndex(*extension_id); 1604 return apps_on_page.rbegin()->CreateAfter();
1468 int page = GetPageIndex(*extension_id);
1469 if (page == on_page && value > max_value)
1470 max_value = value;
1471 }
1472 return max_value + 1;
1473 }
1474
1475 int ExtensionPrefs::GetNaturalAppPageIndex() {
1476 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1477 if (!extensions)
1478 return 0;
1479
1480 std::map<int, int> page_counts;
1481 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys();
1482 extension_id != extensions->end_keys(); ++extension_id) {
1483 int page_index = GetPageIndex(*extension_id);
1484 if (page_index >= 0)
1485 page_counts[page_index] = page_counts[page_index] + 1;
1486 }
1487 for (int i = 0; ; i++) {
1488 std::map<int, int>::const_iterator it = page_counts.find(i);
1489 if (it == page_counts.end() || it->second < kNaturalAppPageSize)
1490 return i;
1491 } 1605 }
1492 } 1606 }
1493 1607
1608 StringOrdinal ExtensionPrefs::GetNaturalAppPageIndex() const {
1609 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1610 if (!extensions)
1611 return StringOrdinal();
1612
1613 if (page_index_map_.size() == 0) {
1614 return StringOrdinal::CreateValidOrdinal();
1615 }
1616
1617 std::map<StringOrdinal, int>::const_iterator it = page_index_map_.begin();
1618 for (; it != page_index_map_.end(); ++it) {
1619 if (it->second < kNaturalAppPageSize) {
1620 return it->first;
1621 }
Finnur 2011/11/17 14:59:25 same here: no braces (also on line 1613, 1615).
csharp 2011/11/17 19:51:58 Done.
1622 }
1623
1624 // Add a new page as all existing pages are full.
1625 StringOrdinal last_element = page_index_map_.rbegin()->first;
1626 return last_element.CreateAfter();
1627 }
1628
1494 void ExtensionPrefs::SetAppLauncherOrder( 1629 void ExtensionPrefs::SetAppLauncherOrder(
1495 const std::vector<std::string>& extension_ids) { 1630 const std::vector<std::string>& extension_ids,
1496 for (size_t i = 0; i < extension_ids.size(); ++i) 1631 const std::string& moved_extension_id) {
1497 SetAppLaunchIndex(extension_ids.at(i), i); 1632 std::vector<std::string>::const_iterator it =
1633 find(extension_ids.begin(), extension_ids.end(), moved_extension_id);
1634 size_t position = std::distance(extension_ids.begin(), it);
1635
1636 // We need at least 2 apps for the moved apps old value to be invalidated.
1637 if (extension_ids.size() >= 2) {
1638 if (position == 0U) {
1639 // The app is moving to the start of the group, so we need to get the
1640 // previous first StringOrdinal and create the new value before it.
1641 ++it;
1642 SetAppLaunchIndex(moved_extension_id,
1643 GetAppLaunchIndex(*it).CreateBefore());
1644 } else if (position == extension_ids.size() - 1U) {
1645 // The app is moving to the end of the group, so we need to get the
1646 // previous last StringOrdinal and create the new value after it.
1647 --it;
1648 SetAppLaunchIndex(moved_extension_id,
1649 GetAppLaunchIndex(*it).CreateAfter());
1650 } else {
1651 // The app is going into a middle position, which means there are valid
1652 // StringOrdinals before and after it. We get the new adjacent
1653 // StringOrdinals and create our new value between them.
1654 ++it;
1655 StringOrdinal value_after = GetAppLaunchIndex(*it);
1656 advance(it, -2);
1657 StringOrdinal value_before = GetAppLaunchIndex(*it);
1658 SetAppLaunchIndex(moved_extension_id,
1659 value_before.CreateBetween(value_after));
1660 }
1661 }
1498 1662
1499 content::NotificationService::current()->Notify( 1663 content::NotificationService::current()->Notify(
1500 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, 1664 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED,
1501 content::Source<ExtensionPrefs>(this), 1665 content::Source<ExtensionPrefs>(this),
1502 content::NotificationService::NoDetails()); 1666 content::NotificationService::NoDetails());
1503 } 1667 }
1504 1668
1505 int ExtensionPrefs::GetPageIndex(const std::string& extension_id) { 1669 StringOrdinal ExtensionPrefs::GetPageIndex(const std::string& extension_id)
1506 int value = -1; 1670 const {
1507 ReadExtensionPrefInteger(extension_id, kPrefPageIndex, &value); 1671 std::string raw_string_data;
1508 return value; 1672 ReadExtensionPrefString(extension_id, kPrefPageIndex, &raw_string_data);
1673 return StringOrdinal(raw_string_data);
Finnur 2011/11/17 14:59:25 Same here: Is the idea here to return a blank ordi
csharp 2011/11/17 19:51:58 Yes, and comment added to clarify. On 2011/11/17
1509 } 1674 }
1510 1675
1511 void ExtensionPrefs::SetPageIndex(const std::string& extension_id, int index) { 1676 void ExtensionPrefs::SetPageIndex(const std::string& extension_id,
1677 const StringOrdinal& page_index) {
1678 UpdatePageIndexMap(GetPageIndex(extension_id), page_index);
1512 UpdateExtensionPref(extension_id, kPrefPageIndex, 1679 UpdateExtensionPref(extension_id, kPrefPageIndex,
1513 Value::CreateIntegerValue(index)); 1680 Value::CreateStringValue(page_index.ToString()));
1514 } 1681 }
1515 1682
1516 void ExtensionPrefs::ClearPageIndex(const std::string& extension_id) { 1683 void ExtensionPrefs::ClearPageIndex(const std::string& extension_id) {
1684 UpdatePageIndexMap(GetPageIndex(extension_id), StringOrdinal());
1517 UpdateExtensionPref(extension_id, kPrefPageIndex, NULL); 1685 UpdateExtensionPref(extension_id, kPrefPageIndex, NULL);
1518 } 1686 }
1519 1687
1688 void ExtensionPrefs::InitializePageIndexMap(
1689 const ExtensionIdSet& extension_ids) {
1690 for (ExtensionIdSet::const_iterator ext_it = extension_ids.begin();
1691 ext_it != extension_ids.end(); ++ext_it) {
1692 UpdatePageIndexMap(StringOrdinal(), GetPageIndex(*ext_it));
Finnur 2011/11/17 14:59:25 Are there cases here where you can end up with {St
csharp 2011/11/17 19:51:58 It is possible, but the map only updates for valid
1693 }
1694 }
1695
1696 void ExtensionPrefs::UpdatePageIndexMap(const StringOrdinal& old_value,
1697 const StringOrdinal& new_value) {
1698 if (new_value.IsValid())
1699 ++page_index_map_[new_value];
1700
1701 if (old_value.IsValid()) {
1702 --page_index_map_[old_value];
1703
1704 if (page_index_map_[old_value] == 0)
1705 page_index_map_.erase(old_value);
1706 }
1707 }
1708
1709 int ExtensionPrefs::PageIndexAsInteger(const StringOrdinal& page_index) const {
1710 if (!page_index.IsValid())
1711 return -1;
1712
1713 std::map<StringOrdinal, int>::const_iterator it =
1714 page_index_map_.find(page_index);
1715 if (it != page_index_map_.end()) {
1716 return std::distance(page_index_map_.begin(), it);
1717 } else {
1718 return -1;
1719 }
1720 }
1721
1722 StringOrdinal ExtensionPrefs::PageIndexAsStringOrdinal(size_t page_index) {
1723 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1724 if (!extensions)
1725 return StringOrdinal();
1726
1727 if (page_index_map_.size() == 0) {
1728 return StringOrdinal::CreateValidOrdinal();
1729 }
Finnur 2011/11/17 14:59:25 nit: You don't need braces, your teeth look fine..
csharp 2011/11/17 19:51:58 Done and :/
Finnur 2011/11/18 09:35:15 Awww... isn't that smile lovely? :) On 2011/11/17
1730
1731 if (page_index < page_index_map_.size()) {
1732 std::map<StringOrdinal, int, StringOrdinal::Comparsion>::const_iterator it =
1733 page_index_map_.begin();
1734 advance(it, page_index);
1735
1736 return it->first;
1737 } else {
1738 // Create new pages until we reach the desired index.
1739 while (page_index >= page_index_map_.size()) {
1740 StringOrdinal new_page = page_index_map_.rbegin()->first.CreateAfter();
1741 page_index_map_[new_page] = 0;
1742 }
1743 return page_index_map_.rbegin()->first;
1744 }
1745 }
1746
1520 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { 1747 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) {
1521 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); 1748 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp);
1522 } 1749 }
1523 1750
1524 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { 1751 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) {
1525 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, 1752 UpdateExtensionPref(extension_id, kPrefUserDraggedApp,
1526 Value::CreateBooleanValue(true)); 1753 Value::CreateBooleanValue(true));
1527 } 1754 }
1528 1755
1529 void ExtensionPrefs::SetUpdateUrlData(const std::string& extension_id, 1756 void ExtensionPrefs::SetUpdateUrlData(const std::string& extension_id,
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
1654 // Create empty preferences dictionary for each extension (these dictionaries 1881 // Create empty preferences dictionary for each extension (these dictionaries
1655 // are pruned when persisting the preferences to disk). 1882 // are pruned when persisting the preferences to disk).
1656 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1883 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1657 ext_id != extension_ids.end(); ++ext_id) { 1884 ext_id != extension_ids.end(); ++ext_id) {
1658 ScopedExtensionPrefUpdate update(prefs_, *ext_id); 1885 ScopedExtensionPrefUpdate update(prefs_, *ext_id);
1659 // This creates an empty dictionary if none is stored. 1886 // This creates an empty dictionary if none is stored.
1660 update.Get(); 1887 update.Get();
1661 } 1888 }
1662 1889
1663 FixMissingPrefs(extension_ids); 1890 FixMissingPrefs(extension_ids);
1891 InitializePageIndexMap(extension_ids);
1664 MigratePermissions(extension_ids); 1892 MigratePermissions(extension_ids);
1893 MigrateAppIndex(extension_ids);
1665 1894
1666 // Store extension controlled preference values in the 1895 // Store extension controlled preference values in the
1667 // |extension_pref_value_map_|, which then informs the subscribers 1896 // |extension_pref_value_map_|, which then informs the subscribers
1668 // (ExtensionPrefStores) about the winning values. 1897 // (ExtensionPrefStores) about the winning values.
1669 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1898 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1670 ext_id != extension_ids.end(); ++ext_id) { 1899 ext_id != extension_ids.end(); ++ext_id) {
1671 extension_pref_value_map_->RegisterExtension( 1900 extension_pref_value_map_->RegisterExtension(
1672 *ext_id, 1901 *ext_id,
1673 GetInstallTime(*ext_id), 1902 GetInstallTime(*ext_id),
1674 !IsExtensionDisabled(*ext_id)); 1903 !IsExtensionDisabled(*ext_id));
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1830 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, 2059 prefs->RegisterListPref(prefs::kExtensionInstallAllowList,
1831 PrefService::UNSYNCABLE_PREF); 2060 PrefService::UNSYNCABLE_PREF);
1832 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, 2061 prefs->RegisterListPref(prefs::kExtensionInstallDenyList,
1833 PrefService::UNSYNCABLE_PREF); 2062 PrefService::UNSYNCABLE_PREF);
1834 prefs->RegisterListPref(prefs::kExtensionInstallForceList, 2063 prefs->RegisterListPref(prefs::kExtensionInstallForceList,
1835 PrefService::UNSYNCABLE_PREF); 2064 PrefService::UNSYNCABLE_PREF);
1836 prefs->RegisterStringPref(kWebStoreLogin, 2065 prefs->RegisterStringPref(kWebStoreLogin,
1837 std::string() /* default_value */, 2066 std::string() /* default_value */,
1838 PrefService::UNSYNCABLE_PREF); 2067 PrefService::UNSYNCABLE_PREF);
1839 } 2068 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698