| 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 // MediaFileSystemRegistry implementation. | 5 // MediaFileSystemRegistry implementation. |
| 6 | 6 |
| 7 #include "chrome/browser/media_gallery/media_file_system_registry.h" | 7 #include "chrome/browser/media_gallery/media_file_system_registry.h" |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 DCHECK(mtp_device_host.get()); | 271 DCHECK(mtp_device_host.get()); |
| 272 media_device_map_references_[pref_id] = mtp_device_host; | 272 media_device_map_references_[pref_id] = mtp_device_host; |
| 273 #else | 273 #else |
| 274 NOTIMPLEMENTED(); | 274 NOTIMPLEMENTED(); |
| 275 continue; | 275 continue; |
| 276 #endif | 276 #endif |
| 277 } | 277 } |
| 278 DCHECK(!fsid.empty()); | 278 DCHECK(!fsid.empty()); |
| 279 | 279 |
| 280 MediaFileSystemInfo new_entry( | 280 MediaFileSystemInfo new_entry( |
| 281 MakeJSONFileSystemName(gallery_info.display_name, | 281 MakeJSONFileSystemName(gallery_info, pref_id, device_id), |
| 282 pref_id, | |
| 283 device_id), | |
| 284 path, | 282 path, |
| 285 fsid); | 283 fsid); |
| 286 result.push_back(new_entry); | 284 result.push_back(new_entry); |
| 287 new_galleries.insert(pref_id); | 285 new_galleries.insert(pref_id); |
| 288 pref_id_map_[pref_id] = new_entry; | 286 pref_id_map_[pref_id] = new_entry; |
| 289 } | 287 } |
| 290 | 288 |
| 291 if (result.size() == 0) { | 289 if (result.size() == 0) { |
| 292 rph_refs_.clear(); | 290 rph_refs_.clear(); |
| 293 CleanUp(); | 291 CleanUp(); |
| 294 } else { | 292 } else { |
| 295 RevokeOldGalleries(new_galleries); | 293 RevokeOldGalleries(new_galleries); |
| 296 } | 294 } |
| 297 | 295 |
| 298 callback.Run(result); | 296 callback.Run(result); |
| 299 } | 297 } |
| 300 | 298 |
| 301 std::string GetTransientIdForRemovableDeviceId(const std::string& device_id) { | 299 std::string GetTransientIdForRemovableDeviceId(const std::string& device_id) { |
| 302 if (!MediaStorageUtil::IsRemovableDevice(device_id)) | 300 if (!MediaStorageUtil::IsRemovableDevice(device_id)) |
| 303 return std::string(); | 301 return std::string(); |
| 304 MediaFileSystemRegistry* registry = | 302 MediaFileSystemRegistry* registry = |
| 305 file_system_context_->GetMediaFileSystemRegistry(); | 303 file_system_context_->GetMediaFileSystemRegistry(); |
| 306 return registry->GetTransientIdForDeviceId(device_id); | 304 return registry->GetTransientIdForDeviceId(device_id); |
| 307 } | 305 } |
| 308 | 306 |
| 309 // Make a JSON string out of |name|, |pref_id| and |device_id|. The IDs makes | 307 // Make a JSON string out of |gallery_info|, |pref_id| and |device_id|. |
| 310 // the combined name unique. The JSON string should not contain any slashes. | 308 // The IDs makes the combined name unique. |
| 311 std::string MakeJSONFileSystemName(const string16& name, | 309 // The JSON string should not contain any slashes. |
| 310 std::string MakeJSONFileSystemName(const MediaGalleryPrefInfo& gallery_info, |
| 312 const MediaGalleryPrefId& pref_id, | 311 const MediaGalleryPrefId& pref_id, |
| 313 const std::string& device_id) { | 312 const std::string& device_id) { |
| 314 string16 sanitized_name; | 313 string16 sanitized_name; |
| 315 string16 separators = | 314 string16 separators = |
| 316 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 315 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 317 FilePath::kSeparators | 316 FilePath::kSeparators |
| 318 #else | 317 #else |
| 319 ASCIIToUTF16(FilePath::kSeparators) | 318 ASCIIToUTF16(FilePath::kSeparators) |
| 320 #endif | 319 #endif |
| 321 ; // NOLINT | 320 ; // NOLINT |
| 321 const string16& name = gallery_info.display_name; |
| 322 ReplaceChars(name, separators.c_str(), ASCIIToUTF16("_"), &sanitized_name); | 322 ReplaceChars(name, separators.c_str(), ASCIIToUTF16("_"), &sanitized_name); |
| 323 | 323 |
| 324 base::DictionaryValue dict_value; | 324 base::DictionaryValue dict_value; |
| 325 dict_value.SetStringWithoutPathExpansion("name", sanitized_name); | 325 dict_value.SetStringWithoutPathExpansion( |
| 326 MediaFileSystemRegistry::kNameKey, sanitized_name); |
| 326 | 327 |
| 327 // This should have been a StringValue, but it's a bit late to change it. | 328 // This should have been a StringValue, but it's a bit late to change it. |
| 328 dict_value.SetIntegerWithoutPathExpansion("galleryId", pref_id); | 329 dict_value.SetIntegerWithoutPathExpansion( |
| 330 MediaFileSystemRegistry::kGalleryIdKey, pref_id); |
| 329 | 331 |
| 330 // |device_id| can be empty, in which case, just omit it. | 332 // |device_id| can be empty, in which case, just omit it. |
| 331 std::string transient_device_id = | 333 std::string transient_device_id = |
| 332 GetTransientIdForRemovableDeviceId(device_id); | 334 GetTransientIdForRemovableDeviceId(device_id); |
| 333 if (!transient_device_id.empty()) | 335 if (!transient_device_id.empty()) { |
| 334 dict_value.SetStringWithoutPathExpansion("deviceId", transient_device_id); | 336 dict_value.SetStringWithoutPathExpansion( |
| 337 MediaFileSystemRegistry::kDeviceIdKey, transient_device_id); |
| 338 } |
| 339 |
| 340 DCHECK(gallery_info.type == MediaGalleryPrefInfo::kAutoDetected || |
| 341 gallery_info.type == MediaGalleryPrefInfo::kUserAdded); |
| 342 dict_value.SetBooleanWithoutPathExpansion( |
| 343 MediaFileSystemRegistry::kUserAddedKey, |
| 344 gallery_info.type == MediaGalleryPrefInfo::kUserAdded); |
| 335 | 345 |
| 336 std::string json_string; | 346 std::string json_string; |
| 337 base::JSONWriter::Write(&dict_value, &json_string); | 347 base::JSONWriter::Write(&dict_value, &json_string); |
| 338 return json_string; | 348 return json_string; |
| 339 } | 349 } |
| 340 | 350 |
| 341 void OnRendererProcessTerminated(const RenderProcessHost* rph) { | 351 void OnRendererProcessTerminated(const RenderProcessHost* rph) { |
| 342 RenderProcessHostRefCount::const_iterator rph_info = rph_refs_.find(rph); | 352 RenderProcessHostRefCount::const_iterator rph_info = rph_refs_.find(rph); |
| 343 DCHECK(rph_info != rph_refs_.end()); | 353 DCHECK(rph_info != rph_refs_.end()); |
| 344 // We're going to remove everything from the set, so we make a copy | 354 // We're going to remove everything from the set, so we make a copy |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 // A registrar for listening notifications. | 426 // A registrar for listening notifications. |
| 417 content::NotificationRegistrar registrar_; | 427 content::NotificationRegistrar registrar_; |
| 418 | 428 |
| 419 DISALLOW_COPY_AND_ASSIGN(ExtensionGalleriesHost); | 429 DISALLOW_COPY_AND_ASSIGN(ExtensionGalleriesHost); |
| 420 }; | 430 }; |
| 421 | 431 |
| 422 /****************** | 432 /****************** |
| 423 * Public methods | 433 * Public methods |
| 424 ******************/ | 434 ******************/ |
| 425 | 435 |
| 436 const char MediaFileSystemRegistry::kDeviceIdKey[] = "deviceId"; |
| 437 const char MediaFileSystemRegistry::kGalleryIdKey[] = "galleryId"; |
| 438 const char MediaFileSystemRegistry::kNameKey[] = "name"; |
| 439 const char MediaFileSystemRegistry::kUserAddedKey[] = "userAdded"; |
| 440 |
| 426 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension( | 441 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension( |
| 427 const content::RenderViewHost* rvh, | 442 const content::RenderViewHost* rvh, |
| 428 const extensions::Extension* extension, | 443 const extensions::Extension* extension, |
| 429 const MediaFileSystemsCallback& callback) { | 444 const MediaFileSystemsCallback& callback) { |
| 430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 445 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 431 | 446 |
| 432 Profile* profile = | 447 Profile* profile = |
| 433 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); | 448 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); |
| 434 MediaGalleriesPreferences* preferences = GetPreferences(profile); | 449 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
| 435 MediaGalleryPrefIdSet galleries = | 450 MediaGalleryPrefIdSet galleries = |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 } | 706 } |
| 692 | 707 |
| 693 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 708 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 694 ScopedMTPDeviceMapEntry* | 709 ScopedMTPDeviceMapEntry* |
| 695 MediaFileSystemRegistry::GetOrCreateScopedMTPDeviceMapEntry( | 710 MediaFileSystemRegistry::GetOrCreateScopedMTPDeviceMapEntry( |
| 696 const FilePath::StringType& device_location) { | 711 const FilePath::StringType& device_location) { |
| 697 MTPDeviceDelegateMap::iterator delegate_it = | 712 MTPDeviceDelegateMap::iterator delegate_it = |
| 698 mtp_device_delegate_map_.find(device_location); | 713 mtp_device_delegate_map_.find(device_location); |
| 699 if (delegate_it != mtp_device_delegate_map_.end()) | 714 if (delegate_it != mtp_device_delegate_map_.end()) |
| 700 return delegate_it->second; | 715 return delegate_it->second; |
| 701 ScopedMTPDeviceMapEntry* mtp_device_host = new ScopedMTPDeviceMapEntry( | 716 ScopedMTPDeviceMapEntry* mtp_device_host = |
| 702 device_location, base::Bind( | 717 new ScopedMTPDeviceMapEntry( |
| 703 &MediaFileSystemRegistry::RemoveScopedMTPDeviceMapEntry, | 718 device_location, |
| 704 base::Unretained(this), device_location)); | 719 base::Bind( |
| 720 &MediaFileSystemRegistry::RemoveScopedMTPDeviceMapEntry, |
| 721 base::Unretained(this), |
| 722 device_location)); |
| 705 mtp_device_delegate_map_[device_location] = mtp_device_host; | 723 mtp_device_delegate_map_[device_location] = mtp_device_host; |
| 706 return mtp_device_host; | 724 return mtp_device_host; |
| 707 } | 725 } |
| 708 | 726 |
| 709 void MediaFileSystemRegistry::RemoveScopedMTPDeviceMapEntry( | 727 void MediaFileSystemRegistry::RemoveScopedMTPDeviceMapEntry( |
| 710 const FilePath::StringType& device_location) { | 728 const FilePath::StringType& device_location) { |
| 711 MTPDeviceDelegateMap::iterator delegate_it = | 729 MTPDeviceDelegateMap::iterator delegate_it = |
| 712 mtp_device_delegate_map_.find(device_location); | 730 mtp_device_delegate_map_.find(device_location); |
| 713 DCHECK(delegate_it != mtp_device_delegate_map_.end()); | 731 DCHECK(delegate_it != mtp_device_delegate_map_.end()); |
| 714 mtp_device_delegate_map_.erase(delegate_it); | 732 mtp_device_delegate_map_.erase(delegate_it); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 730 | 748 |
| 731 PrefChangeRegistrarMap::iterator pref_it = | 749 PrefChangeRegistrarMap::iterator pref_it = |
| 732 pref_change_registrar_map_.find(profile); | 750 pref_change_registrar_map_.find(profile); |
| 733 DCHECK(pref_it != pref_change_registrar_map_.end()); | 751 DCHECK(pref_it != pref_change_registrar_map_.end()); |
| 734 delete pref_it->second; | 752 delete pref_it->second; |
| 735 pref_change_registrar_map_.erase(pref_it); | 753 pref_change_registrar_map_.erase(pref_it); |
| 736 } | 754 } |
| 737 } | 755 } |
| 738 | 756 |
| 739 } // namespace chrome | 757 } // namespace chrome |
| OLD | NEW |