| 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 "base/base_paths_posix.h" | 7 #include "base/base_paths_posix.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/i18n/time_formatting.h" | 9 #include "base/i18n/time_formatting.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 if (extensions::PermissionsData::HasAPIPermission( | 87 if (extensions::PermissionsData::HasAPIPermission( |
| 88 *i, extensions::APIPermission::kMediaGalleries) || | 88 *i, extensions::APIPermission::kMediaGalleries) || |
| 89 extensions::PermissionsData::HasAPIPermission( | 89 extensions::PermissionsData::HasAPIPermission( |
| 90 *i, extensions::APIPermission::kMediaGalleriesPrivate)) { | 90 *i, extensions::APIPermission::kMediaGalleriesPrivate)) { |
| 91 count++; | 91 count++; |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 return count; | 94 return count; |
| 95 } | 95 } |
| 96 | 96 |
| 97 bool GetPrefId(const DictionaryValue& dict, MediaGalleryPrefId* value) { | 97 bool GetPrefId(const base::DictionaryValue& dict, MediaGalleryPrefId* value) { |
| 98 std::string string_id; | 98 std::string string_id; |
| 99 if (!dict.GetString(kMediaGalleriesPrefIdKey, &string_id) || | 99 if (!dict.GetString(kMediaGalleriesPrefIdKey, &string_id) || |
| 100 !base::StringToUint64(string_id, value)) { | 100 !base::StringToUint64(string_id, value)) { |
| 101 return false; | 101 return false; |
| 102 } | 102 } |
| 103 | 103 |
| 104 return true; | 104 return true; |
| 105 } | 105 } |
| 106 | 106 |
| 107 bool GetType(const DictionaryValue& dict, MediaGalleryPrefInfo::Type* type) { | 107 bool GetType(const base::DictionaryValue& dict, |
| 108 MediaGalleryPrefInfo::Type* type) { |
| 108 std::string string_type; | 109 std::string string_type; |
| 109 if (!dict.GetString(kMediaGalleriesTypeKey, &string_type)) | 110 if (!dict.GetString(kMediaGalleriesTypeKey, &string_type)) |
| 110 return false; | 111 return false; |
| 111 | 112 |
| 112 if (string_type == kMediaGalleriesTypeAutoDetectedValue) { | 113 if (string_type == kMediaGalleriesTypeAutoDetectedValue) { |
| 113 *type = MediaGalleryPrefInfo::kAutoDetected; | 114 *type = MediaGalleryPrefInfo::kAutoDetected; |
| 114 return true; | 115 return true; |
| 115 } | 116 } |
| 116 if (string_type == kMediaGalleriesTypeUserAddedValue) { | 117 if (string_type == kMediaGalleriesTypeUserAddedValue) { |
| 117 *type = MediaGalleryPrefInfo::kUserAdded; | 118 *type = MediaGalleryPrefInfo::kUserAdded; |
| 118 return true; | 119 return true; |
| 119 } | 120 } |
| 120 if (string_type == kMediaGalleriesTypeBlackListedValue) { | 121 if (string_type == kMediaGalleriesTypeBlackListedValue) { |
| 121 *type = MediaGalleryPrefInfo::kBlackListed; | 122 *type = MediaGalleryPrefInfo::kBlackListed; |
| 122 return true; | 123 return true; |
| 123 } | 124 } |
| 124 | 125 |
| 125 return false; | 126 return false; |
| 126 } | 127 } |
| 127 | 128 |
| 128 bool PopulateGalleryPrefInfoFromDictionary( | 129 bool PopulateGalleryPrefInfoFromDictionary( |
| 129 const DictionaryValue& dict, MediaGalleryPrefInfo* out_gallery_info) { | 130 const base::DictionaryValue& dict, MediaGalleryPrefInfo* out_gallery_info) { |
| 130 MediaGalleryPrefId pref_id; | 131 MediaGalleryPrefId pref_id; |
| 131 base::string16 display_name; | 132 base::string16 display_name; |
| 132 std::string device_id; | 133 std::string device_id; |
| 133 base::FilePath::StringType path; | 134 base::FilePath::StringType path; |
| 134 MediaGalleryPrefInfo::Type type = MediaGalleryPrefInfo::kAutoDetected; | 135 MediaGalleryPrefInfo::Type type = MediaGalleryPrefInfo::kAutoDetected; |
| 135 base::string16 volume_label; | 136 base::string16 volume_label; |
| 136 base::string16 vendor_name; | 137 base::string16 vendor_name; |
| 137 base::string16 model_name; | 138 base::string16 model_name; |
| 138 double total_size_in_bytes = 0.0; | 139 double total_size_in_bytes = 0.0; |
| 139 double last_attach_time = 0.0; | 140 double last_attach_time = 0.0; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 168 out_gallery_info->model_name = model_name; | 169 out_gallery_info->model_name = model_name; |
| 169 out_gallery_info->total_size_in_bytes = total_size_in_bytes; | 170 out_gallery_info->total_size_in_bytes = total_size_in_bytes; |
| 170 out_gallery_info->last_attach_time = | 171 out_gallery_info->last_attach_time = |
| 171 base::Time::FromInternalValue(last_attach_time); | 172 base::Time::FromInternalValue(last_attach_time); |
| 172 out_gallery_info->volume_metadata_valid = volume_metadata_valid; | 173 out_gallery_info->volume_metadata_valid = volume_metadata_valid; |
| 173 out_gallery_info->prefs_version = prefs_version; | 174 out_gallery_info->prefs_version = prefs_version; |
| 174 | 175 |
| 175 return true; | 176 return true; |
| 176 } | 177 } |
| 177 | 178 |
| 178 DictionaryValue* CreateGalleryPrefInfoDictionary( | 179 base::DictionaryValue* CreateGalleryPrefInfoDictionary( |
| 179 const MediaGalleryPrefInfo& gallery) { | 180 const MediaGalleryPrefInfo& gallery) { |
| 180 DictionaryValue* dict = new DictionaryValue(); | 181 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 181 dict->SetString(kMediaGalleriesPrefIdKey, | 182 dict->SetString(kMediaGalleriesPrefIdKey, |
| 182 base::Uint64ToString(gallery.pref_id)); | 183 base::Uint64ToString(gallery.pref_id)); |
| 183 if (!gallery.volume_metadata_valid) | 184 if (!gallery.volume_metadata_valid) |
| 184 dict->SetString(kMediaGalleriesDisplayNameKey, gallery.display_name); | 185 dict->SetString(kMediaGalleriesDisplayNameKey, gallery.display_name); |
| 185 dict->SetString(kMediaGalleriesDeviceIdKey, gallery.device_id); | 186 dict->SetString(kMediaGalleriesDeviceIdKey, gallery.device_id); |
| 186 dict->SetString(kMediaGalleriesPathKey, gallery.path.value()); | 187 dict->SetString(kMediaGalleriesPathKey, gallery.path.value()); |
| 187 | 188 |
| 188 const char* type = NULL; | 189 const char* type = NULL; |
| 189 switch (gallery.type) { | 190 switch (gallery.type) { |
| 190 case MediaGalleryPrefInfo::kAutoDetected: | 191 case MediaGalleryPrefInfo::kAutoDetected: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 bool HasAutoDetectedGalleryPermission(const extensions::Extension& extension) { | 227 bool HasAutoDetectedGalleryPermission(const extensions::Extension& extension) { |
| 227 extensions::MediaGalleriesPermission::CheckParam param( | 228 extensions::MediaGalleriesPermission::CheckParam param( |
| 228 extensions::MediaGalleriesPermission::kAllAutoDetectedPermission); | 229 extensions::MediaGalleriesPermission::kAllAutoDetectedPermission); |
| 229 return extensions::PermissionsData::CheckAPIPermissionWithParam( | 230 return extensions::PermissionsData::CheckAPIPermissionWithParam( |
| 230 &extension, extensions::APIPermission::kMediaGalleries, ¶m); | 231 &extension, extensions::APIPermission::kMediaGalleries, ¶m); |
| 231 } | 232 } |
| 232 | 233 |
| 233 // Retrieves the MediaGalleryPermission from the given dictionary; DCHECKs on | 234 // Retrieves the MediaGalleryPermission from the given dictionary; DCHECKs on |
| 234 // failure. | 235 // failure. |
| 235 bool GetMediaGalleryPermissionFromDictionary( | 236 bool GetMediaGalleryPermissionFromDictionary( |
| 236 const DictionaryValue* dict, | 237 const base::DictionaryValue* dict, |
| 237 MediaGalleryPermission* out_permission) { | 238 MediaGalleryPermission* out_permission) { |
| 238 std::string string_id; | 239 std::string string_id; |
| 239 if (dict->GetString(kMediaGalleryIdKey, &string_id) && | 240 if (dict->GetString(kMediaGalleryIdKey, &string_id) && |
| 240 base::StringToUint64(string_id, &out_permission->pref_id) && | 241 base::StringToUint64(string_id, &out_permission->pref_id) && |
| 241 dict->GetBoolean(kMediaGalleryHasPermissionKey, | 242 dict->GetBoolean(kMediaGalleryHasPermissionKey, |
| 242 &out_permission->has_permission)) { | 243 &out_permission->has_permission)) { |
| 243 return true; | 244 return true; |
| 244 } | 245 } |
| 245 NOTREACHED(); | 246 NOTREACHED(); |
| 246 return false; | 247 return false; |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 | 505 |
| 505 bool MediaGalleriesPreferences::UpdateDeviceIDForSingletonType( | 506 bool MediaGalleriesPreferences::UpdateDeviceIDForSingletonType( |
| 506 const std::string& device_id) { | 507 const std::string& device_id) { |
| 507 StorageInfo::Type singleton_type; | 508 StorageInfo::Type singleton_type; |
| 508 if (!StorageInfo::CrackDeviceId(device_id, &singleton_type, NULL)) | 509 if (!StorageInfo::CrackDeviceId(device_id, &singleton_type, NULL)) |
| 509 return false; | 510 return false; |
| 510 | 511 |
| 511 PrefService* prefs = profile_->GetPrefs(); | 512 PrefService* prefs = profile_->GetPrefs(); |
| 512 scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate( | 513 scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate( |
| 513 prefs, prefs::kMediaGalleriesRememberedGalleries)); | 514 prefs, prefs::kMediaGalleriesRememberedGalleries)); |
| 514 ListValue* list = update->Get(); | 515 base::ListValue* list = update->Get(); |
| 515 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { | 516 for (base::ListValue::iterator iter = list->begin(); |
| 517 iter != list->end(); ++iter) { |
| 516 // All of these calls should succeed, but preferences file can be corrupt. | 518 // All of these calls should succeed, but preferences file can be corrupt. |
| 517 DictionaryValue* dict; | 519 base::DictionaryValue* dict; |
| 518 if (!(*iter)->GetAsDictionary(&dict)) | 520 if (!(*iter)->GetAsDictionary(&dict)) |
| 519 continue; | 521 continue; |
| 520 std::string this_device_id; | 522 std::string this_device_id; |
| 521 if (!dict->GetString(kMediaGalleriesDeviceIdKey, &this_device_id)) | 523 if (!dict->GetString(kMediaGalleriesDeviceIdKey, &this_device_id)) |
| 522 continue; | 524 continue; |
| 523 if (this_device_id == device_id) | 525 if (this_device_id == device_id) |
| 524 return true; // No update is necessary. | 526 return true; // No update is necessary. |
| 525 StorageInfo::Type device_type; | 527 StorageInfo::Type device_type; |
| 526 if (!StorageInfo::CrackDeviceId(this_device_id, &device_type, NULL)) | 528 if (!StorageInfo::CrackDeviceId(this_device_id, &device_type, NULL)) |
| 527 continue; | 529 continue; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 } | 570 } |
| 569 | 571 |
| 570 OnInitializationCallbackReturned(); | 572 OnInitializationCallbackReturned(); |
| 571 } | 573 } |
| 572 | 574 |
| 573 void MediaGalleriesPreferences::InitFromPrefs() { | 575 void MediaGalleriesPreferences::InitFromPrefs() { |
| 574 known_galleries_.clear(); | 576 known_galleries_.clear(); |
| 575 device_map_.clear(); | 577 device_map_.clear(); |
| 576 | 578 |
| 577 PrefService* prefs = profile_->GetPrefs(); | 579 PrefService* prefs = profile_->GetPrefs(); |
| 578 const ListValue* list = prefs->GetList( | 580 const base::ListValue* list = prefs->GetList( |
| 579 prefs::kMediaGalleriesRememberedGalleries); | 581 prefs::kMediaGalleriesRememberedGalleries); |
| 580 if (list) { | 582 if (list) { |
| 581 for (ListValue::const_iterator it = list->begin(); | 583 for (base::ListValue::const_iterator it = list->begin(); |
| 582 it != list->end(); ++it) { | 584 it != list->end(); ++it) { |
| 583 const DictionaryValue* dict = NULL; | 585 const base::DictionaryValue* dict = NULL; |
| 584 if (!(*it)->GetAsDictionary(&dict)) | 586 if (!(*it)->GetAsDictionary(&dict)) |
| 585 continue; | 587 continue; |
| 586 | 588 |
| 587 MediaGalleryPrefInfo gallery_info; | 589 MediaGalleryPrefInfo gallery_info; |
| 588 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) | 590 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) |
| 589 continue; | 591 continue; |
| 590 | 592 |
| 591 known_galleries_[gallery_info.pref_id] = gallery_info; | 593 known_galleries_[gallery_info.pref_id] = gallery_info; |
| 592 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); | 594 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); |
| 593 } | 595 } |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 (existing.total_size_in_bytes != total_size_in_bytes) || | 752 (existing.total_size_in_bytes != total_size_in_bytes) || |
| 751 (existing.last_attach_time != last_attach_time)); | 753 (existing.last_attach_time != last_attach_time)); |
| 752 | 754 |
| 753 if (!update_gallery_name && !update_gallery_type && | 755 if (!update_gallery_name && !update_gallery_type && |
| 754 !update_gallery_metadata) | 756 !update_gallery_metadata) |
| 755 return *pref_id_it; | 757 return *pref_id_it; |
| 756 | 758 |
| 757 PrefService* prefs = profile_->GetPrefs(); | 759 PrefService* prefs = profile_->GetPrefs(); |
| 758 scoped_ptr<ListPrefUpdate> update( | 760 scoped_ptr<ListPrefUpdate> update( |
| 759 new ListPrefUpdate(prefs, prefs::kMediaGalleriesRememberedGalleries)); | 761 new ListPrefUpdate(prefs, prefs::kMediaGalleriesRememberedGalleries)); |
| 760 ListValue* list = update->Get(); | 762 base::ListValue* list = update->Get(); |
| 761 | 763 |
| 762 for (ListValue::const_iterator list_iter = list->begin(); | 764 for (base::ListValue::const_iterator list_iter = list->begin(); |
| 763 list_iter != list->end(); | 765 list_iter != list->end(); |
| 764 ++list_iter) { | 766 ++list_iter) { |
| 765 DictionaryValue* dict; | 767 base::DictionaryValue* dict; |
| 766 MediaGalleryPrefId iter_id; | 768 MediaGalleryPrefId iter_id; |
| 767 if ((*list_iter)->GetAsDictionary(&dict) && | 769 if ((*list_iter)->GetAsDictionary(&dict) && |
| 768 GetPrefId(*dict, &iter_id) && | 770 GetPrefId(*dict, &iter_id) && |
| 769 *pref_id_it == iter_id) { | 771 *pref_id_it == iter_id) { |
| 770 if (update_gallery_type) { | 772 if (update_gallery_type) { |
| 771 dict->SetString(kMediaGalleriesTypeKey, | 773 dict->SetString(kMediaGalleriesTypeKey, |
| 772 kMediaGalleriesTypeAutoDetectedValue); | 774 kMediaGalleriesTypeAutoDetectedValue); |
| 773 } | 775 } |
| 774 if (update_gallery_name) | 776 if (update_gallery_name) |
| 775 dict->SetString(kMediaGalleriesDisplayNameKey, display_name); | 777 dict->SetString(kMediaGalleriesDisplayNameKey, display_name); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 815 gallery_info.vendor_name = vendor_name; | 817 gallery_info.vendor_name = vendor_name; |
| 816 gallery_info.model_name = model_name; | 818 gallery_info.model_name = model_name; |
| 817 gallery_info.total_size_in_bytes = total_size_in_bytes; | 819 gallery_info.total_size_in_bytes = total_size_in_bytes; |
| 818 gallery_info.last_attach_time = last_attach_time; | 820 gallery_info.last_attach_time = last_attach_time; |
| 819 } | 821 } |
| 820 gallery_info.volume_metadata_valid = volume_metadata_valid; | 822 gallery_info.volume_metadata_valid = volume_metadata_valid; |
| 821 gallery_info.prefs_version = prefs_version; | 823 gallery_info.prefs_version = prefs_version; |
| 822 | 824 |
| 823 { | 825 { |
| 824 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); | 826 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); |
| 825 ListValue* list = update.Get(); | 827 base::ListValue* list = update.Get(); |
| 826 list->Append(CreateGalleryPrefInfoDictionary(gallery_info)); | 828 list->Append(CreateGalleryPrefInfoDictionary(gallery_info)); |
| 827 } | 829 } |
| 828 InitFromPrefs(); | 830 InitFromPrefs(); |
| 829 FOR_EACH_OBSERVER(GalleryChangeObserver, | 831 FOR_EACH_OBSERVER(GalleryChangeObserver, |
| 830 gallery_change_observers_, | 832 gallery_change_observers_, |
| 831 OnGalleryAdded(this, gallery_info.pref_id)); | 833 OnGalleryAdded(this, gallery_info.pref_id)); |
| 832 | 834 |
| 833 return gallery_info.pref_id; | 835 return gallery_info.pref_id; |
| 834 } | 836 } |
| 835 | 837 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 852 gallery_info.last_attach_time, | 854 gallery_info.last_attach_time, |
| 853 gallery_info.volume_metadata_valid, | 855 gallery_info.volume_metadata_valid, |
| 854 gallery_info.prefs_version); | 856 gallery_info.prefs_version); |
| 855 } | 857 } |
| 856 | 858 |
| 857 void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) { | 859 void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) { |
| 858 DCHECK(IsInitialized()); | 860 DCHECK(IsInitialized()); |
| 859 PrefService* prefs = profile_->GetPrefs(); | 861 PrefService* prefs = profile_->GetPrefs(); |
| 860 scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate( | 862 scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate( |
| 861 prefs, prefs::kMediaGalleriesRememberedGalleries)); | 863 prefs, prefs::kMediaGalleriesRememberedGalleries)); |
| 862 ListValue* list = update->Get(); | 864 base::ListValue* list = update->Get(); |
| 863 | 865 |
| 864 if (!ContainsKey(known_galleries_, pref_id)) | 866 if (!ContainsKey(known_galleries_, pref_id)) |
| 865 return; | 867 return; |
| 866 | 868 |
| 867 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { | 869 for (base::ListValue::iterator iter = list->begin(); |
| 868 DictionaryValue* dict; | 870 iter != list->end(); ++iter) { |
| 871 base::DictionaryValue* dict; |
| 869 MediaGalleryPrefId iter_id; | 872 MediaGalleryPrefId iter_id; |
| 870 if ((*iter)->GetAsDictionary(&dict) && GetPrefId(*dict, &iter_id) && | 873 if ((*iter)->GetAsDictionary(&dict) && GetPrefId(*dict, &iter_id) && |
| 871 pref_id == iter_id) { | 874 pref_id == iter_id) { |
| 872 RemoveGalleryPermissionsFromPrefs(pref_id); | 875 RemoveGalleryPermissionsFromPrefs(pref_id); |
| 873 MediaGalleryPrefInfo::Type type; | 876 MediaGalleryPrefInfo::Type type; |
| 874 if (GetType(*dict, &type) && | 877 if (GetType(*dict, &type) && |
| 875 type == MediaGalleryPrefInfo::kAutoDetected) { | 878 type == MediaGalleryPrefInfo::kAutoDetected) { |
| 876 dict->SetString(kMediaGalleriesTypeKey, | 879 dict->SetString(kMediaGalleriesTypeKey, |
| 877 kMediaGalleriesTypeBlackListedValue); | 880 kMediaGalleriesTypeBlackListedValue); |
| 878 } else { | 881 } else { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 } | 990 } |
| 988 | 991 |
| 989 bool MediaGalleriesPreferences::SetGalleryPermissionInPrefs( | 992 bool MediaGalleriesPreferences::SetGalleryPermissionInPrefs( |
| 990 const std::string& extension_id, | 993 const std::string& extension_id, |
| 991 MediaGalleryPrefId gallery_id, | 994 MediaGalleryPrefId gallery_id, |
| 992 bool has_access) { | 995 bool has_access) { |
| 993 DCHECK(IsInitialized()); | 996 DCHECK(IsInitialized()); |
| 994 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), | 997 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), |
| 995 extension_id, | 998 extension_id, |
| 996 kMediaGalleriesPermissions); | 999 kMediaGalleriesPermissions); |
| 997 ListValue* permissions = update.Get(); | 1000 base::ListValue* permissions = update.Get(); |
| 998 if (!permissions) { | 1001 if (!permissions) { |
| 999 permissions = update.Create(); | 1002 permissions = update.Create(); |
| 1000 } else { | 1003 } else { |
| 1001 // If the gallery is already in the list, update the permission... | 1004 // If the gallery is already in the list, update the permission... |
| 1002 for (ListValue::iterator iter = permissions->begin(); | 1005 for (base::ListValue::iterator iter = permissions->begin(); |
| 1003 iter != permissions->end(); ++iter) { | 1006 iter != permissions->end(); ++iter) { |
| 1004 DictionaryValue* dict = NULL; | 1007 base::DictionaryValue* dict = NULL; |
| 1005 if (!(*iter)->GetAsDictionary(&dict)) | 1008 if (!(*iter)->GetAsDictionary(&dict)) |
| 1006 continue; | 1009 continue; |
| 1007 MediaGalleryPermission perm; | 1010 MediaGalleryPermission perm; |
| 1008 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) | 1011 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
| 1009 continue; | 1012 continue; |
| 1010 if (perm.pref_id == gallery_id) { | 1013 if (perm.pref_id == gallery_id) { |
| 1011 if (has_access != perm.has_permission) { | 1014 if (has_access != perm.has_permission) { |
| 1012 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); | 1015 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); |
| 1013 return true; | 1016 return true; |
| 1014 } else { | 1017 } else { |
| 1015 return false; | 1018 return false; |
| 1016 } | 1019 } |
| 1017 } | 1020 } |
| 1018 } | 1021 } |
| 1019 } | 1022 } |
| 1020 // ...Otherwise, add a new entry for the gallery. | 1023 // ...Otherwise, add a new entry for the gallery. |
| 1021 DictionaryValue* dict = new DictionaryValue; | 1024 base::DictionaryValue* dict = new base::DictionaryValue; |
| 1022 dict->SetString(kMediaGalleryIdKey, base::Uint64ToString(gallery_id)); | 1025 dict->SetString(kMediaGalleryIdKey, base::Uint64ToString(gallery_id)); |
| 1023 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); | 1026 dict->SetBoolean(kMediaGalleryHasPermissionKey, has_access); |
| 1024 permissions->Append(dict); | 1027 permissions->Append(dict); |
| 1025 return true; | 1028 return true; |
| 1026 } | 1029 } |
| 1027 | 1030 |
| 1028 bool MediaGalleriesPreferences::UnsetGalleryPermissionInPrefs( | 1031 bool MediaGalleriesPreferences::UnsetGalleryPermissionInPrefs( |
| 1029 const std::string& extension_id, | 1032 const std::string& extension_id, |
| 1030 MediaGalleryPrefId gallery_id) { | 1033 MediaGalleryPrefId gallery_id) { |
| 1031 DCHECK(IsInitialized()); | 1034 DCHECK(IsInitialized()); |
| 1032 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), | 1035 ExtensionPrefs::ScopedListUpdate update(GetExtensionPrefs(), |
| 1033 extension_id, | 1036 extension_id, |
| 1034 kMediaGalleriesPermissions); | 1037 kMediaGalleriesPermissions); |
| 1035 ListValue* permissions = update.Get(); | 1038 base::ListValue* permissions = update.Get(); |
| 1036 if (!permissions) | 1039 if (!permissions) |
| 1037 return false; | 1040 return false; |
| 1038 | 1041 |
| 1039 for (ListValue::iterator iter = permissions->begin(); | 1042 for (base::ListValue::iterator iter = permissions->begin(); |
| 1040 iter != permissions->end(); ++iter) { | 1043 iter != permissions->end(); ++iter) { |
| 1041 const DictionaryValue* dict = NULL; | 1044 const base::DictionaryValue* dict = NULL; |
| 1042 if (!(*iter)->GetAsDictionary(&dict)) | 1045 if (!(*iter)->GetAsDictionary(&dict)) |
| 1043 continue; | 1046 continue; |
| 1044 MediaGalleryPermission perm; | 1047 MediaGalleryPermission perm; |
| 1045 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) | 1048 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
| 1046 continue; | 1049 continue; |
| 1047 if (perm.pref_id == gallery_id) { | 1050 if (perm.pref_id == gallery_id) { |
| 1048 permissions->Erase(iter, NULL); | 1051 permissions->Erase(iter, NULL); |
| 1049 return true; | 1052 return true; |
| 1050 } | 1053 } |
| 1051 } | 1054 } |
| 1052 return false; | 1055 return false; |
| 1053 } | 1056 } |
| 1054 | 1057 |
| 1055 std::vector<MediaGalleryPermission> | 1058 std::vector<MediaGalleryPermission> |
| 1056 MediaGalleriesPreferences::GetGalleryPermissionsFromPrefs( | 1059 MediaGalleriesPreferences::GetGalleryPermissionsFromPrefs( |
| 1057 const std::string& extension_id) const { | 1060 const std::string& extension_id) const { |
| 1058 DCHECK(IsInitialized()); | 1061 DCHECK(IsInitialized()); |
| 1059 std::vector<MediaGalleryPermission> result; | 1062 std::vector<MediaGalleryPermission> result; |
| 1060 const ListValue* permissions; | 1063 const base::ListValue* permissions; |
| 1061 if (!GetExtensionPrefs()->ReadPrefAsList(extension_id, | 1064 if (!GetExtensionPrefs()->ReadPrefAsList(extension_id, |
| 1062 kMediaGalleriesPermissions, | 1065 kMediaGalleriesPermissions, |
| 1063 &permissions)) { | 1066 &permissions)) { |
| 1064 return result; | 1067 return result; |
| 1065 } | 1068 } |
| 1066 | 1069 |
| 1067 for (ListValue::const_iterator iter = permissions->begin(); | 1070 for (base::ListValue::const_iterator iter = permissions->begin(); |
| 1068 iter != permissions->end(); ++iter) { | 1071 iter != permissions->end(); ++iter) { |
| 1069 DictionaryValue* dict = NULL; | 1072 base::DictionaryValue* dict = NULL; |
| 1070 if (!(*iter)->GetAsDictionary(&dict)) | 1073 if (!(*iter)->GetAsDictionary(&dict)) |
| 1071 continue; | 1074 continue; |
| 1072 MediaGalleryPermission perm; | 1075 MediaGalleryPermission perm; |
| 1073 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) | 1076 if (!GetMediaGalleryPermissionFromDictionary(dict, &perm)) |
| 1074 continue; | 1077 continue; |
| 1075 result.push_back(perm); | 1078 result.push_back(perm); |
| 1076 } | 1079 } |
| 1077 | 1080 |
| 1078 return result; | 1081 return result; |
| 1079 } | 1082 } |
| 1080 | 1083 |
| 1081 void MediaGalleriesPreferences::RemoveGalleryPermissionsFromPrefs( | 1084 void MediaGalleriesPreferences::RemoveGalleryPermissionsFromPrefs( |
| 1082 MediaGalleryPrefId gallery_id) { | 1085 MediaGalleryPrefId gallery_id) { |
| 1083 DCHECK(IsInitialized()); | 1086 DCHECK(IsInitialized()); |
| 1084 ExtensionPrefs* prefs = GetExtensionPrefs(); | 1087 ExtensionPrefs* prefs = GetExtensionPrefs(); |
| 1085 const DictionaryValue* extensions = | 1088 const base::DictionaryValue* extensions = |
| 1086 prefs->pref_service()->GetDictionary(prefs::kExtensionsPref); | 1089 prefs->pref_service()->GetDictionary(prefs::kExtensionsPref); |
| 1087 if (!extensions) | 1090 if (!extensions) |
| 1088 return; | 1091 return; |
| 1089 | 1092 |
| 1090 for (DictionaryValue::Iterator iter(*extensions); !iter.IsAtEnd(); | 1093 for (base::DictionaryValue::Iterator iter(*extensions); !iter.IsAtEnd(); |
| 1091 iter.Advance()) { | 1094 iter.Advance()) { |
| 1092 if (!extensions::Extension::IdIsValid(iter.key())) { | 1095 if (!extensions::Extension::IdIsValid(iter.key())) { |
| 1093 NOTREACHED(); | 1096 NOTREACHED(); |
| 1094 continue; | 1097 continue; |
| 1095 } | 1098 } |
| 1096 UnsetGalleryPermissionInPrefs(iter.key(), gallery_id); | 1099 UnsetGalleryPermissionInPrefs(iter.key(), gallery_id); |
| 1097 } | 1100 } |
| 1098 } | 1101 } |
| 1099 | 1102 |
| 1100 ExtensionPrefs* MediaGalleriesPreferences::GetExtensionPrefs() const { | 1103 ExtensionPrefs* MediaGalleriesPreferences::GetExtensionPrefs() const { |
| 1101 DCHECK(IsInitialized()); | 1104 DCHECK(IsInitialized()); |
| 1102 if (extension_prefs_for_testing_) | 1105 if (extension_prefs_for_testing_) |
| 1103 return extension_prefs_for_testing_; | 1106 return extension_prefs_for_testing_; |
| 1104 return extensions::ExtensionPrefs::Get(profile_); | 1107 return extensions::ExtensionPrefs::Get(profile_); |
| 1105 } | 1108 } |
| 1106 | 1109 |
| 1107 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( | 1110 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( |
| 1108 extensions::ExtensionPrefs* extension_prefs) { | 1111 extensions::ExtensionPrefs* extension_prefs) { |
| 1109 DCHECK(IsInitialized()); | 1112 DCHECK(IsInitialized()); |
| 1110 extension_prefs_for_testing_ = extension_prefs; | 1113 extension_prefs_for_testing_ = extension_prefs; |
| 1111 } | 1114 } |
| OLD | NEW |