Chromium Code Reviews| Index: chrome/browser/media_galleries/fileapi/mtp_device_map_service.cc |
| diff --git a/chrome/browser/media_galleries/fileapi/mtp_device_map_service.cc b/chrome/browser/media_galleries/fileapi/mtp_device_map_service.cc |
| index b85a9bb9971bc1ccaa1323f0918985b3ef268ba4..51d2ec1819f7f26b74c08cca7b21f0a24ed2d072 100644 |
| --- a/chrome/browser/media_galleries/fileapi/mtp_device_map_service.cc |
| +++ b/chrome/browser/media_galleries/fileapi/mtp_device_map_service.cc |
| @@ -26,75 +26,113 @@ MTPDeviceMapService* MTPDeviceMapService::GetInstance() { |
| void MTPDeviceMapService::RegisterMTPFileSystem( |
| const base::FilePath::StringType& device_location, |
| - const std::string& fsid) { |
| + const std::string& filesystem_id, |
| + const bool read_only) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + DCHECK(!device_location.empty()); |
| + DCHECK(!filesystem_id.empty()); |
| - if (!ContainsKey(mtp_device_usage_map_, device_location)) { |
| + const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
| + if (!ContainsKey(mtp_device_usage_map_, key)) { |
| // Note that this initializes the delegate asynchronously, but since |
| // the delegate will only be used from the IO thread, it is guaranteed |
| // to be created before use of it expects it to be there. |
| - CreateMTPDeviceAsyncDelegate(device_location, |
| + CreateMTPDeviceAsyncDelegate( |
| + device_location, read_only, |
| base::Bind(&MTPDeviceMapService::AddAsyncDelegate, |
| - base::Unretained(this), device_location)); |
| - mtp_device_usage_map_[device_location] = 0; |
| + base::Unretained(this), device_location, read_only)); |
| + mtp_device_usage_map_[key] = 0; |
| } |
| - mtp_device_usage_map_[device_location]++; |
| - mtp_device_map_[fsid] = device_location; |
| + mtp_device_usage_map_[key]++; |
| + mtp_device_map_[filesystem_id] = make_pair(device_location, read_only); |
| } |
| -void MTPDeviceMapService::RevokeMTPFileSystem(const std::string& fsid) { |
| +void MTPDeviceMapService::RevokeMTPFileSystem( |
| + const std::string& filesystem_id) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + DCHECK(!filesystem_id.empty()); |
| - MTPDeviceFileSystemMap::iterator it = mtp_device_map_.find(fsid); |
| + MTPDeviceFileSystemMap::iterator it = mtp_device_map_.find(filesystem_id); |
| if (it != mtp_device_map_.end()) { |
| - base::FilePath::StringType device_location = it->second; |
| + const base::FilePath::StringType device_location = it->second.first; |
| + const bool read_only = it->second.second; |
| + |
| mtp_device_map_.erase(it); |
| - MTPDeviceUsageMap::iterator delegate_it = |
| - mtp_device_usage_map_.find(device_location); |
| + |
| + const AsyncDelegateKey key = |
| + GetAsyncDelegateKey(device_location, read_only); |
| + MTPDeviceUsageMap::iterator delegate_it = mtp_device_usage_map_.find(key); |
| DCHECK(delegate_it != mtp_device_usage_map_.end()); |
| - mtp_device_usage_map_[device_location]--; |
| - if (mtp_device_usage_map_[device_location] == 0) { |
| + |
| + mtp_device_usage_map_[key]--; |
| + if (mtp_device_usage_map_[key] == 0) { |
| mtp_device_usage_map_.erase(delegate_it); |
| - RemoveAsyncDelegate(device_location); |
| + RemoveAsyncDelegate(device_location, read_only); |
| } |
| } |
| } |
| void MTPDeviceMapService::AddAsyncDelegate( |
| const base::FilePath::StringType& device_location, |
| + const bool read_only, |
| MTPDeviceAsyncDelegate* delegate) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| DCHECK(delegate); |
| DCHECK(!device_location.empty()); |
| - if (ContainsKey(async_delegate_map_, device_location)) |
| + |
| + const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
| + if (ContainsKey(async_delegate_map_, key)) |
| return; |
| - async_delegate_map_[device_location] = delegate; |
| + async_delegate_map_[key] = delegate; |
| } |
| void MTPDeviceMapService::RemoveAsyncDelegate( |
| - const base::FilePath::StringType& device_location) { |
| + const base::FilePath::StringType& device_location, |
| + const bool read_only) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| - AsyncDelegateMap::iterator it = async_delegate_map_.find(device_location); |
| + DCHECK(!device_location.empty()); |
| + |
| + const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
| + AsyncDelegateMap::iterator it = async_delegate_map_.find(key); |
| DCHECK(it != async_delegate_map_.end()); |
| it->second->CancelPendingTasksAndDeleteDelegate(); |
| async_delegate_map_.erase(it); |
| } |
| +MTPDeviceMapService::AsyncDelegateKey MTPDeviceMapService::GetAsyncDelegateKey( |
| + const base::FilePath::StringType& device_location, |
| + const bool read_only) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + |
| + base::FilePath::StringType key; |
| + key.append(read_only ? FILE_PATH_LITERAL("ReadOnly") |
| + : FILE_PATH_LITERAL("ReadWrite")); |
| + key.append(FILE_PATH_LITERAL("|")); |
| + key.append(device_location); |
| + return key; |
| +} |
| + |
| MTPDeviceAsyncDelegate* MTPDeviceMapService::GetMTPDeviceAsyncDelegate( |
| const std::string& filesystem_id) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| - base::FilePath device_path; |
| - if (!storage::ExternalMountPoints::GetSystemInstance()->GetRegisteredPath( |
|
Lei Zhang
2015/03/02 23:41:38
I can't remember why we do this. I'll need to dig
Lei Zhang
2015/03/03 00:16:34
I suspect the reason for this is:
MediaFileSystem
yawano
2015/03/03 09:16:54
Done. Yes, we needed the check. I added it. Thank
|
| - filesystem_id, &device_path)) { |
| + DCHECK(!filesystem_id.empty()); |
| + |
| + MTPDeviceFileSystemMap::const_iterator mtp_device_map_it = |
| + mtp_device_map_.find(filesystem_id); |
| + if (mtp_device_map_it == mtp_device_map_.end()) |
| return NULL; |
| - } |
| - const base::FilePath::StringType& device_location = device_path.value(); |
| - DCHECK(!device_location.empty()); |
| - AsyncDelegateMap::const_iterator it = |
| - async_delegate_map_.find(device_location); |
| - return (it != async_delegate_map_.end()) ? it->second : NULL; |
| + const base::FilePath::StringType& device_location = |
| + mtp_device_map_it->second.first; |
| + const bool read_only = mtp_device_map_it->second.second; |
| + const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
| + |
| + AsyncDelegateMap::const_iterator async_delegate_map_it = |
| + async_delegate_map_.find(key); |
| + return (async_delegate_map_it != async_delegate_map_.end()) |
| + ? async_delegate_map_it->second |
| + : NULL; |
| } |
| MTPDeviceMapService::MTPDeviceMapService() { |