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

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

Powered by Google App Engine
This is Rietveld 408576698