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/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 type = kMediaGalleriesTypeBlackListedValue; | 109 type = kMediaGalleriesTypeBlackListedValue; |
110 break; | 110 break; |
111 default: | 111 default: |
112 NOTREACHED(); | 112 NOTREACHED(); |
113 break; | 113 break; |
114 } | 114 } |
115 dict->SetString(kMediaGalleriesTypeKey, type); | 115 dict->SetString(kMediaGalleriesTypeKey, type); |
116 return dict; | 116 return dict; |
117 } | 117 } |
118 | 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) { | 119 FilePath MakePathRelative(FilePath path) { |
137 if (!path.IsAbsolute()) | 120 if (!path.IsAbsolute()) |
138 return path; | 121 return path; |
139 | 122 |
140 FilePath relative; | 123 FilePath relative; |
141 std::vector<FilePath::StringType> components; | 124 std::vector<FilePath::StringType> components; |
142 path.GetComponents(&components); | 125 path.GetComponents(&components); |
143 | 126 |
144 // On Windows, the first component may be the drive letter with the second | 127 // On Windows, the first component may be the drive letter with the second |
145 // being \\. | 128 // being \\. |
(...skipping 29 matching lines...) Expand all Loading... | |
175 AddGallery(device_id, display_name, pictures_path, false /*user added*/); | 158 AddGallery(device_id, display_name, pictures_path, false /*user added*/); |
176 } | 159 } |
177 } | 160 } |
178 InitFromPrefs(); | 161 InitFromPrefs(); |
179 } | 162 } |
180 | 163 |
181 MediaGalleriesPreferences::~MediaGalleriesPreferences() {} | 164 MediaGalleriesPreferences::~MediaGalleriesPreferences() {} |
182 | 165 |
183 void MediaGalleriesPreferences::InitFromPrefs() { | 166 void MediaGalleriesPreferences::InitFromPrefs() { |
184 known_galleries_.clear(); | 167 known_galleries_.clear(); |
168 device_map_.clear(); | |
185 | 169 |
186 PrefService* prefs = profile_->GetPrefs(); | 170 PrefService* prefs = profile_->GetPrefs(); |
187 const ListValue* list = prefs->GetList( | 171 const ListValue* list = prefs->GetList( |
188 prefs::kMediaGalleriesRememberedGalleries); | 172 prefs::kMediaGalleriesRememberedGalleries); |
189 if (!list) | 173 if (!list) |
190 return; | 174 return; |
191 | 175 |
192 for (ListValue::const_iterator it = list->begin(); it != list->end(); ++it) { | 176 for (ListValue::const_iterator it = list->begin(); it != list->end(); ++it) { |
193 const DictionaryValue* dict = NULL; | 177 const DictionaryValue* dict = NULL; |
194 if (!(*it)->GetAsDictionary(&dict)) | 178 if (!(*it)->GetAsDictionary(&dict)) |
195 continue; | 179 continue; |
196 | 180 |
197 MediaGalleryPrefInfo gallery_info; | 181 MediaGalleryPrefInfo gallery_info; |
198 if (PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) | 182 if (PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) |
199 known_galleries_[gallery_info.pref_id] = gallery_info; | 183 known_galleries_[gallery_info.pref_id] = gallery_info; |
184 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); | |
Lei Zhang
2012/09/15 01:17:17
I think you are missing brackets after the if stat
| |
200 } | 185 } |
201 } | 186 } |
202 | 187 |
203 bool MediaGalleriesPreferences::LookUpGalleryByPath( | 188 bool MediaGalleriesPreferences::LookUpGalleryByPath( |
204 const FilePath& path, | 189 const FilePath& path, |
205 MediaGalleryPrefInfo* gallery_info) const { | 190 MediaGalleryPrefInfo* gallery_info) const { |
206 std::string device_id = | 191 std::string device_id = |
207 MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); | 192 MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); |
208 MediaGalleryPrefId pref_id; | 193 FilePath relative_path = MakePathRelative(path); |
209 if (!FindPrefIdFromDeviceId(known_galleries_, device_id, &pref_id)) { | 194 MediaGalleryPrefIdSet galleries_on_device = |
210 if (gallery_info) { | 195 LookUpGalleriesByDeviceId(device_id); |
211 gallery_info->pref_id = kInvalidMediaGalleryPrefId; | 196 for (MediaGalleryPrefIdSet::const_iterator it = galleries_on_device.begin(); |
212 gallery_info->display_name = ComputeDisplayName(path); | 197 it != galleries_on_device.end(); |
213 gallery_info->device_id = device_id; | 198 ++it) { |
214 gallery_info->path = MakePathRelative(path); | 199 const MediaGalleryPrefInfo& gallery = known_galleries_.find(*it)->second; |
215 gallery_info->type = MediaGalleryPrefInfo::kUserAdded; | 200 if (gallery.path != relative_path) |
216 } | 201 continue; |
217 return false; | 202 |
203 if (gallery_info) | |
204 *gallery_info = gallery; | |
205 return true; | |
218 } | 206 } |
219 | 207 |
220 if (gallery_info) { | 208 if (gallery_info) { |
221 MediaGalleriesPrefInfoMap::const_iterator it = | 209 gallery_info->pref_id = kInvalidMediaGalleryPrefId; |
222 known_galleries_.find(pref_id); | 210 gallery_info->display_name = ComputeDisplayName(path); |
223 DCHECK(it != known_galleries_.end()); | 211 gallery_info->device_id = device_id; |
224 *gallery_info = it->second; | 212 gallery_info->path = relative_path; |
213 gallery_info->type = MediaGalleryPrefInfo::kUserAdded; | |
225 } | 214 } |
226 return true; | 215 return false; |
227 } | 216 } |
228 | 217 |
218 MediaGalleryPrefIdSet MediaGalleriesPreferences::LookUpGalleriesByDeviceId( | |
219 const std::string& device_id) const { | |
220 DeviceIdPrefIdsMap::const_iterator found = device_map_.find(device_id); | |
221 if (found == device_map_.end()) { | |
222 MediaGalleryPrefIdSet result; | |
223 return result; | |
224 } | |
225 | |
226 return found->second; | |
227 } | |
228 | |
229 | |
229 MediaGalleryPrefId MediaGalleriesPreferences::AddGallery( | 230 MediaGalleryPrefId MediaGalleriesPreferences::AddGallery( |
230 const std::string& device_id, const string16& display_name, | 231 const std::string& device_id, const string16& display_name, |
231 const FilePath& path, bool user_added) { | 232 const FilePath& path, bool user_added) { |
232 DCHECK(display_name.length() > 0); | 233 DCHECK(display_name.length() > 0); |
233 MediaGalleryPrefId existing_id; | 234 FilePath relative_path = MakePathRelative(path); |
234 if (FindPrefIdFromDeviceId(known_galleries_, device_id, &existing_id)) { | 235 MediaGalleryPrefIdSet galleries_on_device = |
235 const MediaGalleryPrefInfo& existing = known_galleries_[existing_id]; | 236 LookUpGalleriesByDeviceId(device_id); |
237 for (MediaGalleryPrefIdSet::const_iterator it = galleries_on_device.begin(); | |
238 it != galleries_on_device.end(); | |
239 ++it) { | |
240 if (known_galleries_[*it].path != relative_path) | |
241 continue; | |
242 | |
243 const MediaGalleryPrefInfo& existing = known_galleries_[*it]; | |
236 if (existing.type == MediaGalleryPrefInfo::kBlackListed) { | 244 if (existing.type == MediaGalleryPrefInfo::kBlackListed) { |
237 PrefService* prefs = profile_->GetPrefs(); | 245 PrefService* prefs = profile_->GetPrefs(); |
238 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); | 246 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); |
239 ListValue* list = update.Get(); | 247 ListValue* list = update.Get(); |
240 | 248 |
241 for (ListValue::const_iterator it = list->begin(); | 249 for (ListValue::const_iterator list_iter = list->begin(); |
242 it != list->end(); | 250 list_iter != list->end(); |
243 ++it) { | 251 ++list_iter) { |
244 DictionaryValue* dict; | 252 DictionaryValue* dict; |
245 MediaGalleryPrefId iter_id; | 253 MediaGalleryPrefId iter_id; |
246 if ((*it)->GetAsDictionary(&dict) && | 254 if ((*list_iter)->GetAsDictionary(&dict) && |
247 GetPrefId(*dict, &iter_id) && | 255 GetPrefId(*dict, &iter_id) && |
248 existing_id == iter_id) { | 256 *it == iter_id) { |
249 dict->SetString(kMediaGalleriesTypeKey, | 257 dict->SetString(kMediaGalleriesTypeKey, |
250 kMediaGalleriesTypeAutoDetectedValue); | 258 kMediaGalleriesTypeAutoDetectedValue); |
251 InitFromPrefs(); | 259 InitFromPrefs(); |
252 break; | 260 break; |
253 } | 261 } |
254 } | 262 } |
255 } | 263 } |
256 return existing_id; | 264 return *it; |
257 } | 265 } |
258 | 266 |
259 FilePath relative_path = MakePathRelative(path); | |
260 PrefService* prefs = profile_->GetPrefs(); | 267 PrefService* prefs = profile_->GetPrefs(); |
261 | 268 |
262 MediaGalleryPrefInfo gallery_info; | 269 MediaGalleryPrefInfo gallery_info; |
263 gallery_info.pref_id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId); | 270 gallery_info.pref_id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId); |
264 prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery_info.pref_id + 1); | 271 prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery_info.pref_id + 1); |
265 gallery_info.display_name = display_name; | 272 gallery_info.display_name = display_name; |
266 gallery_info.device_id = device_id; | 273 gallery_info.device_id = device_id; |
267 gallery_info.path = relative_path; | 274 gallery_info.path = relative_path; |
268 gallery_info.type = MediaGalleryPrefInfo::kAutoDetected; | 275 gallery_info.type = MediaGalleryPrefInfo::kAutoDetected; |
269 if (user_added) | 276 if (user_added) |
(...skipping 13 matching lines...) Expand all Loading... | |
283 MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); | 290 MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); |
284 string16 display_name = ComputeDisplayName(path); | 291 string16 display_name = ComputeDisplayName(path); |
285 return AddGallery(device_id, display_name, path, true); | 292 return AddGallery(device_id, display_name, path, true); |
286 } | 293 } |
287 | 294 |
288 void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) { | 295 void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) { |
289 PrefService* prefs = profile_->GetPrefs(); | 296 PrefService* prefs = profile_->GetPrefs(); |
290 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); | 297 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); |
291 ListValue* list = update.Get(); | 298 ListValue* list = update.Get(); |
292 | 299 |
300 if (known_galleries_.find(pref_id) == known_galleries_.end()) | |
301 return; | |
302 | |
293 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { | 303 for (ListValue::iterator iter = list->begin(); iter != list->end(); ++iter) { |
294 DictionaryValue* dict; | 304 DictionaryValue* dict; |
295 MediaGalleryPrefId iter_id; | 305 MediaGalleryPrefId iter_id; |
296 if ((*iter)->GetAsDictionary(&dict) && GetPrefId(*dict, &iter_id) && | 306 if ((*iter)->GetAsDictionary(&dict) && GetPrefId(*dict, &iter_id) && |
297 pref_id == iter_id) { | 307 pref_id == iter_id) { |
298 GetExtensionPrefs()->RemoveMediaGalleryPermissions(pref_id); | 308 GetExtensionPrefs()->RemoveMediaGalleryPermissions(pref_id); |
299 MediaGalleryPrefInfo::Type type; | 309 MediaGalleryPrefInfo::Type type; |
300 if (GetType(*dict, &type) && | 310 if (GetType(*dict, &type) && |
301 type == MediaGalleryPrefInfo::kAutoDetected) { | 311 type == MediaGalleryPrefInfo::kAutoDetected) { |
302 dict->SetString(kMediaGalleriesTypeKey, | 312 dict->SetString(kMediaGalleriesTypeKey, |
303 kMediaGalleriesTypeBlackListedValue); | 313 kMediaGalleriesTypeBlackListedValue); |
304 } else { | 314 } else { |
305 list->Erase(iter, NULL); | 315 list->Erase(iter, NULL); |
306 } | 316 } |
307 InitFromPrefs(); | 317 InitFromPrefs(); |
308 return; | 318 return; |
309 } | 319 } |
310 } | 320 } |
311 } | 321 } |
312 | 322 |
313 std::set<MediaGalleryPrefId> | 323 MediaGalleryPrefIdSet MediaGalleriesPreferences::GalleriesForExtension( |
314 MediaGalleriesPreferences::GalleriesForExtension( | |
315 const extensions::Extension& extension) const { | 324 const extensions::Extension& extension) const { |
316 std::set<MediaGalleryPrefId> result; | 325 MediaGalleryPrefIdSet result; |
317 if (extension.HasAPIPermission( | 326 if (extension.HasAPIPermission( |
318 extensions::APIPermission::kMediaGalleriesAllGalleries)) { | 327 extensions::APIPermission::kMediaGalleriesAllGalleries)) { |
319 for (MediaGalleriesPrefInfoMap::const_iterator it = | 328 for (MediaGalleriesPrefInfoMap::const_iterator it = |
320 known_galleries_.begin(); it != known_galleries_.end(); ++it) { | 329 known_galleries_.begin(); it != known_galleries_.end(); ++it) { |
321 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) | 330 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) |
322 result.insert(it->second.pref_id); | 331 result.insert(it->second.pref_id); |
323 } | 332 } |
324 } | 333 } |
325 | 334 |
326 std::vector<MediaGalleryPermission> stored_permissions = | 335 std::vector<MediaGalleryPermission> stored_permissions = |
(...skipping 14 matching lines...) Expand all Loading... | |
341 } | 350 } |
342 } | 351 } |
343 } | 352 } |
344 return result; | 353 return result; |
345 } | 354 } |
346 | 355 |
347 void MediaGalleriesPreferences::SetGalleryPermissionForExtension( | 356 void MediaGalleriesPreferences::SetGalleryPermissionForExtension( |
348 const extensions::Extension& extension, | 357 const extensions::Extension& extension, |
349 MediaGalleryPrefId pref_id, | 358 MediaGalleryPrefId pref_id, |
350 bool has_permission) { | 359 bool has_permission) { |
360 // The gallery may not exist anymore if the user opened a second config | |
361 // surface concurrently and removed it. Drop the permission update if so. | |
362 MediaGalleriesPrefInfoMap::iterator gallery_info = | |
363 known_galleries_.find(pref_id); | |
364 if (gallery_info == known_galleries_.end()) | |
365 return; | |
366 | |
351 bool all_permission = extension.HasAPIPermission( | 367 bool all_permission = extension.HasAPIPermission( |
352 extensions::APIPermission::kMediaGalleriesAllGalleries); | 368 extensions::APIPermission::kMediaGalleriesAllGalleries); |
353 if (has_permission && all_permission) { | 369 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) { | 370 if (gallery_info->second.type == MediaGalleryPrefInfo::kAutoDetected) { |
358 GetExtensionPrefs()->UnsetMediaGalleryPermission(extension.id(), pref_id); | 371 GetExtensionPrefs()->UnsetMediaGalleryPermission(extension.id(), pref_id); |
359 return; | 372 return; |
360 } | 373 } |
361 } | 374 } |
362 | 375 |
363 if (!has_permission && !all_permission) { | 376 if (!has_permission && !all_permission) { |
364 GetExtensionPrefs()->UnsetMediaGalleryPermission(extension.id(), pref_id); | 377 GetExtensionPrefs()->UnsetMediaGalleryPermission(extension.id(), pref_id); |
365 } else { | 378 } else { |
366 GetExtensionPrefs()->SetMediaGalleryPermission(extension.id(), pref_id, | 379 GetExtensionPrefs()->SetMediaGalleryPermission(extension.id(), pref_id, |
(...skipping 30 matching lines...) Expand all Loading... | |
397 } | 410 } |
398 | 411 |
399 extensions::ExtensionPrefs* | 412 extensions::ExtensionPrefs* |
400 MediaGalleriesPreferences::GetExtensionPrefs() const { | 413 MediaGalleriesPreferences::GetExtensionPrefs() const { |
401 ExtensionService* extension_service = | 414 ExtensionService* extension_service = |
402 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 415 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
403 return extension_service->extension_prefs(); | 416 return extension_service->extension_prefs(); |
404 } | 417 } |
405 | 418 |
406 } // namespace chrome | 419 } // namespace chrome |
OLD | NEW |