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

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: Self review 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/path_service.h"
8 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
9 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/extensions/extension_prefs.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/media_gallery/media_file_system_registry.h"
10 #include "chrome/browser/prefs/pref_service.h" 15 #include "chrome/browser/prefs/pref_service.h"
11 #include "chrome/browser/prefs/scoped_user_pref_update.h" 16 #include "chrome/browser/prefs/scoped_user_pref_update.h"
12 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/extensions/extension.h"
21 #include "chrome/common/extensions/permissions/api_permission.h"
14 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
15 #include "grit/generated_resources.h" 23 #include "grit/generated_resources.h"
16 24
25 namespace chrome {
26
17 namespace { 27 namespace {
18 28
19 const char kMediaGalleriesIdKey[] = "id"; 29 const char kMediaGalleriesDeviceIdKey[] = "deviceId";
30 const char kMediaGalleriesDisplayNameKey[] = "displayName";
20 const char kMediaGalleriesPathKey[] = "path"; 31 const char kMediaGalleriesPathKey[] = "path";
21 const char kMediaGalleriesDisplayNameKey[] = "displayName"; 32 const char kMediaGalleriesPrefIdKey[] = "prefId";
33 const char kMediaGalleriesTypeKey[] = "type";
34 const char kMediaGalleriesTypeAutoDetectedValue[] = "autoDetected";
35 const char kMediaGalleriesTypeUserAddedValue[] = "userAdded";
36 const char kMediaGalleriesTypeBlackListedValue[] = "blackListed";
22 37
23 bool GetId(const DictionaryValue* dict, uint64* value) { 38 bool GetPrefId(const DictionaryValue* dict, MediaGalleryPrefId* value) {
24 std::string string_id; 39 std::string string_id;
25 if (!dict->GetString(kMediaGalleriesIdKey, &string_id) || 40 if (!dict->GetString(kMediaGalleriesPrefIdKey, &string_id) ||
26 !base::StringToUint64(string_id, value)) { 41 !base::StringToUint64(string_id, value)) {
27 return false; 42 return false;
28 } 43 }
29 44
30 return true; 45 return true;
31 } 46 }
32 47
33 bool PopulateGalleryFromDictionary(const DictionaryValue* dict, 48 bool GetType(const DictionaryValue* dict, MediaGalleryPrefInfo::Type* type) {
34 MediaGallery* out_gallery) { 49 std::string string_type;
35 uint64 id; 50 if (!dict->GetString(kMediaGalleriesTypeKey, &string_type))
51 return false;
52
53 if (string_type.compare(kMediaGalleriesTypeAutoDetectedValue) == 0) {
Evan Stade 2012/08/03 18:53:43 string_type == kMediaGalleriesTypeAutoDetectedValu
vandebo (ex-Chrome) 2012/08/03 19:03:07 Done.
54 *type = MediaGalleryPrefInfo::kAutoDetected;
55 return true;
56 } else if (string_type.compare(kMediaGalleriesTypeUserAddedValue) == 0) {
Evan Stade 2012/08/03 18:53:43 no need for else after a return.
vandebo (ex-Chrome) 2012/08/03 19:03:07 Done.
57 *type = MediaGalleryPrefInfo::kUserAdded;
58 return true;
59 } else if (string_type.compare(kMediaGalleriesTypeBlackListedValue) == 0) {
60 *type = MediaGalleryPrefInfo::kBlackListed;
61 return true;
62 }
63
64 return false;
65 }
66
67 bool PopulateGalleryPrefInfoFromDictionary(
68 const DictionaryValue* dict, MediaGalleryPrefInfo* out_gallery_info) {
69 MediaGalleryPrefId pref_id;
70 string16 display_name;
71 std::string device_id;
36 FilePath::StringType path; 72 FilePath::StringType path;
37 string16 display_name; 73 MediaGalleryPrefInfo::Type type = MediaGalleryPrefInfo::kAutoDetected;
38 if (!GetId(dict, &id) || 74 if (!GetPrefId(dict, &pref_id) ||
75 !dict->GetString(kMediaGalleriesDisplayNameKey, &display_name) ||
76 !dict->GetString(kMediaGalleriesDeviceIdKey, &device_id) ||
39 !dict->GetString(kMediaGalleriesPathKey, &path) || 77 !dict->GetString(kMediaGalleriesPathKey, &path) ||
40 !dict->GetString(kMediaGalleriesDisplayNameKey, &display_name)) { 78 !GetType(dict, &type)) {
41 return false; 79 return false;
42 } 80 }
43 81
44 out_gallery->id = id; 82 out_gallery_info->pref_id = pref_id;
45 out_gallery->path = FilePath(path); 83 out_gallery_info->display_name = display_name;
46 out_gallery->display_name = display_name; 84 out_gallery_info->device_id = device_id;
85 out_gallery_info->path = FilePath(path);
86 out_gallery_info->type = type;
47 return true; 87 return true;
48 } 88 }
49 89
50 DictionaryValue* CreateGalleryDictionary(const MediaGallery& gallery) { 90 DictionaryValue* CreateGalleryPrefInfoDictionary(
91 const MediaGalleryPrefInfo& gallery) {
51 DictionaryValue* dict = new DictionaryValue(); 92 DictionaryValue* dict = new DictionaryValue();
52 dict->SetString(kMediaGalleriesIdKey, base::Uint64ToString(gallery.id)); 93 dict->SetString(kMediaGalleriesPrefIdKey,
94 base::Uint64ToString(gallery.pref_id));
95 dict->SetString(kMediaGalleriesDisplayNameKey, gallery.display_name);
96 dict->SetString(kMediaGalleriesDeviceIdKey, gallery.device_id);
53 dict->SetString(kMediaGalleriesPathKey, gallery.path.value()); 97 dict->SetString(kMediaGalleriesPathKey, gallery.path.value());
54 // TODO(estade): save |path| and |identifier|. 98 switch (gallery.type) {
55 dict->SetString(kMediaGalleriesDisplayNameKey, gallery.display_name); 99 case MediaGalleryPrefInfo::kAutoDetected:
100 dict->SetString(kMediaGalleriesTypeKey,
101 kMediaGalleriesTypeAutoDetectedValue);
102 break;
103 case MediaGalleryPrefInfo::kUserAdded:
104 dict->SetString(kMediaGalleriesTypeKey,
105 kMediaGalleriesTypeUserAddedValue);
106 break;
107 case MediaGalleryPrefInfo::kBlackListed:
108 dict->SetString(kMediaGalleriesTypeKey,
109 kMediaGalleriesTypeBlackListedValue);
110 break;
111 }
56 return dict; 112 return dict;
57 } 113 }
58 114
115 bool FindPrefIdFromDeviceId(const MediaGalleriesPrefInfoMap& known_galleries,
116 const std::string& device_id,
117 MediaGalleryPrefId* pref_id) {
118 // TODO(vandebo) Handle multiple galleries that use different paths.
119 // TODO(vandebo) Should we keep a second map device_id->pref_id?
120 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
121 it != known_galleries.end();
122 ++it) {
123 if (it->second.device_id == device_id) {
124 if (pref_id)
125 *pref_id = it->second.pref_id;
126 return true;
127 }
128 }
129 return false;
130 }
131
132 FilePath MakePathRelative(FilePath path) {
133 if (!path.IsAbsolute())
134 return path;
135
136 FilePath relative;
137 std::vector<FilePath::StringType> components;
138 path.GetComponents(&components);
139 for (size_t i = 1; i < components.size(); i++)
140 relative = relative.Append(components[i]);
141 return relative;
142 }
143
59 } // namespace 144 } // namespace
60 145
61 MediaGallery::MediaGallery() : id(0) {} 146 MediaGalleryPrefInfo::MediaGalleryPrefInfo()
62 MediaGallery::~MediaGallery() {} 147 : pref_id(kInvalidMediaGalleryPrefId) {
148 }
149 MediaGalleryPrefInfo::~MediaGalleryPrefInfo() {}
63 150
64 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) 151 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile)
65 : profile_(profile) { 152 : profile_(profile) {
66 DCHECK(UserInteractionIsEnabled()); 153 DCHECK(UserInteractionIsEnabled());
154
155 // Populate the default galleries if this is a fresh profile.
156 MediaGalleryPrefId current_id =
157 profile_->GetPrefs()->GetUint64(prefs::kMediaGalleriesUniqueId);
158 if (current_id == kInvalidMediaGalleryPrefId + 1) {
159 FilePath pictures_path;
160 if (PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path)) {
161 std::string device_id = MediaFileSystemRegistry::GetInstance()->
162 GetDeviceIdFromPath(pictures_path);
163 string16 display_name = ComputeDisplayName(pictures_path);
164 AddGallery(device_id, display_name, pictures_path, false /*user added*/);
165 }
166 }
67 InitFromPrefs(); 167 InitFromPrefs();
68 } 168 }
69 169
70 MediaGalleriesPreferences::~MediaGalleriesPreferences() {} 170 MediaGalleriesPreferences::~MediaGalleriesPreferences() {}
71 171
72 void MediaGalleriesPreferences::InitFromPrefs() { 172 void MediaGalleriesPreferences::InitFromPrefs() {
73 remembered_galleries_.clear(); 173 known_galleries_.clear();
74 174
75 PrefService* prefs = profile_->GetPrefs(); 175 PrefService* prefs = profile_->GetPrefs();
76 const ListValue* list = prefs->GetList( 176 const ListValue* list = prefs->GetList(
77 prefs::kMediaGalleriesRememberedGalleries); 177 prefs::kMediaGalleriesRememberedGalleries);
178 if (!list)
179 return;
78 180
79 for (ListValue::const_iterator it = list->begin(); it != list->end(); it++) { 181 for (ListValue::const_iterator it = list->begin(); it != list->end(); it++) {
80 const DictionaryValue* dict = NULL; 182 const DictionaryValue* dict = NULL;
81 if (!(*it)->GetAsDictionary(&dict)) 183 if (!(*it)->GetAsDictionary(&dict))
82 continue; 184 continue;
83 185
84 MediaGallery gallery; 186 MediaGalleryPrefInfo gallery_info;
85 if (PopulateGalleryFromDictionary(dict, &gallery)) 187 if (PopulateGalleryPrefInfoFromDictionary(dict, &gallery_info))
86 remembered_galleries_.push_back(gallery); 188 known_galleries_[gallery_info.pref_id] = gallery_info;
87 } 189 }
88 } 190 }
89 191
90 void MediaGalleriesPreferences::AddGalleryByPath(const FilePath& path) { 192 bool MediaGalleriesPreferences::LookUpGalleryByPath(
193 const FilePath& path,
194 MediaGalleryPrefInfo* gallery_info) const {
195 std::string device_id =
196 MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
197 MediaGalleryPrefId pref_id;
198 if (!FindPrefIdFromDeviceId(known_galleries_, device_id, &pref_id)) {
199 if (gallery_info) {
200 gallery_info->pref_id = kInvalidMediaGalleryPrefId;
201 gallery_info->display_name = ComputeDisplayName(path);
202 gallery_info->device_id = device_id;
203 gallery_info->path = MakePathRelative(path);
204 gallery_info->type = MediaGalleryPrefInfo::kUserAdded;
205 }
206 return false;
207 }
208
209 if (gallery_info) {
210 MediaGalleriesPrefInfoMap::const_iterator it =
211 known_galleries_.find(pref_id);
212 DCHECK(it != known_galleries_.end());
213 *gallery_info = it->second;
214 }
215 return true;
216 }
217
218 MediaGalleryPrefId MediaGalleriesPreferences::AddGallery(
219 const std::string& device_id, const string16& display_name,
220 const FilePath& path, bool user_added) {
221 DCHECK(display_name.length() > 0);
222 MediaGalleryPrefId existing_id;
223 if (FindPrefIdFromDeviceId(known_galleries_, device_id, &existing_id)) {
224 const MediaGalleryPrefInfo& existing = known_galleries_[existing_id];
225 if (existing.type == MediaGalleryPrefInfo::kBlackListed) {
226 PrefService* prefs = profile_->GetPrefs();
227 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries);
228 ListValue* list = update.Get();
229
230 for (ListValue::iterator it = list->begin(); it != list->end(); ++it) {
231 DictionaryValue* dict;
232 MediaGalleryPrefId iter_id;
233 if ((*it)->GetAsDictionary(&dict) &&
234 GetPrefId(dict, &iter_id) &&
235 existing_id == iter_id) {
236 dict->SetString(kMediaGalleriesTypeKey,
237 kMediaGalleriesTypeAutoDetectedValue);
238 InitFromPrefs();
239 break;
240 }
241 }
242 }
243 return existing_id;
244 }
245
246 FilePath relative_path = MakePathRelative(path);
91 PrefService* prefs = profile_->GetPrefs(); 247 PrefService* prefs = profile_->GetPrefs();
92 248
93 MediaGallery gallery; 249 MediaGalleryPrefInfo gallery_info;
94 gallery.id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId); 250 gallery_info.pref_id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId);
95 prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery.id + 1); 251 prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery_info.pref_id + 1);
96 gallery.display_name = path.BaseName().LossyDisplayName(); 252 gallery_info.display_name = display_name;
97 // TODO(estade): make this relative to base_path. 253 gallery_info.device_id = device_id;
98 gallery.path = path; 254 gallery_info.path = relative_path;
99 // TODO(estade): populate the rest of the fields. 255 gallery_info.type = MediaGalleryPrefInfo::kAutoDetected;
256 if (user_added)
257 gallery_info.type = MediaGalleryPrefInfo::kUserAdded;
100 258
101 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); 259 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries);
102 ListValue* list = update.Get(); 260 ListValue* list = update.Get();
103 list->Append(CreateGalleryDictionary(gallery)); 261 list->Append(CreateGalleryPrefInfoDictionary(gallery_info));
104 262 InitFromPrefs();
105 remembered_galleries_.push_back(gallery); 263
106 } 264 return gallery_info.pref_id;
107 265 }
108 void MediaGalleriesPreferences::ForgetGalleryById(uint64 id) { 266
267 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryByPath(
268 const FilePath& path) {
269 std::string device_id =
270 MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
271 string16 display_name = ComputeDisplayName(path);
272 return AddGallery(device_id, display_name, path, true);
273 }
274
275 void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) {
109 PrefService* prefs = profile_->GetPrefs(); 276 PrefService* prefs = profile_->GetPrefs();
110 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); 277 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries);
111 ListValue* list = update.Get(); 278 ListValue* list = update.Get();
112 279
113 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { 280 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) {
114 DictionaryValue* dict; 281 DictionaryValue* dict;
115 uint64 iter_id; 282 MediaGalleryPrefId iter_id;
116 if ((*iter)->GetAsDictionary(&dict) && GetId(dict, &iter_id) && 283 if ((*iter)->GetAsDictionary(&dict) && GetPrefId(dict, &iter_id) &&
117 id == iter_id) { 284 pref_id == iter_id) {
118 list->Erase(iter, NULL); 285 MediaGalleryPrefInfo::Type type;
286 if (GetType(dict, &type) && type == MediaGalleryPrefInfo::kAutoDetected) {
287 dict->SetString(kMediaGalleriesTypeKey,
288 kMediaGalleriesTypeBlackListedValue);
289 } else {
290 GetExtensionPrefs()->RemoveMediaGalleryPermissions(pref_id);
291 list->Erase(iter, NULL);
292 }
119 InitFromPrefs(); 293 InitFromPrefs();
120 return; 294 return;
121 } 295 }
122 } 296 }
123 } 297 }
124 298
299 std::set<MediaGalleryPrefId>
300 MediaGalleriesPreferences::GalleriesForExtension(
301 const extensions::Extension& extension) const {
302 std::set<MediaGalleryPrefId> result;
303 if (extension.HasAPIPermission(
304 extensions::APIPermission::kMediaGalleriesAllGalleries)) {
305 for (MediaGalleriesPrefInfoMap::const_iterator it =
306 known_galleries_.begin(); it != known_galleries_.end(); ++it) {
307 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
308 result.insert(it->second.pref_id);
309 }
310 }
311
312 std::vector<MediaGalleryPermission> stored_permissions =
313 GetExtensionPrefs()->GetMediaGalleryPermissions(extension.id());
314
315 for (std::vector<MediaGalleryPermission>::const_iterator it =
316 stored_permissions.begin(); it != stored_permissions.end(); ++it) {
317 if (!it->has_permission) {
318 result.erase(it->pref_id);
319 } else {
320 MediaGalleriesPrefInfoMap::const_iterator gallery =
321 known_galleries_.find(it->pref_id);
322 DCHECK(gallery != known_galleries_.end());
323 if (gallery->second.type != MediaGalleryPrefInfo::kBlackListed) {
Evan Stade 2012/08/03 18:53:43 no curlies
vandebo (ex-Chrome) 2012/08/03 19:03:07 I prefer curlies if there is an else. I omit them
324 result.insert(it->pref_id);
325 } else {
326 NOT_REACHED();
Evan Stade 2012/08/03 18:53:43 no underscore (right?)
vandebo (ex-Chrome) 2012/08/03 19:03:07 Done.
327 }
328 }
329 }
330 return result;
331 }
332
333 void MediaGalleriesPreferences::SetGalleryPermissionForExtension(
334 const extensions::Extension& extension,
335 MediaGalleryPrefId pref_id,
336 bool has_permission) {
337 bool all_permission = extension.HasAPIPermission(
338 extensions::APIPermission::kMediaGalleriesAllGalleries);
339 if (has_permission && all_permission) {
340 MediaGalleriesPrefInfoMap::iterator gallery_info =
341 known_galleries_.find(pref_id);
342 DCHECK(gallery_info != known_galleries_.end());
343 if (gallery_info->second.type == MediaGalleryPrefInfo::kAutoDetected) {
344 GetExtensionPrefs()->UnsetMediaGalleryPermission(extension.id(), pref_id);
345 return;
346 }
347 }
348
349 if (!has_permission && !all_permission) {
350 GetExtensionPrefs()->UnsetMediaGalleryPermission(extension.id(), pref_id);
351 } else {
352 GetExtensionPrefs()->SetMediaGalleryPermission(extension.id(), pref_id,
353 has_permission);
354 }
355 }
356
125 void MediaGalleriesPreferences::Shutdown() { 357 void MediaGalleriesPreferences::Shutdown() {
126 profile_ = NULL; 358 profile_ = NULL;
127 } 359 }
128 360
129 // static 361 // static
130 bool MediaGalleriesPreferences::UserInteractionIsEnabled() { 362 bool MediaGalleriesPreferences::UserInteractionIsEnabled() {
131 return CommandLine::ForCurrentProcess()->HasSwitch( 363 return CommandLine::ForCurrentProcess()->HasSwitch(
132 switches::kEnableMediaGalleryUI); 364 switches::kEnableMediaGalleryUI);
133 } 365 }
134 366
135 // static 367 // static
368 string16 MediaGalleriesPreferences::ComputeDisplayName(const FilePath& path) {
369 // Assumes that path is a directory and not a file.
370 return path.BaseName().LossyDisplayName();
371 }
372
373 // static
136 void MediaGalleriesPreferences::RegisterUserPrefs(PrefService* prefs) { 374 void MediaGalleriesPreferences::RegisterUserPrefs(PrefService* prefs) {
137 if (!UserInteractionIsEnabled()) 375 if (!UserInteractionIsEnabled())
138 return; 376 return;
139 377
140 prefs->RegisterListPref(prefs::kMediaGalleriesRememberedGalleries, 378 prefs->RegisterListPref(prefs::kMediaGalleriesRememberedGalleries,
141 PrefService::UNSYNCABLE_PREF); 379 PrefService::UNSYNCABLE_PREF);
142 prefs->RegisterUint64Pref(prefs::kMediaGalleriesUniqueId, 0, 380 prefs->RegisterUint64Pref(prefs::kMediaGalleriesUniqueId,
381 kInvalidMediaGalleryPrefId + 1,
143 PrefService::UNSYNCABLE_PREF); 382 PrefService::UNSYNCABLE_PREF);
144 } 383 }
384
385 extensions::ExtensionPrefs*
386 MediaGalleriesPreferences::GetExtensionPrefs() const {
387 ExtensionService* extension_service =
388 extensions::ExtensionSystem::Get(profile_)->extension_service();
389 return extension_service->extension_prefs();
390 }
391
392 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698