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