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_galleries/media_file_system_registry.h" | 5 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 for (std::set<MediaGalleryPrefId>::const_iterator id = galleries.begin(); | 278 for (std::set<MediaGalleryPrefId>::const_iterator id = galleries.begin(); |
279 id != galleries.end(); | 279 id != galleries.end(); |
280 ++id) { | 280 ++id) { |
281 device_ids->insert(galleries_info.find(*id)->second.device_id); | 281 device_ids->insert(galleries_info.find(*id)->second.device_id); |
282 } | 282 } |
283 MediaStorageUtil::FilterAttachedDevices(device_ids, base::Bind( | 283 MediaStorageUtil::FilterAttachedDevices(device_ids, base::Bind( |
284 &ExtensionGalleriesHost::GetMediaFileSystemsForAttachedDevices, this, | 284 &ExtensionGalleriesHost::GetMediaFileSystemsForAttachedDevices, this, |
285 base::Owned(device_ids), galleries, galleries_info, callback)); | 285 base::Owned(device_ids), galleries, galleries_info, callback)); |
286 } | 286 } |
287 | 287 |
288 void RevokeOldGalleries(const MediaGalleryPrefIdSet& new_galleries) { | |
289 if (new_galleries.size() == pref_id_map_.size()) | |
290 return; | |
291 | |
292 MediaGalleryPrefIdSet old_galleries; | |
293 for (PrefIdFsInfoMap::const_iterator it = pref_id_map_.begin(); | |
294 it != pref_id_map_.end(); | |
295 ++it) { | |
296 old_galleries.insert(it->first); | |
297 } | |
298 MediaGalleryPrefIdSet invalid_galleries = | |
299 base::STLSetDifference<MediaGalleryPrefIdSet>(old_galleries, | |
300 new_galleries); | |
301 for (MediaGalleryPrefIdSet::const_iterator it = invalid_galleries.begin(); | |
302 it != invalid_galleries.end(); | |
303 ++it) { | |
304 RevokeGalleryByPrefId(*it); | |
305 } | |
306 } | |
307 | |
308 // Revoke the file system for |id| if this extension has created one for |id|. | 288 // Revoke the file system for |id| if this extension has created one for |id|. |
309 void RevokeGalleryByPrefId(MediaGalleryPrefId id) { | 289 void RevokeGalleryByPrefId(MediaGalleryPrefId id) { |
310 PrefIdFsInfoMap::iterator gallery = pref_id_map_.find(id); | 290 PrefIdFsInfoMap::iterator gallery = pref_id_map_.find(id); |
311 if (gallery == pref_id_map_.end()) | 291 if (gallery == pref_id_map_.end()) |
312 return; | 292 return; |
313 | 293 |
314 file_system_context_->RevokeFileSystem(gallery->second.fsid); | 294 file_system_context_->RevokeFileSystem(gallery->second.fsid); |
315 pref_id_map_.erase(gallery); | 295 pref_id_map_.erase(gallery); |
316 | 296 |
317 if (pref_id_map_.empty()) { | 297 if (pref_id_map_.empty()) { |
(...skipping 27 matching lines...) Expand all Loading... |
345 std::vector<MediaFileSystemInfo> result; | 325 std::vector<MediaFileSystemInfo> result; |
346 | 326 |
347 if (rph_refs_.empty()) { | 327 if (rph_refs_.empty()) { |
348 // We're actually in the middle of shutdown, and Filter...() lagging | 328 // We're actually in the middle of shutdown, and Filter...() lagging |
349 // which can invoke this method interleaved in the destruction callback | 329 // which can invoke this method interleaved in the destruction callback |
350 // sequence and re-populate pref_id_map_. | 330 // sequence and re-populate pref_id_map_. |
351 callback.Run(result); | 331 callback.Run(result); |
352 return; | 332 return; |
353 } | 333 } |
354 | 334 |
355 MediaGalleryPrefIdSet new_galleries; | |
356 for (std::set<MediaGalleryPrefId>::const_iterator pref_id_it = | 335 for (std::set<MediaGalleryPrefId>::const_iterator pref_id_it = |
357 galleries.begin(); | 336 galleries.begin(); |
358 pref_id_it != galleries.end(); | 337 pref_id_it != galleries.end(); |
359 ++pref_id_it) { | 338 ++pref_id_it) { |
360 const MediaGalleryPrefId& pref_id = *pref_id_it; | 339 const MediaGalleryPrefId& pref_id = *pref_id_it; |
361 const MediaGalleryPrefInfo& gallery_info = | 340 const MediaGalleryPrefInfo& gallery_info = |
362 galleries_info.find(pref_id)->second; | 341 galleries_info.find(pref_id)->second; |
363 const std::string& device_id = gallery_info.device_id; | 342 const std::string& device_id = gallery_info.device_id; |
364 if (!ContainsKey(*attached_devices, device_id)) | 343 if (!ContainsKey(*attached_devices, device_id)) |
365 continue; | 344 continue; |
366 | 345 |
367 PrefIdFsInfoMap::const_iterator existing_info = | 346 PrefIdFsInfoMap::const_iterator existing_info = |
368 pref_id_map_.find(pref_id); | 347 pref_id_map_.find(pref_id); |
369 if (existing_info != pref_id_map_.end()) { | 348 if (existing_info != pref_id_map_.end()) { |
370 result.push_back(existing_info->second); | 349 result.push_back(existing_info->second); |
371 new_galleries.insert(pref_id); | |
372 continue; | 350 continue; |
373 } | 351 } |
374 | 352 |
375 base::FilePath path = gallery_info.AbsolutePath(); | 353 base::FilePath path = gallery_info.AbsolutePath(); |
376 if (!MediaStorageUtil::CanCreateFileSystem(device_id, path)) | 354 if (!MediaStorageUtil::CanCreateFileSystem(device_id, path)) |
377 continue; | 355 continue; |
378 | 356 |
379 std::string fsid = | 357 std::string fsid = |
380 file_system_context_->RegisterFileSystem(device_id, path); | 358 file_system_context_->RegisterFileSystem(device_id, path); |
381 if (fsid.empty()) | 359 if (fsid.empty()) |
382 continue; | 360 continue; |
383 | 361 |
384 MediaFileSystemInfo new_entry( | 362 MediaFileSystemInfo new_entry( |
385 gallery_info.GetGalleryDisplayName(), | 363 gallery_info.GetGalleryDisplayName(), |
386 path, | 364 path, |
387 fsid, | 365 fsid, |
388 pref_id, | 366 pref_id, |
389 GetTransientIdForRemovableDeviceId(device_id), | 367 GetTransientIdForRemovableDeviceId(device_id), |
390 StorageInfo::IsRemovableDevice(device_id), | 368 StorageInfo::IsRemovableDevice(device_id), |
391 StorageInfo::IsMediaDevice(device_id)); | 369 StorageInfo::IsMediaDevice(device_id)); |
392 result.push_back(new_entry); | 370 result.push_back(new_entry); |
393 new_galleries.insert(pref_id); | |
394 pref_id_map_[pref_id] = new_entry; | 371 pref_id_map_[pref_id] = new_entry; |
395 } | 372 } |
396 | 373 |
397 if (result.size() == 0) { | 374 if (result.size() == 0) { |
398 rph_refs_.Reset(); | 375 rph_refs_.Reset(); |
399 CleanUp(); | 376 CleanUp(); |
400 } else { | |
401 RevokeOldGalleries(new_galleries); | |
402 } | 377 } |
403 | 378 |
| 379 DCHECK_EQ(pref_id_map_.size(), result.size()); |
404 callback.Run(result); | 380 callback.Run(result); |
405 } | 381 } |
406 | 382 |
407 std::string GetTransientIdForRemovableDeviceId(const std::string& device_id) { | 383 std::string GetTransientIdForRemovableDeviceId(const std::string& device_id) { |
408 if (!StorageInfo::IsRemovableDevice(device_id)) | 384 if (!StorageInfo::IsRemovableDevice(device_id)) |
409 return std::string(); | 385 return std::string(); |
410 | 386 |
411 return StorageMonitor::GetInstance()->GetTransientIdForDeviceId(device_id); | 387 return StorageMonitor::GetInstance()->GetTransientIdForDeviceId(device_id); |
412 } | 388 } |
413 | 389 |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
730 DCHECK_EQ(1U, erase_count); | 706 DCHECK_EQ(1U, erase_count); |
731 if (extension_hosts->second.empty()) { | 707 if (extension_hosts->second.empty()) { |
732 // When a profile has no ExtensionGalleriesHosts left, remove the | 708 // When a profile has no ExtensionGalleriesHosts left, remove the |
733 // matching gallery-change-watcher since it is no longer needed. Leave the | 709 // matching gallery-change-watcher since it is no longer needed. Leave the |
734 // |extension_hosts| entry alone, since it indicates the profile has been | 710 // |extension_hosts| entry alone, since it indicates the profile has been |
735 // previously used. | 711 // previously used. |
736 MediaGalleriesPreferences* preferences = GetPreferences(profile); | 712 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
737 preferences->RemoveGalleryChangeObserver(this); | 713 preferences->RemoveGalleryChangeObserver(this); |
738 } | 714 } |
739 } | 715 } |
OLD | NEW |