Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: chrome/browser/media_gallery/media_galleries_preferences.cc

Issue 10821077: Add gallery permissions to Media Galleries Preferences (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Compiles Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_gallery/media_galleries_preferences.h" 5 #include "chrome/browser/media_gallery/media_galleries_preferences.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/string_number_conversions.h" 8 #include "base/string_number_conversions.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/extensions/extension_prefs.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_system.h"
13 #include "chrome/browser/media_gallery/media_file_system_registry.h"
10 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/pref_service.h"
11 #include "chrome/browser/prefs/scoped_user_pref_update.h" 15 #include "chrome/browser/prefs/scoped_user_pref_update.h"
12 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/extensions/extension.h"
19 #include "chrome/common/extensions/permissions/api_permission.h"
14 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
15 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
16 22
23 namespace chrome {
24
17 namespace { 25 namespace {
18 26
19 const char kMediaGalleriesIdKey[] = "id"; 27 const char kMediaGalleriesDeviceIdKey[] = "deviceId";
28 const char kMediaGalleriesDisplayNameKey[] = "displayName";
20 const char kMediaGalleriesPathKey[] = "path"; 29 const char kMediaGalleriesPathKey[] = "path";
21 const char kMediaGalleriesDisplayNameKey[] = "displayName"; 30 const char kMediaGalleriesPrefIdKey[] = "prefId";
31 const char kMediaGalleriesTypeKey[] = "type";
32 const char kMediaGalleriesTypeAutoDetectedValue[] = "autoDetected";
33 const char kMediaGalleriesTypeUserAddedValue[] = "userAdded";
34 const char kMediaGalleriesTypeBlackListedValue[] = "blackListed";
22 35
23 bool GetId(const DictionaryValue* dict, uint64* value) { 36 bool GetPrefId(const DictionaryValue* dict, MediaGalleryPrefId* value) {
24 std::string string_id; 37 std::string string_id;
25 if (!dict->GetString(kMediaGalleriesIdKey, &string_id) || 38 if (!dict->GetString(kMediaGalleriesPrefIdKey, &string_id) ||
26 !base::StringToUint64(string_id, value)) { 39 !base::StringToUint64(string_id, value)) {
27 return false; 40 return false;
28 } 41 }
29 42
30 return true; 43 return true;
31 } 44 }
32 45
33 bool PopulateGalleryFromDictionary(const DictionaryValue* dict, 46 bool GetType(const DictionaryValue* dict, MediaGalleryPrefInfo::Type* type) {
34 MediaGallery* out_gallery) { 47 std::string string_type;
35 uint64 id; 48 if (!dict->GetString(kMediaGalleriesPrefIdKey, &string_type))
49 return false;
50
51 if (string_type.compare(kMediaGalleriesTypeAutoDetectedValue) == 0) {
52 *type = MediaGalleryPrefInfo::kAutoDetected;
53 return true;
54 } else if (string_type.compare(kMediaGalleriesTypeUserAddedValue) == 0) {
55 *type = MediaGalleryPrefInfo::kUserAdded;
56 return true;
57 } else if (string_type.compare(kMediaGalleriesTypeBlackListedValue) == 0) {
58 *type = MediaGalleryPrefInfo::kBlackListed;
59 return true;
60 }
61
62 return false;
63 }
64
65 bool PopulateGalleryPrefInfoFromDictionary(
66 const DictionaryValue* dict, MediaGalleryPrefInfo* out_gallery_info) {
67 MediaGalleryPrefId pref_id;
68 string16 display_name;
69 std::string device_id;
36 FilePath::StringType path; 70 FilePath::StringType path;
37 string16 display_name; 71 MediaGalleryPrefInfo::Type type = MediaGalleryPrefInfo::kAutoDetected;
38 if (!GetId(dict, &id) || 72 if (!GetPrefId(dict, &pref_id) ||
73 !dict->GetString(kMediaGalleriesDisplayNameKey, &display_name) ||
74 !dict->GetString(kMediaGalleriesDeviceIdKey, &device_id) ||
39 !dict->GetString(kMediaGalleriesPathKey, &path) || 75 !dict->GetString(kMediaGalleriesPathKey, &path) ||
40 !dict->GetString(kMediaGalleriesDisplayNameKey, &display_name)) { 76 !GetType(dict, &type)) {
41 return false; 77 return false;
42 } 78 }
43 79
44 out_gallery->id = id; 80 out_gallery_info->pref_id = pref_id;
45 out_gallery->path = FilePath(path); 81 out_gallery_info->display_name = display_name;
46 out_gallery->display_name = display_name; 82 out_gallery_info->device_id = device_id;
83 out_gallery_info->path = FilePath(path);
84 out_gallery_info->type = type;
47 return true; 85 return true;
48 } 86 }
49 87
50 DictionaryValue* CreateGalleryDictionary(const MediaGallery& gallery) { 88 DictionaryValue* CreateGalleryPrefInfoDictionary(
89 const MediaGalleryPrefInfo& gallery) {
51 DictionaryValue* dict = new DictionaryValue(); 90 DictionaryValue* dict = new DictionaryValue();
52 dict->SetString(kMediaGalleriesIdKey, base::Uint64ToString(gallery.id)); 91 dict->SetString(kMediaGalleriesPrefIdKey,
92 base::Uint64ToString(gallery.pref_id));
93 dict->SetString(kMediaGalleriesDisplayNameKey, gallery.display_name);
94 dict->SetString(kMediaGalleriesDeviceIdKey, gallery.device_id);
53 dict->SetString(kMediaGalleriesPathKey, gallery.path.value()); 95 dict->SetString(kMediaGalleriesPathKey, gallery.path.value());
54 // TODO(estade): save |path| and |identifier|. 96 switch (gallery.type) {
55 dict->SetString(kMediaGalleriesDisplayNameKey, gallery.display_name); 97 case MediaGalleryPrefInfo::kAutoDetected:
98 dict->SetString(kMediaGalleriesTypeKey,
99 kMediaGalleriesTypeAutoDetectedValue);
100 break;
101 case MediaGalleryPrefInfo::kUserAdded:
102 dict->SetString(kMediaGalleriesTypeKey,
103 kMediaGalleriesTypeUserAddedValue);
104 break;
105 case MediaGalleryPrefInfo::kBlackListed:
106 dict->SetString(kMediaGalleriesTypeKey,
107 kMediaGalleriesTypeBlackListedValue);
108 break;
109 }
56 return dict; 110 return dict;
57 } 111 }
58 112
113 bool FindPrefIdFromDeviceId(const MediaGalleriesPrefInfoMap& known_galleries,
114 const std::string& device_id,
115 MediaGalleryPrefId* pref_id) {
116 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
117 it != known_galleries.end();
118 ++it) {
119 if (it->second.device_id == device_id) {
120 if (pref_id)
121 *pref_id = it->second.pref_id;
122 return true;
123 }
124 }
125 return false;
126 }
127
59 } // namespace 128 } // namespace
60 129
61 MediaGallery::MediaGallery() : id(0) {} 130 MediaGalleryPrefInfo::MediaGalleryPrefInfo() : pref_id(0) {}
62 MediaGallery::~MediaGallery() {} 131 MediaGalleryPrefInfo::~MediaGalleryPrefInfo() {}
63 132
64 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) 133 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile)
65 : profile_(profile) { 134 : profile_(profile) {
66 DCHECK(UserInteractionIsEnabled()); 135 DCHECK(UserInteractionIsEnabled());
67 InitFromPrefs(); 136 InitFromPrefs();
68 } 137 }
69 138
70 MediaGalleriesPreferences::~MediaGalleriesPreferences() {} 139 MediaGalleriesPreferences::~MediaGalleriesPreferences() {}
71 140
72 void MediaGalleriesPreferences::InitFromPrefs() { 141 void MediaGalleriesPreferences::InitFromPrefs() {
73 remembered_galleries_.clear(); 142 known_galleries_.clear();
74 143
75 PrefService* prefs = profile_->GetPrefs(); 144 PrefService* prefs = profile_->GetPrefs();
76 const ListValue* list = prefs->GetList( 145 const ListValue* list = prefs->GetList(
77 prefs::kMediaGalleriesRememberedGalleries); 146 prefs::kMediaGalleriesRememberedGalleries);
78 147
79 for (ListValue::const_iterator it = list->begin(); it != list->end(); it++) { 148 for (ListValue::const_iterator it = list->begin(); it != list->end(); it++) {
80 const DictionaryValue* dict = NULL; 149 const DictionaryValue* dict = NULL;
81 if (!(*it)->GetAsDictionary(&dict)) 150 if (!(*it)->GetAsDictionary(&dict))
82 continue; 151 continue;
83 152
84 MediaGallery gallery; 153 MediaGalleryPrefInfo gallery_info;
85 if (PopulateGalleryFromDictionary(dict, &gallery)) 154 if (PopulateGalleryPrefInfoFromDictionary(dict, &gallery_info))
86 remembered_galleries_.push_back(gallery); 155 known_galleries_[gallery_info.pref_id] = gallery_info;
87 } 156 }
88 } 157 }
89 158
90 void MediaGalleriesPreferences::AddGalleryByPath(const FilePath& path) { 159 bool MediaGalleriesPreferences::LookupGalleryByPath(
160 const FilePath& path,
161 MediaGalleryPrefInfo* gallery_info) const {
162 std::string device_id =
163 MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
164 MediaGalleryPrefId pref_id;
165 if (!FindPrefIdFromDeviceId(known_galleries_, device_id, &pref_id))
166 return false;
167
168 if (gallery_info) {
169 MediaGalleriesPrefInfoMap::const_iterator it =
170 known_galleries_.find(pref_id);
171 DCHECK(it != known_galleries_.end());
172 *gallery_info = it->second;
173 }
174 return true;
175 }
176
177 MediaGalleryPrefId MediaGalleriesPreferences::AddMediaGallery(
178 const std::string& device_id, const string16& display_name,
179 const FilePath& path, bool user_added) {
180 MediaGalleryPrefId existing_id;
181 if (FindPrefIdFromDeviceId(known_galleries_, device_id, &existing_id)) {
182 const MediaGalleryPrefInfo& existing = known_galleries_[existing_id];
183 if (existing.type == MediaGalleryPrefInfo::kBlackListed) {
184 PrefService* prefs = profile_->GetPrefs();
185 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries);
186 ListValue* list = update.Get();
187
188 for (ListValue::iterator it = list->begin(); it != list->end(); ++it) {
189 DictionaryValue* dict;
190 MediaGalleryPrefId iter_id;
191 if ((*it)->GetAsDictionary(&dict) &&
192 GetPrefId(dict, &iter_id) &&
193 existing_id == iter_id) {
194 dict->SetString(kMediaGalleriesTypeKey,
195 kMediaGalleriesTypeAutoDetectedValue);
196 InitFromPrefs();
197 break;
198 }
199 }
200 }
201 return existing_id;
202 }
203
91 PrefService* prefs = profile_->GetPrefs(); 204 PrefService* prefs = profile_->GetPrefs();
92 205
93 MediaGallery gallery; 206 MediaGalleryPrefInfo gallery_info;
94 gallery.id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId); 207 gallery_info.pref_id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId);
95 prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery.id + 1); 208 prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery_info.pref_id + 1);
96 gallery.display_name = path.BaseName().LossyDisplayName(); 209 gallery_info.display_name = display_name;
97 // TODO(estade): make this relative to base_path. 210 gallery_info.path = path;
98 gallery.path = path; 211 gallery_info.type = MediaGalleryPrefInfo::kAutoDetected;
99 // TODO(estade): populate the rest of the fields. 212 if (user_added)
213 gallery_info.type = MediaGalleryPrefInfo::kUserAdded;
100 214
101 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); 215 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries);
102 ListValue* list = update.Get(); 216 ListValue* list = update.Get();
103 list->Append(CreateGalleryDictionary(gallery)); 217 list->Append(CreateGalleryPrefInfoDictionary(gallery_info));
104 218
105 remembered_galleries_.push_back(gallery); 219 known_galleries_[gallery_info.pref_id] = gallery_info;
220 return gallery_info.pref_id;
106 } 221 }
107 222
108 void MediaGalleriesPreferences::ForgetGalleryById(uint64 id) { 223 MediaGalleryPrefId MediaGalleriesPreferences::AddMediaGalleryByPath(
224 const FilePath& path) {
225 std::string device_id =
226 MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
227 string16 display_name = path.DirName().BaseName().LossyDisplayName();
228 return AddMediaGallery(device_id, display_name, path, true);
229 }
230
231 void MediaGalleriesPreferences::ForgetMediaGalleryById(
232 MediaGalleryPrefId pref_id) {
109 PrefService* prefs = profile_->GetPrefs(); 233 PrefService* prefs = profile_->GetPrefs();
110 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); 234 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries);
111 ListValue* list = update.Get(); 235 ListValue* list = update.Get();
112 236
113 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { 237 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) {
114 DictionaryValue* dict; 238 DictionaryValue* dict;
115 uint64 iter_id; 239 MediaGalleryPrefId iter_id;
116 if ((*iter)->GetAsDictionary(&dict) && GetId(dict, &iter_id) && 240 if ((*iter)->GetAsDictionary(&dict) && GetPrefId(dict, &iter_id) &&
117 id == iter_id) { 241 pref_id == iter_id) {
118 list->Erase(iter, NULL); 242 MediaGalleryPrefInfo::Type type;
243 if (GetType(dict, &type) && type == MediaGalleryPrefInfo::kAutoDetected) {
244 dict->SetString(kMediaGalleriesTypeKey,
245 kMediaGalleriesTypeBlackListedValue);
246 } else {
247 GetExtensionPrefs()->RemoveMediaGalleryPermissions(pref_id);
248 list->Erase(iter, NULL);
249 }
119 InitFromPrefs(); 250 InitFromPrefs();
120 return; 251 return;
121 } 252 }
122 } 253 }
123 } 254 }
124 255
256 std::vector<MediaGalleryPrefId>
257 MediaGalleriesPreferences::MediaGalleriesForExtension(
258 const extensions::Extension& extension) const {
259 std::set<MediaGalleryPrefId> ids;
260 if (extension.HasAPIPermission(
261 extensions::APIPermission::kMediaGalleriesAllGalleries)) {
262 for (MediaGalleriesPrefInfoMap::const_iterator it =
263 known_galleries_.begin(); it != known_galleries_.end(); ++it) {
264 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
265 ids.insert(it->second.pref_id);
266 }
267 }
268
269 std::vector<MediaGalleryPermission> stored_permissions =
270 GetExtensionPrefs()->GetMediaGalleryPermissions(extension.id());
271
272 for (std::vector<MediaGalleryPermission>::const_iterator it =
273 stored_permissions.begin(); it != stored_permissions.end(); ++it) {
274 if (it->has_permission) {
275 MediaGalleriesPrefInfoMap::const_iterator gallery =
276 known_galleries_.find(it->pref_id);
277 DCHECK(gallery != known_galleries_.end());
278 if (gallery->second.type == MediaGalleryPrefInfo::kBlackListed) {
279 ids.erase(it->pref_id);
280 } else {
281 ids.insert(it->pref_id);
282 }
283 }
284 }
285
286 std::vector<MediaGalleryPrefId> result;
287 result.assign(ids.begin(), ids.end());
288 return result;
289 }
290
291 void MediaGalleriesPreferences::SetMediaGalleryPermissionForExtension(
292 const extensions::Extension& extension,
293 MediaGalleryPrefId pref_id,
294 bool has_permission) {
295 if (has_permission &&
296 extension.HasAPIPermission(
297 extensions::APIPermission::kMediaGalleriesAllGalleries)) {
298 MediaGalleriesPrefInfoMap::iterator gallery_info =
299 known_galleries_.find(pref_id);
300 DCHECK(gallery_info != known_galleries_.end());
301 if (gallery_info->second.type == MediaGalleryPrefInfo::kAutoDetected)
Evan Stade 2012/08/02 18:00:54 seems like in this case rather than returning you
vandebo (ex-Chrome) 2012/08/02 18:26:02 That all sounds right, will do that
vandebo (ex-Chrome) 2012/08/02 18:56:00 Done.
302 return;
303 }
304 GetExtensionPrefs()->SetMediaGalleryPermission(extension.id(), pref_id,
305 has_permission);
306 }
307
125 void MediaGalleriesPreferences::Shutdown() { 308 void MediaGalleriesPreferences::Shutdown() {
126 profile_ = NULL; 309 profile_ = NULL;
127 } 310 }
128 311
129 // static 312 // static
130 bool MediaGalleriesPreferences::UserInteractionIsEnabled() { 313 bool MediaGalleriesPreferences::UserInteractionIsEnabled() {
131 return CommandLine::ForCurrentProcess()->HasSwitch( 314 return CommandLine::ForCurrentProcess()->HasSwitch(
132 switches::kEnableMediaGalleryUI); 315 switches::kEnableMediaGalleryUI);
133 } 316 }
134 317
135 // static 318 // static
136 void MediaGalleriesPreferences::RegisterUserPrefs(PrefService* prefs) { 319 void MediaGalleriesPreferences::RegisterUserPrefs(PrefService* prefs) {
137 if (!UserInteractionIsEnabled()) 320 if (!UserInteractionIsEnabled())
138 return; 321 return;
139 322
140 prefs->RegisterListPref(prefs::kMediaGalleriesRememberedGalleries, 323 prefs->RegisterListPref(prefs::kMediaGalleriesRememberedGalleries,
141 PrefService::UNSYNCABLE_PREF); 324 PrefService::UNSYNCABLE_PREF);
142 prefs->RegisterUint64Pref(prefs::kMediaGalleriesUniqueId, 0, 325 prefs->RegisterUint64Pref(prefs::kMediaGalleriesUniqueId, 0,
143 PrefService::UNSYNCABLE_PREF); 326 PrefService::UNSYNCABLE_PREF);
144 } 327 }
328
329 extensions::ExtensionPrefs*
330 MediaGalleriesPreferences::GetExtensionPrefs() const {
331 ExtensionService* extension_service =
332 extensions::ExtensionSystem::Get(profile_)->extension_service();
333 return extension_service->extension_prefs();
334 }
335
336 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698