| 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" |
| 11 #include "chrome/browser/prefs/pref_notifier.h" | 11 #include "chrome/browser/prefs/pref_notifier.h" |
| 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/url_pattern.h" | 16 #include "chrome/common/extensions/url_pattern.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "content/common/notification_service.h" | 18 #include "content/common/notification_service.h" |
| 19 | 19 |
| 20 using base::Time; | 20 using base::Time; |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // The number of apps per page. This isn't a hard limit, but new apps installed |
| 25 // from the webstore will overflow onto a new page if this limit is reached. |
| 26 const int kNaturalAppPageSize = 18; |
| 27 |
| 24 // Additional preferences keys | 28 // Additional preferences keys |
| 25 | 29 |
| 26 // Where an extension was installed from. (see Extension::Location) | 30 // Where an extension was installed from. (see Extension::Location) |
| 27 const char kPrefLocation[] = "location"; | 31 const char kPrefLocation[] = "location"; |
| 28 | 32 |
| 29 // Enabled, disabled, killed, etc. (see Extension::State) | 33 // Enabled, disabled, killed, etc. (see Extension::State) |
| 30 const char kPrefState[] = "state"; | 34 const char kPrefState[] = "state"; |
| 31 | 35 |
| 32 // The path to the current version's manifest file. | 36 // The path to the current version's manifest file. |
| 33 const char kPrefPath[] = "path"; | 37 const char kPrefPath[] = "path"; |
| (...skipping 939 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 | 977 |
| 974 FilePath::StringType path = MakePathRelative(install_directory_, | 978 FilePath::StringType path = MakePathRelative(install_directory_, |
| 975 extension->path()); | 979 extension->path()); |
| 976 extension_dict->Set(kPrefPath, Value::CreateStringValue(path)); | 980 extension_dict->Set(kPrefPath, Value::CreateStringValue(path)); |
| 977 // We store prefs about LOAD extensions, but don't cache their manifest | 981 // We store prefs about LOAD extensions, but don't cache their manifest |
| 978 // since it may change on disk. | 982 // since it may change on disk. |
| 979 if (extension->location() != Extension::LOAD) { | 983 if (extension->location() != Extension::LOAD) { |
| 980 extension_dict->Set(kPrefManifest, | 984 extension_dict->Set(kPrefManifest, |
| 981 extension->manifest_value()->DeepCopy()); | 985 extension->manifest_value()->DeepCopy()); |
| 982 } | 986 } |
| 983 extension_dict->Set(kPrefPageIndex, | 987 |
| 984 Value::CreateIntegerValue(page_index)); | 988 if (extension->is_app()) { |
| 985 extension_dict->Set(kPrefAppLaunchIndex, | 989 if (page_index == -1) |
| 986 Value::CreateIntegerValue(GetNextAppLaunchIndex(page_index))); | 990 page_index = GetNaturalAppPageIndex(); |
| 991 extension_dict->Set(kPrefPageIndex, |
| 992 Value::CreateIntegerValue(page_index)); |
| 993 extension_dict->Set(kPrefAppLaunchIndex, |
| 994 Value::CreateIntegerValue(GetNextAppLaunchIndex(page_index))); |
| 995 } |
| 987 extension_pref_value_map_->RegisterExtension( | 996 extension_pref_value_map_->RegisterExtension( |
| 988 id, install_time, initial_state == Extension::ENABLED); | 997 id, install_time, initial_state == Extension::ENABLED); |
| 989 content_settings_store_->RegisterExtension( | 998 content_settings_store_->RegisterExtension( |
| 990 id, install_time, initial_state == Extension::ENABLED); | 999 id, install_time, initial_state == Extension::ENABLED); |
| 991 } | 1000 } |
| 992 | 1001 |
| 993 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, | 1002 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, |
| 994 const Extension::Location& location, | 1003 const Extension::Location& location, |
| 995 bool external_uninstall) { | 1004 bool external_uninstall) { |
| 996 // For external extensions, we save a preference reminding ourself not to try | 1005 // For external extensions, we save a preference reminding ourself not to try |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1315 int ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) { | 1324 int ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) { |
| 1316 int value; | 1325 int value; |
| 1317 if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value)) | 1326 if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value)) |
| 1318 return value; | 1327 return value; |
| 1319 | 1328 |
| 1320 return -1; | 1329 return -1; |
| 1321 } | 1330 } |
| 1322 | 1331 |
| 1323 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, | 1332 void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id, |
| 1324 int index) { | 1333 int index) { |
| 1325 DCHECK_GE(index, 0); | |
| 1326 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, | 1334 UpdateExtensionPref(extension_id, kPrefAppLaunchIndex, |
| 1327 Value::CreateIntegerValue(index)); | 1335 Value::CreateIntegerValue(index)); |
| 1328 } | 1336 } |
| 1329 | 1337 |
| 1330 int ExtensionPrefs::GetNextAppLaunchIndex(int on_page) { | 1338 int ExtensionPrefs::GetNextAppLaunchIndex(int on_page) { |
| 1331 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 1339 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| 1332 if (!extensions) | 1340 if (!extensions) |
| 1333 return 0; | 1341 return 0; |
| 1334 | 1342 |
| 1335 int max_value = -1; | 1343 int max_value = -1; |
| 1336 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); | 1344 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); |
| 1337 extension_id != extensions->end_keys(); ++extension_id) { | 1345 extension_id != extensions->end_keys(); ++extension_id) { |
| 1338 int value = GetAppLaunchIndex(*extension_id); | 1346 int value = GetAppLaunchIndex(*extension_id); |
| 1339 int page = GetPageIndex(*extension_id); | 1347 int page = GetPageIndex(*extension_id); |
| 1340 if (page == on_page && value > max_value) | 1348 if (page == on_page && value > max_value) |
| 1341 max_value = value; | 1349 max_value = value; |
| 1342 } | 1350 } |
| 1343 return max_value + 1; | 1351 return max_value + 1; |
| 1344 } | 1352 } |
| 1345 | 1353 |
| 1354 int ExtensionPrefs::GetNaturalAppPageIndex() { |
| 1355 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| 1356 if (!extensions) |
| 1357 return 0; |
| 1358 |
| 1359 std::map<int, int> page_counts; |
| 1360 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); |
| 1361 extension_id != extensions->end_keys(); ++extension_id) { |
| 1362 int page_index = GetPageIndex(*extension_id); |
| 1363 if (page_index >= 0) |
| 1364 page_counts[page_index] = page_counts[page_index] + 1; |
| 1365 } |
| 1366 for (int i = 0; ; i++) { |
| 1367 std::map<int, int>::const_iterator it = page_counts.find(i); |
| 1368 if (it == page_counts.end() || it->second < kNaturalAppPageSize) |
| 1369 return i; |
| 1370 } |
| 1371 } |
| 1372 |
| 1346 void ExtensionPrefs::SetAppLauncherOrder( | 1373 void ExtensionPrefs::SetAppLauncherOrder( |
| 1347 const std::vector<std::string>& extension_ids) { | 1374 const std::vector<std::string>& extension_ids) { |
| 1348 for (size_t i = 0; i < extension_ids.size(); ++i) | 1375 for (size_t i = 0; i < extension_ids.size(); ++i) |
| 1349 SetAppLaunchIndex(extension_ids.at(i), i); | 1376 SetAppLaunchIndex(extension_ids.at(i), i); |
| 1350 | 1377 |
| 1351 NotificationService::current()->Notify( | 1378 NotificationService::current()->Notify( |
| 1352 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, | 1379 chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, |
| 1353 Source<ExtensionPrefs>(this), | 1380 Source<ExtensionPrefs>(this), |
| 1354 NotificationService::NoDetails()); | 1381 NotificationService::NoDetails()); |
| 1355 } | 1382 } |
| 1356 | 1383 |
| 1357 int ExtensionPrefs::GetPageIndex(const std::string& extension_id) { | 1384 int ExtensionPrefs::GetPageIndex(const std::string& extension_id) { |
| 1358 int value = -1; | 1385 int value = -1; |
| 1359 ReadExtensionPrefInteger(extension_id, kPrefPageIndex, &value); | 1386 ReadExtensionPrefInteger(extension_id, kPrefPageIndex, &value); |
| 1360 return value; | 1387 return value; |
| 1361 } | 1388 } |
| 1362 | 1389 |
| 1363 void ExtensionPrefs::SetPageIndex(const std::string& extension_id, int index) { | 1390 void ExtensionPrefs::SetPageIndex(const std::string& extension_id, int index) { |
| 1364 CHECK_GE(index, 0); | |
| 1365 UpdateExtensionPref(extension_id, kPrefPageIndex, | 1391 UpdateExtensionPref(extension_id, kPrefPageIndex, |
| 1366 Value::CreateIntegerValue(index)); | 1392 Value::CreateIntegerValue(index)); |
| 1367 } | 1393 } |
| 1368 | 1394 |
| 1369 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { | 1395 bool ExtensionPrefs::WasAppDraggedByUser(const std::string& extension_id) { |
| 1370 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); | 1396 return ReadExtensionPrefBoolean(extension_id, kPrefUserDraggedApp); |
| 1371 } | 1397 } |
| 1372 | 1398 |
| 1373 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { | 1399 void ExtensionPrefs::SetAppDraggedByUser(const std::string& extension_id) { |
| 1374 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, | 1400 UpdateExtensionPref(extension_id, kPrefUserDraggedApp, |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1673 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, | 1699 prefs->RegisterListPref(prefs::kExtensionInstallAllowList, |
| 1674 PrefService::UNSYNCABLE_PREF); | 1700 PrefService::UNSYNCABLE_PREF); |
| 1675 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, | 1701 prefs->RegisterListPref(prefs::kExtensionInstallDenyList, |
| 1676 PrefService::UNSYNCABLE_PREF); | 1702 PrefService::UNSYNCABLE_PREF); |
| 1677 prefs->RegisterListPref(prefs::kExtensionInstallForceList, | 1703 prefs->RegisterListPref(prefs::kExtensionInstallForceList, |
| 1678 PrefService::UNSYNCABLE_PREF); | 1704 PrefService::UNSYNCABLE_PREF); |
| 1679 prefs->RegisterStringPref(kWebStoreLogin, | 1705 prefs->RegisterStringPref(kWebStoreLogin, |
| 1680 std::string() /* default_value */, | 1706 std::string() /* default_value */, |
| 1681 PrefService::UNSYNCABLE_PREF); | 1707 PrefService::UNSYNCABLE_PREF); |
| 1682 } | 1708 } |
| OLD | NEW |