| 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> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
| 15 #include "base/json/json_writer.h" |
| 15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/string_util.h" |
| 17 #include "base/system_monitor/system_monitor.h" | 19 #include "base/system_monitor/system_monitor.h" |
| 18 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 21 #include "base/values.h" |
| 19 #include "chrome/browser/media_gallery/media_galleries_preferences.h" | 22 #include "chrome/browser/media_gallery/media_galleries_preferences.h" |
| 20 #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" | 23 #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/system_monitor/media_storage_util.h" | 25 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 23 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
| 24 #include "chrome/common/extensions/extension_constants.h" | 27 #include "chrome/common/extensions/extension_constants.h" |
| 25 #include "chrome/common/extensions/extension.h" | 28 #include "chrome/common/extensions/extension.h" |
| 26 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/notification_observer.h" | 30 #include "content/public/browser/notification_observer.h" |
| 28 #include "content/public/browser/notification_registrar.h" | 31 #include "content/public/browser/notification_registrar.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 // Sanity checks for |path|. | 78 // Sanity checks for |path|. |
| 76 CHECK(path.IsAbsolute()); | 79 CHECK(path.IsAbsolute()); |
| 77 CHECK(!path.ReferencesParent()); | 80 CHECK(!path.ReferencesParent()); |
| 78 std::string fs_name(extension_misc::kMediaFileSystemPathPart); | 81 std::string fs_name(extension_misc::kMediaFileSystemPathPart); |
| 79 const std::string fsid = isolated_context->RegisterFileSystemForPath( | 82 const std::string fsid = isolated_context->RegisterFileSystemForPath( |
| 80 fileapi::kFileSystemTypeNativeMedia, path, &fs_name); | 83 fileapi::kFileSystemTypeNativeMedia, path, &fs_name); |
| 81 CHECK(!fsid.empty()); | 84 CHECK(!fsid.empty()); |
| 82 return fsid; | 85 return fsid; |
| 83 } | 86 } |
| 84 | 87 |
| 88 // Make a JSON string out of |name| and |id|. The |id| makes the combined name |
| 89 // unique. The JSON string should not contain any slashes. |
| 90 std::string MakeJSONFileSystemName(const string16& name, |
| 91 const MediaGalleryPrefId& id) { |
| 92 string16 sanitized_name; |
| 93 string16 separators = |
| 94 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 95 FilePath::kSeparators |
| 96 #else |
| 97 ASCIIToUTF16(FilePath::kSeparators) |
| 98 #endif |
| 99 ; // NOLINT |
| 100 ReplaceChars(name, separators.c_str(), ASCIIToUTF16("_"), &sanitized_name); |
| 101 |
| 102 base::DictionaryValue dict_value; |
| 103 dict_value.SetWithoutPathExpansion( |
| 104 "name", Value::CreateStringValue(sanitized_name)); |
| 105 dict_value.SetWithoutPathExpansion("id", Value::CreateIntegerValue(id)); |
| 106 |
| 107 std::string json_string; |
| 108 base::JSONWriter::Write(&dict_value, &json_string); |
| 109 return json_string; |
| 110 } |
| 111 |
| 85 } // namespace | 112 } // namespace |
| 86 | 113 |
| 114 MediaFileSystemInfo::MediaFileSystemInfo(const std::string& fs_name, |
| 115 const FilePath& fs_path, |
| 116 const std::string& filesystem_id) |
| 117 : name(fs_name), |
| 118 path(fs_path), |
| 119 fsid(filesystem_id) { |
| 120 } |
| 121 |
| 122 MediaFileSystemInfo::MediaFileSystemInfo() {} |
| 123 |
| 87 #if defined(SUPPORT_MEDIA_FILESYSTEM) | 124 #if defined(SUPPORT_MEDIA_FILESYSTEM) |
| 88 // Class to manage MediaDeviceDelegateImpl object for the attached media | 125 // Class to manage MediaDeviceDelegateImpl object for the attached media |
| 89 // device. Refcounted to reuse the same media device delegate entry across | 126 // device. Refcounted to reuse the same media device delegate entry across |
| 90 // extensions. This class supports WeakPtr (extends SupportsWeakPtr) to expose | 127 // extensions. This class supports WeakPtr (extends SupportsWeakPtr) to expose |
| 91 // itself as a weak pointer to MediaFileSystemRegistry. | 128 // itself as a weak pointer to MediaFileSystemRegistry. |
| 92 class ScopedMediaDeviceMapEntry | 129 class ScopedMediaDeviceMapEntry |
| 93 : public base::RefCounted<ScopedMediaDeviceMapEntry>, | 130 : public base::RefCounted<ScopedMediaDeviceMapEntry>, |
| 94 public base::SupportsWeakPtr<ScopedMediaDeviceMapEntry> { | 131 public base::SupportsWeakPtr<ScopedMediaDeviceMapEntry> { |
| 95 public: | 132 public: |
| 96 // |no_references_callback| is called when the last ScopedMediaDeviceMapEntry | 133 // |no_references_callback| is called when the last ScopedMediaDeviceMapEntry |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 const MediaStorageUtil::DeviceIdSet* attached_devices, | 309 const MediaStorageUtil::DeviceIdSet* attached_devices, |
| 273 const MediaGalleryPrefIdSet& galleries, | 310 const MediaGalleryPrefIdSet& galleries, |
| 274 const MediaGalleriesPrefInfoMap& galleries_info, | 311 const MediaGalleriesPrefInfoMap& galleries_info, |
| 275 const MediaFileSystemsCallback& callback) { | 312 const MediaFileSystemsCallback& callback) { |
| 276 std::vector<MediaFileSystemInfo> result; | 313 std::vector<MediaFileSystemInfo> result; |
| 277 MediaGalleryPrefIdSet new_galleries; | 314 MediaGalleryPrefIdSet new_galleries; |
| 278 for (std::set<MediaGalleryPrefId>::const_iterator pref_id_it = | 315 for (std::set<MediaGalleryPrefId>::const_iterator pref_id_it = |
| 279 galleries.begin(); | 316 galleries.begin(); |
| 280 pref_id_it != galleries.end(); | 317 pref_id_it != galleries.end(); |
| 281 ++pref_id_it) { | 318 ++pref_id_it) { |
| 319 const MediaGalleryPrefId& pref_id = *pref_id_it; |
| 282 const MediaGalleryPrefInfo& gallery_info = | 320 const MediaGalleryPrefInfo& gallery_info = |
| 283 galleries_info.find(*pref_id_it)->second; | 321 galleries_info.find(pref_id)->second; |
| 284 const std::string& device_id = gallery_info.device_id; | 322 const std::string& device_id = gallery_info.device_id; |
| 285 if (!ContainsKey(*attached_devices, device_id)) | 323 if (!ContainsKey(*attached_devices, device_id)) |
| 286 continue; | 324 continue; |
| 287 | 325 |
| 288 PrefIdFsInfoMap::const_iterator existing_info = | 326 PrefIdFsInfoMap::const_iterator existing_info = |
| 289 pref_id_map_.find(*pref_id_it); | 327 pref_id_map_.find(pref_id); |
| 290 if (existing_info != pref_id_map_.end()) { | 328 if (existing_info != pref_id_map_.end()) { |
| 291 result.push_back(existing_info->second); | 329 result.push_back(existing_info->second); |
| 292 new_galleries.insert(*pref_id_it); | 330 new_galleries.insert(pref_id); |
| 293 continue; | 331 continue; |
| 294 } | 332 } |
| 295 | 333 |
| 296 FilePath path = gallery_info.AbsolutePath(); | 334 FilePath path = gallery_info.AbsolutePath(); |
| 297 if (!path.IsAbsolute()) | 335 if (!path.IsAbsolute()) |
| 298 continue; | 336 continue; |
| 299 | 337 |
| 300 std::string fsid; | 338 std::string fsid; |
| 301 if (MediaStorageUtil::IsMassStorageDevice(device_id)) { | 339 if (MediaStorageUtil::IsMassStorageDevice(device_id)) { |
| 302 fsid = RegisterFileSystemForMassStorage(device_id, path); | 340 fsid = RegisterFileSystemForMassStorage(device_id, path); |
| 303 } else { | 341 } else { |
| 304 #if defined(SUPPORT_MEDIA_FILESYSTEM) | 342 #if defined(SUPPORT_MEDIA_FILESYSTEM) |
| 305 scoped_refptr<ScopedMediaDeviceMapEntry> mtp_device_host; | 343 scoped_refptr<ScopedMediaDeviceMapEntry> mtp_device_host; |
| 306 fsid = registry_->RegisterFileSystemForMtpDevice( | 344 fsid = registry_->RegisterFileSystemForMtpDevice( |
| 307 device_id, path, &mtp_device_host); | 345 device_id, path, &mtp_device_host); |
| 308 DCHECK(mtp_device_host.get()); | 346 DCHECK(mtp_device_host.get()); |
| 309 media_device_map_references_[*pref_id_it] = mtp_device_host; | 347 media_device_map_references_[pref_id] = mtp_device_host; |
| 310 #else | 348 #else |
| 311 NOTIMPLEMENTED(); | 349 NOTIMPLEMENTED(); |
| 312 continue; | 350 continue; |
| 313 #endif | 351 #endif |
| 314 } | 352 } |
| 315 DCHECK(!fsid.empty()); | 353 DCHECK(!fsid.empty()); |
| 316 | 354 |
| 317 MediaFileSystemInfo new_entry; | 355 MediaFileSystemInfo new_entry( |
| 318 new_entry.name = gallery_info.display_name; | 356 MakeJSONFileSystemName(gallery_info.display_name, pref_id), |
| 319 new_entry.path = path; | 357 path, |
| 320 new_entry.fsid = fsid; | 358 fsid); |
| 321 result.push_back(new_entry); | 359 result.push_back(new_entry); |
| 322 new_galleries.insert(*pref_id_it); | 360 new_galleries.insert(pref_id); |
| 323 pref_id_map_[*pref_id_it] = new_entry; | 361 pref_id_map_[pref_id] = new_entry; |
| 324 } | 362 } |
| 325 | 363 |
| 326 RevokeOldGalleries(new_galleries); | 364 RevokeOldGalleries(new_galleries); |
| 327 | 365 |
| 328 callback.Run(result); | 366 callback.Run(result); |
| 329 } | 367 } |
| 330 | 368 |
| 331 void RevokeOldGalleries(const MediaGalleryPrefIdSet& new_galleries) { | 369 void RevokeOldGalleries(const MediaGalleryPrefIdSet& new_galleries) { |
| 332 // TODO(vandebo) We need a way of getting notification when permission for | 370 // TODO(vandebo) We need a way of getting notification when permission for |
| 333 // galleries are revoked (http://crbug.com/145855). For now, invalidate | 371 // galleries are revoked (http://crbug.com/145855). For now, invalidate |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 extension_hosts_map_.find(profile); | 667 extension_hosts_map_.find(profile); |
| 630 DCHECK(extension_hosts != extension_hosts_map_.end()); | 668 DCHECK(extension_hosts != extension_hosts_map_.end()); |
| 631 ExtensionHostMap::size_type erase_count = | 669 ExtensionHostMap::size_type erase_count = |
| 632 extension_hosts->second.erase(extension_id); | 670 extension_hosts->second.erase(extension_id); |
| 633 DCHECK_EQ(1U, erase_count); | 671 DCHECK_EQ(1U, erase_count); |
| 634 if (extension_hosts->second.empty()) | 672 if (extension_hosts->second.empty()) |
| 635 extension_hosts_map_.erase(extension_hosts); | 673 extension_hosts_map_.erase(extension_hosts); |
| 636 } | 674 } |
| 637 | 675 |
| 638 } // namespace chrome | 676 } // namespace chrome |
| OLD | NEW |