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 kPrefAppLaunchIndex[] = "app_launcher_index_string_ordinal"; |
114 | 115 |
115 // A preference determining the page on which an app appears in the NTP. | 116 // A preference determining the page on which an app appears in the NTP. |
116 const char kPrefPageIndex[] = "page_index"; | 117 const char kPrefPageIndexDeprecated[] = "page_index"; |
| 118 const char kPrefPageIndex[] = "page_index_string_ordinal"; |
117 | 119 |
118 // A preference specifying if the user dragged the app on the NTP. | 120 // A preference specifying if the user dragged the app on the NTP. |
119 const char kPrefUserDraggedApp[] = "user_dragged_app_ntp"; | 121 const char kPrefUserDraggedApp[] = "user_dragged_app_ntp"; |
120 | 122 |
121 // A preference for storing extra data sent in update checks for an extension. | 123 // A preference for storing extra data sent in update checks for an extension. |
122 const char kUpdateUrlData[] = "update_url_data"; | 124 const char kUpdateUrlData[] = "update_url_data"; |
123 | 125 |
124 // Whether the browser action is visible in the toolbar. | 126 // Whether the browser action is visible in the toolbar. |
125 const char kBrowserActionVisible[] = "browser_action_visible"; | 127 const char kBrowserActionVisible[] = "browser_action_visible"; |
126 | 128 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 the order they |
| 245 // appear on the NTP. This is used by the code that migrates the data |
| 246 // from integers to StringOrdinals. |
| 247 struct ApplicationNTPOldPosition { |
| 248 const std::string* application_id; |
| 249 const int page_index; |
| 250 const int app_launch_index; |
| 251 |
| 252 ApplicationNTPOldPosition(const std::string& app_id, int page, int app_launch) |
| 253 : application_id(&app_id), |
| 254 page_index(page), |
| 255 app_launch_index(app_launch) {} |
| 256 }; |
| 257 |
| 258 struct SortByNTPOldPosition { |
| 259 bool operator() (const ApplicationNTPOldPosition& lhs, |
| 260 const ApplicationNTPOldPosition& rhs) const { |
| 261 if (lhs.page_index != rhs.page_index) |
| 262 return lhs.page_index < rhs.page_index; |
| 263 |
| 264 return lhs.app_launch_index < rhs.app_launch_index; |
| 265 } |
| 266 }; |
| 267 |
242 } // namespace | 268 } // namespace |
243 | 269 |
244 ExtensionPrefs::ExtensionPrefs( | 270 ExtensionPrefs::ExtensionPrefs( |
245 PrefService* prefs, | 271 PrefService* prefs, |
246 const FilePath& root_dir, | 272 const FilePath& root_dir, |
247 ExtensionPrefValueMap* extension_pref_value_map) | 273 ExtensionPrefValueMap* extension_pref_value_map) |
248 : prefs_(prefs), | 274 : prefs_(prefs), |
249 install_directory_(root_dir), | 275 install_directory_(root_dir), |
250 extension_pref_value_map_(extension_pref_value_map), | 276 extension_pref_value_map_(extension_pref_value_map), |
251 content_settings_store_(new ExtensionContentSettingsStore()) { | 277 content_settings_store_(new ExtensionContentSettingsStore()) { |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 const DictionaryValue* ext = GetExtensionPref(extension_id); | 434 const DictionaryValue* ext = GetExtensionPref(extension_id); |
409 ListValue* out = NULL; | 435 ListValue* out = NULL; |
410 if (!ext || !ext->GetList(pref_key, &out)) | 436 if (!ext || !ext->GetList(pref_key, &out)) |
411 return false; | 437 return false; |
412 if (out_value) | 438 if (out_value) |
413 *out_value = out; | 439 *out_value = out; |
414 | 440 |
415 return true; | 441 return true; |
416 } | 442 } |
417 | 443 |
| 444 bool ExtensionPrefs::ReadExtensionPrefString( |
| 445 const std::string& extension_id, const std::string& pref_key, |
| 446 std::string* out_value) const { |
| 447 const DictionaryValue* ext = GetExtensionPref(extension_id); |
| 448 |
| 449 if (!ext || !ext->GetString(pref_key, out_value)) |
| 450 return false; |
| 451 |
| 452 return true; |
| 453 } |
| 454 |
418 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet( | 455 bool ExtensionPrefs::ReadExtensionPrefURLPatternSet( |
419 const std::string& extension_id, | 456 const std::string& extension_id, |
420 const std::string& pref_key, | 457 const std::string& pref_key, |
421 URLPatternSet* result, | 458 URLPatternSet* result, |
422 int valid_schemes) { | 459 int valid_schemes) { |
423 const ListValue* value = NULL; | 460 const ListValue* value = NULL; |
424 if (!ReadExtensionPrefList(extension_id, pref_key, &value)) | 461 if (!ReadExtensionPrefList(extension_id, pref_key, &value)) |
425 return false; | 462 return false; |
426 | 463 |
427 result->ClearPatterns(); | 464 result->ClearPatterns(); |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
851 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) { | 888 if (ext->GetList(kPrefOldGrantedHosts, &hosts)) { |
852 UpdateExtensionPref( | 889 UpdateExtensionPref( |
853 *ext_id, explicit_hosts, hosts->DeepCopy()); | 890 *ext_id, explicit_hosts, hosts->DeepCopy()); |
854 | 891 |
855 // We can get rid of the old one by setting it to an empty list. | 892 // We can get rid of the old one by setting it to an empty list. |
856 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue()); | 893 UpdateExtensionPref(*ext_id, kPrefOldGrantedHosts, new ListValue()); |
857 } | 894 } |
858 } | 895 } |
859 } | 896 } |
860 | 897 |
| 898 void ExtensionPrefs::MigrateAppIndex(const ExtensionIdSet& extension_ids) { |
| 899 if (extension_ids.begin() == extension_ids.end()) |
| 900 return; |
| 901 |
| 902 // Create a sorted set of all the extensions that need either their page |
| 903 // or launch index converted. |
| 904 std::set<ApplicationNTPOldPosition, SortByNTPOldPosition> apps_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 int old_app_launch_index = 0; |
| 909 |
| 910 bool has_old_values = ReadExtensionPrefInteger(*ext_id, |
| 911 kPrefPageIndexDeprecated, |
| 912 &old_page_index); |
| 913 has_old_values |= ReadExtensionPrefInteger(*ext_id, |
| 914 kPrefAppLaunchIndexDeprecated, |
| 915 &old_app_launch_index); |
| 916 |
| 917 if (has_old_values) { |
| 918 // Its ok to use both values, even if they weren't read, because this |
| 919 // only affects the sorting, which can handle apps with the default |
| 920 // integer values. |
| 921 apps_to_convert.insert(ApplicationNTPOldPosition( |
| 922 *ext_id, old_page_index, old_app_launch_index)); |
| 923 } |
| 924 } |
| 925 |
| 926 if (apps_to_convert.empty()) |
| 927 return; |
| 928 |
| 929 // Create the new values and remove the old preferences. |
| 930 for (std::set<ApplicationNTPOldPosition, SortByNTPOldPosition>::iterator ext = |
| 931 apps_to_convert.begin(); ext != apps_to_convert.end(); ++ext) { |
| 932 int old_page_index; |
| 933 if (ReadExtensionPrefInteger(*(ext->application_id), |
| 934 kPrefPageIndexDeprecated, |
| 935 &old_page_index)) { |
| 936 SetPageIndex(*(ext->application_id), |
| 937 PageIndexAsStringOrdinal(old_page_index)); |
| 938 UpdateExtensionPref( |
| 939 *(ext->application_id), kPrefPageIndexDeprecated, NULL); |
| 940 } |
| 941 |
| 942 int old_app_launch_index; |
| 943 if (ReadExtensionPrefInteger(*(ext->application_id), |
| 944 kPrefAppLaunchIndexDeprecated, |
| 945 &old_app_launch_index)) { |
| 946 StringOrdinal page = GetPageIndex(*(ext->application_id)); |
| 947 SetAppLaunchIndex(*(ext->application_id), |
| 948 GetNextAppLaunchIndex(page)); |
| 949 UpdateExtensionPref( |
| 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_index) { |
1076 const std::string& id = extension->id(); | 1170 const std::string& id = extension->id(); |
1077 CHECK(Extension::IdIsValid(id)); | 1171 CHECK(Extension::IdIsValid(id)); |
1078 ScopedExtensionPrefUpdate update(prefs_, id); | 1172 ScopedExtensionPrefUpdate update(prefs_, id); |
1079 DictionaryValue* extension_dict = update.Get(); | 1173 DictionaryValue* extension_dict = update.Get(); |
1080 const base::Time install_time = GetCurrentTime(); | 1174 const base::Time install_time = GetCurrentTime(); |
1081 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state)); | 1175 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state)); |
1082 extension_dict->Set(kPrefLocation, | 1176 extension_dict->Set(kPrefLocation, |
1083 Value::CreateIntegerValue(extension->location())); | 1177 Value::CreateIntegerValue(extension->location())); |
1084 extension_dict->Set(kPrefFromWebStore, | 1178 extension_dict->Set(kPrefFromWebStore, |
1085 Value::CreateBooleanValue(from_webstore)); | 1179 Value::CreateBooleanValue(from_webstore)); |
(...skipping 11 matching lines...) Expand all 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_index.IsValid()) |
1108 page_index = GetNaturalAppPageIndex(); | 1202 page_index = GetNaturalAppPageIndex(); |
1109 extension_dict->Set(kPrefPageIndex, | 1203 extension_dict->Set(kPrefPageIndex, |
1110 Value::CreateIntegerValue(page_index)); | 1204 Value::CreateStringValue(page_index.ToString())); |
| 1205 UpdatePageIndexMap(StringOrdinal(), page_index); |
| 1206 |
1111 extension_dict->Set(kPrefAppLaunchIndex, | 1207 extension_dict->Set(kPrefAppLaunchIndex, |
1112 Value::CreateIntegerValue(GetNextAppLaunchIndex(page_index))); | 1208 Value::CreateStringValue( |
| 1209 GetNextAppLaunchIndex(page_index).ToString())); |
1113 } | 1210 } |
1114 extension_pref_value_map_->RegisterExtension( | 1211 extension_pref_value_map_->RegisterExtension( |
1115 id, install_time, initial_state == Extension::ENABLED); | 1212 id, install_time, initial_state == Extension::ENABLED); |
1116 content_settings_store_->RegisterExtension( | 1213 content_settings_store_->RegisterExtension( |
1117 id, install_time, initial_state == Extension::ENABLED); | 1214 id, install_time, initial_state == Extension::ENABLED); |
1118 } | 1215 } |
1119 | 1216 |
1120 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, | 1217 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, |
1121 const Extension::Location& location, | 1218 const Extension::Location& location, |
1122 bool external_uninstall) { | 1219 bool external_uninstall) { |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 void ExtensionPrefs::GetAllAppLaunchIndicesOnPageSorted( |
1446 int value; | 1543 const StringOrdinal& target_page_index, |
1447 if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value)) | 1544 std::set<StringOrdinal, StringOrdinalLessThan>* indices_on_page) const { |
1448 return value; | 1545 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| 1546 if (!extensions || !target_page_index.IsValid()) |
| 1547 return; |
1449 | 1548 |
1450 return -1; | 1549 for (DictionaryValue::key_iterator ext_it = extensions->begin_keys(); |
| 1550 ext_it != extensions->end_keys(); ++ext_it) { |
| 1551 StringOrdinal page_index = GetPageIndex(*ext_it); |
| 1552 if (page_index.IsValid() && page_index.Equal(target_page_index)) { |
| 1553 StringOrdinal app_launch_index = GetAppLaunchIndex(*ext_it); |
| 1554 if (app_launch_index.IsValid()) |
| 1555 indices_on_page->insert(app_launch_index); |
| 1556 } |
| 1557 } |
| 1558 } |
| 1559 |
| 1560 StringOrdinal ExtensionPrefs::GetFirstAppPage() const { |
| 1561 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| 1562 if (!extensions) |
| 1563 return StringOrdinal(); |
| 1564 |
| 1565 if (page_index_map_.size() == 0) |
| 1566 return StringOrdinal::CreateInitialOrdinal(); |
| 1567 |
| 1568 return page_index_map_.begin()->first; |
| 1569 } |
| 1570 |
| 1571 StringOrdinal ExtensionPrefs::CreateFirstAppLaunchIndex( |
| 1572 const StringOrdinal& page_index) const { |
| 1573 std::set<StringOrdinal, StringOrdinalLessThan> apps_on_page; |
| 1574 GetAllAppLaunchIndicesOnPageSorted(page_index, &apps_on_page); |
| 1575 |
| 1576 if (apps_on_page.empty()) |
| 1577 return StringOrdinal::CreateInitialOrdinal(); |
| 1578 else |
| 1579 return apps_on_page.begin()->CreateBefore(); |
| 1580 } |
| 1581 |
| 1582 StringOrdinal ExtensionPrefs::GetAppLaunchIndex( |
| 1583 const std::string& extension_id) const { |
| 1584 std::string raw_value; |
| 1585 // If the preference read fails then raw_value will still be unset and we |
| 1586 // will return an invalid StringOrdinal to signal that no app launch index |
| 1587 // was found. |
| 1588 ReadExtensionPrefString(extension_id, kPrefAppLaunchIndex, &raw_value); |
| 1589 return StringOrdinal(raw_value); |
1451 } | 1590 } |
1452 | 1591 |
1453 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, | 1592 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, |
1454 int index) { | 1593 const StringOrdinal& index) { |
1455 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, | 1594 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, |
1456 Value::CreateIntegerValue(index)); | 1595 Value::CreateStringValue(index.ToString())); |
1457 } | 1596 } |
1458 | 1597 |
1459 int ExtensionPrefs::GetNextAppLaunchIndex(int on_page) { | 1598 StringOrdinal ExtensionPrefs::GetNextAppLaunchIndex( |
| 1599 const StringOrdinal& on_page) const { |
| 1600 std::set<StringOrdinal, StringOrdinalLessThan> apps_on_page; |
| 1601 GetAllAppLaunchIndicesOnPageSorted(on_page, &apps_on_page); |
| 1602 |
| 1603 if (apps_on_page.empty()) |
| 1604 return StringOrdinal::CreateInitialOrdinal(); |
| 1605 else |
| 1606 return apps_on_page.rbegin()->CreateAfter(); |
| 1607 } |
| 1608 |
| 1609 StringOrdinal ExtensionPrefs::GetNaturalAppPageIndex() const { |
1460 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 1610 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
1461 if (!extensions) | 1611 if (!extensions) |
1462 return 0; | 1612 return StringOrdinal(); |
1463 | 1613 |
1464 int max_value = -1; | 1614 if (page_index_map_.size() == 0) |
1465 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); | 1615 return StringOrdinal::CreateInitialOrdinal(); |
1466 extension_id != extensions->end_keys(); ++extension_id) { | 1616 |
1467 int value = GetAppLaunchIndex(*extension_id); | 1617 std::map<StringOrdinal, int>::const_iterator it = page_index_map_.begin(); |
1468 int page = GetPageIndex(*extension_id); | 1618 for (; it != page_index_map_.end(); ++it) { |
1469 if (page == on_page && value > max_value) | 1619 if (it->second < kNaturalAppPageSize) |
1470 max_value = value; | 1620 return it->first; |
1471 } | 1621 } |
1472 return max_value + 1; | |
1473 } | |
1474 | 1622 |
1475 int ExtensionPrefs::GetNaturalAppPageIndex() { | 1623 // Add a new page as all existing pages are full. |
1476 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 1624 StringOrdinal last_element = page_index_map_.rbegin()->first; |
1477 if (!extensions) | 1625 return last_element.CreateAfter(); |
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 } | |
1492 } | 1626 } |
1493 | 1627 |
1494 void ExtensionPrefs::SetAppLauncherOrder( | 1628 void ExtensionPrefs::SetAppLauncherOrder( |
1495 const std::vector<std::string>& extension_ids) { | 1629 const std::vector<std::string>& extension_ids, |
1496 for (size_t i = 0; i < extension_ids.size(); ++i) | 1630 const std::string& moved_extension_id) { |
1497 SetAppLaunchIndex(extension_ids.at(i), i); | 1631 std::vector<std::string>::const_iterator it = |
| 1632 find(extension_ids.begin(), extension_ids.end(), moved_extension_id); |
| 1633 size_t position = std::distance(extension_ids.begin(), it); |
| 1634 |
| 1635 // We need at least 2 apps for the moved apps old value to be invalidated. |
| 1636 if (extension_ids.size() >= 2) { |
| 1637 if (position == 0U) { |
| 1638 // The app is moving to the start of the group, so we need to get the |
| 1639 // previous first StringOrdinal and create the new value before it. |
| 1640 ++it; |
| 1641 SetAppLaunchIndex(moved_extension_id, |
| 1642 GetAppLaunchIndex(*it).CreateBefore()); |
| 1643 } else if (position == extension_ids.size() - 1U) { |
| 1644 // The app is moving to the end of the group, so we need to get the |
| 1645 // previous last StringOrdinal and create the new value after it. |
| 1646 --it; |
| 1647 SetAppLaunchIndex(moved_extension_id, |
| 1648 GetAppLaunchIndex(*it).CreateAfter()); |
| 1649 } else { |
| 1650 // The app is going into a middle position, which means there are valid |
| 1651 // StringOrdinals before and after it. We get the new adjacent |
| 1652 // StringOrdinals and create our new value between them. |
| 1653 ++it; |
| 1654 StringOrdinal value_after = GetAppLaunchIndex(*it); |
| 1655 advance(it, -2); |
| 1656 StringOrdinal value_before = GetAppLaunchIndex(*it); |
| 1657 SetAppLaunchIndex(moved_extension_id, |
| 1658 value_before.CreateBetween(value_after)); |
| 1659 } |
| 1660 } |
1498 | 1661 |
1499 content::NotificationService::current()->Notify( | 1662 content::NotificationService::current()->Notify( |
1500 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, | 1663 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, |
1501 content::Source<ExtensionPrefs>(this), | 1664 content::Source<ExtensionPrefs>(this), |
1502 content::NotificationService::NoDetails()); | 1665 content::NotificationService::NoDetails()); |
1503 } | 1666 } |
1504 | 1667 |
1505 int ExtensionPrefs::GetPageIndex(const std::string& extension_id) { | 1668 StringOrdinal ExtensionPrefs::GetPageIndex(const std::string& extension_id) |
1506 int value = -1; | 1669 const { |
1507 ReadExtensionPrefInteger(extension_id, kPrefPageIndex, &value); | 1670 std::string raw_data; |
1508 return value; | 1671 // If the preference read fails then raw_value will still be unset and we will |
| 1672 // return an invalid StringOrdinal to signal that no page index was found. |
| 1673 ReadExtensionPrefString(extension_id, kPrefPageIndex, &raw_data); |
| 1674 return StringOrdinal(raw_data); |
1509 } | 1675 } |
1510 | 1676 |
1511 void ExtensionPrefs::SetPageIndex(const std::string& extension_id, int index) { | 1677 void ExtensionPrefs::SetPageIndex(const std::string& extension_id, |
| 1678 const StringOrdinal& page_index) { |
| 1679 UpdatePageIndexMap(GetPageIndex(extension_id), page_index); |
1512 UpdateExtensionPref(extension_id, kPrefPageIndex, | 1680 UpdateExtensionPref(extension_id, kPrefPageIndex, |
1513 Value::CreateIntegerValue(index)); | 1681 Value::CreateStringValue(page_index.ToString())); |
1514 } | 1682 } |
1515 | 1683 |
1516 void ExtensionPrefs::ClearPageIndex(const std::string& extension_id) { | 1684 void ExtensionPrefs::ClearPageIndex(const std::string& extension_id) { |
| 1685 UpdatePageIndexMap(GetPageIndex(extension_id), StringOrdinal()); |
1517 UpdateExtensionPref(extension_id, kPrefPageIndex, NULL); | 1686 UpdateExtensionPref(extension_id, kPrefPageIndex, NULL); |
1518 } | 1687 } |
1519 | 1688 |
| 1689 void ExtensionPrefs::InitializePageIndexMap( |
| 1690 const ExtensionIdSet& extension_ids) { |
| 1691 for (ExtensionIdSet::const_iterator ext_it = extension_ids.begin(); |
| 1692 ext_it != extension_ids.end(); ++ext_it) { |
| 1693 UpdatePageIndexMap(StringOrdinal(), GetPageIndex(*ext_it)); |
| 1694 } |
| 1695 } |
| 1696 |
| 1697 void ExtensionPrefs::UpdatePageIndexMap(const StringOrdinal& old_value, |
| 1698 const StringOrdinal& new_value) { |
| 1699 if (new_value.IsValid()) |
| 1700 ++page_index_map_[new_value]; |
| 1701 |
| 1702 if (old_value.IsValid()) { |
| 1703 --page_index_map_[old_value]; |
| 1704 |
| 1705 if (page_index_map_[old_value] == 0) |
| 1706 page_index_map_.erase(old_value); |
| 1707 } |
| 1708 } |
| 1709 |
| 1710 int ExtensionPrefs::PageIndexAsInteger(const StringOrdinal& page_index) const { |
| 1711 if (!page_index.IsValid()) |
| 1712 return -1; |
| 1713 |
| 1714 std::map<StringOrdinal, int>::const_iterator it = |
| 1715 page_index_map_.find(page_index); |
| 1716 if (it != page_index_map_.end()) { |
| 1717 return std::distance(page_index_map_.begin(), it); |
| 1718 } else { |
| 1719 return -1; |
| 1720 } |
| 1721 } |
| 1722 |
| 1723 StringOrdinal ExtensionPrefs::PageIndexAsStringOrdinal(size_t page_index) { |
| 1724 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| 1725 if (!extensions) |
| 1726 return StringOrdinal(); |
| 1727 |
| 1728 if (page_index_map_.size() == 0) |
| 1729 return StringOrdinal::CreateInitialOrdinal(); |
| 1730 |
| 1731 if (page_index < page_index_map_.size()) { |
| 1732 std::map<StringOrdinal, int, StringOrdinalLessThan>::const_iterator it = |
| 1733 page_index_map_.begin(); |
| 1734 advance(it, page_index); |
| 1735 |
| 1736 return it->first; |
| 1737 } else { |
| 1738 // Create new pages until we reach the desired index. |
| 1739 while (page_index >= page_index_map_.size()) { |
| 1740 StringOrdinal new_page = page_index_map_.rbegin()->first.CreateAfter(); |
| 1741 page_index_map_[new_page] = 0; |
| 1742 } |
| 1743 return page_index_map_.rbegin()->first; |
| 1744 } |
| 1745 } |
| 1746 |
1520 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { | 1747 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { |
1521 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); | 1748 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); |
1522 } | 1749 } |
1523 | 1750 |
1524 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { | 1751 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { |
1525 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, | 1752 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, |
1526 Value::CreateBooleanValue(true)); | 1753 Value::CreateBooleanValue(true)); |
1527 } | 1754 } |
1528 | 1755 |
1529 void ExtensionPrefs::SetUpdateUrlData(const std::string& extension_id, | 1756 void ExtensionPrefs::SetUpdateUrlData(const std::string& extension_id, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1654 // Create empty preferences dictionary for each extension (these dictionaries | 1881 // Create empty preferences dictionary for each extension (these dictionaries |
1655 // are pruned when persisting the preferences to disk). | 1882 // are pruned when persisting the preferences to disk). |
1656 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | 1883 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); |
1657 ext_id != extension_ids.end(); ++ext_id) { | 1884 ext_id != extension_ids.end(); ++ext_id) { |
1658 ScopedExtensionPrefUpdate update(prefs_, *ext_id); | 1885 ScopedExtensionPrefUpdate update(prefs_, *ext_id); |
1659 // This creates an empty dictionary if none is stored. | 1886 // This creates an empty dictionary if none is stored. |
1660 update.Get(); | 1887 update.Get(); |
1661 } | 1888 } |
1662 | 1889 |
1663 FixMissingPrefs(extension_ids); | 1890 FixMissingPrefs(extension_ids); |
| 1891 InitializePageIndexMap(extension_ids); |
1664 MigratePermissions(extension_ids); | 1892 MigratePermissions(extension_ids); |
| 1893 MigrateAppIndex(extension_ids); |
1665 | 1894 |
1666 // Store extension controlled preference values in the | 1895 // Store extension controlled preference values in the |
1667 // |extension_pref_value_map_|, which then informs the subscribers | 1896 // |extension_pref_value_map_|, which then informs the subscribers |
1668 // (ExtensionPrefStores) about the winning values. | 1897 // (ExtensionPrefStores) about the winning values. |
1669 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | 1898 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); |
1670 ext_id != extension_ids.end(); ++ext_id) { | 1899 ext_id != extension_ids.end(); ++ext_id) { |
1671 extension_pref_value_map_->RegisterExtension( | 1900 extension_pref_value_map_->RegisterExtension( |
1672 *ext_id, | 1901 *ext_id, |
1673 GetInstallTime(*ext_id), | 1902 GetInstallTime(*ext_id), |
1674 !IsExtensionDisabled(*ext_id)); | 1903 !IsExtensionDisabled(*ext_id)); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1830 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, | 2059 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, |
1831 PrefService::UNSYNCABLE_PREF); | 2060 PrefService::UNSYNCABLE_PREF); |
1832 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, | 2061 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, |
1833 PrefService::UNSYNCABLE_PREF); | 2062 PrefService::UNSYNCABLE_PREF); |
1834 prefs->RegisterListPref(prefs::kExtensionInstallForceList, | 2063 prefs->RegisterListPref(prefs::kExtensionInstallForceList, |
1835 PrefService::UNSYNCABLE_PREF); | 2064 PrefService::UNSYNCABLE_PREF); |
1836 prefs->RegisterStringPref(kWebStoreLogin, | 2065 prefs->RegisterStringPref(kWebStoreLogin, |
1837 std::string() /* default_value */, | 2066 std::string() /* default_value */, |
1838 PrefService::UNSYNCABLE_PREF); | 2067 PrefService::UNSYNCABLE_PREF); |
1839 } | 2068 } |
OLD | NEW |