| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // back informs the caller. | 84 // back informs the caller. |
| 85 class RPHReferenceManager { | 85 class RPHReferenceManager { |
| 86 public: | 86 public: |
| 87 // |no_references_callback| is called when the last WebContents reference | 87 // |no_references_callback| is called when the last WebContents reference |
| 88 // goes away. WebContents references are added through | 88 // goes away. WebContents references are added through |
| 89 // ReferenceFromWebContents(). | 89 // ReferenceFromWebContents(). |
| 90 explicit RPHReferenceManager(const base::Closure& no_references_callback); | 90 explicit RPHReferenceManager(const base::Closure& no_references_callback); |
| 91 virtual ~RPHReferenceManager(); | 91 virtual ~RPHReferenceManager(); |
| 92 | 92 |
| 93 // Remove all references, but don't call |no_references_callback|. | 93 // Remove all references, but don't call |no_references_callback|. |
| 94 void Reset() { STLDeleteValues(&observer_map_); } | 94 void Reset() { base::STLDeleteValues(&observer_map_); } |
| 95 | 95 |
| 96 // Returns true if there are no references; | 96 // Returns true if there are no references; |
| 97 bool empty() const { return observer_map_.empty(); } | 97 bool empty() const { return observer_map_.empty(); } |
| 98 | 98 |
| 99 // Adds a reference to the passed |contents|. Calling this multiple times with | 99 // Adds a reference to the passed |contents|. Calling this multiple times with |
| 100 // the same |contents| is a no-op. | 100 // the same |contents| is a no-op. |
| 101 void ReferenceFromWebContents(content::WebContents* contents); | 101 void ReferenceFromWebContents(content::WebContents* contents); |
| 102 | 102 |
| 103 private: | 103 private: |
| 104 class RPHWebContentsObserver : public content::WebContentsObserver { | 104 class RPHWebContentsObserver : public content::WebContentsObserver { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 } | 154 } |
| 155 | 155 |
| 156 RPHReferenceManager::~RPHReferenceManager() { | 156 RPHReferenceManager::~RPHReferenceManager() { |
| 157 Reset(); | 157 Reset(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void RPHReferenceManager::ReferenceFromWebContents( | 160 void RPHReferenceManager::ReferenceFromWebContents( |
| 161 content::WebContents* contents) { | 161 content::WebContents* contents) { |
| 162 RenderProcessHost* rph = contents->GetRenderProcessHost(); | 162 RenderProcessHost* rph = contents->GetRenderProcessHost(); |
| 163 RPHObserver* state = NULL; | 163 RPHObserver* state = NULL; |
| 164 if (!ContainsKey(observer_map_, rph)) { | 164 if (!base::ContainsKey(observer_map_, rph)) { |
| 165 state = new RPHObserver(this, rph); | 165 state = new RPHObserver(this, rph); |
| 166 observer_map_[rph] = state; | 166 observer_map_[rph] = state; |
| 167 } else { | 167 } else { |
| 168 state = observer_map_[rph]; | 168 state = observer_map_[rph]; |
| 169 } | 169 } |
| 170 | 170 |
| 171 state->AddWebContentsObserver(contents); | 171 state->AddWebContentsObserver(contents); |
| 172 } | 172 } |
| 173 | 173 |
| 174 RPHReferenceManager::RPHWebContentsObserver::RPHWebContentsObserver( | 174 RPHReferenceManager::RPHWebContentsObserver::RPHWebContentsObserver( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 191 } | 191 } |
| 192 | 192 |
| 193 RPHReferenceManager::RPHObserver::RPHObserver( | 193 RPHReferenceManager::RPHObserver::RPHObserver( |
| 194 RPHReferenceManager* manager, RenderProcessHost* host) | 194 RPHReferenceManager* manager, RenderProcessHost* host) |
| 195 : manager_(manager), | 195 : manager_(manager), |
| 196 host_(host) { | 196 host_(host) { |
| 197 host->AddObserver(this); | 197 host->AddObserver(this); |
| 198 } | 198 } |
| 199 | 199 |
| 200 RPHReferenceManager::RPHObserver::~RPHObserver() { | 200 RPHReferenceManager::RPHObserver::~RPHObserver() { |
| 201 STLDeleteValues(&observed_web_contentses_); | 201 base::STLDeleteValues(&observed_web_contentses_); |
| 202 if (host_) | 202 if (host_) |
| 203 host_->RemoveObserver(this); | 203 host_->RemoveObserver(this); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void RPHReferenceManager::RPHObserver::AddWebContentsObserver( | 206 void RPHReferenceManager::RPHObserver::AddWebContentsObserver( |
| 207 WebContents* web_contents) { | 207 WebContents* web_contents) { |
| 208 if (ContainsKey(observed_web_contentses_, web_contents)) | 208 if (base::ContainsKey(observed_web_contentses_, web_contents)) |
| 209 return; | 209 return; |
| 210 | 210 |
| 211 RPHWebContentsObserver* observer = | 211 RPHWebContentsObserver* observer = |
| 212 new RPHWebContentsObserver(manager_, web_contents); | 212 new RPHWebContentsObserver(manager_, web_contents); |
| 213 observed_web_contentses_[web_contents] = observer; | 213 observed_web_contentses_[web_contents] = observer; |
| 214 } | 214 } |
| 215 | 215 |
| 216 void RPHReferenceManager::RPHObserver::RemoveWebContentsObserver( | 216 void RPHReferenceManager::RPHObserver::RemoveWebContentsObserver( |
| 217 WebContents* web_contents) { | 217 WebContents* web_contents) { |
| 218 WCObserverMap::iterator wco_iter = | 218 WCObserverMap::iterator wco_iter = |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 } | 384 } |
| 385 | 385 |
| 386 for (std::set<MediaGalleryPrefId>::const_iterator pref_id_it = | 386 for (std::set<MediaGalleryPrefId>::const_iterator pref_id_it = |
| 387 galleries.begin(); | 387 galleries.begin(); |
| 388 pref_id_it != galleries.end(); | 388 pref_id_it != galleries.end(); |
| 389 ++pref_id_it) { | 389 ++pref_id_it) { |
| 390 const MediaGalleryPrefId& pref_id = *pref_id_it; | 390 const MediaGalleryPrefId& pref_id = *pref_id_it; |
| 391 const MediaGalleryPrefInfo& gallery_info = | 391 const MediaGalleryPrefInfo& gallery_info = |
| 392 galleries_info.find(pref_id)->second; | 392 galleries_info.find(pref_id)->second; |
| 393 const std::string& device_id = gallery_info.device_id; | 393 const std::string& device_id = gallery_info.device_id; |
| 394 if (!ContainsKey(*attached_devices, device_id)) | 394 if (!base::ContainsKey(*attached_devices, device_id)) |
| 395 continue; | 395 continue; |
| 396 | 396 |
| 397 PrefIdFsInfoMap::const_iterator existing_info = | 397 PrefIdFsInfoMap::const_iterator existing_info = |
| 398 pref_id_map_.find(pref_id); | 398 pref_id_map_.find(pref_id); |
| 399 if (existing_info != pref_id_map_.end()) { | 399 if (existing_info != pref_id_map_.end()) { |
| 400 result.push_back(existing_info->second); | 400 result.push_back(existing_info->second); |
| 401 continue; | 401 continue; |
| 402 } | 402 } |
| 403 | 403 |
| 404 base::FilePath path = gallery_info.AbsolutePath(); | 404 base::FilePath path = gallery_info.AbsolutePath(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 | 439 |
| 440 // If rph_refs is empty then we're actually in the middle of shutdown, and | 440 // If rph_refs is empty then we're actually in the middle of shutdown, and |
| 441 // Filter...() lagging which can invoke this method interleaved in the | 441 // Filter...() lagging which can invoke this method interleaved in the |
| 442 // destruction callback sequence and re-populate pref_id_map_. | 442 // destruction callback sequence and re-populate pref_id_map_. |
| 443 if (!attached_device->empty() && !rph_refs_.empty()) { | 443 if (!attached_device->empty() && !rph_refs_.empty()) { |
| 444 std::string fs_name = MediaFileSystemBackend::ConstructMountName( | 444 std::string fs_name = MediaFileSystemBackend::ConstructMountName( |
| 445 profile_path_, extension_id_, gallery.pref_id); | 445 profile_path_, extension_id_, gallery.pref_id); |
| 446 base::FilePath path = gallery.AbsolutePath(); | 446 base::FilePath path = gallery.AbsolutePath(); |
| 447 const std::string& device_id = gallery.device_id; | 447 const std::string& device_id = gallery.device_id; |
| 448 | 448 |
| 449 if (ContainsKey(pref_id_map_, gallery.pref_id)) { | 449 if (base::ContainsKey(pref_id_map_, gallery.pref_id)) { |
| 450 result = base::File::FILE_OK; | 450 result = base::File::FILE_OK; |
| 451 } else if (MediaStorageUtil::CanCreateFileSystem(device_id, path) && | 451 } else if (MediaStorageUtil::CanCreateFileSystem(device_id, path) && |
| 452 file_system_context_->RegisterFileSystem(device_id, fs_name, | 452 file_system_context_->RegisterFileSystem(device_id, fs_name, |
| 453 path)) { | 453 path)) { |
| 454 result = base::File::FILE_OK; | 454 result = base::File::FILE_OK; |
| 455 pref_id_map_[gallery.pref_id] = MediaFileSystemInfo( | 455 pref_id_map_[gallery.pref_id] = MediaFileSystemInfo( |
| 456 gallery.GetGalleryDisplayName(), | 456 gallery.GetGalleryDisplayName(), |
| 457 file_system_context_->GetRegisteredPath(fs_name), | 457 file_system_context_->GetRegisteredPath(fs_name), |
| 458 fs_name, | 458 fs_name, |
| 459 gallery.pref_id, | 459 gallery.pref_id, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 DCHECK_NE(kInvalidMediaGalleryPrefId, pref_id); | 554 DCHECK_NE(kInvalidMediaGalleryPrefId, pref_id); |
| 555 | 555 |
| 556 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 556 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 557 MediaGalleriesPreferences* preferences = GetPreferences(profile); | 557 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
| 558 MediaGalleriesPrefInfoMap::const_iterator gallery = | 558 MediaGalleriesPrefInfoMap::const_iterator gallery = |
| 559 preferences->known_galleries().find(pref_id); | 559 preferences->known_galleries().find(pref_id); |
| 560 MediaGalleryPrefIdSet permitted_galleries = | 560 MediaGalleryPrefIdSet permitted_galleries = |
| 561 preferences->GalleriesForExtension(*extension); | 561 preferences->GalleriesForExtension(*extension); |
| 562 | 562 |
| 563 if (gallery == preferences->known_galleries().end() || | 563 if (gallery == preferences->known_galleries().end() || |
| 564 !ContainsKey(permitted_galleries, pref_id)) { | 564 !base::ContainsKey(permitted_galleries, pref_id)) { |
| 565 BrowserThread::PostTask( | 565 BrowserThread::PostTask( |
| 566 BrowserThread::IO, FROM_HERE, | 566 BrowserThread::IO, FROM_HERE, |
| 567 base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); | 567 base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); |
| 568 return; | 568 return; |
| 569 } | 569 } |
| 570 | 570 |
| 571 ExtensionGalleriesHost* extension_host = | 571 ExtensionGalleriesHost* extension_host = |
| 572 GetExtensionGalleryHost(profile, preferences, extension->id()); | 572 GetExtensionGalleryHost(profile, preferences, extension->id()); |
| 573 | 573 |
| 574 // This must come before the GetMediaFileSystems call to make sure the | 574 // This must come before the GetMediaFileSystems call to make sure the |
| 575 // contents of the context is referenced before the filesystems are retrieved. | 575 // contents of the context is referenced before the filesystems are retrieved. |
| 576 extension_host->ReferenceFromWebContents(contents); | 576 extension_host->ReferenceFromWebContents(contents); |
| 577 | 577 |
| 578 extension_host->RegisterMediaFileSystem(gallery->second, callback); | 578 extension_host->RegisterMediaFileSystem(gallery->second, callback); |
| 579 } | 579 } |
| 580 | 580 |
| 581 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( | 581 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( |
| 582 Profile* profile) { | 582 Profile* profile) { |
| 583 // Create an empty ExtensionHostMap for this profile on first initialization. | 583 // Create an empty ExtensionHostMap for this profile on first initialization. |
| 584 if (!ContainsKey(extension_hosts_map_, profile)) { | 584 if (!base::ContainsKey(extension_hosts_map_, profile)) { |
| 585 extension_hosts_map_[profile] = ExtensionHostMap(); | 585 extension_hosts_map_[profile] = ExtensionHostMap(); |
| 586 DCHECK(!ContainsKey(profile_subscription_map_, profile)); | 586 DCHECK(!base::ContainsKey(profile_subscription_map_, profile)); |
| 587 profile_subscription_map_[profile] = | 587 profile_subscription_map_[profile] = |
| 588 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( | 588 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( |
| 589 base::Bind(&MediaFileSystemRegistry::OnProfileShutdown, | 589 base::Bind(&MediaFileSystemRegistry::OnProfileShutdown, |
| 590 base::Unretained(this), profile)); | 590 base::Unretained(this), profile)); |
| 591 media_galleries::UsageCount(media_galleries::PROFILES_WITH_USAGE); | 591 media_galleries::UsageCount(media_galleries::PROFILES_WITH_USAGE); |
| 592 } | 592 } |
| 593 | 593 |
| 594 return MediaGalleriesPreferencesFactory::GetForProfile(profile); | 594 return MediaGalleriesPreferencesFactory::GetForProfile(profile); |
| 595 } | 595 } |
| 596 | 596 |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 // even delete |extension_host_map| altogether. So do this in two loops to | 802 // even delete |extension_host_map| altogether. So do this in two loops to |
| 803 // avoid using an invalidated iterator or deleted map. | 803 // avoid using an invalidated iterator or deleted map. |
| 804 std::vector<const extensions::Extension*> extensions; | 804 std::vector<const extensions::Extension*> extensions; |
| 805 for (ExtensionHostMap::const_iterator it = extension_host_map.begin(); | 805 for (ExtensionHostMap::const_iterator it = extension_host_map.begin(); |
| 806 it != extension_host_map.end(); | 806 it != extension_host_map.end(); |
| 807 ++it) { | 807 ++it) { |
| 808 extensions.push_back( | 808 extensions.push_back( |
| 809 extension_registry->enabled_extensions().GetByID(it->first)); | 809 extension_registry->enabled_extensions().GetByID(it->first)); |
| 810 } | 810 } |
| 811 for (size_t i = 0; i < extensions.size(); ++i) { | 811 for (size_t i = 0; i < extensions.size(); ++i) { |
| 812 if (!ContainsKey(extension_hosts_map_, profile)) | 812 if (!base::ContainsKey(extension_hosts_map_, profile)) |
| 813 break; | 813 break; |
| 814 ExtensionHostMap::const_iterator gallery_host_it = | 814 ExtensionHostMap::const_iterator gallery_host_it = |
| 815 extension_host_map.find(extensions[i]->id()); | 815 extension_host_map.find(extensions[i]->id()); |
| 816 if (gallery_host_it == extension_host_map.end()) | 816 if (gallery_host_it == extension_host_map.end()) |
| 817 continue; | 817 continue; |
| 818 gallery_host_it->second->RevokeGalleryByPrefId(pref_id); | 818 gallery_host_it->second->RevokeGalleryByPrefId(pref_id); |
| 819 } | 819 } |
| 820 } | 820 } |
| 821 | 821 |
| 822 ExtensionGalleriesHost* MediaFileSystemRegistry::GetExtensionGalleryHost( | 822 ExtensionGalleriesHost* MediaFileSystemRegistry::GetExtensionGalleryHost( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 870 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 871 | 871 |
| 872 auto extension_hosts_it = extension_hosts_map_.find(profile); | 872 auto extension_hosts_it = extension_hosts_map_.find(profile); |
| 873 DCHECK(extension_hosts_it != extension_hosts_map_.end()); | 873 DCHECK(extension_hosts_it != extension_hosts_map_.end()); |
| 874 extension_hosts_map_.erase(extension_hosts_it); | 874 extension_hosts_map_.erase(extension_hosts_it); |
| 875 | 875 |
| 876 auto profile_subscription_it = profile_subscription_map_.find(profile); | 876 auto profile_subscription_it = profile_subscription_map_.find(profile); |
| 877 DCHECK(profile_subscription_it != profile_subscription_map_.end()); | 877 DCHECK(profile_subscription_it != profile_subscription_map_.end()); |
| 878 profile_subscription_map_.erase(profile_subscription_it); | 878 profile_subscription_map_.erase(profile_subscription_it); |
| 879 } | 879 } |
| OLD | NEW |