| 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..6941878f69c33e8a1711cfa7292fb9c98103a952 100644
|
| --- a/chrome/browser/media_galleries/fileapi/mtp_device_map_service.cc
|
| +++ b/chrome/browser/media_galleries/fileapi/mtp_device_map_service.cc
|
| @@ -26,64 +26,101 @@ 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);
|
| }
|
|
|
| +// static
|
| +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);
|
| + DCHECK(!filesystem_id.empty());
|
| +
|
| + // File system may be already revoked on ExternalMountPoints side, we check
|
| + // here that the file system is still valid.
|
| base::FilePath device_path;
|
| if (!storage::ExternalMountPoints::GetSystemInstance()->GetRegisteredPath(
|
| filesystem_id, &device_path)) {
|
| @@ -91,10 +128,21 @@ MTPDeviceAsyncDelegate* MTPDeviceMapService::GetMTPDeviceAsyncDelegate(
|
| }
|
|
|
| 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;
|
| +
|
| + MTPDeviceFileSystemMap::const_iterator mtp_device_map_it =
|
| + mtp_device_map_.find(filesystem_id);
|
| + if (mtp_device_map_it == mtp_device_map_.end())
|
| + return NULL;
|
| +
|
| + DCHECK_EQ(device_path.value(), 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() {
|
|
|