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

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

Powered by Google App Engine
This is Rietveld 408576698