| 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 const FilePath& fs_path, | 119 const FilePath& fs_path, |
| 120 const std::string& filesystem_id) | 120 const std::string& filesystem_id) |
| 121 : name(fs_name), | 121 : name(fs_name), |
| 122 path(fs_path), | 122 path(fs_path), |
| 123 fsid(filesystem_id) { | 123 fsid(filesystem_id) { |
| 124 } | 124 } |
| 125 | 125 |
| 126 MediaFileSystemInfo::MediaFileSystemInfo() {} | 126 MediaFileSystemInfo::MediaFileSystemInfo() {} |
| 127 | 127 |
| 128 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 128 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 129 // Class to manage MTPDeviceDelegateImpl object for the attached MTP device. | 129 // Class to manage the lifetime of MTPDeviceDelegateImpl object for the |
| 130 // Refcounted to reuse the same MTP device delegate entry across extensions. | 130 // attached media transfer protocol (MTP) device. This object is constructed for |
| 131 // This class supports WeakPtr (extends SupportsWeakPtr) to expose itself as | 131 // each MTP device. |
| 132 // a weak pointer to MediaFileSystemRegistry. | 132 class ScopedMTPDeviceMapEntry { |
| 133 class ScopedMTPDeviceMapEntry | |
| 134 : public base::RefCounted<ScopedMTPDeviceMapEntry>, | |
| 135 public base::SupportsWeakPtr<ScopedMTPDeviceMapEntry> { | |
| 136 public: | 133 public: |
| 137 // |no_references_callback| is called when the last ScopedMTPDeviceMapEntry | 134 explicit ScopedMTPDeviceMapEntry(const FilePath::StringType& device_location) |
| 138 // reference goes away. | 135 : device_location_(device_location) { |
| 139 ScopedMTPDeviceMapEntry(const FilePath::StringType& device_location, | |
| 140 const base::Closure& no_references_callback) | |
| 141 : device_location_(device_location), | |
| 142 no_references_callback_(no_references_callback) { | |
| 143 BrowserThread::PostTask( | 136 BrowserThread::PostTask( |
| 144 BrowserThread::IO, FROM_HERE, | 137 BrowserThread::IO, FROM_HERE, |
| 145 Bind(&MTPDeviceMapService::AddDelegate, | 138 Bind(&MTPDeviceMapService::AddDelegate, |
| 146 base::Unretained(MTPDeviceMapService::GetInstance()), | 139 base::Unretained(MTPDeviceMapService::GetInstance()), |
| 147 device_location_, | 140 device_location_, |
| 148 make_scoped_refptr(new MTPDeviceDelegateImpl(device_location)))); | 141 new MTPDeviceDelegateImpl(device_location))); |
| 149 } | 142 } |
| 150 | 143 |
| 151 private: | 144 // Destructed when the last user of this MTP device is destroyed or when the |
| 152 // Friend declaration for ref counted implementation. | 145 // MTP device is detached from the system or when the browser is in shutdown |
| 153 friend class base::RefCounted<ScopedMTPDeviceMapEntry>; | 146 // mode or when the extension revokes the MTP device gallery permissions. |
| 154 | |
| 155 // Private because this class is ref-counted. | |
| 156 ~ScopedMTPDeviceMapEntry() { | 147 ~ScopedMTPDeviceMapEntry() { |
| 157 BrowserThread::PostTask( | 148 BrowserThread::PostTask( |
| 158 BrowserThread::IO, FROM_HERE, | 149 BrowserThread::IO, FROM_HERE, |
| 159 Bind(&MTPDeviceMapService::RemoveDelegate, | 150 Bind(&MTPDeviceMapService::RemoveDelegate, |
| 160 base::Unretained(MTPDeviceMapService::GetInstance()), | 151 base::Unretained(MTPDeviceMapService::GetInstance()), |
| 161 device_location_)); | 152 device_location_)); |
| 162 no_references_callback_.Run(); | |
| 163 } | 153 } |
| 164 | 154 |
| 165 // Store the MTP or PTP device location. | 155 private: |
| 156 // The MTP or PTP device location. |
| 166 const FilePath::StringType device_location_; | 157 const FilePath::StringType device_location_; |
| 167 | 158 |
| 168 // A callback to call when the last reference of this object goes away. | |
| 169 base::Closure no_references_callback_; | |
| 170 | |
| 171 DISALLOW_COPY_AND_ASSIGN(ScopedMTPDeviceMapEntry); | 159 DISALLOW_COPY_AND_ASSIGN(ScopedMTPDeviceMapEntry); |
| 172 }; | 160 }; |
| 173 #endif | 161 #endif |
| 174 | 162 |
| 175 // The main owner of this class is | 163 // The main owner of this class is |
| 176 // |MediaFileSystemRegistry::extension_hosts_map_|, but a callback may | 164 // |MediaFileSystemRegistry::extension_hosts_map_|, but a callback may |
| 177 // temporarily hold a reference. | 165 // temporarily hold a reference. |
| 178 class ExtensionGalleriesHost | 166 class ExtensionGalleriesHost |
| 179 : public base::RefCountedThreadSafe<ExtensionGalleriesHost>, | 167 : public base::RefCountedThreadSafe<ExtensionGalleriesHost>, |
| 180 public content::NotificationObserver { | 168 public content::NotificationObserver { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 // Revoke the file system for |id| if this extension has created one for |id|. | 221 // Revoke the file system for |id| if this extension has created one for |id|. |
| 234 void RevokeGalleryByPrefId(MediaGalleryPrefId id) { | 222 void RevokeGalleryByPrefId(MediaGalleryPrefId id) { |
| 235 PrefIdFsInfoMap::iterator gallery = pref_id_map_.find(id); | 223 PrefIdFsInfoMap::iterator gallery = pref_id_map_.find(id); |
| 236 if (gallery == pref_id_map_.end()) | 224 if (gallery == pref_id_map_.end()) |
| 237 return; | 225 return; |
| 238 | 226 |
| 239 file_system_context_->RevokeFileSystem(gallery->second.fsid); | 227 file_system_context_->RevokeFileSystem(gallery->second.fsid); |
| 240 pref_id_map_.erase(gallery); | 228 pref_id_map_.erase(gallery); |
| 241 | 229 |
| 242 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 230 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 243 MediaDeviceEntryReferencesMap::iterator mtp_device_host = | 231 for (MTPDeviceGalleryReferencesMap::iterator it = |
| 244 media_device_map_references_.find(id); | 232 mtp_device_gallery_references_.begin(); |
| 245 if (mtp_device_host != media_device_map_references_.end()) | 233 it != mtp_device_gallery_references_.end(); ++it) { |
| 246 media_device_map_references_.erase(mtp_device_host); | 234 MediaGalleryPrefIdSet::iterator found = it->second.find(id); |
| 235 if (found == it->second.end()) |
| 236 continue; |
| 237 it->second.erase(id); |
| 238 if (it->second.empty()) { |
| 239 file_system_context_->RemoveMTPDeviceReferenceForHost(it->first, this); |
| 240 mtp_device_gallery_references_.erase(it); |
| 241 } |
| 242 break; |
| 243 } |
| 247 #endif | 244 #endif |
| 248 } | 245 } |
| 249 | 246 |
| 250 // Indicate that the passed |rvh| will reference the file system ids created | 247 // Indicate that the passed |rvh| will reference the file system ids created |
| 251 // by this class. It is safe to call this multiple times with the same RVH. | 248 // by this class. It is safe to call this multiple times with the same RVH. |
| 252 void ReferenceFromRVH(const content::RenderViewHost* rvh) { | 249 void ReferenceFromRVH(const content::RenderViewHost* rvh) { |
| 253 WebContents* contents = WebContents::FromRenderViewHost(rvh); | 250 WebContents* contents = WebContents::FromRenderViewHost(rvh); |
| 254 if (registrar_.IsRegistered(this, | 251 if (registrar_.IsRegistered(this, |
| 255 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 252 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 256 content::Source<WebContents>(contents))) { | 253 content::Source<WebContents>(contents))) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 267 if (rph_refs_[rph].size() == 1) { | 264 if (rph_refs_[rph].size() == 1) { |
| 268 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 265 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 269 content::Source<RenderProcessHost>(rph)); | 266 content::Source<RenderProcessHost>(rph)); |
| 270 } | 267 } |
| 271 } | 268 } |
| 272 | 269 |
| 273 private: | 270 private: |
| 274 typedef std::map<MediaGalleryPrefId, MediaFileSystemInfo> | 271 typedef std::map<MediaGalleryPrefId, MediaFileSystemInfo> |
| 275 PrefIdFsInfoMap; | 272 PrefIdFsInfoMap; |
| 276 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 273 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 277 typedef std::map<MediaGalleryPrefId, | 274 typedef std::map<const FilePath::StringType, MediaGalleryPrefIdSet> |
| 278 scoped_refptr<ScopedMTPDeviceMapEntry> > | 275 MTPDeviceGalleryReferencesMap; |
| 279 MediaDeviceEntryReferencesMap; | |
| 280 #endif | 276 #endif |
| 281 typedef std::map<const RenderProcessHost*, std::set<const WebContents*> > | 277 typedef std::map<const RenderProcessHost*, std::set<const WebContents*> > |
| 282 RenderProcessHostRefCount; | 278 RenderProcessHostRefCount; |
| 283 | 279 |
| 284 // Private destructor and friend declaration for ref counted implementation. | 280 // Private destructor and friend declaration for ref counted implementation. |
| 285 friend class base::RefCountedThreadSafe<ExtensionGalleriesHost>; | 281 friend class base::RefCountedThreadSafe<ExtensionGalleriesHost>; |
| 286 | 282 |
| 287 virtual ~ExtensionGalleriesHost() { | 283 virtual ~ExtensionGalleriesHost() { |
| 288 DCHECK(rph_refs_.empty()); | 284 DCHECK(rph_refs_.empty()); |
| 289 DCHECK(pref_id_map_.empty()); | 285 DCHECK(pref_id_map_.empty()); |
| 290 | 286 |
| 291 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 287 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 292 DCHECK(media_device_map_references_.empty()); | 288 DCHECK(mtp_device_gallery_references_.empty()); |
| 293 #endif | 289 #endif |
| 294 } | 290 } |
| 295 | 291 |
| 296 // NotificationObserver implementation. | 292 // NotificationObserver implementation. |
| 297 virtual void Observe(int type, | 293 virtual void Observe(int type, |
| 298 const content::NotificationSource& source, | 294 const content::NotificationSource& source, |
| 299 const content::NotificationDetails& details) OVERRIDE { | 295 const content::NotificationDetails& details) OVERRIDE { |
| 300 switch (type) { | 296 switch (type) { |
| 301 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | 297 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 302 OnRendererProcessClosed( | 298 OnRendererProcessClosed( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 FilePath path = gallery_info.AbsolutePath(); | 347 FilePath path = gallery_info.AbsolutePath(); |
| 352 if (!path.IsAbsolute()) | 348 if (!path.IsAbsolute()) |
| 353 continue; | 349 continue; |
| 354 | 350 |
| 355 std::string fsid; | 351 std::string fsid; |
| 356 if (MediaStorageUtil::IsMassStorageDevice(device_id)) { | 352 if (MediaStorageUtil::IsMassStorageDevice(device_id)) { |
| 357 fsid = file_system_context_->RegisterFileSystemForMassStorage( | 353 fsid = file_system_context_->RegisterFileSystemForMassStorage( |
| 358 device_id, path); | 354 device_id, path); |
| 359 } else { | 355 } else { |
| 360 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 356 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 361 scoped_refptr<ScopedMTPDeviceMapEntry> mtp_device_host; | |
| 362 fsid = file_system_context_->RegisterFileSystemForMTPDevice( | 357 fsid = file_system_context_->RegisterFileSystemForMTPDevice( |
| 363 device_id, path, &mtp_device_host); | 358 device_id, path, this); |
| 364 DCHECK(mtp_device_host.get()); | 359 mtp_device_gallery_references_[path.value()].insert(pref_id); |
| 365 media_device_map_references_[pref_id] = mtp_device_host; | |
| 366 #else | 360 #else |
| 367 NOTIMPLEMENTED(); | 361 NOTIMPLEMENTED(); |
| 368 continue; | 362 continue; |
| 369 #endif | 363 #endif |
| 370 } | 364 } |
| 371 DCHECK(!fsid.empty()); | 365 DCHECK(!fsid.empty()); |
| 372 | 366 |
| 373 MediaFileSystemInfo new_entry( | 367 MediaFileSystemInfo new_entry( |
| 374 MakeJSONFileSystemName(gallery_info.display_name, | 368 MakeJSONFileSystemName(gallery_info.display_name, |
| 375 pref_id, | 369 pref_id, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 rph_refs_.erase(process_refs); | 413 rph_refs_.erase(process_refs); |
| 420 } | 414 } |
| 421 | 415 |
| 422 if (rph_refs_.empty()) { | 416 if (rph_refs_.empty()) { |
| 423 for (PrefIdFsInfoMap::const_iterator it = pref_id_map_.begin(); | 417 for (PrefIdFsInfoMap::const_iterator it = pref_id_map_.begin(); |
| 424 it != pref_id_map_.end(); | 418 it != pref_id_map_.end(); |
| 425 ++it) { | 419 ++it) { |
| 426 file_system_context_->RevokeFileSystem(it->second.fsid); | 420 file_system_context_->RevokeFileSystem(it->second.fsid); |
| 427 } | 421 } |
| 428 pref_id_map_.clear(); | 422 pref_id_map_.clear(); |
| 429 | |
| 430 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 423 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 431 media_device_map_references_.clear(); | 424 for (MTPDeviceGalleryReferencesMap::iterator it = |
| 425 mtp_device_gallery_references_.begin(); |
| 426 it != mtp_device_gallery_references_.end(); ++it) { |
| 427 file_system_context_->RemoveMTPDeviceReferenceForHost(it->first, this); |
| 428 } |
| 429 mtp_device_gallery_references_.clear(); |
| 432 #endif | 430 #endif |
| 433 | |
| 434 no_references_callback_.Run(); | 431 no_references_callback_.Run(); |
| 435 } | 432 } |
| 436 } | 433 } |
| 437 | 434 |
| 438 // MediaFileSystemRegistry owns |this| and |file_system_context_|, so it's | 435 // MediaFileSystemRegistry owns |this| and |file_system_context_|, so it's |
| 439 // safe to store a raw pointer. | 436 // safe to store a raw pointer. |
| 440 MediaFileSystemContext* file_system_context_; | 437 MediaFileSystemContext* file_system_context_; |
| 441 | 438 |
| 442 // A callback to call when the last RVH reference goes away. | 439 // A callback to call when the last RVH reference goes away. |
| 443 base::Closure no_references_callback_; | 440 base::Closure no_references_callback_; |
| 444 | 441 |
| 445 // A map from the gallery preferences id to the file system information. | 442 // A map from the gallery preferences id to the file system information. |
| 446 PrefIdFsInfoMap pref_id_map_; | 443 PrefIdFsInfoMap pref_id_map_; |
| 447 | 444 |
| 448 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 445 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 449 // A map from the gallery preferences id to the corresponding media device | 446 // A map from the MTP device id to a set of media galleries that references to |
| 450 // host object. | 447 // that device. |
| 451 MediaDeviceEntryReferencesMap media_device_map_references_; | 448 MTPDeviceGalleryReferencesMap mtp_device_gallery_references_; |
| 452 #endif | 449 #endif |
| 453 | 450 |
| 454 // The set of render processes and web contents that may have references to | 451 // The set of render processes and web contents that may have references to |
| 455 // the file system ids this instance manages. | 452 // the file system ids this instance manages. |
| 456 RenderProcessHostRefCount rph_refs_; | 453 RenderProcessHostRefCount rph_refs_; |
| 457 | 454 |
| 458 // A registrar for listening notifications. | 455 // A registrar for listening notifications. |
| 459 content::NotificationRegistrar registrar_; | 456 content::NotificationRegistrar registrar_; |
| 460 | 457 |
| 461 DISALLOW_COPY_AND_ASSIGN(ExtensionGalleriesHost); | 458 DISALLOW_COPY_AND_ASSIGN(ExtensionGalleriesHost); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 existing_devices[i].name, FilePath(), | 526 existing_devices[i].name, FilePath(), |
| 530 false /*not user added*/); | 527 false /*not user added*/); |
| 531 } | 528 } |
| 532 return preferences; | 529 return preferences; |
| 533 } | 530 } |
| 534 | 531 |
| 535 void MediaFileSystemRegistry::OnRemovableStorageAttached( | 532 void MediaFileSystemRegistry::OnRemovableStorageAttached( |
| 536 const std::string& id, const string16& name, | 533 const std::string& id, const string16& name, |
| 537 const FilePath::StringType& location) { | 534 const FilePath::StringType& location) { |
| 538 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 535 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 539 | |
| 540 if (!MediaStorageUtil::IsMediaDevice(id)) | 536 if (!MediaStorageUtil::IsMediaDevice(id)) |
| 541 return; | 537 return; |
| 542 | 538 |
| 543 for (ExtensionGalleriesHostMap::iterator profile_it = | 539 for (ExtensionGalleriesHostMap::iterator profile_it = |
| 544 extension_hosts_map_.begin(); | 540 extension_hosts_map_.begin(); |
| 545 profile_it != extension_hosts_map_.end(); | 541 profile_it != extension_hosts_map_.end(); |
| 546 ++profile_it) { | 542 ++profile_it) { |
| 547 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); | 543 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); |
| 548 preferences->AddGallery(id, name, FilePath(), false /*not user added*/); | 544 preferences->AddGallery(id, name, FilePath(), false /*not user added*/); |
| 549 } | 545 } |
| 550 } | 546 } |
| 551 | 547 |
| 552 void MediaFileSystemRegistry::OnRemovableStorageDetached( | 548 void MediaFileSystemRegistry::OnRemovableStorageDetached( |
| 553 const std::string& id) { | 549 const std::string& id) { |
| 554 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 550 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 555 | |
| 556 // Since revoking a gallery in the ExtensionGalleriesHost may cause it | 551 // Since revoking a gallery in the ExtensionGalleriesHost may cause it |
| 557 // to be removed from the map and therefore invalidate any iterator pointing | 552 // to be removed from the map and therefore invalidate any iterator pointing |
| 558 // to it, this code first copies all the invalid gallery ids and the | 553 // to it, this code first copies all the invalid gallery ids and the |
| 559 // extension hosts in which they may appear (per profile) and revoked it in | 554 // extension hosts in which they may appear (per profile) and revoked it in |
| 560 // a second step. | 555 // a second step. |
| 561 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; | 556 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; |
| 562 | 557 |
| 563 for (ExtensionGalleriesHostMap::iterator profile_it = | 558 for (ExtensionGalleriesHostMap::iterator profile_it = |
| 564 extension_hosts_map_.begin(); | 559 extension_hosts_map_.begin(); |
| 565 profile_it != extension_hosts_map_.end(); | 560 profile_it != extension_hosts_map_.end(); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 const std::string fsid = | 617 const std::string fsid = |
| 623 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 618 IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
| 624 fileapi::kFileSystemTypeNativeMedia, path, &fs_name); | 619 fileapi::kFileSystemTypeNativeMedia, path, &fs_name); |
| 625 CHECK(!fsid.empty()); | 620 CHECK(!fsid.empty()); |
| 626 return fsid; | 621 return fsid; |
| 627 } | 622 } |
| 628 | 623 |
| 629 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 624 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 630 virtual std::string RegisterFileSystemForMTPDevice( | 625 virtual std::string RegisterFileSystemForMTPDevice( |
| 631 const std::string& device_id, const FilePath& path, | 626 const std::string& device_id, const FilePath& path, |
| 632 scoped_refptr<ScopedMTPDeviceMapEntry>* entry) { | 627 const ExtensionGalleriesHost* galleries_host) { |
| 633 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 628 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 634 DCHECK(!MediaStorageUtil::IsMassStorageDevice(device_id)); | 629 DCHECK(!MediaStorageUtil::IsMassStorageDevice(device_id)); |
| 635 | 630 |
| 636 // Sanity checks for |path|. | 631 // Sanity checks for |path|. |
| 637 CHECK(path.IsAbsolute()); | 632 CHECK(path.IsAbsolute()); |
| 638 CHECK(!path.ReferencesParent()); | 633 CHECK(!path.ReferencesParent()); |
| 639 std::string fs_name(extension_misc::kMediaFileSystemPathPart); | 634 std::string fs_name(extension_misc::kMediaFileSystemPathPart); |
| 640 const std::string fsid = | 635 const std::string fsid = |
| 641 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 636 IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
| 642 fileapi::kFileSystemTypeDeviceMedia, path, &fs_name); | 637 fileapi::kFileSystemTypeDeviceMedia, path, &fs_name); |
| 643 CHECK(!fsid.empty()); | 638 CHECK(!fsid.empty()); |
| 644 DCHECK(entry); | 639 registry_->AddGalleriesHostReferenceForMTPDevice(path.value(), |
| 645 *entry = registry_->GetOrCreateScopedMTPDeviceMapEntry(path.value()); | 640 galleries_host); |
| 646 return fsid; | 641 return fsid; |
| 647 } | 642 } |
| 643 |
| 644 virtual void RemoveMTPDeviceReferenceForHost( |
| 645 const FilePath::StringType& device_location, |
| 646 const ExtensionGalleriesHost* galleries_host) { |
| 647 registry_->RemoveGalleriesHostReferenceForMTPDevice(device_location, |
| 648 galleries_host); |
| 649 } |
| 648 #endif | 650 #endif |
| 649 | 651 |
| 650 virtual void RevokeFileSystem(const std::string& fsid) { | 652 virtual void RevokeFileSystem(const std::string& fsid) { |
| 651 IsolatedContext::GetInstance()->RevokeFileSystem(fsid); | 653 IsolatedContext::GetInstance()->RevokeFileSystem(fsid); |
| 652 } | 654 } |
| 653 | 655 |
| 654 private: | 656 private: |
| 655 MediaFileSystemRegistry* registry_; | 657 MediaFileSystemRegistry* registry_; |
| 656 | 658 |
| 657 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); | 659 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); |
| 658 }; | 660 }; |
| 659 | 661 |
| 660 MediaFileSystemRegistry::MediaFileSystemRegistry() | 662 MediaFileSystemRegistry::MediaFileSystemRegistry() |
| 661 : file_system_context_(new MediaFileSystemContextImpl(this)) { | 663 : file_system_context_(new MediaFileSystemContextImpl(this)) { |
| 662 // SystemMonitor may be NULL in unit tests. | 664 // SystemMonitor may be NULL in unit tests. |
| 663 SystemMonitor* system_monitor = SystemMonitor::Get(); | 665 SystemMonitor* system_monitor = SystemMonitor::Get(); |
| 664 if (system_monitor) | 666 if (system_monitor) |
| 665 system_monitor->AddDevicesChangedObserver(this); | 667 system_monitor->AddDevicesChangedObserver(this); |
| 666 } | 668 } |
| 667 | 669 |
| 668 MediaFileSystemRegistry::~MediaFileSystemRegistry() { | 670 MediaFileSystemRegistry::~MediaFileSystemRegistry() { |
| 669 // SystemMonitor may be NULL in unit tests. | 671 // SystemMonitor may be NULL in unit tests. |
| 670 SystemMonitor* system_monitor = SystemMonitor::Get(); | 672 SystemMonitor* system_monitor = SystemMonitor::Get(); |
| 671 if (system_monitor) | 673 if (system_monitor) |
| 672 system_monitor->RemoveDevicesChangedObserver(this); | 674 system_monitor->RemoveDevicesChangedObserver(this); |
| 675 DCHECK(mtp_device_delegate_map_.empty()); |
| 676 DCHECK(mtp_device_references_map_.empty()); |
| 673 } | 677 } |
| 674 | 678 |
| 675 void MediaFileSystemRegistry::OnPreferenceChanged( | 679 void MediaFileSystemRegistry::OnPreferenceChanged( |
| 676 PrefServiceBase* prefs, | 680 PrefServiceBase* prefs, |
| 677 const std::string& pref_name) { | 681 const std::string& pref_name) { |
| 678 DCHECK_EQ(std::string(prefs::kMediaGalleriesRememberedGalleries), pref_name); | 682 DCHECK_EQ(std::string(prefs::kMediaGalleriesRememberedGalleries), pref_name); |
| 679 | 683 |
| 680 // Find the Profile that contains the source PrefService. | 684 // Find the Profile that contains the source PrefService. |
| 681 PrefChangeRegistrarMap::iterator pref_change_it = | 685 PrefChangeRegistrarMap::iterator pref_change_it = |
| 682 pref_change_registrar_map_.begin(); | 686 pref_change_registrar_map_.begin(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 705 gallery_host_it != extension_host_map.end(); | 709 gallery_host_it != extension_host_map.end(); |
| 706 ++gallery_host_it) { | 710 ++gallery_host_it) { |
| 707 const extensions::Extension* extension = | 711 const extensions::Extension* extension = |
| 708 extensions_set->GetByID(gallery_host_it->first); | 712 extensions_set->GetByID(gallery_host_it->first); |
| 709 gallery_host_it->second->RevokeOldGalleries( | 713 gallery_host_it->second->RevokeOldGalleries( |
| 710 preferences->GalleriesForExtension(*extension)); | 714 preferences->GalleriesForExtension(*extension)); |
| 711 } | 715 } |
| 712 } | 716 } |
| 713 | 717 |
| 714 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 718 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 715 ScopedMTPDeviceMapEntry* | 719 void MediaFileSystemRegistry::AddGalleriesHostReferenceForMTPDevice( |
| 716 MediaFileSystemRegistry::GetOrCreateScopedMTPDeviceMapEntry( | 720 const FilePath::StringType& device_location, |
| 717 const FilePath::StringType& device_location) { | 721 const ExtensionGalleriesHost* galleries_host) { |
| 718 MTPDeviceDelegateMap::iterator delegate_it = | 722 MTPDeviceDelegateMap::iterator delegate_it = |
| 719 mtp_delegate_map_.find(device_location); | 723 mtp_device_delegate_map_.find(device_location); |
| 720 if (delegate_it != mtp_delegate_map_.end() && delegate_it->second.get()) | 724 if (delegate_it == mtp_device_delegate_map_.end()) { |
| 721 return delegate_it->second; | 725 mtp_device_delegate_map_[device_location] = |
| 722 ScopedMTPDeviceMapEntry* mtp_device_host = new ScopedMTPDeviceMapEntry( | 726 new ScopedMTPDeviceMapEntry(device_location); |
| 723 device_location, base::Bind( | 727 } |
| 724 &MediaFileSystemRegistry::RemoveScopedMTPDeviceMapEntry, | 728 mtp_device_references_map_[device_location].insert(galleries_host); |
| 725 base::Unretained(this), device_location)); | |
| 726 mtp_delegate_map_[device_location] = mtp_device_host->AsWeakPtr(); | |
| 727 return mtp_device_host; | |
| 728 } | 729 } |
| 729 | 730 |
| 730 void MediaFileSystemRegistry::RemoveScopedMTPDeviceMapEntry( | 731 void MediaFileSystemRegistry::RemoveGalleriesHostReferenceForMTPDevice( |
| 731 const FilePath::StringType& device_location) { | 732 const FilePath::StringType& device_location, |
| 732 MTPDeviceDelegateMap::iterator delegate_it = | 733 const ExtensionGalleriesHost* galleries_host) { |
| 733 mtp_delegate_map_.find(device_location); | 734 MTPDeviceReferencesMap::iterator hosts_it = |
| 734 DCHECK(delegate_it != mtp_delegate_map_.end()); | 735 mtp_device_references_map_.find(device_location); |
| 735 mtp_delegate_map_.erase(delegate_it); | 736 DCHECK(hosts_it != mtp_device_references_map_.end()); |
| 737 DCHECK(hosts_it->second.find(galleries_host) != hosts_it->second.end()); |
| 738 hosts_it->second.erase(galleries_host); |
| 739 if (hosts_it->second.empty()) { |
| 740 delete mtp_device_delegate_map_[device_location]; |
| 741 mtp_device_delegate_map_.erase(device_location); |
| 742 mtp_device_references_map_.erase(hosts_it); |
| 743 } |
| 736 } | 744 } |
| 737 #endif | 745 #endif |
| 738 | 746 |
| 739 void MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty( | 747 void MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty( |
| 740 Profile* profile, const std::string& extension_id) { | 748 Profile* profile, const std::string& extension_id) { |
| 741 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 749 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 742 | 750 |
| 743 ExtensionGalleriesHostMap::iterator extension_hosts = | 751 ExtensionGalleriesHostMap::iterator extension_hosts = |
| 744 extension_hosts_map_.find(profile); | 752 extension_hosts_map_.find(profile); |
| 745 DCHECK(extension_hosts != extension_hosts_map_.end()); | 753 DCHECK(extension_hosts != extension_hosts_map_.end()); |
| 746 ExtensionHostMap::size_type erase_count = | 754 ExtensionHostMap::size_type erase_count = |
| 747 extension_hosts->second.erase(extension_id); | 755 extension_hosts->second.erase(extension_id); |
| 748 DCHECK_EQ(1U, erase_count); | 756 DCHECK_EQ(1U, erase_count); |
| 749 if (extension_hosts->second.empty()) { | 757 if (extension_hosts->second.empty()) { |
| 750 extension_hosts_map_.erase(extension_hosts); | 758 extension_hosts_map_.erase(extension_hosts); |
| 751 | 759 |
| 752 PrefChangeRegistrarMap::iterator pref_it = | 760 PrefChangeRegistrarMap::iterator pref_it = |
| 753 pref_change_registrar_map_.find(profile); | 761 pref_change_registrar_map_.find(profile); |
| 754 DCHECK(pref_it != pref_change_registrar_map_.end()); | 762 DCHECK(pref_it != pref_change_registrar_map_.end()); |
| 755 delete pref_it->second; | 763 delete pref_it->second; |
| 756 pref_change_registrar_map_.erase(pref_it); | 764 pref_change_registrar_map_.erase(pref_it); |
| 757 } | 765 } |
| 758 } | 766 } |
| 759 | 767 |
| 760 } // namespace chrome | 768 } // namespace chrome |
| OLD | NEW |