Chromium Code Reviews| Index: chrome/browser/media_gallery/media_galleries_preferences.cc |
| diff --git a/chrome/browser/media_gallery/media_galleries_preferences.cc b/chrome/browser/media_gallery/media_galleries_preferences.cc |
| index 600688ea88e654a7a52ef3fe3fb8a9df889f489a..4e8f9bb00a84c032fd2565fad2fd1a90bd01bee7 100644 |
| --- a/chrome/browser/media_gallery/media_galleries_preferences.cc |
| +++ b/chrome/browser/media_gallery/media_galleries_preferences.cc |
| @@ -7,22 +7,35 @@ |
| #include "base/command_line.h" |
| #include "base/string_number_conversions.h" |
| #include "base/values.h" |
| +#include "chrome/browser/extensions/extension_prefs.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/extensions/extension_system.h" |
| +#include "chrome/browser/media_gallery/media_file_system_registry.h" |
| #include "chrome/browser/prefs/pref_service.h" |
| #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/chrome_switches.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "chrome/common/extensions/permissions/api_permission.h" |
| #include "chrome/common/pref_names.h" |
| #include "grit/generated_resources.h" |
| +namespace chrome { |
| + |
| namespace { |
| -const char kMediaGalleriesIdKey[] = "id"; |
| -const char kMediaGalleriesPathKey[] = "path"; |
| +const char kMediaGalleriesDeviceIdKey[] = "deviceId"; |
| const char kMediaGalleriesDisplayNameKey[] = "displayName"; |
| +const char kMediaGalleriesPathKey[] = "path"; |
| +const char kMediaGalleriesPrefIdKey[] = "prefId"; |
| +const char kMediaGalleriesTypeKey[] = "type"; |
| +const char kMediaGalleriesTypeAutoDetectedValue[] = "autoDetected"; |
| +const char kMediaGalleriesTypeUserAddedValue[] = "userAdded"; |
| +const char kMediaGalleriesTypeBlackListedValue[] = "blackListed"; |
| -bool GetId(const DictionaryValue* dict, uint64* value) { |
| +bool GetPrefId(const DictionaryValue* dict, MediaGalleryPrefId* value) { |
| std::string string_id; |
| - if (!dict->GetString(kMediaGalleriesIdKey, &string_id) || |
| + if (!dict->GetString(kMediaGalleriesPrefIdKey, &string_id) || |
| !base::StringToUint64(string_id, value)) { |
| return false; |
| } |
| @@ -30,36 +43,96 @@ bool GetId(const DictionaryValue* dict, uint64* value) { |
| return true; |
| } |
| -bool PopulateGalleryFromDictionary(const DictionaryValue* dict, |
| - MediaGallery* out_gallery) { |
| - uint64 id; |
| - FilePath::StringType path; |
| +bool GetType(const DictionaryValue* dict, MediaGalleryPrefInfo::Type* type) { |
| + std::string string_type; |
| + if (!dict->GetString(kMediaGalleriesPrefIdKey, &string_type)) |
| + return false; |
| + |
| + if (string_type.compare(kMediaGalleriesTypeAutoDetectedValue) == 0) { |
| + *type = MediaGalleryPrefInfo::kAutoDetected; |
| + return true; |
| + } else if (string_type.compare(kMediaGalleriesTypeUserAddedValue) == 0) { |
| + *type = MediaGalleryPrefInfo::kUserAdded; |
| + return true; |
| + } else if (string_type.compare(kMediaGalleriesTypeBlackListedValue) == 0) { |
| + *type = MediaGalleryPrefInfo::kBlackListed; |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +bool PopulateGalleryPrefInfoFromDictionary( |
| + const DictionaryValue* dict, MediaGalleryPrefInfo* out_gallery_info) { |
| + MediaGalleryPrefId pref_id; |
| string16 display_name; |
| - if (!GetId(dict, &id) || |
| + std::string device_id; |
| + FilePath::StringType path; |
| + MediaGalleryPrefInfo::Type type = MediaGalleryPrefInfo::kAutoDetected; |
| + if (!GetPrefId(dict, &pref_id) || |
| + !dict->GetString(kMediaGalleriesDisplayNameKey, &display_name) || |
| + !dict->GetString(kMediaGalleriesDeviceIdKey, &device_id) || |
| !dict->GetString(kMediaGalleriesPathKey, &path) || |
| - !dict->GetString(kMediaGalleriesDisplayNameKey, &display_name)) { |
| + !GetType(dict, &type)) { |
| return false; |
| } |
| - out_gallery->id = id; |
| - out_gallery->path = FilePath(path); |
| - out_gallery->display_name = display_name; |
| + out_gallery_info->pref_id = pref_id; |
| + out_gallery_info->display_name = display_name; |
| + out_gallery_info->device_id = device_id; |
| + out_gallery_info->path = FilePath(path); |
| + out_gallery_info->type = type; |
| return true; |
| } |
| -DictionaryValue* CreateGalleryDictionary(const MediaGallery& gallery) { |
| +DictionaryValue* CreateGalleryPrefInfoDictionary( |
| + const MediaGalleryPrefInfo& gallery) { |
| DictionaryValue* dict = new DictionaryValue(); |
| - dict->SetString(kMediaGalleriesIdKey, base::Uint64ToString(gallery.id)); |
| - dict->SetString(kMediaGalleriesPathKey, gallery.path.value()); |
| - // TODO(estade): save |path| and |identifier|. |
| + dict->SetString(kMediaGalleriesPrefIdKey, |
| + base::Uint64ToString(gallery.pref_id)); |
| dict->SetString(kMediaGalleriesDisplayNameKey, gallery.display_name); |
| + dict->SetString(kMediaGalleriesDeviceIdKey, gallery.device_id); |
| + dict->SetString(kMediaGalleriesPathKey, gallery.path.value()); |
| + switch (gallery.type) { |
| + case MediaGalleryPrefInfo::kAutoDetected: |
| + dict->SetString(kMediaGalleriesTypeKey, |
| + kMediaGalleriesTypeAutoDetectedValue); |
| + break; |
| + case MediaGalleryPrefInfo::kUserAdded: |
| + dict->SetString(kMediaGalleriesTypeKey, |
| + kMediaGalleriesTypeUserAddedValue); |
| + break; |
| + case MediaGalleryPrefInfo::kBlackListed: |
| + dict->SetString(kMediaGalleriesTypeKey, |
| + kMediaGalleriesTypeBlackListedValue); |
| + break; |
| + } |
| return dict; |
| } |
| +bool FindPrefIdFromDeviceId(const MediaGalleriesPrefInfoMap& known_galleries, |
| + const std::string& device_id, |
| + MediaGalleryPrefId* pref_id) { |
| + for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); |
| + it != known_galleries.end(); |
| + ++it) { |
| + if (it->second.device_id == device_id) { |
| + if (pref_id) |
| + *pref_id = it->second.pref_id; |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +string16 ComputeDisplayName(const FilePath& path) { |
| + return path.DirName().BaseName().LossyDisplayName(); |
| +} |
| + |
| } // namespace |
| -MediaGallery::MediaGallery() : id(0) {} |
| -MediaGallery::~MediaGallery() {} |
| +MediaGalleryPrefInfo::MediaGalleryPrefInfo() : pref_id(0) {} |
| +MediaGalleryPrefInfo::~MediaGalleryPrefInfo() {} |
| MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) |
| : profile_(profile) { |
| @@ -70,7 +143,7 @@ MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) |
| MediaGalleriesPreferences::~MediaGalleriesPreferences() {} |
| void MediaGalleriesPreferences::InitFromPrefs() { |
| - remembered_galleries_.clear(); |
| + known_galleries_.clear(); |
| PrefService* prefs = profile_->GetPrefs(); |
| const ListValue* list = prefs->GetList( |
| @@ -81,47 +154,168 @@ void MediaGalleriesPreferences::InitFromPrefs() { |
| if (!(*it)->GetAsDictionary(&dict)) |
| continue; |
| - MediaGallery gallery; |
| - if (PopulateGalleryFromDictionary(dict, &gallery)) |
| - remembered_galleries_.push_back(gallery); |
| + MediaGalleryPrefInfo gallery_info; |
| + if (PopulateGalleryPrefInfoFromDictionary(dict, &gallery_info)) |
| + known_galleries_[gallery_info.pref_id] = gallery_info; |
| + } |
| +} |
| + |
| +bool MediaGalleriesPreferences::LookUpGalleryByPath( |
| + const FilePath& path, |
| + MediaGalleryPrefInfo* gallery_info) const { |
| + std::string device_id = |
| + MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); |
| + MediaGalleryPrefId pref_id; |
| + if (!FindPrefIdFromDeviceId(known_galleries_, device_id, &pref_id)) { |
| + if (gallery_info) { |
| + gallery_info->pref_id = MediaGalleryPrefInfo::kInvalidPrefId; |
| + gallery_info->display_name = ComputeDisplayName(path); |
| + gallery_info->device_id = device_id; |
| + gallery_info->path = path; |
| + gallery_info->type = MediaGalleryPrefInfo::kUserAdded; |
| + } |
| + return false; |
| + } |
| + |
| + if (gallery_info) { |
| + MediaGalleriesPrefInfoMap::const_iterator it = |
| + known_galleries_.find(pref_id); |
| + DCHECK(it != known_galleries_.end()); |
| + *gallery_info = it->second; |
| } |
| + return true; |
| } |
| -void MediaGalleriesPreferences::AddGalleryByPath(const FilePath& path) { |
| +MediaGalleryPrefId MediaGalleriesPreferences::AddGallery( |
| + const std::string& device_id, const string16& display_name, |
| + const FilePath& path, bool user_added) { |
| + MediaGalleryPrefId existing_id; |
| + if (FindPrefIdFromDeviceId(known_galleries_, device_id, &existing_id)) { |
| + const MediaGalleryPrefInfo& existing = known_galleries_[existing_id]; |
| + if (existing.type == MediaGalleryPrefInfo::kBlackListed) { |
| + PrefService* prefs = profile_->GetPrefs(); |
| + ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); |
| + ListValue* list = update.Get(); |
| + |
| + for (ListValue::iterator it = list->begin(); it != list->end(); ++it) { |
| + DictionaryValue* dict; |
| + MediaGalleryPrefId iter_id; |
| + if ((*it)->GetAsDictionary(&dict) && |
| + GetPrefId(dict, &iter_id) && |
| + existing_id == iter_id) { |
| + dict->SetString(kMediaGalleriesTypeKey, |
| + kMediaGalleriesTypeAutoDetectedValue); |
| + InitFromPrefs(); |
| + break; |
| + } |
| + } |
| + } |
| + return existing_id; |
| + } |
| + |
| PrefService* prefs = profile_->GetPrefs(); |
| - MediaGallery gallery; |
| - gallery.id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId); |
| - prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery.id + 1); |
| - gallery.display_name = path.BaseName().LossyDisplayName(); |
| - // TODO(estade): make this relative to base_path. |
| - gallery.path = path; |
| - // TODO(estade): populate the rest of the fields. |
| + MediaGalleryPrefInfo gallery_info; |
| + gallery_info.pref_id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId); |
| + prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery_info.pref_id + 1); |
| + gallery_info.display_name = display_name; |
|
Evan Stade
2012/08/02 18:36:35
should you Compute if empty?
vandebo (ex-Chrome)
2012/08/02 18:56:00
It's a bug if the caller passes an empty display n
Evan Stade
2012/08/02 21:07:16
if that's a bug you should DCHECK it.
vandebo (ex-Chrome)
2012/08/03 18:43:11
Done.
|
| + gallery_info.path = path; |
| + gallery_info.type = MediaGalleryPrefInfo::kAutoDetected; |
| + if (user_added) |
| + gallery_info.type = MediaGalleryPrefInfo::kUserAdded; |
| ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); |
| ListValue* list = update.Get(); |
| - list->Append(CreateGalleryDictionary(gallery)); |
| + list->Append(CreateGalleryPrefInfoDictionary(gallery_info)); |
| - remembered_galleries_.push_back(gallery); |
| + known_galleries_[gallery_info.pref_id] = gallery_info; |
| + return gallery_info.pref_id; |
| } |
| -void MediaGalleriesPreferences::ForgetGalleryById(uint64 id) { |
| +MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryByPath( |
| + const FilePath& path) { |
| + std::string device_id = |
| + MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); |
| + string16 display_name = ComputeDisplayName(path); |
| + return AddGallery(device_id, display_name, path, true); |
| +} |
| + |
| +void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) { |
| PrefService* prefs = profile_->GetPrefs(); |
| ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); |
| ListValue* list = update.Get(); |
| for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { |
| DictionaryValue* dict; |
| - uint64 iter_id; |
| - if ((*iter)->GetAsDictionary(&dict) && GetId(dict, &iter_id) && |
| - id == iter_id) { |
| - list->Erase(iter, NULL); |
| + MediaGalleryPrefId iter_id; |
| + if ((*iter)->GetAsDictionary(&dict) && GetPrefId(dict, &iter_id) && |
| + pref_id == iter_id) { |
| + MediaGalleryPrefInfo::Type type; |
| + if (GetType(dict, &type) && type == MediaGalleryPrefInfo::kAutoDetected) { |
| + dict->SetString(kMediaGalleriesTypeKey, |
| + kMediaGalleriesTypeBlackListedValue); |
| + } else { |
| + GetExtensionPrefs()->RemoveMediaGalleryPermissions(pref_id); |
| + list->Erase(iter, NULL); |
| + } |
| InitFromPrefs(); |
| return; |
| } |
| } |
| } |
| +std::vector<MediaGalleryPrefId> |
| +MediaGalleriesPreferences::GalleriesForExtension( |
| + const extensions::Extension& extension) const { |
| + std::set<MediaGalleryPrefId> ids; |
| + if (extension.HasAPIPermission( |
| + extensions::APIPermission::kMediaGalleriesAllGalleries)) { |
| + for (MediaGalleriesPrefInfoMap::const_iterator it = |
| + known_galleries_.begin(); it != known_galleries_.end(); ++it) { |
| + if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) |
| + ids.insert(it->second.pref_id); |
| + } |
| + } |
| + |
| + std::vector<MediaGalleryPermission> stored_permissions = |
| + GetExtensionPrefs()->GetMediaGalleryPermissions(extension.id()); |
| + |
| + for (std::vector<MediaGalleryPermission>::const_iterator it = |
| + stored_permissions.begin(); it != stored_permissions.end(); ++it) { |
| + if (it->has_permission) { |
| + MediaGalleriesPrefInfoMap::const_iterator gallery = |
| + known_galleries_.find(it->pref_id); |
| + DCHECK(gallery != known_galleries_.end()); |
| + if (gallery->second.type == MediaGalleryPrefInfo::kBlackListed) { |
| + ids.erase(it->pref_id); |
| + } else { |
| + ids.insert(it->pref_id); |
| + } |
| + } |
| + } |
| + |
| + std::vector<MediaGalleryPrefId> result; |
| + result.assign(ids.begin(), ids.end()); |
| + return result; |
| +} |
| + |
| +void MediaGalleriesPreferences::SetGalleryPermissionForExtension( |
| + const extensions::Extension& extension, |
| + MediaGalleryPrefId pref_id, |
| + bool has_permission) { |
| + if (has_permission && |
| + extension.HasAPIPermission( |
| + extensions::APIPermission::kMediaGalleriesAllGalleries)) { |
| + MediaGalleriesPrefInfoMap::iterator gallery_info = |
| + known_galleries_.find(pref_id); |
| + DCHECK(gallery_info != known_galleries_.end()); |
| + if (gallery_info->second.type == MediaGalleryPrefInfo::kAutoDetected) |
| + return; |
| + } |
| + GetExtensionPrefs()->SetMediaGalleryPermission(extension.id(), pref_id, |
| + has_permission); |
| +} |
| + |
| void MediaGalleriesPreferences::Shutdown() { |
| profile_ = NULL; |
| } |
| @@ -139,6 +333,16 @@ void MediaGalleriesPreferences::RegisterUserPrefs(PrefService* prefs) { |
| prefs->RegisterListPref(prefs::kMediaGalleriesRememberedGalleries, |
| PrefService::UNSYNCABLE_PREF); |
| - prefs->RegisterUint64Pref(prefs::kMediaGalleriesUniqueId, 0, |
| + prefs->RegisterUint64Pref(prefs::kMediaGalleriesUniqueId, |
| + MediaGalleryPrefInfo::kInvalidPrefId + 1, |
| PrefService::UNSYNCABLE_PREF); |
| } |
| + |
| +extensions::ExtensionPrefs* |
| +MediaGalleriesPreferences::GetExtensionPrefs() const { |
| + ExtensionService* extension_service = |
| + extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| + return extension_service->extension_prefs(); |
| +} |
| + |
| +} // namespace chrome |