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/fileapi/mtp_device_map_service.h" | 5 #include "chrome/browser/media_galleries/fileapi/mtp_device_map_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h" | 11 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "storage/browser/fileapi/external_mount_points.h" | 13 #include "storage/browser/fileapi/external_mount_points.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 base::LazyInstance<MTPDeviceMapService> g_mtp_device_map_service = | 17 base::LazyInstance<MTPDeviceMapService> g_mtp_device_map_service = |
18 LAZY_INSTANCE_INITIALIZER; | 18 LAZY_INSTANCE_INITIALIZER; |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 // static | 22 // static |
23 MTPDeviceMapService* MTPDeviceMapService::GetInstance() { | 23 MTPDeviceMapService* MTPDeviceMapService::GetInstance() { |
24 return g_mtp_device_map_service.Pointer(); | 24 return g_mtp_device_map_service.Pointer(); |
25 } | 25 } |
26 | 26 |
27 void MTPDeviceMapService::RegisterMTPFileSystem( | 27 void MTPDeviceMapService::RegisterMTPFileSystem( |
28 const base::FilePath::StringType& device_location, | 28 const base::FilePath::StringType& device_location, |
29 const std::string& fsid) { | 29 const std::string& filesystem_id, |
| 30 const bool read_only) { |
30 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 31 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 32 DCHECK(!device_location.empty()); |
| 33 DCHECK(!filesystem_id.empty()); |
31 | 34 |
32 if (!ContainsKey(mtp_device_usage_map_, device_location)) { | 35 const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
| 36 if (!ContainsKey(mtp_device_usage_map_, key)) { |
33 // Note that this initializes the delegate asynchronously, but since | 37 // Note that this initializes the delegate asynchronously, but since |
34 // the delegate will only be used from the IO thread, it is guaranteed | 38 // the delegate will only be used from the IO thread, it is guaranteed |
35 // to be created before use of it expects it to be there. | 39 // to be created before use of it expects it to be there. |
36 CreateMTPDeviceAsyncDelegate(device_location, | 40 CreateMTPDeviceAsyncDelegate( |
| 41 device_location, read_only, |
37 base::Bind(&MTPDeviceMapService::AddAsyncDelegate, | 42 base::Bind(&MTPDeviceMapService::AddAsyncDelegate, |
38 base::Unretained(this), device_location)); | 43 base::Unretained(this), device_location, read_only)); |
39 mtp_device_usage_map_[device_location] = 0; | 44 mtp_device_usage_map_[key] = 0; |
40 } | 45 } |
41 | 46 |
42 mtp_device_usage_map_[device_location]++; | 47 mtp_device_usage_map_[key]++; |
43 mtp_device_map_[fsid] = device_location; | 48 mtp_device_map_[filesystem_id] = make_pair(device_location, read_only); |
44 } | 49 } |
45 | 50 |
46 void MTPDeviceMapService::RevokeMTPFileSystem(const std::string& fsid) { | 51 void MTPDeviceMapService::RevokeMTPFileSystem( |
| 52 const std::string& filesystem_id) { |
47 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 53 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 54 DCHECK(!filesystem_id.empty()); |
48 | 55 |
49 MTPDeviceFileSystemMap::iterator it = mtp_device_map_.find(fsid); | 56 MTPDeviceFileSystemMap::iterator it = mtp_device_map_.find(filesystem_id); |
50 if (it != mtp_device_map_.end()) { | 57 if (it != mtp_device_map_.end()) { |
51 base::FilePath::StringType device_location = it->second; | 58 const base::FilePath::StringType device_location = it->second.first; |
| 59 const bool read_only = it->second.second; |
| 60 |
52 mtp_device_map_.erase(it); | 61 mtp_device_map_.erase(it); |
53 MTPDeviceUsageMap::iterator delegate_it = | 62 |
54 mtp_device_usage_map_.find(device_location); | 63 const AsyncDelegateKey key = |
| 64 GetAsyncDelegateKey(device_location, read_only); |
| 65 MTPDeviceUsageMap::iterator delegate_it = mtp_device_usage_map_.find(key); |
55 DCHECK(delegate_it != mtp_device_usage_map_.end()); | 66 DCHECK(delegate_it != mtp_device_usage_map_.end()); |
56 mtp_device_usage_map_[device_location]--; | 67 |
57 if (mtp_device_usage_map_[device_location] == 0) { | 68 mtp_device_usage_map_[key]--; |
| 69 if (mtp_device_usage_map_[key] == 0) { |
58 mtp_device_usage_map_.erase(delegate_it); | 70 mtp_device_usage_map_.erase(delegate_it); |
59 RemoveAsyncDelegate(device_location); | 71 RemoveAsyncDelegate(device_location, read_only); |
60 } | 72 } |
61 } | 73 } |
62 } | 74 } |
63 | 75 |
64 void MTPDeviceMapService::AddAsyncDelegate( | 76 void MTPDeviceMapService::AddAsyncDelegate( |
65 const base::FilePath::StringType& device_location, | 77 const base::FilePath::StringType& device_location, |
| 78 const bool read_only, |
66 MTPDeviceAsyncDelegate* delegate) { | 79 MTPDeviceAsyncDelegate* delegate) { |
67 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 80 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
68 DCHECK(delegate); | 81 DCHECK(delegate); |
69 DCHECK(!device_location.empty()); | 82 DCHECK(!device_location.empty()); |
70 if (ContainsKey(async_delegate_map_, device_location)) | 83 |
| 84 const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
| 85 if (ContainsKey(async_delegate_map_, key)) |
71 return; | 86 return; |
72 async_delegate_map_[device_location] = delegate; | 87 async_delegate_map_[key] = delegate; |
73 } | 88 } |
74 | 89 |
75 void MTPDeviceMapService::RemoveAsyncDelegate( | 90 void MTPDeviceMapService::RemoveAsyncDelegate( |
76 const base::FilePath::StringType& device_location) { | 91 const base::FilePath::StringType& device_location, |
| 92 const bool read_only) { |
77 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 93 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
78 AsyncDelegateMap::iterator it = async_delegate_map_.find(device_location); | 94 DCHECK(!device_location.empty()); |
| 95 |
| 96 const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
| 97 AsyncDelegateMap::iterator it = async_delegate_map_.find(key); |
79 DCHECK(it != async_delegate_map_.end()); | 98 DCHECK(it != async_delegate_map_.end()); |
80 it->second->CancelPendingTasksAndDeleteDelegate(); | 99 it->second->CancelPendingTasksAndDeleteDelegate(); |
81 async_delegate_map_.erase(it); | 100 async_delegate_map_.erase(it); |
82 } | 101 } |
83 | 102 |
| 103 // static |
| 104 MTPDeviceMapService::AsyncDelegateKey MTPDeviceMapService::GetAsyncDelegateKey( |
| 105 const base::FilePath::StringType& device_location, |
| 106 const bool read_only) { |
| 107 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 108 |
| 109 base::FilePath::StringType key; |
| 110 key.append(read_only ? FILE_PATH_LITERAL("ReadOnly") |
| 111 : FILE_PATH_LITERAL("ReadWrite")); |
| 112 key.append(FILE_PATH_LITERAL("|")); |
| 113 key.append(device_location); |
| 114 return key; |
| 115 } |
| 116 |
84 MTPDeviceAsyncDelegate* MTPDeviceMapService::GetMTPDeviceAsyncDelegate( | 117 MTPDeviceAsyncDelegate* MTPDeviceMapService::GetMTPDeviceAsyncDelegate( |
85 const std::string& filesystem_id) { | 118 const std::string& filesystem_id) { |
86 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 119 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 120 DCHECK(!filesystem_id.empty()); |
| 121 |
| 122 // File system may be already revoked on ExternalMountPoints side, we check |
| 123 // here that the file system is still valid. |
87 base::FilePath device_path; | 124 base::FilePath device_path; |
88 if (!storage::ExternalMountPoints::GetSystemInstance()->GetRegisteredPath( | 125 if (!storage::ExternalMountPoints::GetSystemInstance()->GetRegisteredPath( |
89 filesystem_id, &device_path)) { | 126 filesystem_id, &device_path)) { |
90 return NULL; | 127 return NULL; |
91 } | 128 } |
92 | 129 |
93 const base::FilePath::StringType& device_location = device_path.value(); | 130 const base::FilePath::StringType& device_location = device_path.value(); |
94 DCHECK(!device_location.empty()); | 131 |
95 AsyncDelegateMap::const_iterator it = | 132 MTPDeviceFileSystemMap::const_iterator mtp_device_map_it = |
96 async_delegate_map_.find(device_location); | 133 mtp_device_map_.find(filesystem_id); |
97 return (it != async_delegate_map_.end()) ? it->second : NULL; | 134 if (mtp_device_map_it == mtp_device_map_.end()) |
| 135 return NULL; |
| 136 |
| 137 DCHECK_EQ(device_path.value(), mtp_device_map_it->second.first); |
| 138 const bool read_only = mtp_device_map_it->second.second; |
| 139 const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
| 140 |
| 141 AsyncDelegateMap::const_iterator async_delegate_map_it = |
| 142 async_delegate_map_.find(key); |
| 143 return (async_delegate_map_it != async_delegate_map_.end()) |
| 144 ? async_delegate_map_it->second |
| 145 : NULL; |
98 } | 146 } |
99 | 147 |
100 MTPDeviceMapService::MTPDeviceMapService() { | 148 MTPDeviceMapService::MTPDeviceMapService() { |
101 } | 149 } |
102 | 150 |
103 MTPDeviceMapService::~MTPDeviceMapService() { | 151 MTPDeviceMapService::~MTPDeviceMapService() { |
104 DCHECK(mtp_device_usage_map_.empty()); | 152 DCHECK(mtp_device_usage_map_.empty()); |
105 } | 153 } |
OLD | NEW |