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 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) {} | |
akalin
2011/11/21 19:49:24
add a default constructor and init int args to 0.
csharp
2011/11/23 15:38:07
Done.
| |
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()) | |
akalin
2011/11/21 19:49:24
use .empty()
csharp
2011/11/23 15:38:07
Done.
| |
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) { | |
akalin
2011/11/21 19:49:24
Hmm, I don't understand why you have to stuff thes
csharp
2011/11/23 15:38:07
Ok, SetPageOrdinal was rolled up into the first lo
| |
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 = | |
akalin
2011/11/21 19:49:24
I think this can be a const iterator
csharp
2011/11/23 15:38:07
Done.
| |
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 SetPageOrdinal(*(ext->application_id), | |
937 PageIntegerAsStringOrdinal(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 = GetPageOrdinal(*(ext->application_id)); | |
947 SetAppLaunchOrdinal(*(ext->application_id), | |
948 GetNextAppLaunchOrdinal(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_ordinal) { |
1076 const std::string& id = extension->id(); | 1170 const std::string& id = extension->id(); |
1077 CHECK(Extension::IdIsValid(id)); | 1171 CHECK(Extension::IdIsValid(id)); |
1078 ScopedExtensionPrefUpdate update(prefs_, id); | 1172 ScopedExtensionPrefUpdate update(prefs_, id); |
1079 DictionaryValue* extension_dict = update.Get(); | 1173 DictionaryValue* extension_dict = update.Get(); |
1080 const base::Time install_time = GetCurrentTime(); | 1174 const base::Time install_time = GetCurrentTime(); |
1081 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state)); | 1175 extension_dict->Set(kPrefState, Value::CreateIntegerValue(initial_state)); |
1082 extension_dict->Set(kPrefLocation, | 1176 extension_dict->Set(kPrefLocation, |
1083 Value::CreateIntegerValue(extension->location())); | 1177 Value::CreateIntegerValue(extension->location())); |
1084 extension_dict->Set(kPrefFromWebStore, | 1178 extension_dict->Set(kPrefFromWebStore, |
1085 Value::CreateBooleanValue(from_webstore)); | 1179 Value::CreateBooleanValue(from_webstore)); |
(...skipping 11 matching lines...) Expand all 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 void ExtensionPrefs::GetAllAppLaunchOrdinalsOnPageSorted( |
1446 int value; | 1543 const StringOrdinal& target_page_ordinal, |
1447 if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value)) | 1544 std::set<StringOrdinal, StringOrdinalLessThan>* ordinals_on_page) const { |
akalin
2011/11/21 19:49:24
looks like you only really care about the min/max
csharp
2011/11/23 15:38:07
Done.
| |
1448 return value; | 1545 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
1546 if (!extensions || !target_page_ordinal.IsValid()) | |
akalin
2011/11/21 19:49:24
can this fn actually be called with an invalid pag
csharp
2011/11/23 15:38:07
They should always be valid, adding CHECK.
On 201
| |
1547 return; | |
1449 | 1548 |
1450 return -1; | 1549 for (DictionaryValue::key_iterator ext_it = extensions->begin_keys(); |
1451 } | 1550 ext_it != extensions->end_keys(); ++ext_it) { |
1452 | 1551 StringOrdinal page_ordinal = GetPageOrdinal(*ext_it); |
1453 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, | 1552 if (page_ordinal.IsValid() && page_ordinal.Equal(target_page_ordinal)) { |
1454 int index) { | 1553 StringOrdinal app_launch_ordinal = GetAppLaunchOrdinal(*ext_it); |
1455 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, | 1554 if (app_launch_ordinal.IsValid()) |
1456 Value::CreateIntegerValue(index)); | 1555 ordinals_on_page->insert(app_launch_ordinal); |
1457 } | 1556 } |
1458 | |
1459 int ExtensionPrefs::GetNextAppLaunchIndex(int on_page) { | |
1460 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | |
1461 if (!extensions) | |
1462 return 0; | |
1463 | |
1464 int max_value = -1; | |
1465 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); | |
1466 extension_id != extensions->end_keys(); ++extension_id) { | |
1467 int value = GetAppLaunchIndex(*extension_id); | |
1468 int page = GetPageIndex(*extension_id); | |
1469 if (page == on_page && value > max_value) | |
1470 max_value = value; | |
1471 } | |
1472 return max_value + 1; | |
1473 } | |
1474 | |
1475 int ExtensionPrefs::GetNaturalAppPageIndex() { | |
1476 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | |
1477 if (!extensions) | |
1478 return 0; | |
1479 | |
1480 std::map<int, int> page_counts; | |
1481 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); | |
1482 extension_id != extensions->end_keys(); ++extension_id) { | |
1483 int page_index = GetPageIndex(*extension_id); | |
1484 if (page_index >= 0) | |
1485 page_counts[page_index] = page_counts[page_index] + 1; | |
1486 } | |
1487 for (int i = 0; ; i++) { | |
1488 std::map<int, int>::const_iterator it = page_counts.find(i); | |
1489 if (it == page_counts.end() || it->second < kNaturalAppPageSize) | |
1490 return i; | |
1491 } | 1557 } |
1492 } | 1558 } |
1493 | 1559 |
1560 StringOrdinal ExtensionPrefs::GetFirstAppPage() const { | |
1561 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | |
1562 if (!extensions) | |
akalin
2011/11/21 19:49:24
can this actually happen? if not, CHECK. If so,
csharp
2011/11/23 15:38:07
It looks like this should always work, so I'll add
| |
1563 return StringOrdinal(); | |
1564 | |
1565 if (page_ordinal_map_.size() == 0) | |
akalin
2011/11/21 19:49:24
use .empty()
csharp
2011/11/23 15:38:07
Done.
| |
1566 return StringOrdinal::CreateInitialOrdinal(); | |
1567 | |
1568 return page_ordinal_map_.begin()->first; | |
1569 } | |
1570 | |
1571 StringOrdinal ExtensionPrefs::CreateFirstAppLaunchOrdinal( | |
1572 const StringOrdinal& page_ordinal) const { | |
1573 std::set<StringOrdinal, StringOrdinalLessThan> apps_on_page; | |
1574 GetAllAppLaunchOrdinalsOnPageSorted(page_ordinal, &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::GetAppLaunchOrdinal( | |
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 ordinal | |
1587 // was found. | |
1588 ReadExtensionPrefString(extension_id, kPrefAppLaunchOrdinal, &raw_value); | |
1589 return StringOrdinal(raw_value); | |
1590 } | |
1591 | |
1592 void ExtensionPrefs::SetAppLaunchOrdinal(const std::string& extension_id, | |
1593 const StringOrdinal& ordinal) { | |
akalin
2011/11/21 19:49:24
indent
csharp
2011/11/23 15:38:07
Done.
| |
1594 UpdateExtensionPref(extension_id, kPrefAppLaunchOrdinal, | |
1595 Value::CreateStringValue(ordinal.ToString())); | |
1596 } | |
1597 | |
1598 StringOrdinal ExtensionPrefs::GetNextAppLaunchOrdinal( | |
1599 const StringOrdinal& on_page) const { | |
1600 std::set<StringOrdinal, StringOrdinalLessThan> apps_on_page; | |
1601 GetAllAppLaunchOrdinalsOnPageSorted(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::GetNaturalAppPageOrdinal() const { | |
1610 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | |
1611 if (!extensions) | |
akalin
2011/11/21 19:49:24
same question as GetFirstAppPage
csharp
2011/11/23 15:38:07
Done.
| |
1612 return StringOrdinal(); | |
1613 | |
1614 if (page_ordinal_map_.size() == 0) | |
akalin
2011/11/21 19:49:24
use empty
csharp
2011/11/23 15:38:07
Done.
| |
1615 return StringOrdinal::CreateInitialOrdinal(); | |
1616 | |
1617 std::map<StringOrdinal, int>::const_iterator it = page_ordinal_map_.begin(); | |
1618 for (; it != page_ordinal_map_.end(); ++it) { | |
1619 if (it->second < kNaturalAppPageSize) | |
1620 return it->first; | |
1621 } | |
1622 | |
1623 // Add a new page as all existing pages are full. | |
1624 StringOrdinal last_element = page_ordinal_map_.rbegin()->first; | |
1625 return last_element.CreateAfter(); | |
1626 } | |
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); | |
akalin
2011/11/21 19:49:24
use *it - begin() (since we know it's constant-tim
csharp
2011/11/23 15:38:07
Done.
| |
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 SetAppLaunchOrdinal(moved_extension_id, | |
1642 GetAppLaunchOrdinal(*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 SetAppLaunchOrdinal(moved_extension_id, | |
1648 GetAppLaunchOrdinal(*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 = GetAppLaunchOrdinal(*it); | |
1655 advance(it, -2); | |
akalin
2011/11/21 19:49:24
it -= 2
csharp
2011/11/23 15:38:07
Done.
| |
1656 StringOrdinal value_before = GetAppLaunchOrdinal(*it); | |
1657 SetAppLaunchOrdinal(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::GetPageOrdinal(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 ordinal was found. | |
1673 ReadExtensionPrefString(extension_id, kPrefPageOrdinal, &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::SetPageOrdinal(const std::string& extension_id, |
1512 UpdateExtensionPref(extension_id, kPrefPageIndex, | 1678 const StringOrdinal& page_ordinal) { |
1513 Value::CreateIntegerValue(index)); | 1679 UpdatePageOrdinalMap(GetPageOrdinal(extension_id), page_ordinal); |
1680 UpdateExtensionPref(extension_id, kPrefPageOrdinal, | |
1681 Value::CreateStringValue(page_ordinal.ToString())); | |
1514 } | 1682 } |
1515 | 1683 |
1516 void ExtensionPrefs::ClearPageIndex(const std::string& extension_id) { | 1684 void ExtensionPrefs::ClearPageOrdinal(const std::string& extension_id) { |
1517 UpdateExtensionPref(extension_id, kPrefPageIndex, NULL); | 1685 UpdatePageOrdinalMap(GetPageOrdinal(extension_id), StringOrdinal()); |
1686 UpdateExtensionPref(extension_id, kPrefPageOrdinal, NULL); | |
1687 } | |
1688 | |
1689 void ExtensionPrefs::InitializePageOrdinalMap( | |
1690 const ExtensionIdSet& extension_ids) { | |
1691 for (ExtensionIdSet::const_iterator ext_it = extension_ids.begin(); | |
1692 ext_it != extension_ids.end(); ++ext_it) { | |
1693 UpdatePageOrdinalMap(StringOrdinal(), GetPageOrdinal(*ext_it)); | |
1694 } | |
1695 } | |
1696 | |
1697 void ExtensionPrefs::UpdatePageOrdinalMap(const StringOrdinal& old_value, | |
1698 const StringOrdinal& new_value) { | |
akalin
2011/11/21 19:49:24
indent
csharp
2011/11/23 15:38:07
Done.
| |
1699 if (new_value.IsValid()) | |
1700 ++page_ordinal_map_[new_value]; | |
1701 | |
1702 if (old_value.IsValid()) { | |
1703 --page_ordinal_map_[old_value]; | |
1704 | |
1705 if (page_ordinal_map_[old_value] == 0) | |
1706 page_ordinal_map_.erase(old_value); | |
1707 } | |
1708 } | |
1709 | |
1710 int ExtensionPrefs::PageStringOrdinalAsInteger( | |
1711 const StringOrdinal& page_ordinal) | |
1712 const { | |
1713 if (!page_ordinal.IsValid()) | |
1714 return -1; | |
1715 | |
1716 std::map<StringOrdinal, int>::const_iterator it = | |
1717 page_ordinal_map_.find(page_ordinal); | |
1718 if (it != page_ordinal_map_.end()) { | |
1719 return std::distance(page_ordinal_map_.begin(), it); | |
1720 } else { | |
1721 return -1; | |
1722 } | |
1723 } | |
1724 | |
1725 StringOrdinal ExtensionPrefs::PageIntegerAsStringOrdinal(size_t page_index) { | |
1726 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | |
1727 if (!extensions) | |
1728 return StringOrdinal(); | |
1729 | |
1730 if (page_ordinal_map_.size() == 0) | |
akalin
2011/11/21 19:49:24
use empty()
csharp
2011/11/23 15:38:07
Done.
| |
1731 return StringOrdinal::CreateInitialOrdinal(); | |
1732 | |
1733 if (page_index < page_ordinal_map_.size()) { | |
1734 std::map<StringOrdinal, int, StringOrdinalLessThan>::const_iterator it = | |
1735 page_ordinal_map_.begin(); | |
1736 advance(it, page_index); | |
1737 | |
1738 return it->first; | |
1739 } else { | |
1740 // Create new pages until we reach the desired index. | |
1741 while (page_index >= page_ordinal_map_.size()) { | |
1742 StringOrdinal new_page = page_ordinal_map_.rbegin()->first.CreateAfter(); | |
1743 page_ordinal_map_[new_page] = 0; | |
1744 } | |
1745 return page_ordinal_map_.rbegin()->first; | |
1746 } | |
1518 } | 1747 } |
1519 | 1748 |
1520 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { | 1749 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { |
1521 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); | 1750 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); |
1522 } | 1751 } |
1523 | 1752 |
1524 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { | 1753 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { |
1525 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, | 1754 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, |
1526 Value::CreateBooleanValue(true)); | 1755 Value::CreateBooleanValue(true)); |
1527 } | 1756 } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1654 // Create empty preferences dictionary for each extension (these dictionaries | 1883 // Create empty preferences dictionary for each extension (these dictionaries |
1655 // are pruned when persisting the preferences to disk). | 1884 // are pruned when persisting the preferences to disk). |
1656 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | 1885 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); |
1657 ext_id != extension_ids.end(); ++ext_id) { | 1886 ext_id != extension_ids.end(); ++ext_id) { |
1658 ScopedExtensionPrefUpdate update(prefs_, *ext_id); | 1887 ScopedExtensionPrefUpdate update(prefs_, *ext_id); |
1659 // This creates an empty dictionary if none is stored. | 1888 // This creates an empty dictionary if none is stored. |
1660 update.Get(); | 1889 update.Get(); |
1661 } | 1890 } |
1662 | 1891 |
1663 FixMissingPrefs(extension_ids); | 1892 FixMissingPrefs(extension_ids); |
1893 InitializePageOrdinalMap(extension_ids); | |
1664 MigratePermissions(extension_ids); | 1894 MigratePermissions(extension_ids); |
1895 MigrateAppIndex(extension_ids); | |
1665 | 1896 |
1666 // Store extension controlled preference values in the | 1897 // Store extension controlled preference values in the |
1667 // |extension_pref_value_map_|, which then informs the subscribers | 1898 // |extension_pref_value_map_|, which then informs the subscribers |
1668 // (ExtensionPrefStores) about the winning values. | 1899 // (ExtensionPrefStores) about the winning values. |
1669 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | 1900 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); |
1670 ext_id != extension_ids.end(); ++ext_id) { | 1901 ext_id != extension_ids.end(); ++ext_id) { |
1671 extension_pref_value_map_->RegisterExtension( | 1902 extension_pref_value_map_->RegisterExtension( |
1672 *ext_id, | 1903 *ext_id, |
1673 GetInstallTime(*ext_id), | 1904 GetInstallTime(*ext_id), |
1674 !IsExtensionDisabled(*ext_id)); | 1905 !IsExtensionDisabled(*ext_id)); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1830 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, | 2061 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, |
1831 PrefService::UNSYNCABLE_PREF); | 2062 PrefService::UNSYNCABLE_PREF); |
1832 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, | 2063 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, |
1833 PrefService::UNSYNCABLE_PREF); | 2064 PrefService::UNSYNCABLE_PREF); |
1834 prefs->RegisterListPref(prefs::kExtensionInstallForceList, | 2065 prefs->RegisterListPref(prefs::kExtensionInstallForceList, |
1835 PrefService::UNSYNCABLE_PREF); | 2066 PrefService::UNSYNCABLE_PREF); |
1836 prefs->RegisterStringPref(kWebStoreLogin, | 2067 prefs->RegisterStringPref(kWebStoreLogin, |
1837 std::string() /* default_value */, | 2068 std::string() /* default_value */, |
1838 PrefService::UNSYNCABLE_PREF); | 2069 PrefService::UNSYNCABLE_PREF); |
1839 } | 2070 } |
OLD | NEW |