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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 const std::string extension_id_; | 234 const std::string extension_id_; |
233 const std::string incognito_or_regular_path_; | 235 const std::string incognito_or_regular_path_; |
234 | 236 |
235 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionControlledPrefUpdate); | 237 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionControlledPrefUpdate); |
236 }; | 238 }; |
237 | 239 |
238 std::string JoinPrefs(std::string parent, const char* child) { | 240 std::string JoinPrefs(std::string parent, const char* child) { |
239 return parent + "." + child; | 241 return parent + "." + child; |
240 } | 242 } |
241 | 243 |
244 // A simple structure used when sorting applications based on their app launch | |
245 // index. This is used by the code that migrates the data from integers to | |
246 // StringOrdinals. | |
247 struct ApplicationNTPOldAppLaunch { | |
248 const std::string* application_id; | |
249 const int app_launch_index; | |
250 | |
251 ApplicationNTPOldAppLaunch() : application_id(0), | |
252 app_launch_index(0) {} | |
253 | |
254 ApplicationNTPOldAppLaunch(const std::string& app_id, int app_launch) | |
255 : application_id(&app_id), | |
256 app_launch_index(app_launch) {} | |
257 }; | |
258 | |
259 struct SortByNTPOldAppLaunch { | |
260 bool operator() (const ApplicationNTPOldAppLaunch& lhs, | |
261 const ApplicationNTPOldAppLaunch& rhs) const { | |
262 return lhs.app_launch_index < rhs.app_launch_index; | |
263 } | |
264 }; | |
265 | |
242 } // namespace | 266 } // namespace |
243 | 267 |
244 ExtensionPrefs::ExtensionPrefs( | 268 ExtensionPrefs::ExtensionPrefs( |
245 PrefService* prefs, | 269 PrefService* prefs, |
246 const FilePath& root_dir, | 270 const FilePath& root_dir, |
247 ExtensionPrefValueMap* extension_pref_value_map) | 271 ExtensionPrefValueMap* extension_pref_value_map) |
248 : prefs_(prefs), | 272 : prefs_(prefs), |
249 install_directory_(root_dir), | 273 install_directory_(root_dir), |
250 extension_pref_value_map_(extension_pref_value_map), | 274 extension_pref_value_map_(extension_pref_value_map), |
251 content_settings_store_(new ExtensionContentSettingsStore()) { | 275 content_settings_store_(new ExtensionContentSettingsStore()) { |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 const DictionaryValue* ext = GetExtensionPref(extension_id); | 432 const DictionaryValue* ext = GetExtensionPref(extension_id); |
409 ListValue* out = NULL; | 433 ListValue* out = NULL; |
410 if (!ext || !ext->GetList(pref_key, &out)) | 434 if (!ext || !ext->GetList(pref_key, &out)) |
411 return false; | 435 return false; |
412 if (out_value) | 436 if (out_value) |
413 *out_value = out; | 437 *out_value = out; |
414 | 438 |
415 return true; | 439 return true; |
416 } | 440 } |
417 | 441 |
442 bool ExtensionPrefs::ReadExtensionPrefString( | |
443 const std::string& extension_id, const std::string& pref_key, | |
444 std::string* out_value) const { | |
445 const DictionaryValue* ext = GetExtensionPref(extension_id); | |
446 | |
447 if (!ext || !ext->GetString(pref_key, out_value)) | |
448 return false; | |
449 | |
450 return true; | |
451 } | |
452 | |
418 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet( | 453 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet( |
419 const std::string& extension_id, | 454 const std::string& extension_id, |
420 const std::string& pref_key, | 455 const std::string& pref_key, |
421 URLPatternSet* result, | 456 URLPatternSet* result, |
422 int valid_schemes) { | 457 int valid_schemes) { |
423 const ListValue* value = NULL; | 458 const ListValue* value = NULL; |
424 if (!ReadExtensionPrefList(extension_id, pref_key, &value)) | 459 if (!ReadExtensionPrefList(extension_id, pref_key, &value)) |
425 return false; | 460 return false; |
426 | 461 |
427 result->ClearPatterns(); | 462 result->ClearPatterns(); |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
851 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) { | 886 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) { |
852 UpdateExtensionPref( | 887 UpdateExtensionPref( |
853 *ext_id, explicit_hosts, hosts->DeepCopy()); | 888 *ext_id, explicit_hosts, hosts->DeepCopy()); |
854 | 889 |
855 // We can get rid of the old one by setting it to an empty list. | 890 // We can get rid of the old one by setting it to an empty list. |
856 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue()); | 891 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue()); |
857 } | 892 } |
858 } | 893 } |
859 } | 894 } |
860 | 895 |
896 void ExtensionPrefs::MigrateAppIndex(const ExtensionIdSet& extension_ids) { | |
897 if (extension_ids.empty()) | |
898 return; | |
899 | |
900 // Convert all the page index values to page ordinals. If there are any | |
901 // app launch values that need to be migrated, inserted them into a sorted | |
902 // set to be dealt with later. | |
903 std::multiset<ApplicationNTPOldAppLaunch, SortByNTPOldAppLaunch> | |
904 app_launches_to_convert; | |
905 for (ExtensionIdSet::const_iterator ext_id = extension_ids.begin(); | |
906 ext_id != extension_ids.end(); ++ext_id) { | |
907 int old_page_index = 0; | |
908 if (ReadExtensionPrefInteger(*ext_id, | |
909 kPrefPageIndexDeprecated, | |
910 &old_page_index)) { | |
911 SetPageOrdinal(*ext_id, PageIntegerAsStringOrdinal(old_page_index)); | |
912 UpdateExtensionPref(*ext_id, kPrefPageIndexDeprecated, NULL); | |
913 } | |
914 | |
915 int old_app_launch_index = 0; | |
916 if (ReadExtensionPrefInteger(*ext_id, | |
917 kPrefAppLaunchIndexDeprecated, | |
918 &old_app_launch_index)) | |
919 // We can't update the app launch index value yet, because we use | |
920 // GetNextAppLaunchOrdinal to get the new ordinal value and it requires | |
921 // all the ordinals with lower values to have already been migrated. | |
922 app_launches_to_convert.insert(ApplicationNTPOldAppLaunch( | |
923 *ext_id, | |
924 old_app_launch_index)); | |
925 } | |
926 | |
927 if (app_launches_to_convert.empty()) | |
928 return; | |
929 | |
930 // Create the new app launch ordinals and remove the old preferences. Since | |
931 // the set is sorted, each time we migrate an apps index, we know that all of | |
932 // the remaining apps will appear further down the NTP than it or on a | |
933 // different page. | |
934 for (std::multiset<ApplicationNTPOldAppLaunch, SortByNTPOldAppLaunch> | |
935 ::const_iterator ext = app_launches_to_convert.begin(); | |
936 ext != app_launches_to_convert.end(); ++ext) { | |
937 int old_app_launch_index; | |
938 if (ReadExtensionPrefInteger(*(ext->application_id), | |
akalin
2011/11/23 18:50:19
why is this conditional still needed? you already
csharp
2011/11/23 22:10:01
Done.
| |
939 kPrefAppLaunchIndexDeprecated, | |
940 &old_app_launch_index)) { | |
941 StringOrdinal page = GetPageOrdinal(*(ext->application_id)); | |
942 if (!page.IsValid()) { | |
akalin
2011/11/23 18:50:19
can you fold this into the first loop? it can be
csharp
2011/11/23 22:10:01
I thought it could, but unit tests proved me wrong
akalin
2011/11/29 18:39:09
Ah, I see. But it can certainly still be in the f
csharp
2011/11/29 21:21:40
Done.
| |
943 page = GetNaturalAppPageOrdinal(); | |
944 SetPageOrdinal(*(ext->application_id), page); | |
945 } | |
946 | |
947 SetAppLaunchOrdinal(*(ext->application_id), | |
948 GetNextAppLaunchOrdinal(page)); | |
949 UpdateExtensionPref( | |
akalin
2011/11/23 18:50:19
can you do this in the first loop?
csharp
2011/11/23 22:10:01
Done.
| |
950 *(ext->application_id), kPrefAppLaunchIndexDeprecated, NULL); | |
951 } | |
952 } | |
953 } | |
954 | |
861 ExtensionPermissionSet* ExtensionPrefs::GetGrantedPermissions( | 955 ExtensionPermissionSet* ExtensionPrefs::GetGrantedPermissions( |
862 const std::string& extension_id) { | 956 const std::string& extension_id) { |
863 CHECK(Extension::IdIsValid(extension_id)); | 957 CHECK(Extension::IdIsValid(extension_id)); |
864 return ReadExtensionPrefPermissionSet(extension_id, kPrefGrantedPermissions); | 958 return ReadExtensionPrefPermissionSet(extension_id, kPrefGrantedPermissions); |
865 } | 959 } |
866 | 960 |
867 void ExtensionPrefs::AddGrantedPermissions( | 961 void ExtensionPrefs::AddGrantedPermissions( |
868 const std::string& extension_id, | 962 const std::string& extension_id, |
869 const ExtensionPermissionSet* permissions) { | 963 const ExtensionPermissionSet* permissions) { |
870 CHECK(Extension::IdIsValid(extension_id)); | 964 CHECK(Extension::IdIsValid(extension_id)); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1065 iter != extension_ids.end(); ++iter) { | 1159 iter != extension_ids.end(); ++iter) { |
1066 toolbar_order->Append(new StringValue(*iter)); | 1160 toolbar_order->Append(new StringValue(*iter)); |
1067 } | 1161 } |
1068 SavePrefs(); | 1162 SavePrefs(); |
1069 } | 1163 } |
1070 | 1164 |
1071 void ExtensionPrefs::OnExtensionInstalled( | 1165 void ExtensionPrefs::OnExtensionInstalled( |
1072 const Extension* extension, | 1166 const Extension* extension, |
1073 Extension::State initial_state, | 1167 Extension::State initial_state, |
1074 bool from_webstore, | 1168 bool from_webstore, |
1075 int page_index) { | 1169 StringOrdinal page_ordinal) { |
akalin
2011/11/23 18:50:19
const ref
csharp
2011/11/23 22:10:01
Done.
| |
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 Loading... | |
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 page_ordinal = GetNaturalAppPageOrdinal(); |
1109 extension_dict->Set(kPrefPageIndex, | 1203 extension_dict->Set(kPrefPageOrdinal, |
1110 Value::CreateIntegerValue(page_index)); | 1204 Value::CreateStringValue(page_ordinal.ToString())); |
1111 extension_dict->Set(kPrefAppLaunchIndex, | 1205 UpdatePageOrdinalMap(StringOrdinal(), page_ordinal); |
1112 Value::CreateIntegerValue(GetNextAppLaunchIndex(page_index))); | 1206 |
1207 extension_dict->Set(kPrefAppLaunchOrdinal, | |
1208 Value::CreateStringValue( | |
1209 GetNextAppLaunchOrdinal(page_ordinal).ToString())); | |
1113 } | 1210 } |
1114 extension_pref_value_map_->RegisterExtension( | 1211 extension_pref_value_map_->RegisterExtension( |
1115 id, install_time, initial_state == Extension::ENABLED); | 1212 id, install_time, initial_state == Extension::ENABLED); |
1116 content_settings_store_->RegisterExtension( | 1213 content_settings_store_->RegisterExtension( |
1117 id, install_time, initial_state == Extension::ENABLED); | 1214 id, install_time, initial_state == Extension::ENABLED); |
1118 } | 1215 } |
1119 | 1216 |
1120 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, | 1217 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, |
1121 const Extension::Location& location, | 1218 const Extension::Location& location, |
1122 bool external_uninstall) { | 1219 bool external_uninstall) { |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1435 return true; | 1532 return true; |
1436 } | 1533 } |
1437 return false; | 1534 return false; |
1438 } | 1535 } |
1439 | 1536 |
1440 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) { | 1537 void ExtensionPrefs::SetWebStoreLogin(const std::string& login) { |
1441 prefs_->SetString(kWebStoreLogin, login); | 1538 prefs_->SetString(kWebStoreLogin, login); |
1442 SavePrefs(); | 1539 SavePrefs(); |
1443 } | 1540 } |
1444 | 1541 |
1445 int ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) { | 1542 bool ExtensionPrefs::GetMaxAndMinAppLaunchOrdinalsOnPage( |
1446 int value; | 1543 const StringOrdinal& target_page_ordinal, |
1447 if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value)) | 1544 StringOrdinal* min_app_launch_value, |
1448 return value; | 1545 StringOrdinal* max_app_launch_value) const { |
1546 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | |
1547 CHECK(extensions); | |
1548 CHECK(target_page_ordinal.IsValid()); | |
1549 CHECK(min_app_launch_value); | |
1550 CHECK(max_app_launch_value); | |
1449 | 1551 |
1450 return -1; | 1552 StringOrdinal* min_value = NULL; |
1553 StringOrdinal* max_value = NULL; | |
1554 for (DictionaryValue::key_iterator ext_it = extensions->begin_keys(); | |
1555 ext_it != extensions->end_keys(); ++ext_it) { | |
1556 StringOrdinal page_ordinal = GetPageOrdinal(*ext_it); | |
1557 if (page_ordinal.IsValid() && page_ordinal.Equal(target_page_ordinal)) { | |
1558 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(*ext_it); | |
1559 if (app_launch_ordinal.IsValid()) { | |
1560 if (min_value == NULL || !min_value->IsValid() || | |
1561 app_launch_ordinal.LessThan(*min_value)) | |
1562 min_value = &app_launch_ordinal; | |
1563 | |
1564 if (max_value == NULL || !max_value->IsValid() || | |
1565 max_value->LessThan(app_launch_ordinal)) | |
1566 max_value = &app_launch_ordinal; | |
1567 } | |
1568 } | |
1569 } | |
1570 | |
1571 if (min_value != NULL) | |
1572 *min_app_launch_value = *min_value; | |
1573 if (max_value != NULL) | |
1574 *max_app_launch_value = *max_value; | |
1575 | |
1576 // If |min_app_launch_value| is valid then we had at least 1 app on the page, | |
1577 // so both positions are valid. | |
1578 return min_app_launch_value->IsValid(); | |
1451 } | 1579 } |
1452 | 1580 |
1453 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, | 1581 StringOrdinal ExtensionPrefs::GetFirstAppPage() const { |
1454 int index) { | 1582 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
1455 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, | 1583 CHECK(extensions); |
1456 Value::CreateIntegerValue(index)); | 1584 |
1585 if (page_ordinal_map_.empty()) | |
1586 return StringOrdinal::CreateInitialOrdinal(); | |
1587 | |
1588 return page_ordinal_map_.begin()->first; | |
1457 } | 1589 } |
1458 | 1590 |
1459 int ExtensionPrefs::GetNextAppLaunchIndex(int on_page) { | 1591 StringOrdinal ExtensionPrefs::CreateFirstAppLaunchOrdinal( |
1460 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 1592 const StringOrdinal& page_ordinal) const { |
1461 if (!extensions) | 1593 StringOrdinal max_ordinal; |
1462 return 0; | 1594 StringOrdinal min_ordinal; |
1595 GetMaxAndMinAppLaunchOrdinalsOnPage(page_ordinal, &min_ordinal, &max_ordinal); | |
1463 | 1596 |
1464 int max_value = -1; | 1597 if (min_ordinal.IsValid()) |
1465 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); | 1598 return min_ordinal.CreateBefore(); |
1466 extension_id != extensions->end_keys(); ++extension_id) { | 1599 else |
1467 int value = GetAppLaunchIndex(*extension_id); | 1600 return StringOrdinal::CreateInitialOrdinal(); |
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 } | 1601 } |
1474 | 1602 |
1475 int ExtensionPrefs::GetNaturalAppPageIndex() { | 1603 StringOrdinal ExtensionPrefs::GetAppLaunchOrdinal( |
1604 const std::string& extension_id) const { | |
1605 std::string raw_value; | |
1606 // If the preference read fails then raw_value will still be unset and we | |
1607 // will return an invalid StringOrdinal to signal that no app launch ordinal | |
1608 // was found. | |
1609 ReadExtensionPrefString(extension_id, kPrefAppLaunchOrdinal, &raw_value); | |
1610 return StringOrdinal(raw_value); | |
1611 } | |
1612 | |
1613 void ExtensionPrefs::SetAppLaunchOrdinal(const std::string& extension_id, | |
1614 const StringOrdinal& ordinal) { | |
1615 UpdateExtensionPref(extension_id, kPrefAppLaunchOrdinal, | |
1616 Value::CreateStringValue(ordinal.ToString())); | |
1617 } | |
1618 | |
1619 StringOrdinal ExtensionPrefs::GetNextAppLaunchOrdinal( | |
1620 const StringOrdinal& page_ordinal) const { | |
1621 StringOrdinal max_ordinal; | |
1622 StringOrdinal min_ordinal; | |
1623 GetMaxAndMinAppLaunchOrdinalsOnPage(page_ordinal, &min_ordinal, &max_ordinal); | |
1624 | |
1625 if (max_ordinal.IsValid()) | |
1626 return max_ordinal.CreateAfter(); | |
1627 else | |
1628 return StringOrdinal::CreateInitialOrdinal(); | |
1629 } | |
1630 | |
1631 StringOrdinal ExtensionPrefs::GetNaturalAppPageOrdinal() const { | |
1476 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 1632 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
1477 if (!extensions) | 1633 CHECK(extensions); |
1478 return 0; | |
1479 | 1634 |
1480 std::map<int, int> page_counts; | 1635 if (page_ordinal_map_.empty()) |
1481 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); | 1636 return StringOrdinal::CreateInitialOrdinal(); |
1482 extension_id != extensions->end_keys(); ++extension_id) { | 1637 |
1483 int page_index = GetPageIndex(*extension_id); | 1638 std::map<StringOrdinal, int>::const_iterator it = page_ordinal_map_.begin(); |
1484 if (page_index >= 0) | 1639 for (; it != page_ordinal_map_.end(); ++it) { |
1485 page_counts[page_index] = page_counts[page_index] + 1; | 1640 if (it->second < kNaturalAppPageSize) |
1641 return it->first; | |
1486 } | 1642 } |
1487 for (int i = 0; ; i++) { | 1643 |
1488 std::map<int, int>::const_iterator it = page_counts.find(i); | 1644 // Add a new page as all existing pages are full. |
1489 if (it == page_counts.end() || it->second < kNaturalAppPageSize) | 1645 StringOrdinal last_element = page_ordinal_map_.rbegin()->first; |
1490 return i; | 1646 return last_element.CreateAfter(); |
1491 } | |
1492 } | 1647 } |
1493 | 1648 |
1494 void ExtensionPrefs::SetAppLauncherOrder( | 1649 void ExtensionPrefs::SetAppLauncherOrder( |
1495 const std::vector<std::string>& extension_ids) { | 1650 const std::vector<std::string>& extension_ids, |
1496 for (size_t i = 0; i < extension_ids.size(); ++i) | 1651 const std::string& moved_extension_id) { |
1497 SetAppLaunchIndex(extension_ids.at(i), i); | 1652 std::vector<std::string>::const_iterator it = |
1653 find(extension_ids.begin(), extension_ids.end(), moved_extension_id); | |
1654 CHECK(it != extension_ids.end()); | |
1655 size_t position = it - extension_ids.begin(); | |
1656 | |
1657 // We need at least 2 apps for the moved apps old value to be invalidated. | |
1658 if (extension_ids.size() >= 2) { | |
1659 if (position == 0U) { | |
1660 // The app is moving to the start of the group, so we need to get the | |
1661 // previous first StringOrdinal and create the new value before it. | |
1662 ++it; | |
1663 SetAppLaunchOrdinal(moved_extension_id, | |
1664 GetAppLaunchOrdinal(*it).CreateBefore()); | |
1665 } else if (position == extension_ids.size() - 1U) { | |
1666 // The app is moving to the end of the group, so we need to get the | |
1667 // previous last StringOrdinal and create the new value after it. | |
1668 --it; | |
1669 SetAppLaunchOrdinal(moved_extension_id, | |
1670 GetAppLaunchOrdinal(*it).CreateAfter()); | |
1671 } else { | |
1672 // The app is going into a middle position, which means there are valid | |
1673 // StringOrdinals before and after it. We get the new adjacent | |
1674 // StringOrdinals and create our new value between them. | |
1675 ++it; | |
1676 StringOrdinal value_after = GetAppLaunchOrdinal(*it); | |
1677 it -= 2; | |
1678 StringOrdinal value_before = GetAppLaunchOrdinal(*it); | |
1679 SetAppLaunchOrdinal(moved_extension_id, | |
1680 value_before.CreateBetween(value_after)); | |
1681 } | |
1682 } | |
1498 | 1683 |
1499 content::NotificationService::current()->Notify( | 1684 content::NotificationService::current()->Notify( |
1500 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, | 1685 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, |
1501 content::Source<ExtensionPrefs>(this), | 1686 content::Source<ExtensionPrefs>(this), |
1502 content::NotificationService::NoDetails()); | 1687 content::NotificationService::NoDetails()); |
1503 } | 1688 } |
1504 | 1689 |
1505 int ExtensionPrefs::GetPageIndex(const std::string& extension_id) { | 1690 StringOrdinal ExtensionPrefs::GetPageOrdinal(const std::string& extension_id) |
1506 int value = -1; | 1691 const { |
1507 ReadExtensionPrefInteger(extension_id, kPrefPageIndex, &value); | 1692 std::string raw_data; |
1508 return value; | 1693 // If the preference read fails then raw_value will still be unset and we will |
akalin
2011/11/23 18:50:19
raw_value -> raw_data
csharp
2011/11/23 22:10:01
Done.
| |
1694 // return an invalid StringOrdinal to signal that no page ordinal was found. | |
1695 ReadExtensionPrefString(extension_id, kPrefPageOrdinal, &raw_data); | |
1696 return StringOrdinal(raw_data); | |
1509 } | 1697 } |
1510 | 1698 |
1511 void ExtensionPrefs::SetPageIndex(const std::string& extension_id, int index) { | 1699 void ExtensionPrefs::SetPageOrdinal(const std::string& extension_id, |
1512 UpdateExtensionPref(extension_id, kPrefPageIndex, | 1700 const StringOrdinal& page_ordinal) { |
akalin
2011/11/23 18:50:19
indent
csharp
2011/11/23 22:10:01
Done.
| |
1513 Value::CreateIntegerValue(index)); | 1701 UpdatePageOrdinalMap(GetPageOrdinal(extension_id), page_ordinal); |
1702 UpdateExtensionPref(extension_id, kPrefPageOrdinal, | |
1703 Value::CreateStringValue(page_ordinal.ToString())); | |
1514 } | 1704 } |
1515 | 1705 |
1516 void ExtensionPrefs::ClearPageIndex(const std::string& extension_id) { | 1706 void ExtensionPrefs::ClearPageOrdinal(const std::string& extension_id) { |
1517 UpdateExtensionPref(extension_id, kPrefPageIndex, NULL); | 1707 UpdatePageOrdinalMap(GetPageOrdinal(extension_id), StringOrdinal()); |
1708 UpdateExtensionPref(extension_id, kPrefPageOrdinal, NULL); | |
1709 } | |
1710 | |
1711 void ExtensionPrefs::InitializePageOrdinalMap( | |
1712 const ExtensionIdSet& extension_ids) { | |
1713 for (ExtensionIdSet::const_iterator ext_it = extension_ids.begin(); | |
1714 ext_it != extension_ids.end(); ++ext_it) { | |
1715 UpdatePageOrdinalMap(StringOrdinal(), GetPageOrdinal(*ext_it)); | |
1716 } | |
1717 } | |
1718 | |
1719 void ExtensionPrefs::UpdatePageOrdinalMap(const StringOrdinal& old_value, | |
1720 const StringOrdinal& new_value) { | |
1721 if (new_value.IsValid()) | |
1722 ++page_ordinal_map_[new_value]; | |
1723 | |
1724 if (old_value.IsValid()) { | |
1725 --page_ordinal_map_[old_value]; | |
1726 | |
1727 if (page_ordinal_map_[old_value] == 0) | |
1728 page_ordinal_map_.erase(old_value); | |
1729 } | |
1730 } | |
1731 | |
1732 int ExtensionPrefs::PageStringOrdinalAsInteger( | |
1733 const StringOrdinal& page_ordinal) | |
1734 const { | |
1735 if (!page_ordinal.IsValid()) | |
1736 return -1; | |
1737 | |
1738 std::map<StringOrdinal, int>::const_iterator it = | |
1739 page_ordinal_map_.find(page_ordinal); | |
1740 if (it != page_ordinal_map_.end()) { | |
1741 return std::distance(page_ordinal_map_.begin(), it); | |
1742 } else { | |
1743 return -1; | |
1744 } | |
1745 } | |
1746 | |
1747 StringOrdinal ExtensionPrefs::PageIntegerAsStringOrdinal(size_t page_index) { | |
1748 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | |
1749 if (!extensions) | |
1750 return StringOrdinal(); | |
1751 | |
1752 if (page_index < page_ordinal_map_.size()) { | |
1753 std::map<StringOrdinal, int, StringOrdinalLessThan>::const_iterator it = | |
1754 page_ordinal_map_.begin(); | |
1755 advance(it, page_index); | |
1756 | |
1757 return it->first; | |
1758 } else { | |
1759 if (page_ordinal_map_.empty()) | |
1760 page_ordinal_map_[StringOrdinal::CreateInitialOrdinal()] = 0; | |
1761 | |
1762 // Create new pages until we reach the desired index. | |
1763 while (page_index >= page_ordinal_map_.size()) { | |
1764 StringOrdinal new_page = page_ordinal_map_.rbegin()->first.CreateAfter(); | |
1765 page_ordinal_map_[new_page] = 0; | |
1766 } | |
1767 return page_ordinal_map_.rbegin()->first; | |
1768 } | |
1518 } | 1769 } |
1519 | 1770 |
1520 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { | 1771 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { |
1521 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); | 1772 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); |
1522 } | 1773 } |
1523 | 1774 |
1524 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { | 1775 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { |
1525 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, | 1776 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, |
1526 Value::CreateBooleanValue(true)); | 1777 Value::CreateBooleanValue(true)); |
1527 } | 1778 } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1654 // Create empty preferences dictionary for each extension (these dictionaries | 1905 // Create empty preferences dictionary for each extension (these dictionaries |
1655 // are pruned when persisting the preferences to disk). | 1906 // are pruned when persisting the preferences to disk). |
1656 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | 1907 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); |
1657 ext_id != extension_ids.end(); ++ext_id) { | 1908 ext_id != extension_ids.end(); ++ext_id) { |
1658 ScopedExtensionPrefUpdate update(prefs_, *ext_id); | 1909 ScopedExtensionPrefUpdate update(prefs_, *ext_id); |
1659 // This creates an empty dictionary if none is stored. | 1910 // This creates an empty dictionary if none is stored. |
1660 update.Get(); | 1911 update.Get(); |
1661 } | 1912 } |
1662 | 1913 |
1663 FixMissingPrefs(extension_ids); | 1914 FixMissingPrefs(extension_ids); |
1915 InitializePageOrdinalMap(extension_ids); | |
1664 MigratePermissions(extension_ids); | 1916 MigratePermissions(extension_ids); |
1917 MigrateAppIndex(extension_ids); | |
1665 | 1918 |
1666 // Store extension controlled preference values in the | 1919 // Store extension controlled preference values in the |
1667 // |extension_pref_value_map_|, which then informs the subscribers | 1920 // |extension_pref_value_map_|, which then informs the subscribers |
1668 // (ExtensionPrefStores) about the winning values. | 1921 // (ExtensionPrefStores) about the winning values. |
1669 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | 1922 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); |
1670 ext_id != extension_ids.end(); ++ext_id) { | 1923 ext_id != extension_ids.end(); ++ext_id) { |
1671 extension_pref_value_map_->RegisterExtension( | 1924 extension_pref_value_map_->RegisterExtension( |
1672 *ext_id, | 1925 *ext_id, |
1673 GetInstallTime(*ext_id), | 1926 GetInstallTime(*ext_id), |
1674 !IsExtensionDisabled(*ext_id)); | 1927 !IsExtensionDisabled(*ext_id)); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1822 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, | 2075 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, |
1823 PrefService::UNSYNCABLE_PREF); | 2076 PrefService::UNSYNCABLE_PREF); |
1824 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, | 2077 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, |
1825 PrefService::UNSYNCABLE_PREF); | 2078 PrefService::UNSYNCABLE_PREF); |
1826 prefs->RegisterListPref(prefs::kExtensionInstallForceList, | 2079 prefs->RegisterListPref(prefs::kExtensionInstallForceList, |
1827 PrefService::UNSYNCABLE_PREF); | 2080 PrefService::UNSYNCABLE_PREF); |
1828 prefs->RegisterStringPref(kWebStoreLogin, | 2081 prefs->RegisterStringPref(kWebStoreLogin, |
1829 std::string() /* default_value */, | 2082 std::string() /* default_value */, |
1830 PrefService::UNSYNCABLE_PREF); | 2083 PrefService::UNSYNCABLE_PREF); |
1831 } | 2084 } |
OLD | NEW |