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

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: Correct inlining Created 9 years 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 kPrefAppLaunchOrdinal[] = "app_launcher_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 kPrefPageOrdinal[] = "page_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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 const DictionaryValue* ext = GetExtensionPref(extension_id); 410 const DictionaryValue* ext = GetExtensionPref(extension_id);
409 ListValue* out = NULL; 411 ListValue* out = NULL;
410 if (!ext || !ext->GetList(pref_key, &out)) 412 if (!ext || !ext->GetList(pref_key, &out))
411 return false; 413 return false;
412 if (out_value) 414 if (out_value)
413 *out_value = out; 415 *out_value = out;
414 416
415 return true; 417 return true;
416 } 418 }
417 419
420 bool ExtensionPrefs::ReadExtensionPrefString(
421 const std::string& extension_id, const std::string& pref_key,
422 std::string* out_value) const {
423 const DictionaryValue* ext = GetExtensionPref(extension_id);
424
425 if (!ext || !ext->GetString(pref_key, out_value))
426 return false;
427
428 return true;
429 }
430
418 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet( 431 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet(
419 const std::string& extension_id, 432 const std::string& extension_id,
420 const std::string& pref_key, 433 const std::string& pref_key,
421 URLPatternSet* result, 434 URLPatternSet* result,
422 int valid_schemes) { 435 int valid_schemes) {
423 const ListValue* value = NULL; 436 const ListValue* value = NULL;
424 if (!ReadExtensionPrefList(extension_id, pref_key, &value)) 437 if (!ReadExtensionPrefList(extension_id, pref_key, &value))
425 return false; 438 return false;
426 439
427 result->ClearPatterns(); 440 result->ClearPatterns();
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
856 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) { 869 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) {
857 UpdateExtensionPref( 870 UpdateExtensionPref(
858 *ext_id, explicit_hosts, hosts->DeepCopy()); 871 *ext_id, explicit_hosts, hosts->DeepCopy());
859 872
860 // We can get rid of the old one by setting it to an empty list. 873 // We can get rid of the old one by setting it to an empty list.
861 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue()); 874 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue());
862 } 875 }
863 } 876 }
864 } 877 }
865 878
879 void ExtensionPrefs::MigrateAppIndex(const ExtensionIdSet& extension_ids) {
880 if (extension_ids.empty())
881 return;
882
883 // Convert all the page index values to page ordinals. If there are any
884 // app launch values that need to be migrated, inserted them into a sorted
885 // set to be dealt with later.
886 std::map<StringOrdinal, std::map<int, const std::string*>,
akalin 2011/12/02 19:48:23 use typedefs (scoped to this function) to make thi
csharp 2011/12/05 15:30:24 Done.
887 StringOrdinalLessThan> app_launches_to_convert;
888 for (ExtensionIdSet::const_iterator ext_id = extension_ids.begin();
889 ext_id != extension_ids.end(); ++ext_id) {
890 int old_page_index = 0;
891 StringOrdinal page = GetPageOrdinal(*ext_id);
892 if (ReadExtensionPrefInteger(*ext_id,
893 kPrefPageIndexDeprecated,
894 &old_page_index)) {
895 // Since we require all earlier StringOrdinals to already exist in order
896 // to properly convert from integers and we are iteratoring though them in
akalin 2011/12/02 19:48:23 iteratoring -> iterating
csharp 2011/12/05 15:30:24 Done.
897 // no given order, we create earlier StringOrdinal values as required.
898 // This should be filled in by the time we are done with this loop.
899 if (page_ordinal_map_.empty())
900 page_ordinal_map_[StringOrdinal::CreateInitialOrdinal()] = 0;
901 while (page_ordinal_map_.size() <= static_cast<size_t>(old_page_index)) {
902 StringOrdinal earlier_page =
903 page_ordinal_map_.rbegin()->first.CreateAfter();
904 page_ordinal_map_[earlier_page] = 0;
905 }
906
907 page = PageIntegerAsStringOrdinal(old_page_index);
akalin 2011/12/02 19:48:23 you can do: page = *page_ordinal_map_.rbegin(); D
csharp 2011/12/05 15:30:24 old_page_index isn't always referring to the last
908 SetPageOrdinal(*ext_id, page);
909 UpdateExtensionPref(*ext_id, kPrefPageIndexDeprecated, NULL);
910 }
911
912 int old_app_launch_index = 0;
913 if (ReadExtensionPrefInteger(*ext_id,
914 kPrefAppLaunchIndexDeprecated,
915 &old_app_launch_index)) {
916 // We can't update the app launch index value yet, because we use
917 // GetNextAppLaunchOrdinal to get the new ordinal value and it requires
918 // all the ordinals with lower values to have already been migrated.
919 // A valid page ordinal is also required because otherwise there is
920 // no page to add the app to.
921 if (page.IsValid())
akalin 2011/12/02 19:48:23 use braces for non-simple if statement
csharp 2011/12/05 15:30:24 Done.
922 app_launches_to_convert[page].insert(
akalin 2011/12/02 19:48:23 you can just do app_launches_to_convert[page][old
csharp 2011/12/05 15:30:24 Done.
923 std::make_pair(old_app_launch_index,
924 &(*ext_id)));
925
926 UpdateExtensionPref(*ext_id, kPrefAppLaunchIndexDeprecated, NULL);
927 }
928 }
929
930 // Remove any empty pages that may have been added. This shouldn't occur,
931 // but double check here to prevent future problems with conversions between
932 // integers and StringOrdinals.
933 for (std::map<StringOrdinal, int, StringOrdinalLessThan>::iterator it =
934 page_ordinal_map_.begin(); it != page_ordinal_map_.end();) {
935 if (it->second == 0) {
936 std::map<StringOrdinal, int, StringOrdinalLessThan>::iterator prev_it =
937 it;
938 ++it;
939 page_ordinal_map_.erase(prev_it);
940 } else {
941 ++it;
942 }
943 }
944
945 if (app_launches_to_convert.empty())
946 return;
947
948 // Create the new app launch ordinals and remove the old preferences. Since
949 // the set is sorted, each time we migrate an apps index, we know that all of
950 // the remaining apps will appear further down the NTP than it or on a
951 // different page.
952 for (std::map<StringOrdinal, std::map<int, const std::string*>,
953 StringOrdinalLessThan >::const_iterator page_it =
954 app_launches_to_convert.begin();
955 page_it != app_launches_to_convert.end(); ++page_it) {
956 StringOrdinal page = page_it->first;
957 for (std::map<int, const std::string*>::const_iterator launch_it =
958 page_it->second.begin(); launch_it != page_it->second.end();
959 ++launch_it) {
960 SetAppLaunchOrdinal(*(launch_it->second),
961 CreateNextAppLaunchOrdinal(page));
962 }
963 }
964 }
965
866 ExtensionPermissionSet* ExtensionPrefs::GetGrantedPermissions( 966 ExtensionPermissionSet* ExtensionPrefs::GetGrantedPermissions(
867 const std::string& extension_id) { 967 const std::string& extension_id) {
868 CHECK(Extension::IdIsValid(extension_id)); 968 CHECK(Extension::IdIsValid(extension_id));
869 return ReadExtensionPrefPermissionSet(extension_id, kPrefGrantedPermissions); 969 return ReadExtensionPrefPermissionSet(extension_id, kPrefGrantedPermissions);
870 } 970 }
871 971
872 void ExtensionPrefs::AddGrantedPermissions( 972 void ExtensionPrefs::AddGrantedPermissions(
873 const std::string& extension_id, 973 const std::string& extension_id,
874 const ExtensionPermissionSet* permissions) { 974 const ExtensionPermissionSet* permissions) {
875 CHECK(Extension::IdIsValid(extension_id)); 975 CHECK(Extension::IdIsValid(extension_id));
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 iter != extension_ids.end(); ++iter) { 1171 iter != extension_ids.end(); ++iter) {
1072 toolbar_order->Append(new StringValue(*iter)); 1172 toolbar_order->Append(new StringValue(*iter));
1073 } 1173 }
1074 SavePrefs(); 1174 SavePrefs();
1075 } 1175 }
1076 1176
1077 void ExtensionPrefs::OnExtensionInstalled( 1177 void ExtensionPrefs::OnExtensionInstalled(
1078 const Extension* extension, 1178 const Extension* extension,
1079 Extension::State initial_state, 1179 Extension::State initial_state,
1080 bool from_webstore, 1180 bool from_webstore,
1081 int page_index) { 1181 const StringOrdinal& page_ordinal) {
1082 const std::string& id = extension->id(); 1182 const std::string& id = extension->id();
1083 CHECK(Extension::IdIsValid(id)); 1183 CHECK(Extension::IdIsValid(id));
1084 ScopedExtensionPrefUpdate update(prefs_, id); 1184 ScopedExtensionPrefUpdate update(prefs_, id);
1085 DictionaryValue* extension_dict = update.Get(); 1185 DictionaryValue* extension_dict = update.Get();
1086 const base::Time install_time = GetCurrentTime(); 1186 const base::Time install_time = GetCurrentTime();
1087 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state)); 1187 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state));
1088 extension_dict->Set(kPrefLocation, 1188 extension_dict->Set(kPrefLocation,
1089 Value::CreateIntegerValue(extension->location())); 1189 Value::CreateIntegerValue(extension->location()));
1090 extension_dict->Set(kPrefFromWebStore, 1190 extension_dict->Set(kPrefFromWebStore,
1091 Value::CreateBooleanValue(from_webstore)); 1191 Value::CreateBooleanValue(from_webstore));
(...skipping 11 matching lines...) Expand all
1103 extension->path()); 1203 extension->path());
1104 extension_dict->Set(kPrefPath, Value::CreateStringValue(path)); 1204 extension_dict->Set(kPrefPath, Value::CreateStringValue(path));
1105 // We store prefs about LOAD extensions, but don't cache their manifest 1205 // We store prefs about LOAD extensions, but don't cache their manifest
1106 // since it may change on disk. 1206 // since it may change on disk.
1107 if (extension->location() != Extension::LOAD) { 1207 if (extension->location() != Extension::LOAD) {
1108 extension_dict->Set(kPrefManifest, 1208 extension_dict->Set(kPrefManifest,
1109 extension->manifest_value()->DeepCopy()); 1209 extension->manifest_value()->DeepCopy());
1110 } 1210 }
1111 1211
1112 if (extension->is_app()) { 1212 if (extension->is_app()) {
1113 if (page_index == -1) 1213 StringOrdinal new_page_ordinal =
1114 page_index = GetNaturalAppPageIndex(); 1214 page_ordinal.IsValid() ? page_ordinal : GetNaturalAppPageOrdinal();
1115 extension_dict->Set(kPrefPageIndex, 1215 SetPageOrdinal(id, new_page_ordinal);
1116 Value::CreateIntegerValue(page_index)); 1216 SetAppLaunchOrdinal(id, CreateNextAppLaunchOrdinal(new_page_ordinal));
1117 extension_dict->Set(kPrefAppLaunchIndex,
1118 Value::CreateIntegerValue(GetNextAppLaunchIndex(page_index)));
1119 } 1217 }
1218
1120 extension_pref_value_map_->RegisterExtension( 1219 extension_pref_value_map_->RegisterExtension(
1121 id, install_time, initial_state == Extension::ENABLED); 1220 id, install_time, initial_state == Extension::ENABLED);
1122 content_settings_store_->RegisterExtension( 1221 content_settings_store_->RegisterExtension(
1123 id, install_time, initial_state == Extension::ENABLED); 1222 id, install_time, initial_state == Extension::ENABLED);
1124 } 1223 }
1125 1224
1126 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, 1225 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id,
1127 const Extension::Location& location, 1226 const Extension::Location& location,
1128 bool external_uninstall) { 1227 bool external_uninstall) {
1129 // For external extensions, we save a preference reminding ourself not to try 1228 // For external extensions, we save a preference reminding ourself not to try
1130 // and install the extension anymore (except when |external_uninstall| is 1229 // and install the extension anymore (except when |external_uninstall| is
1131 // true, which signifies that the registry key was deleted or the pref file 1230 // true, which signifies that the registry key was deleted or the pref file
1132 // no longer lists the extension). 1231 // no longer lists the extension).
1133 if (!external_uninstall && Extension::IsExternalLocation(location)) { 1232 if (!external_uninstall && Extension::IsExternalLocation(location)) {
1134 UpdateExtensionPref(extension_id, kPrefState, 1233 UpdateExtensionPref(extension_id, kPrefState,
1135 Value::CreateIntegerValue( 1234 Value::CreateIntegerValue(
1136 Extension::EXTERNAL_EXTENSION_UNINSTALLED)); 1235 Extension::EXTERNAL_EXTENSION_UNINSTALLED));
1137 extension_pref_value_map_->SetExtensionState(extension_id, false); 1236 extension_pref_value_map_->SetExtensionState(extension_id, false);
1138 content_settings_store_->SetExtensionState(extension_id, false); 1237 content_settings_store_->SetExtensionState(extension_id, false);
1139 } else { 1238 } else {
1140 DeleteExtensionPrefs(extension_id); 1239 DeleteExtensionPrefs(extension_id);
1141 } 1240 }
1142 } 1241 }
1143 1242
1243 void ExtensionPrefs::OnExtensionMoved(
1244 const std::string& moved_extension_id,
1245 const std::string& predecessor_extension_id,
1246 const std::string& successor_extension_id) {
1247 // If there are no neighbors then no changes are required, so we can return.
1248 if (predecessor_extension_id.empty() && successor_extension_id.empty())
1249 return;
1250
1251 if (predecessor_extension_id.empty()) {
1252 SetAppLaunchOrdinal(
1253 moved_extension_id,
1254 GetAppLaunchOrdinal(successor_extension_id).CreateBefore());
1255 } else if (successor_extension_id.empty()) {
1256 SetAppLaunchOrdinal(
1257 moved_extension_id,
1258 GetAppLaunchOrdinal(predecessor_extension_id).CreateAfter());
1259 } else {
1260 const StringOrdinal& predecessor_ordinal =
1261 GetAppLaunchOrdinal(predecessor_extension_id);
1262 const StringOrdinal& successor_ordinal =
1263 GetAppLaunchOrdinal(successor_extension_id);
1264 SetAppLaunchOrdinal(moved_extension_id,
1265 predecessor_ordinal.CreateBetween(successor_ordinal));
1266 }
1267
1268 content::NotificationService::current()->Notify(
1269 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED,
1270 content::Source<ExtensionPrefs>(this),
1271 content::NotificationService::NoDetails());
1272 }
1273
1144 void ExtensionPrefs::SetExtensionState(const std::string& extension_id, 1274 void ExtensionPrefs::SetExtensionState(const std::string& extension_id,
1145 Extension::State state) { 1275 Extension::State state) {
1146 UpdateExtensionPref(extension_id, kPrefState, 1276 UpdateExtensionPref(extension_id, kPrefState,
1147 Value::CreateIntegerValue(state)); 1277 Value::CreateIntegerValue(state));
1148 bool enabled = (state == Extension::ENABLED); 1278 bool enabled = (state == Extension::ENABLED);
1149 extension_pref_value_map_->SetExtensionState(extension_id, enabled); 1279 extension_pref_value_map_->SetExtensionState(extension_id, enabled);
1150 content_settings_store_->SetExtensionState(extension_id, enabled); 1280 content_settings_store_->SetExtensionState(extension_id, enabled);
1151 } 1281 }
1152 1282
1153 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) { 1283 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) {
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
1441 return true; 1571 return true;
1442 } 1572 }
1443 return false; 1573 return false;
1444 } 1574 }
1445 1575
1446 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) { 1576 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) {
1447 prefs_->SetString(kWebStoreLogin, login); 1577 prefs_->SetString(kWebStoreLogin, login);
1448 SavePrefs(); 1578 SavePrefs();
1449 } 1579 }
1450 1580
1451 int ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) { 1581 void ExtensionPrefs::GetMinAndMaxAppLaunchOrdinalsOnPage(
akalin 2011/12/02 19:48:23 hmm looks like the callers of this function care a
csharp 2011/12/05 15:30:24 Done.
1452 int value; 1582 const StringOrdinal& target_page_ordinal,
1453 if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value)) 1583 StringOrdinal* min_app_launch_value,
1454 return value; 1584 StringOrdinal* max_app_launch_value) const {
1585 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1586 CHECK(extensions);
1587 CHECK(target_page_ordinal.IsValid());
1588 CHECK(min_app_launch_value);
1589 CHECK(max_app_launch_value);
1455 1590
1456 return -1; 1591 for (DictionaryValue::key_iterator ext_it = extensions->begin_keys();
1457 } 1592 ext_it != extensions->end_keys(); ++ext_it) {
1593 const StringOrdinal& page_ordinal = GetPageOrdinal(*ext_it);
1594 if (page_ordinal.IsValid() && page_ordinal.Equal(target_page_ordinal)) {
1595 const StringOrdinal& app_launch_ordinal = GetAppLaunchOrdinal(*ext_it);
1596 if (app_launch_ordinal.IsValid()) {
1597 if (!min_app_launch_value->IsValid() ||
1598 app_launch_ordinal.LessThan(*min_app_launch_value))
1599 *min_app_launch_value = app_launch_ordinal;
1458 1600
1459 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, 1601 if (!max_app_launch_value->IsValid() ||
1460 int index) { 1602 app_launch_ordinal.GreaterThan(*max_app_launch_value))
1461 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, 1603 *max_app_launch_value = app_launch_ordinal;
1462 Value::CreateIntegerValue(index)); 1604 }
1463 } 1605 }
1464
1465 int ExtensionPrefs::GetNextAppLaunchIndex(int on_page) {
1466 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1467 if (!extensions)
1468 return 0;
1469
1470 int max_value = -1;
1471 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys();
1472 extension_id != extensions->end_keys(); ++extension_id) {
1473 int value = GetAppLaunchIndex(*extension_id);
1474 int page = GetPageIndex(*extension_id);
1475 if (page == on_page && value > max_value)
1476 max_value = value;
1477 }
1478 return max_value + 1;
1479 }
1480
1481 int ExtensionPrefs::GetNaturalAppPageIndex() {
1482 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1483 if (!extensions)
1484 return 0;
1485
1486 std::map<int, int> page_counts;
1487 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys();
1488 extension_id != extensions->end_keys(); ++extension_id) {
1489 int page_index = GetPageIndex(*extension_id);
1490 if (page_index >= 0)
1491 page_counts[page_index] = page_counts[page_index] + 1;
1492 }
1493 for (int i = 0; ; i++) {
1494 std::map<int, int>::const_iterator it = page_counts.find(i);
1495 if (it == page_counts.end() || it->second < kNaturalAppPageSize)
1496 return i;
1497 } 1606 }
1498 } 1607 }
1499 1608
1500 void ExtensionPrefs::SetAppLauncherOrder( 1609 StringOrdinal ExtensionPrefs::GetAppLaunchOrdinal(
1501 const std::vector<std::string>& extension_ids) { 1610 const std::string& extension_id) const {
1502 for (size_t i = 0; i < extension_ids.size(); ++i) 1611 std::string raw_value;
1503 SetAppLaunchIndex(extension_ids.at(i), i); 1612 // If the preference read fails then raw_value will still be unset and we
1504 1613 // will return an invalid StringOrdinal to signal that no app launch ordinal
1505 content::NotificationService::current()->Notify( 1614 // was found.
1506 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, 1615 ReadExtensionPrefString(extension_id, kPrefAppLaunchOrdinal, &raw_value);
1507 content::Source<ExtensionPrefs>(this), 1616 return StringOrdinal(raw_value);
1508 content::NotificationService::NoDetails());
1509 } 1617 }
1510 1618
1511 int ExtensionPrefs::GetPageIndex(const std::string& extension_id) { 1619 void ExtensionPrefs::SetAppLaunchOrdinal(const std::string& extension_id,
1512 int value = -1; 1620 const StringOrdinal& ordinal) {
1513 ReadExtensionPrefInteger(extension_id, kPrefPageIndex, &value); 1621 UpdateExtensionPref(extension_id, kPrefAppLaunchOrdinal,
1514 return value; 1622 Value::CreateStringValue(ordinal.ToString()));
1515 } 1623 }
1516 1624
1517 void ExtensionPrefs::SetPageIndex(const std::string& extension_id, int index) { 1625 StringOrdinal ExtensionPrefs::CreateFirstAppLaunchOrdinal(
1518 UpdateExtensionPref(extension_id, kPrefPageIndex, 1626 const StringOrdinal& page_ordinal) const {
1519 Value::CreateIntegerValue(index)); 1627 StringOrdinal max_ordinal;
1628 StringOrdinal min_ordinal;
1629 GetMinAndMaxAppLaunchOrdinalsOnPage(page_ordinal, &min_ordinal, &max_ordinal);
1630
1631 if (min_ordinal.IsValid())
1632 return min_ordinal.CreateBefore();
1633 else
1634 return StringOrdinal::CreateInitialOrdinal();
1520 } 1635 }
1521 1636
1522 void ExtensionPrefs::ClearPageIndex(const std::string& extension_id) { 1637 StringOrdinal ExtensionPrefs::CreateNextAppLaunchOrdinal(
1523 UpdateExtensionPref(extension_id, kPrefPageIndex, NULL); 1638 const StringOrdinal& page_ordinal) const {
1639 StringOrdinal max_ordinal;
1640 StringOrdinal min_ordinal;
1641 GetMinAndMaxAppLaunchOrdinalsOnPage(page_ordinal, &min_ordinal, &max_ordinal);
1642
1643 if (max_ordinal.IsValid())
1644 return max_ordinal.CreateAfter();
1645 else
1646 return StringOrdinal::CreateInitialOrdinal();
1647 }
1648
1649 StringOrdinal ExtensionPrefs::CreateFirstAppPage() const {
1650 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1651 CHECK(extensions);
1652
1653 if (page_ordinal_map_.empty())
1654 return StringOrdinal::CreateInitialOrdinal();
1655
1656 return page_ordinal_map_.begin()->first;
1657 }
1658
1659 StringOrdinal ExtensionPrefs::GetNaturalAppPageOrdinal() const {
1660 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1661 CHECK(extensions);
1662
1663 if (page_ordinal_map_.empty())
1664 return StringOrdinal::CreateInitialOrdinal();
1665
1666 for (std::map<StringOrdinal, int>::const_iterator it =
1667 page_ordinal_map_.begin(); it != page_ordinal_map_.end(); ++it) {
1668 if (it->second < kNaturalAppPageSize)
1669 return it->first;
1670 }
1671
1672 // Add a new page as all existing pages are full.
1673 StringOrdinal last_element = page_ordinal_map_.rbegin()->first;
1674 return last_element.CreateAfter();
1675 }
1676
1677 StringOrdinal ExtensionPrefs::GetPageOrdinal(const std::string& extension_id)
1678 const {
1679 std::string raw_data;
1680 // If the preference read fails then raw_data will still be unset and we will
1681 // return an invalid StringOrdinal to signal that no page ordinal was found.
1682 ReadExtensionPrefString(extension_id, kPrefPageOrdinal, &raw_data);
1683 return StringOrdinal(raw_data);
1684 }
1685
1686 void ExtensionPrefs::SetPageOrdinal(const std::string& extension_id,
1687 const StringOrdinal& page_ordinal) {
1688 UpdatePageOrdinalMap(GetPageOrdinal(extension_id), page_ordinal);
1689 UpdateExtensionPref(extension_id, kPrefPageOrdinal,
1690 Value::CreateStringValue(page_ordinal.ToString()));
1691 }
1692
1693 void ExtensionPrefs::ClearPageOrdinal(const std::string& extension_id) {
1694 UpdatePageOrdinalMap(GetPageOrdinal(extension_id), StringOrdinal());
1695 UpdateExtensionPref(extension_id, kPrefPageOrdinal, NULL);
1696 }
1697
1698 void ExtensionPrefs::InitializePageOrdinalMap(
1699 const ExtensionIdSet& extension_ids) {
1700 for (ExtensionIdSet::const_iterator ext_it = extension_ids.begin();
1701 ext_it != extension_ids.end(); ++ext_it) {
1702 UpdatePageOrdinalMap(StringOrdinal(), GetPageOrdinal(*ext_it));
1703 }
1704 }
1705
1706 void ExtensionPrefs::UpdatePageOrdinalMap(const StringOrdinal& old_value,
1707 const StringOrdinal& new_value) {
1708 if (new_value.IsValid())
1709 ++page_ordinal_map_[new_value];
1710
1711 if (old_value.IsValid()) {
1712 --page_ordinal_map_[old_value];
1713
1714 if (page_ordinal_map_[old_value] == 0)
1715 page_ordinal_map_.erase(old_value);
1716 }
1717 }
1718
1719 int ExtensionPrefs::PageStringOrdinalAsInteger(
1720 const StringOrdinal& page_ordinal) const {
1721 if (!page_ordinal.IsValid())
1722 return -1;
1723
1724 std::map<StringOrdinal, int>::const_iterator it =
1725 page_ordinal_map_.find(page_ordinal);
1726 if (it != page_ordinal_map_.end()) {
1727 return std::distance(page_ordinal_map_.begin(), it);
1728 } else {
1729 return -1;
1730 }
1731 }
1732
1733 StringOrdinal ExtensionPrefs::PageIntegerAsStringOrdinal(size_t page_index)
1734 const {
1735 // We shouldn't have a page_index that is more than 1 position away from the
1736 // current end as that would imply we have empty pages which is not allowed.
1737 CHECK_LE(page_index, page_ordinal_map_.size());
1738
1739 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1740 if (!extensions)
1741 return StringOrdinal();
1742
1743 if (page_index < page_ordinal_map_.size()) {
1744 std::map<StringOrdinal, int, StringOrdinalLessThan>::const_iterator it =
1745 page_ordinal_map_.begin();
1746 std::advance(it, page_index);
1747
1748 return it->first;
1749
1750 } else {
1751 if (page_ordinal_map_.empty())
1752 return StringOrdinal::CreateInitialOrdinal();
1753 else
1754 return page_ordinal_map_.rbegin()->first.CreateAfter();
1755 }
1524 } 1756 }
1525 1757
1526 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { 1758 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) {
1527 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); 1759 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp);
1528 } 1760 }
1529 1761
1530 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { 1762 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) {
1531 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, 1763 UpdateExtensionPref(extension_id, kPrefUserDraggedApp,
1532 Value::CreateBooleanValue(true)); 1764 Value::CreateBooleanValue(true));
1533 } 1765 }
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1660 // Create empty preferences dictionary for each extension (these dictionaries 1892 // Create empty preferences dictionary for each extension (these dictionaries
1661 // are pruned when persisting the preferences to disk). 1893 // are pruned when persisting the preferences to disk).
1662 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1894 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1663 ext_id != extension_ids.end(); ++ext_id) { 1895 ext_id != extension_ids.end(); ++ext_id) {
1664 ScopedExtensionPrefUpdate update(prefs_, *ext_id); 1896 ScopedExtensionPrefUpdate update(prefs_, *ext_id);
1665 // This creates an empty dictionary if none is stored. 1897 // This creates an empty dictionary if none is stored.
1666 update.Get(); 1898 update.Get();
1667 } 1899 }
1668 1900
1669 FixMissingPrefs(extension_ids); 1901 FixMissingPrefs(extension_ids);
1902 InitializePageOrdinalMap(extension_ids);
1670 MigratePermissions(extension_ids); 1903 MigratePermissions(extension_ids);
1904 MigrateAppIndex(extension_ids);
1671 1905
1672 // Store extension controlled preference values in the 1906 // Store extension controlled preference values in the
1673 // |extension_pref_value_map_|, which then informs the subscribers 1907 // |extension_pref_value_map_|, which then informs the subscribers
1674 // (ExtensionPrefStores) about the winning values. 1908 // (ExtensionPrefStores) about the winning values.
1675 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1909 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1676 ext_id != extension_ids.end(); ++ext_id) { 1910 ext_id != extension_ids.end(); ++ext_id) {
1677 extension_pref_value_map_->RegisterExtension( 1911 extension_pref_value_map_->RegisterExtension(
1678 *ext_id, 1912 *ext_id,
1679 GetInstallTime(*ext_id), 1913 GetInstallTime(*ext_id),
1680 !IsExtensionDisabled(*ext_id)); 1914 !IsExtensionDisabled(*ext_id));
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
1828 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, 2062 prefs->RegisterListPref(prefs::kExtensionInstallAllowList,
1829 PrefService::UNSYNCABLE_PREF); 2063 PrefService::UNSYNCABLE_PREF);
1830 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, 2064 prefs->RegisterListPref(prefs::kExtensionInstallDenyList,
1831 PrefService::UNSYNCABLE_PREF); 2065 PrefService::UNSYNCABLE_PREF);
1832 prefs->RegisterListPref(prefs::kExtensionInstallForceList, 2066 prefs->RegisterListPref(prefs::kExtensionInstallForceList,
1833 PrefService::UNSYNCABLE_PREF); 2067 PrefService::UNSYNCABLE_PREF);
1834 prefs->RegisterStringPref(kWebStoreLogin, 2068 prefs->RegisterStringPref(kWebStoreLogin,
1835 std::string() /* default_value */, 2069 std::string() /* default_value */,
1836 PrefService::UNSYNCABLE_PREF); 2070 PrefService::UNSYNCABLE_PREF);
1837 } 2071 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698