OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/media_galleries/media_galleries_preferences.h" | 5 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 return false; | 614 return false; |
615 | 615 |
616 PrefService* prefs = profile_->GetPrefs(); | 616 PrefService* prefs = profile_->GetPrefs(); |
617 std::unique_ptr<ListPrefUpdate> update( | 617 std::unique_ptr<ListPrefUpdate> update( |
618 new ListPrefUpdate(prefs, prefs::kMediaGalleriesRememberedGalleries)); | 618 new ListPrefUpdate(prefs, prefs::kMediaGalleriesRememberedGalleries)); |
619 base::ListValue* list = update->Get(); | 619 base::ListValue* list = update->Get(); |
620 for (base::ListValue::iterator iter = list->begin(); | 620 for (base::ListValue::iterator iter = list->begin(); |
621 iter != list->end(); ++iter) { | 621 iter != list->end(); ++iter) { |
622 // All of these calls should succeed, but preferences file can be corrupt. | 622 // All of these calls should succeed, but preferences file can be corrupt. |
623 base::DictionaryValue* dict; | 623 base::DictionaryValue* dict; |
624 if (!(*iter)->GetAsDictionary(&dict)) | 624 if (!iter->GetAsDictionary(&dict)) |
625 continue; | 625 continue; |
626 std::string this_device_id; | 626 std::string this_device_id; |
627 if (!dict->GetString(kMediaGalleriesDeviceIdKey, &this_device_id)) | 627 if (!dict->GetString(kMediaGalleriesDeviceIdKey, &this_device_id)) |
628 continue; | 628 continue; |
629 if (this_device_id == device_id) | 629 if (this_device_id == device_id) |
630 return true; // No update is necessary. | 630 return true; // No update is necessary. |
631 StorageInfo::Type device_type; | 631 StorageInfo::Type device_type; |
632 if (!StorageInfo::CrackDeviceId(this_device_id, &device_type, NULL)) | 632 if (!StorageInfo::CrackDeviceId(this_device_id, &device_type, NULL)) |
633 continue; | 633 continue; |
634 | 634 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 known_galleries_.clear(); | 706 known_galleries_.clear(); |
707 device_map_.clear(); | 707 device_map_.clear(); |
708 | 708 |
709 PrefService* prefs = profile_->GetPrefs(); | 709 PrefService* prefs = profile_->GetPrefs(); |
710 const base::ListValue* list = prefs->GetList( | 710 const base::ListValue* list = prefs->GetList( |
711 prefs::kMediaGalleriesRememberedGalleries); | 711 prefs::kMediaGalleriesRememberedGalleries); |
712 if (list) { | 712 if (list) { |
713 for (base::ListValue::const_iterator it = list->begin(); | 713 for (base::ListValue::const_iterator it = list->begin(); |
714 it != list->end(); ++it) { | 714 it != list->end(); ++it) { |
715 const base::DictionaryValue* dict = NULL; | 715 const base::DictionaryValue* dict = NULL; |
716 if (!(*it)->GetAsDictionary(&dict)) | 716 if (!it->GetAsDictionary(&dict)) |
717 continue; | 717 continue; |
718 | 718 |
719 MediaGalleryPrefInfo gallery_info; | 719 MediaGalleryPrefInfo gallery_info; |
720 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) | 720 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) |
721 continue; | 721 continue; |
722 | 722 |
723 known_galleries_[gallery_info.pref_id] = gallery_info; | 723 known_galleries_[gallery_info.pref_id] = gallery_info; |
724 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); | 724 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); |
725 } | 725 } |
726 } | 726 } |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
950 if (!update_gallery_name && !update_gallery_type && | 950 if (!update_gallery_name && !update_gallery_type && |
951 !update_gallery_metadata && !update_scan_counts && | 951 !update_gallery_metadata && !update_scan_counts && |
952 !update_default_gallery_type) | 952 !update_default_gallery_type) |
953 return *pref_id_it; | 953 return *pref_id_it; |
954 | 954 |
955 PrefService* prefs = profile_->GetPrefs(); | 955 PrefService* prefs = profile_->GetPrefs(); |
956 std::unique_ptr<ListPrefUpdate> update( | 956 std::unique_ptr<ListPrefUpdate> update( |
957 new ListPrefUpdate(prefs, prefs::kMediaGalleriesRememberedGalleries)); | 957 new ListPrefUpdate(prefs, prefs::kMediaGalleriesRememberedGalleries)); |
958 base::ListValue* list = update->Get(); | 958 base::ListValue* list = update->Get(); |
959 | 959 |
960 for (base::ListValue::const_iterator list_iter = list->begin(); | 960 for (base::ListValue::iterator list_iter = list->begin(); |
961 list_iter != list->end(); | 961 list_iter != list->end(); ++list_iter) { |
962 ++list_iter) { | |
963 base::DictionaryValue* dict; | 962 base::DictionaryValue* dict; |
964 MediaGalleryPrefId iter_id; | 963 MediaGalleryPrefId iter_id; |
965 if ((*list_iter)->GetAsDictionary(&dict) && | 964 if (list_iter->GetAsDictionary(&dict) && GetPrefId(*dict, &iter_id) && |
966 GetPrefId(*dict, &iter_id) && | |
967 *pref_id_it == iter_id) { | 965 *pref_id_it == iter_id) { |
968 if (update_gallery_type) | 966 if (update_gallery_type) |
969 dict->SetString(kMediaGalleriesTypeKey, TypeToStringValue(new_type)); | 967 dict->SetString(kMediaGalleriesTypeKey, TypeToStringValue(new_type)); |
970 if (update_gallery_name) | 968 if (update_gallery_name) |
971 dict->SetString(kMediaGalleriesDisplayNameKey, display_name); | 969 dict->SetString(kMediaGalleriesDisplayNameKey, display_name); |
972 if (update_gallery_metadata) { | 970 if (update_gallery_metadata) { |
973 dict->SetString(kMediaGalleriesVolumeLabelKey, volume_label); | 971 dict->SetString(kMediaGalleriesVolumeLabelKey, volume_label); |
974 dict->SetString(kMediaGalleriesVendorNameKey, vendor_name); | 972 dict->SetString(kMediaGalleriesVendorNameKey, vendor_name); |
975 dict->SetString(kMediaGalleriesModelNameKey, model_name); | 973 dict->SetString(kMediaGalleriesModelNameKey, model_name); |
976 dict->SetDouble(kMediaGalleriesSizeKey, total_size_in_bytes); | 974 dict->SetDouble(kMediaGalleriesSizeKey, total_size_in_bytes); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1051 base::ListValue* list = update->Get(); | 1049 base::ListValue* list = update->Get(); |
1052 | 1050 |
1053 std::vector<MediaGalleryPrefId> pref_ids; | 1051 std::vector<MediaGalleryPrefId> pref_ids; |
1054 | 1052 |
1055 for (base::ListValue::iterator iter = list->begin(); | 1053 for (base::ListValue::iterator iter = list->begin(); |
1056 iter != list->end(); | 1054 iter != list->end(); |
1057 ++iter) { | 1055 ++iter) { |
1058 base::DictionaryValue* dict; | 1056 base::DictionaryValue* dict; |
1059 MediaGalleryPrefId pref_id; | 1057 MediaGalleryPrefId pref_id; |
1060 | 1058 |
1061 if (!((*iter)->GetAsDictionary(&dict) && GetPrefId(*dict, &pref_id))) | 1059 if (!(iter->GetAsDictionary(&dict) && GetPrefId(*dict, &pref_id))) |
1062 continue; | 1060 continue; |
1063 | 1061 |
1064 std::string default_gallery_type_string; | 1062 std::string default_gallery_type_string; |
1065 | 1063 |
1066 // If the "default gallery type" key is set, just update the paths in place. | 1064 // If the "default gallery type" key is set, just update the paths in place. |
1067 // If it's not set, then AddOrUpdateGalleryInternal will take care of | 1065 // If it's not set, then AddOrUpdateGalleryInternal will take care of |
1068 // setting it as part of migration to prefs version 3. | 1066 // setting it as part of migration to prefs version 3. |
1069 if (dict->GetString(kMediaGalleriesDefaultGalleryTypeKey, | 1067 if (dict->GetString(kMediaGalleriesDefaultGalleryTypeKey, |
1070 &default_gallery_type_string)) { | 1068 &default_gallery_type_string)) { |
1071 std::string device_id; | 1069 std::string device_id; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1148 new ListPrefUpdate(prefs, prefs::kMediaGalleriesRememberedGalleries)); | 1146 new ListPrefUpdate(prefs, prefs::kMediaGalleriesRememberedGalleries)); |
1149 base::ListValue* list = update->Get(); | 1147 base::ListValue* list = update->Get(); |
1150 | 1148 |
1151 if (!base::ContainsKey(known_galleries_, id)) | 1149 if (!base::ContainsKey(known_galleries_, id)) |
1152 return; | 1150 return; |
1153 | 1151 |
1154 for (base::ListValue::iterator iter = list->begin(); | 1152 for (base::ListValue::iterator iter = list->begin(); |
1155 iter != list->end(); ++iter) { | 1153 iter != list->end(); ++iter) { |
1156 base::DictionaryValue* dict; | 1154 base::DictionaryValue* dict; |
1157 MediaGalleryPrefId iter_id; | 1155 MediaGalleryPrefId iter_id; |
1158 if ((*iter)->GetAsDictionary(&dict) && GetPrefId(*dict, &iter_id) && | 1156 if (iter->GetAsDictionary(&dict) && GetPrefId(*dict, &iter_id) && |
1159 id == iter_id) { | 1157 id == iter_id) { |
1160 RemoveGalleryPermissionsFromPrefs(id); | 1158 RemoveGalleryPermissionsFromPrefs(id); |
1161 MediaGalleryPrefInfo::Type type; | 1159 MediaGalleryPrefInfo::Type type; |
1162 if (!erase && GetType(*dict, &type) && | 1160 if (!erase && GetType(*dict, &type) && |
1163 (type == MediaGalleryPrefInfo::kAutoDetected || | 1161 (type == MediaGalleryPrefInfo::kAutoDetected || |
1164 type == MediaGalleryPrefInfo::kScanResult)) { | 1162 type == MediaGalleryPrefInfo::kScanResult)) { |
1165 if (type == MediaGalleryPrefInfo::kAutoDetected) { | 1163 if (type == MediaGalleryPrefInfo::kAutoDetected) { |
1166 dict->SetString(kMediaGalleriesTypeKey, | 1164 dict->SetString(kMediaGalleriesTypeKey, |
1167 kMediaGalleriesTypeBlackListedValue); | 1165 kMediaGalleriesTypeBlackListedValue); |
1168 } else { | 1166 } else { |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1326 extension_id, | 1324 extension_id, |
1327 kMediaGalleriesPermissions); | 1325 kMediaGalleriesPermissions); |
1328 base::ListValue* permissions = update.Get(); | 1326 base::ListValue* permissions = update.Get(); |
1329 if (!permissions) { | 1327 if (!permissions) { |
1330 permissions = update.Create(); | 1328 permissions = update.Create(); |
1331 } else { | 1329 } else { |
1332 // If the gallery is already in the list, update the permission... | 1330 // If the gallery is already in the list, update the permission... |
1333 for (base::ListValue::iterator iter = permissions->begin(); | 1331 for (base::ListValue::iterator iter = permissions->begin(); |
1334 iter != permissions->end(); ++iter) { | 1332 iter != permissions->end(); ++iter) { |
1335 base::DictionaryValue* dict = NULL; | 1333 base::DictionaryValue* dict = NULL; |
1336 if (!(*iter)->GetAsDictionary(&dict)) | 1334 if (!iter->GetAsDictionary(&dict)) |
1337 continue; | 1335 continue; |
1338 MediaGalleryPermission perm; | 1336 MediaGalleryPermission perm; |
1339 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) | 1337 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
1340 continue; | 1338 continue; |
1341 if (perm.pref_id == gallery_id) { | 1339 if (perm.pref_id == gallery_id) { |
1342 if (has_access != perm.has_permission) { | 1340 if (has_access != perm.has_permission) { |
1343 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); | 1341 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); |
1344 return true; | 1342 return true; |
1345 } else { | 1343 } else { |
1346 return false; | 1344 return false; |
(...skipping 16 matching lines...) Expand all Loading... |
1363 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), | 1361 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), |
1364 extension_id, | 1362 extension_id, |
1365 kMediaGalleriesPermissions); | 1363 kMediaGalleriesPermissions); |
1366 base::ListValue* permissions = update.Get(); | 1364 base::ListValue* permissions = update.Get(); |
1367 if (!permissions) | 1365 if (!permissions) |
1368 return false; | 1366 return false; |
1369 | 1367 |
1370 for (base::ListValue::iterator iter = permissions->begin(); | 1368 for (base::ListValue::iterator iter = permissions->begin(); |
1371 iter != permissions->end(); ++iter) { | 1369 iter != permissions->end(); ++iter) { |
1372 const base::DictionaryValue* dict = NULL; | 1370 const base::DictionaryValue* dict = NULL; |
1373 if (!(*iter)->GetAsDictionary(&dict)) | 1371 if (!iter->GetAsDictionary(&dict)) |
1374 continue; | 1372 continue; |
1375 MediaGalleryPermission perm; | 1373 MediaGalleryPermission perm; |
1376 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) | 1374 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
1377 continue; | 1375 continue; |
1378 if (perm.pref_id == gallery_id) { | 1376 if (perm.pref_id == gallery_id) { |
1379 permissions->Erase(iter, NULL); | 1377 permissions->Erase(iter, NULL); |
1380 return true; | 1378 return true; |
1381 } | 1379 } |
1382 } | 1380 } |
1383 return false; | 1381 return false; |
1384 } | 1382 } |
1385 | 1383 |
1386 std::vector<MediaGalleryPermission> | 1384 std::vector<MediaGalleryPermission> |
1387 MediaGalleriesPreferences::GetGalleryPermissionsFromPrefs( | 1385 MediaGalleriesPreferences::GetGalleryPermissionsFromPrefs( |
1388 const std::string& extension_id) const { | 1386 const std::string& extension_id) const { |
1389 DCHECK(IsInitialized()); | 1387 DCHECK(IsInitialized()); |
1390 std::vector<MediaGalleryPermission> result; | 1388 std::vector<MediaGalleryPermission> result; |
1391 const base::ListValue* permissions; | 1389 const base::ListValue* permissions; |
1392 if (!GetExtensionPrefs()->ReadPrefAsList(extension_id, | 1390 if (!GetExtensionPrefs()->ReadPrefAsList(extension_id, |
1393 kMediaGalleriesPermissions, | 1391 kMediaGalleriesPermissions, |
1394 &permissions)) { | 1392 &permissions)) { |
1395 return result; | 1393 return result; |
1396 } | 1394 } |
1397 | 1395 |
1398 for (base::ListValue::const_iterator iter = permissions->begin(); | 1396 for (base::ListValue::const_iterator iter = permissions->begin(); |
1399 iter != permissions->end(); ++iter) { | 1397 iter != permissions->end(); ++iter) { |
1400 base::DictionaryValue* dict = NULL; | 1398 const base::DictionaryValue* dict = NULL; |
1401 if (!(*iter)->GetAsDictionary(&dict)) | 1399 if (!iter->GetAsDictionary(&dict)) |
1402 continue; | 1400 continue; |
1403 MediaGalleryPermission perm; | 1401 MediaGalleryPermission perm; |
1404 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) | 1402 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
1405 continue; | 1403 continue; |
1406 result.push_back(perm); | 1404 result.push_back(perm); |
1407 } | 1405 } |
1408 | 1406 |
1409 return result; | 1407 return result; |
1410 } | 1408 } |
1411 | 1409 |
(...skipping 21 matching lines...) Expand all Loading... |
1433 if (extension_prefs_for_testing_) | 1431 if (extension_prefs_for_testing_) |
1434 return extension_prefs_for_testing_; | 1432 return extension_prefs_for_testing_; |
1435 return extensions::ExtensionPrefs::Get(profile_); | 1433 return extensions::ExtensionPrefs::Get(profile_); |
1436 } | 1434 } |
1437 | 1435 |
1438 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( | 1436 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( |
1439 extensions::ExtensionPrefs* extension_prefs) { | 1437 extensions::ExtensionPrefs* extension_prefs) { |
1440 DCHECK(IsInitialized()); | 1438 DCHECK(IsInitialized()); |
1441 extension_prefs_for_testing_ = extension_prefs; | 1439 extension_prefs_for_testing_ = extension_prefs; |
1442 } | 1440 } |
OLD | NEW |