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

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

Powered by Google App Engine
This is Rietveld 408576698