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

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: Fixing Page Name changes 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 423 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) { 864 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) {
852 UpdateExtensionPref( 865 UpdateExtensionPref(
853 *ext_id, explicit_hosts, hosts->DeepCopy()); 866 *ext_id, explicit_hosts, hosts->DeepCopy());
854 867
855 // We can get rid of the old one by setting it to an empty list. 868 // We can get rid of the old one by setting it to an empty list.
856 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue()); 869 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue());
857 } 870 }
858 } 871 }
859 } 872 }
860 873
874 void ExtensionPrefs::MigrateAppIndex(const ExtensionIdSet& extension_ids) {
875 if (extension_ids.empty())
876 return;
877
878 // Convert all the page index values to page ordinals. If there are any
akalin 2011/12/01 02:57:47 can't you do a two-level map, the outer one keyed
csharp 2011/12/01 20:15:09 Done.
879 // app launch values that need to be migrated, inserted them into a sorted
880 // set to be dealt with later.
881 std::multimap<int,const std::string*> app_launches_to_convert;
akalin 2011/12/01 02:57:47 space after 'int,'
csharp 2011/12/01 20:15:09 Done.
882 for (ExtensionIdSet::const_iterator ext_id = extension_ids.begin();
883 ext_id != extension_ids.end(); ++ext_id) {
884 int old_page_index = 0;
885 StringOrdinal page = GetPageOrdinal(*ext_id);
886 if (ReadExtensionPrefInteger(*ext_id,
887 kPrefPageIndexDeprecated,
888 &old_page_index)) {
889 // Since we require all earlier StringOrdinals to already exist in order
890 // to properly convert from integers and we are iteratoring though them in
891 // no given order, we create earlier StringOrdinal values as required.
892 // This should be filled in by the time we are done with this loop.
893 if (page_ordinal_map_.size() == 0)
akalin 2011/12/01 02:57:47 .empty()
csharp 2011/12/01 20:15:09 Done.
894 page_ordinal_map_[StringOrdinal::CreateInitialOrdinal()] = 0;
895 while (page_ordinal_map_.size() <= static_cast<size_t>(old_page_index)) {
896 StringOrdinal earlier_page =
897 page_ordinal_map_.rbegin()->first.CreateAfter();
898 page_ordinal_map_[earlier_page] = 0;
899 }
900
901 page = PageIntegerAsStringOrdinal(old_page_index);
902 SetPageOrdinal(*ext_id, page);
903 UpdateExtensionPref(*ext_id, kPrefPageIndexDeprecated, NULL);
904 }
905
906 int old_app_launch_index = 0;
907 if (ReadExtensionPrefInteger(*ext_id,
908 kPrefAppLaunchIndexDeprecated,
909 &old_app_launch_index)) {
910 // We can't update the app launch index value yet, because we use
911 // GetNextAppLaunchOrdinal to get the new ordinal value and it requires
912 // all the ordinals with lower values to have already been migrated.
913 // A valid page ordinal is also required because otherwise there is
914 // no page to add the app to.
915 if (page.IsValid())
916 app_launches_to_convert.insert(
917 std::pair<int,const std::string*>(
akalin 2011/12/01 02:57:47 use std::make_pair
csharp 2011/12/01 20:15:09 Done.
918 old_app_launch_index, &(*ext_id)));
919
920 UpdateExtensionPref(*ext_id, kPrefAppLaunchIndexDeprecated, NULL);
921 }
922 }
923
924 // Remove any empty pages that may have been added. This shouldn't occur,
925 // but double check here to prevent future problems with conversions between
926 // integers and StringOrdinals.
927 for (std::map<StringOrdinal, int, StringOrdinalLessThan>::iterator it =
928 page_ordinal_map_.begin(); it != page_ordinal_map_.end();) {
929 if (it->second == 0) {
930 std::map<StringOrdinal, int, StringOrdinalLessThan>::iterator prev_it =
931 it;
932 ++it;
933 page_ordinal_map_.erase(prev_it);
934 } else {
935 ++it;
936 }
937 }
938
939 if (app_launches_to_convert.empty())
940 return;
941
942 // Create the new app launch ordinals and remove the old preferences. Since
943 // the set is sorted, each time we migrate an apps index, we know that all of
944 // the remaining apps will appear further down the NTP than it or on a
945 // different page.
946 for (std::multimap<int, const std::string*>
947 ::const_iterator ext = app_launches_to_convert.begin();
948 ext != app_launches_to_convert.end(); ++ext) {
949 StringOrdinal page = GetPageOrdinal(*(ext->second));
950 SetAppLaunchOrdinal(*(ext->second),
951 CreateNextAppLaunchOrdinal(page));
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 const StringOrdinal& page_ordinal) {
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_ordinal.IsValid()) {
1108 page_index = GetNaturalAppPageIndex(); 1202 SetPageOrdinal(id, page_ordinal);
1109 extension_dict->Set(kPrefPageIndex, 1203 SetAppLaunchOrdinal(id, CreateNextAppLaunchOrdinal(page_ordinal));
1110 Value::CreateIntegerValue(page_index)); 1204
akalin 2011/12/01 02:57:47 remove extra space
csharp 2011/12/01 20:15:09 Done.
1111 extension_dict->Set(kPrefAppLaunchIndex, 1205 } else {
1112 Value::CreateIntegerValue(GetNextAppLaunchIndex(page_index))); 1206 StringOrdinal new_page_ordinal = GetNaturalAppPageOrdinal();
akalin 2011/12/01 02:57:47 by 'inline this code' i meant inline and eliminate
csharp 2011/12/01 20:15:09 Ok, I think I've fixed this but I'm not 100% sure
akalin 2011/12/01 20:42:46 I meant something like: StringOrdinal new_page_or
1207 SetPageOrdinal(id, new_page_ordinal);
1208 SetAppLaunchOrdinal(id, CreateNextAppLaunchOrdinal(new_page_ordinal));
1209 }
1113 } 1210 }
1211
1114 extension_pref_value_map_->RegisterExtension( 1212 extension_pref_value_map_->RegisterExtension(
1115 id, install_time, initial_state == Extension::ENABLED); 1213 id, install_time, initial_state == Extension::ENABLED);
1116 content_settings_store_->RegisterExtension( 1214 content_settings_store_->RegisterExtension(
1117 id, install_time, initial_state == Extension::ENABLED); 1215 id, install_time, initial_state == Extension::ENABLED);
1118 } 1216 }
1119 1217
1120 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, 1218 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id,
1121 const Extension::Location& location, 1219 const Extension::Location& location,
1122 bool external_uninstall) { 1220 bool external_uninstall) {
1123 // For external extensions, we save a preference reminding ourself not to try 1221 // For external extensions, we save a preference reminding ourself not to try
1124 // and install the extension anymore (except when |external_uninstall| is 1222 // and install the extension anymore (except when |external_uninstall| is
1125 // true, which signifies that the registry key was deleted or the pref file 1223 // true, which signifies that the registry key was deleted or the pref file
1126 // no longer lists the extension). 1224 // no longer lists the extension).
1127 if (!external_uninstall && Extension::IsExternalLocation(location)) { 1225 if (!external_uninstall && Extension::IsExternalLocation(location)) {
1128 UpdateExtensionPref(extension_id, kPrefState, 1226 UpdateExtensionPref(extension_id, kPrefState,
1129 Value::CreateIntegerValue( 1227 Value::CreateIntegerValue(
1130 Extension::EXTERNAL_EXTENSION_UNINSTALLED)); 1228 Extension::EXTERNAL_EXTENSION_UNINSTALLED));
1131 extension_pref_value_map_->SetExtensionState(extension_id, false); 1229 extension_pref_value_map_->SetExtensionState(extension_id, false);
1132 content_settings_store_->SetExtensionState(extension_id, false); 1230 content_settings_store_->SetExtensionState(extension_id, false);
1133 } else { 1231 } else {
1134 DeleteExtensionPrefs(extension_id); 1232 DeleteExtensionPrefs(extension_id);
1135 } 1233 }
1136 } 1234 }
1137 1235
1236 void ExtensionPrefs::OnExtensionMoved(
1237 const std::string& moved_extension_id,
1238 const std::string& predecessor_extension_id,
1239 const std::string& successor_extension_id) {
1240 // If there are no neighbors then no changes are required, so we can return.
1241 if (predecessor_extension_id.empty() && successor_extension_id.empty())
1242 return;
1243
1244 if (predecessor_extension_id.empty()) {
1245 SetAppLaunchOrdinal(
1246 moved_extension_id,
1247 GetAppLaunchOrdinal(successor_extension_id).CreateBefore());
1248 } else if (successor_extension_id.empty()) {
1249 SetAppLaunchOrdinal(
1250 moved_extension_id,
1251 GetAppLaunchOrdinal(predecessor_extension_id).CreateAfter());
1252 } else {
1253 StringOrdinal predecessor_ordinal =
akalin 2011/12/01 02:57:47 const ref
csharp 2011/12/01 20:15:09 Done.
1254 GetAppLaunchOrdinal(predecessor_extension_id);
1255 StringOrdinal successor_ordinal =
akalin 2011/12/01 02:57:47 const ref
csharp 2011/12/01 20:15:09 Done.
1256 GetAppLaunchOrdinal(successor_extension_id);
1257 SetAppLaunchOrdinal(moved_extension_id,
1258 predecessor_ordinal.CreateBetween(successor_ordinal));
1259 }
1260
1261 content::NotificationService::current()->Notify(
1262 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED,
1263 content::Source<ExtensionPrefs>(this),
1264 content::NotificationService::NoDetails());
1265 }
1266
1138 void ExtensionPrefs::SetExtensionState(const std::string& extension_id, 1267 void ExtensionPrefs::SetExtensionState(const std::string& extension_id,
1139 Extension::State state) { 1268 Extension::State state) {
1140 UpdateExtensionPref(extension_id, kPrefState, 1269 UpdateExtensionPref(extension_id, kPrefState,
1141 Value::CreateIntegerValue(state)); 1270 Value::CreateIntegerValue(state));
1142 bool enabled = (state == Extension::ENABLED); 1271 bool enabled = (state == Extension::ENABLED);
1143 extension_pref_value_map_->SetExtensionState(extension_id, enabled); 1272 extension_pref_value_map_->SetExtensionState(extension_id, enabled);
1144 content_settings_store_->SetExtensionState(extension_id, enabled); 1273 content_settings_store_->SetExtensionState(extension_id, enabled);
1145 } 1274 }
1146 1275
1147 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) { 1276 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) {
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
1435 return true; 1564 return true;
1436 } 1565 }
1437 return false; 1566 return false;
1438 } 1567 }
1439 1568
1440 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) { 1569 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) {
1441 prefs_->SetString(kWebStoreLogin, login); 1570 prefs_->SetString(kWebStoreLogin, login);
1442 SavePrefs(); 1571 SavePrefs();
1443 } 1572 }
1444 1573
1445 int ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) { 1574 void ExtensionPrefs::GetMinAndMaxAppLaunchOrdinalsOnPage(
1446 int value; 1575 const StringOrdinal& target_page_ordinal,
1447 if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value)) 1576 StringOrdinal* min_app_launch_value,
1448 return value; 1577 StringOrdinal* max_app_launch_value) const {
1578 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1579 CHECK(extensions);
1580 CHECK(target_page_ordinal.IsValid());
1581 CHECK(min_app_launch_value);
1582 CHECK(max_app_launch_value);
1449 1583
1450 return -1; 1584 for (DictionaryValue::key_iterator ext_it = extensions->begin_keys();
1451 } 1585 ext_it != extensions->end_keys(); ++ext_it) {
1586 StringOrdinal page_ordinal = GetPageOrdinal(*ext_it);
1587 if (page_ordinal.IsValid() && page_ordinal.Equal(target_page_ordinal)) {
1588 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(*ext_it);
akalin 2011/12/01 02:57:47 const ref
csharp 2011/12/01 20:15:09 Done.
1589 if (app_launch_ordinal.IsValid()) {
1590 if (!min_app_launch_value->IsValid() ||
1591 app_launch_ordinal.LessThan(*min_app_launch_value))
1592 *min_app_launch_value = app_launch_ordinal;
1452 1593
1453 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, 1594 if (!max_app_launch_value->IsValid() ||
1454 int index) { 1595 max_app_launch_value->LessThan(app_launch_ordinal))
akalin 2011/12/01 02:57:47 surely you mean GreaterThan? Why didn't any tests
csharp 2011/12/01 20:15:09 This was the correct call, the operators are in th
1455 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, 1596 *max_app_launch_value = app_launch_ordinal;
1456 Value::CreateIntegerValue(index)); 1597 }
1457 } 1598 }
1458
1459 int ExtensionPrefs::GetNextAppLaunchIndex(int on_page) {
1460 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1461 if (!extensions)
1462 return 0;
1463
1464 int max_value = -1;
1465 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys();
1466 extension_id != extensions->end_keys(); ++extension_id) {
1467 int value = GetAppLaunchIndex(*extension_id);
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 } 1599 }
1492 } 1600 }
1493 1601
1494 void ExtensionPrefs::SetAppLauncherOrder( 1602 StringOrdinal ExtensionPrefs::GetAppLaunchOrdinal(
1495 const std::vector<std::string>& extension_ids) { 1603 const std::string& extension_id) const {
1496 for (size_t i = 0; i < extension_ids.size(); ++i) 1604 std::string raw_value;
1497 SetAppLaunchIndex(extension_ids.at(i), i); 1605 // If the preference read fails then raw_value will still be unset and we
1498 1606 // will return an invalid StringOrdinal to signal that no app launch ordinal
1499 content::NotificationService::current()->Notify( 1607 // was found.
1500 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, 1608 ReadExtensionPrefString(extension_id, kPrefAppLaunchOrdinal, &raw_value);
1501 content::Source<ExtensionPrefs>(this), 1609 return StringOrdinal(raw_value);
1502 content::NotificationService::NoDetails());
1503 } 1610 }
1504 1611
1505 int ExtensionPrefs::GetPageIndex(const std::string& extension_id) { 1612 void ExtensionPrefs::SetAppLaunchOrdinal(const std::string& extension_id,
1506 int value = -1; 1613 const StringOrdinal& ordinal) {
1507 ReadExtensionPrefInteger(extension_id, kPrefPageIndex, &value); 1614 UpdateExtensionPref(extension_id, kPrefAppLaunchOrdinal,
1508 return value; 1615 Value::CreateStringValue(ordinal.ToString()));
1509 } 1616 }
1510 1617
1511 void ExtensionPrefs::SetPageIndex(const std::string& extension_id, int index) { 1618 StringOrdinal ExtensionPrefs::CreateFirstAppLaunchOrdinal(
1512 UpdateExtensionPref(extension_id, kPrefPageIndex, 1619 const StringOrdinal& page_ordinal) const {
1513 Value::CreateIntegerValue(index)); 1620 StringOrdinal max_ordinal;
1621 StringOrdinal min_ordinal;
1622 GetMinAndMaxAppLaunchOrdinalsOnPage(page_ordinal, &min_ordinal, &max_ordinal);
1623
1624 if (min_ordinal.IsValid())
1625 return min_ordinal.CreateBefore();
1626 else
1627 return StringOrdinal::CreateInitialOrdinal();
1514 } 1628 }
1515 1629
1516 void ExtensionPrefs::ClearPageIndex(const std::string& extension_id) { 1630 StringOrdinal ExtensionPrefs::CreateNextAppLaunchOrdinal(
1517 UpdateExtensionPref(extension_id, kPrefPageIndex, NULL); 1631 const StringOrdinal& page_ordinal) const {
1632 StringOrdinal max_ordinal;
1633 StringOrdinal min_ordinal;
1634 GetMinAndMaxAppLaunchOrdinalsOnPage(page_ordinal, &min_ordinal, &max_ordinal);
1635
1636 if (max_ordinal.IsValid())
1637 return max_ordinal.CreateAfter();
1638 else
1639 return StringOrdinal::CreateInitialOrdinal();
1640 }
1641
1642 StringOrdinal ExtensionPrefs::CreateFirstAppPage() const {
1643 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1644 CHECK(extensions);
1645
1646 if (page_ordinal_map_.empty())
1647 return StringOrdinal::CreateInitialOrdinal();
1648
1649 return page_ordinal_map_.begin()->first;
1650 }
1651
1652 StringOrdinal ExtensionPrefs::GetNaturalAppPageOrdinal() const {
1653 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1654 CHECK(extensions);
1655
1656 if (page_ordinal_map_.empty())
1657 return StringOrdinal::CreateInitialOrdinal();
1658
1659 for (std::map<StringOrdinal, int>::const_iterator it =
1660 page_ordinal_map_.begin(); it != page_ordinal_map_.end(); ++it) {
1661 if (it->second < kNaturalAppPageSize)
1662 return it->first;
1663 }
1664
1665 // Add a new page as all existing pages are full.
1666 StringOrdinal last_element = page_ordinal_map_.rbegin()->first;
1667 return last_element.CreateAfter();
1668 }
1669
1670 StringOrdinal ExtensionPrefs::GetPageOrdinal(const std::string& extension_id)
1671 const {
1672 std::string raw_data;
1673 // If the preference read fails then raw_data will still be unset and we will
1674 // return an invalid StringOrdinal to signal that no page ordinal was found.
1675 ReadExtensionPrefString(extension_id, kPrefPageOrdinal, &raw_data);
1676 return StringOrdinal(raw_data);
1677 }
1678
1679 void ExtensionPrefs::SetPageOrdinal(const std::string& extension_id,
1680 const StringOrdinal& page_ordinal) {
1681 UpdatePageOrdinalMap(GetPageOrdinal(extension_id), page_ordinal);
1682 UpdateExtensionPref(extension_id, kPrefPageOrdinal,
1683 Value::CreateStringValue(page_ordinal.ToString()));
1684 }
1685
1686 void ExtensionPrefs::ClearPageOrdinal(const std::string& extension_id) {
1687 UpdatePageOrdinalMap(GetPageOrdinal(extension_id), StringOrdinal());
1688 UpdateExtensionPref(extension_id, kPrefPageOrdinal, NULL);
1689 }
1690
1691 void ExtensionPrefs::InitializePageOrdinalMap(
1692 const ExtensionIdSet& extension_ids) {
1693 for (ExtensionIdSet::const_iterator ext_it = extension_ids.begin();
1694 ext_it != extension_ids.end(); ++ext_it) {
1695 UpdatePageOrdinalMap(StringOrdinal(), GetPageOrdinal(*ext_it));
1696 }
1697 }
1698
1699 void ExtensionPrefs::UpdatePageOrdinalMap(const StringOrdinal& old_value,
1700 const StringOrdinal& new_value) {
1701 if (new_value.IsValid())
1702 ++page_ordinal_map_[new_value];
1703
1704 if (old_value.IsValid()) {
1705 --page_ordinal_map_[old_value];
1706
1707 if (page_ordinal_map_[old_value] == 0)
1708 page_ordinal_map_.erase(old_value);
1709 }
1710 }
1711
1712 int ExtensionPrefs::PageStringOrdinalAsInteger(
1713 const StringOrdinal& page_ordinal) const {
1714 if (!page_ordinal.IsValid())
1715 return -1;
1716
1717 std::map<StringOrdinal, int>::const_iterator it =
1718 page_ordinal_map_.find(page_ordinal);
1719 if (it != page_ordinal_map_.end()) {
1720 return std::distance(page_ordinal_map_.begin(), it);
1721 } else {
1722 return -1;
1723 }
1724 }
1725
1726 StringOrdinal ExtensionPrefs::PageIntegerAsStringOrdinal(size_t page_index)
1727 const {
1728 // We shouldn't have a page_index that is more than 1 position away from the
1729 // current end as that would imply we have empty pages which is not allowed.
1730 CHECK_LE(page_index, page_ordinal_map_.size());
1731
1732 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
1733 if (!extensions)
1734 return StringOrdinal();
1735
1736 if (page_index < page_ordinal_map_.size()) {
1737 std::map<StringOrdinal, int, StringOrdinalLessThan>::const_iterator it =
1738 page_ordinal_map_.begin();
1739 std::advance(it, page_index);
1740
1741 return it->first;
1742
1743 } else {
1744 if (page_ordinal_map_.empty())
1745 return StringOrdinal::CreateInitialOrdinal();
1746 else
1747 return page_ordinal_map_.rbegin()->first.CreateAfter();
1748 }
1518 } 1749 }
1519 1750
1520 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { 1751 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) {
1521 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); 1752 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp);
1522 } 1753 }
1523 1754
1524 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { 1755 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) {
1525 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, 1756 UpdateExtensionPref(extension_id, kPrefUserDraggedApp,
1526 Value::CreateBooleanValue(true)); 1757 Value::CreateBooleanValue(true));
1527 } 1758 }
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1654 // Create empty preferences dictionary for each extension (these dictionaries 1885 // Create empty preferences dictionary for each extension (these dictionaries
1655 // are pruned when persisting the preferences to disk). 1886 // are pruned when persisting the preferences to disk).
1656 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1887 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1657 ext_id != extension_ids.end(); ++ext_id) { 1888 ext_id != extension_ids.end(); ++ext_id) {
1658 ScopedExtensionPrefUpdate update(prefs_, *ext_id); 1889 ScopedExtensionPrefUpdate update(prefs_, *ext_id);
1659 // This creates an empty dictionary if none is stored. 1890 // This creates an empty dictionary if none is stored.
1660 update.Get(); 1891 update.Get();
1661 } 1892 }
1662 1893
1663 FixMissingPrefs(extension_ids); 1894 FixMissingPrefs(extension_ids);
1895 InitializePageOrdinalMap(extension_ids);
1664 MigratePermissions(extension_ids); 1896 MigratePermissions(extension_ids);
1897 MigrateAppIndex(extension_ids);
1665 1898
1666 // Store extension controlled preference values in the 1899 // Store extension controlled preference values in the
1667 // |extension_pref_value_map_|, which then informs the subscribers 1900 // |extension_pref_value_map_|, which then informs the subscribers
1668 // (ExtensionPrefStores) about the winning values. 1901 // (ExtensionPrefStores) about the winning values.
1669 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); 1902 for (ExtensionIdSet::iterator ext_id = extension_ids.begin();
1670 ext_id != extension_ids.end(); ++ext_id) { 1903 ext_id != extension_ids.end(); ++ext_id) {
1671 extension_pref_value_map_->RegisterExtension( 1904 extension_pref_value_map_->RegisterExtension(
1672 *ext_id, 1905 *ext_id,
1673 GetInstallTime(*ext_id), 1906 GetInstallTime(*ext_id),
1674 !IsExtensionDisabled(*ext_id)); 1907 !IsExtensionDisabled(*ext_id));
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
1822 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, 2055 prefs->RegisterListPref(prefs::kExtensionInstallAllowList,
1823 PrefService::UNSYNCABLE_PREF); 2056 PrefService::UNSYNCABLE_PREF);
1824 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, 2057 prefs->RegisterListPref(prefs::kExtensionInstallDenyList,
1825 PrefService::UNSYNCABLE_PREF); 2058 PrefService::UNSYNCABLE_PREF);
1826 prefs->RegisterListPref(prefs::kExtensionInstallForceList, 2059 prefs->RegisterListPref(prefs::kExtensionInstallForceList,
1827 PrefService::UNSYNCABLE_PREF); 2060 PrefService::UNSYNCABLE_PREF);
1828 prefs->RegisterStringPref(kWebStoreLogin, 2061 prefs->RegisterStringPref(kWebStoreLogin,
1829 std::string() /* default_value */, 2062 std::string() /* default_value */,
1830 PrefService::UNSYNCABLE_PREF); 2063 PrefService::UNSYNCABLE_PREF);
1831 } 2064 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698