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