OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chromeos/file_system_provider/service.h" | 5 #include "chrome/browser/chromeos/file_system_provider/service.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" | 9 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
10 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 10 #include "chrome/browser/chromeos/file_system_provider/observer.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 return new ProvidedFileSystem(event_router, file_system_info); | 36 return new ProvidedFileSystem(event_router, file_system_info); |
37 } | 37 } |
38 | 38 |
39 } // namespace | 39 } // namespace |
40 | 40 |
41 Service::Service(Profile* profile, | 41 Service::Service(Profile* profile, |
42 extensions::ExtensionRegistry* extension_registry) | 42 extensions::ExtensionRegistry* extension_registry) |
43 : profile_(profile), | 43 : profile_(profile), |
44 extension_registry_(extension_registry), | 44 extension_registry_(extension_registry), |
45 file_system_factory_(base::Bind(CreateProvidedFileSystem)), | 45 file_system_factory_(base::Bind(CreateProvidedFileSystem)), |
46 next_id_(1), | 46 next_index_(1), |
47 weak_ptr_factory_(this) { | 47 weak_ptr_factory_(this) { |
48 extension_registry_->AddObserver(this); | 48 extension_registry_->AddObserver(this); |
49 } | 49 } |
50 | 50 |
51 Service::~Service() { | 51 Service::~Service() { |
52 extension_registry_->RemoveObserver(this); | 52 extension_registry_->RemoveObserver(this); |
53 | 53 |
54 ProvidedFileSystemMap::iterator it = file_system_map_.begin(); | 54 ProvidedFileSystemMap::iterator it = file_system_map_.begin(); |
55 while (it != file_system_map_.end()) { | 55 while (it != file_system_map_.end()) { |
56 const int file_system_id = it->first; | 56 const std::string file_system_id = |
| 57 it->second->GetFileSystemInfo().file_system_id(); |
57 const std::string extension_id = | 58 const std::string extension_id = |
58 it->second->GetFileSystemInfo().extension_id(); | 59 it->second->GetFileSystemInfo().extension_id(); |
59 ++it; | 60 ++it; |
60 UnmountFileSystem(extension_id, file_system_id); | 61 UnmountFileSystem(extension_id, file_system_id); |
61 } | 62 } |
62 | 63 |
63 DCHECK_EQ(0u, file_system_map_.size()); | 64 DCHECK_EQ(0u, file_system_map_.size()); |
64 STLDeleteValues(&file_system_map_); | 65 STLDeleteValues(&file_system_map_); |
65 } | 66 } |
66 | 67 |
(...skipping 11 matching lines...) Expand all Loading... |
78 DCHECK(observer); | 79 DCHECK(observer); |
79 observers_.RemoveObserver(observer); | 80 observers_.RemoveObserver(observer); |
80 } | 81 } |
81 | 82 |
82 void Service::SetFileSystemFactoryForTests( | 83 void Service::SetFileSystemFactoryForTests( |
83 const FileSystemFactoryCallback& factory_callback) { | 84 const FileSystemFactoryCallback& factory_callback) { |
84 DCHECK(!factory_callback.is_null()); | 85 DCHECK(!factory_callback.is_null()); |
85 file_system_factory_ = factory_callback; | 86 file_system_factory_ = factory_callback; |
86 } | 87 } |
87 | 88 |
88 int Service::MountFileSystem(const std::string& extension_id, | 89 bool Service::MountFileSystem(const std::string& extension_id, |
89 const std::string& file_system_name) { | 90 const std::string& file_system_id, |
| 91 const std::string& file_system_name) { |
90 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
91 | 93 |
| 94 // If already exists a file system provided by the same extension with this |
| 95 // id, then abort. |
| 96 if (GetProvidedFileSystem(extension_id, file_system_id)) { |
| 97 FOR_EACH_OBSERVER(Observer, |
| 98 observers_, |
| 99 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
| 100 base::File::FILE_ERROR_EXISTS)); |
| 101 return false; |
| 102 } |
| 103 |
92 // Restrict number of file systems to prevent system abusing. | 104 // Restrict number of file systems to prevent system abusing. |
93 if (file_system_map_.size() + 1 > kMaxFileSystems) { | 105 if (file_system_map_.size() + 1 > kMaxFileSystems) { |
94 FOR_EACH_OBSERVER( | 106 FOR_EACH_OBSERVER( |
95 Observer, | 107 Observer, |
96 observers_, | 108 observers_, |
97 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), | 109 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
98 base::File::FILE_ERROR_TOO_MANY_OPENED)); | 110 base::File::FILE_ERROR_TOO_MANY_OPENED)); |
99 return 0; | 111 return false; |
100 } | 112 } |
101 | 113 |
102 // The provided file system id is unique per service, so per profile. | 114 // The provided file system index is unique per service, so per profile. |
103 int file_system_id = next_id_; | 115 // The index is used internally. Externally a file system id is used, which |
| 116 // is unique per extension. |
| 117 int index = next_index_; |
104 | 118 |
105 fileapi::ExternalMountPoints* const mount_points = | 119 fileapi::ExternalMountPoints* const mount_points = |
106 fileapi::ExternalMountPoints::GetSystemInstance(); | 120 fileapi::ExternalMountPoints::GetSystemInstance(); |
107 DCHECK(mount_points); | 121 DCHECK(mount_points); |
108 | 122 |
109 // The mount point path and name are unique per system, since they are system | 123 // The mount point path and name are unique per system, since they are system |
110 // wide. This is necessary for copying between profiles. | 124 // wide. This is necessary for copying between profiles. |
111 const base::FilePath& mount_path = | 125 const base::FilePath& mount_path = |
112 util::GetMountPath(profile_, extension_id, file_system_id); | 126 util::GetMountPath(profile_, extension_id, index); |
113 const std::string mount_point_name = mount_path.BaseName().AsUTF8Unsafe(); | 127 const std::string mount_point_name = mount_path.BaseName().AsUTF8Unsafe(); |
114 | 128 |
115 if (!mount_points->RegisterFileSystem(mount_point_name, | 129 if (!mount_points->RegisterFileSystem(mount_point_name, |
116 fileapi::kFileSystemTypeProvided, | 130 fileapi::kFileSystemTypeProvided, |
117 fileapi::FileSystemMountOption(), | 131 fileapi::FileSystemMountOption(), |
118 mount_path)) { | 132 mount_path)) { |
119 FOR_EACH_OBSERVER( | 133 FOR_EACH_OBSERVER( |
120 Observer, | 134 Observer, |
121 observers_, | 135 observers_, |
122 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), | 136 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
123 base::File::FILE_ERROR_INVALID_OPERATION)); | 137 base::File::FILE_ERROR_INVALID_OPERATION)); |
124 return 0; | 138 return false; |
125 } | 139 } |
126 | 140 |
127 // Store the file system descriptor. Use the mount point name as the file | 141 // Store the file system descriptor. Use the mount point name as the file |
128 // system provider file system id. | 142 // system provider file system id. |
129 // Examples: | 143 // Examples: |
130 // file_system_id = 41 | 144 // file_system_id = 41 |
131 // mount_point_name = b33f1337-41-5aa5 | 145 // mount_point_name = b33f1337-41-5aa5 |
132 // mount_path = /provided/b33f1337-41-5aa5 | 146 // mount_path = /provided/b33f1337-41-5aa5 |
133 ProvidedFileSystemInfo file_system_info( | 147 ProvidedFileSystemInfo file_system_info( |
134 extension_id, file_system_id, file_system_name, mount_path); | 148 extension_id, file_system_id, file_system_name, mount_path); |
135 | 149 |
136 // The event router may be NULL for unit tests. | 150 // The event router may be NULL for unit tests. |
137 extensions::EventRouter* router = extensions::EventRouter::Get(profile_); | 151 extensions::EventRouter* router = extensions::EventRouter::Get(profile_); |
138 | 152 |
139 ProvidedFileSystemInterface* file_system = | 153 ProvidedFileSystemInterface* file_system = |
140 file_system_factory_.Run(router, file_system_info); | 154 file_system_factory_.Run(router, file_system_info); |
141 DCHECK(file_system); | 155 DCHECK(file_system); |
142 file_system_map_[file_system_id] = file_system; | 156 file_system_map_[FileSystemKey(extension_id, file_system_id)] = file_system; |
143 mount_point_name_to_id_map_[mount_point_name] = file_system_id; | 157 mount_point_name_to_key_map_[mount_point_name] = |
| 158 FileSystemKey(extension_id, file_system_id); |
144 | 159 |
145 FOR_EACH_OBSERVER( | 160 FOR_EACH_OBSERVER( |
146 Observer, | 161 Observer, |
147 observers_, | 162 observers_, |
148 OnProvidedFileSystemMount(file_system_info, base::File::FILE_OK)); | 163 OnProvidedFileSystemMount(file_system_info, base::File::FILE_OK)); |
149 | 164 |
150 next_id_++; | 165 next_index_++; |
151 return file_system_id; | 166 return true; |
152 } | 167 } |
153 | 168 |
154 bool Service::UnmountFileSystem(const std::string& extension_id, | 169 bool Service::UnmountFileSystem(const std::string& extension_id, |
155 int file_system_id) { | 170 const std::string& file_system_id) { |
156 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
157 | 172 |
158 const ProvidedFileSystemMap::iterator file_system_it = | 173 const ProvidedFileSystemMap::iterator file_system_it = |
159 file_system_map_.find(file_system_id); | 174 file_system_map_.find(FileSystemKey(extension_id, file_system_id)); |
160 if (file_system_it == file_system_map_.end() || | 175 if (file_system_it == file_system_map_.end()) { |
161 file_system_it->second->GetFileSystemInfo().extension_id() != | |
162 extension_id) { | |
163 const ProvidedFileSystemInfo empty_file_system_info; | 176 const ProvidedFileSystemInfo empty_file_system_info; |
164 FOR_EACH_OBSERVER( | 177 FOR_EACH_OBSERVER( |
165 Observer, | 178 Observer, |
166 observers_, | 179 observers_, |
167 OnProvidedFileSystemUnmount(empty_file_system_info, | 180 OnProvidedFileSystemUnmount(empty_file_system_info, |
168 base::File::FILE_ERROR_NOT_FOUND)); | 181 base::File::FILE_ERROR_NOT_FOUND)); |
169 return false; | 182 return false; |
170 } | 183 } |
171 | 184 |
172 fileapi::ExternalMountPoints* const mount_points = | 185 fileapi::ExternalMountPoints* const mount_points = |
(...skipping 12 matching lines...) Expand all Loading... |
185 OnProvidedFileSystemUnmount(file_system_info, | 198 OnProvidedFileSystemUnmount(file_system_info, |
186 base::File::FILE_ERROR_INVALID_OPERATION)); | 199 base::File::FILE_ERROR_INVALID_OPERATION)); |
187 return false; | 200 return false; |
188 } | 201 } |
189 | 202 |
190 FOR_EACH_OBSERVER( | 203 FOR_EACH_OBSERVER( |
191 Observer, | 204 Observer, |
192 observers_, | 205 observers_, |
193 OnProvidedFileSystemUnmount(file_system_info, base::File::FILE_OK)); | 206 OnProvidedFileSystemUnmount(file_system_info, base::File::FILE_OK)); |
194 | 207 |
195 mount_point_name_to_id_map_.erase(mount_point_name); | 208 mount_point_name_to_key_map_.erase(mount_point_name); |
196 | 209 |
197 delete file_system_it->second; | 210 delete file_system_it->second; |
198 file_system_map_.erase(file_system_it); | 211 file_system_map_.erase(file_system_it); |
199 | 212 |
200 return true; | 213 return true; |
201 } | 214 } |
202 | 215 |
203 bool Service::RequestUnmount(int file_system_id) { | 216 bool Service::RequestUnmount(const std::string& extension_id, |
| 217 const std::string& file_system_id) { |
204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 218 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
205 | 219 |
206 ProvidedFileSystemMap::iterator file_system_it = | 220 ProvidedFileSystemMap::iterator file_system_it = |
207 file_system_map_.find(file_system_id); | 221 file_system_map_.find(FileSystemKey(extension_id, file_system_id)); |
208 if (file_system_it == file_system_map_.end()) | 222 if (file_system_it == file_system_map_.end()) |
209 return false; | 223 return false; |
210 | 224 |
211 file_system_it->second->RequestUnmount( | 225 file_system_it->second->RequestUnmount( |
212 base::Bind(&Service::OnRequestUnmountStatus, | 226 base::Bind(&Service::OnRequestUnmountStatus, |
213 weak_ptr_factory_.GetWeakPtr(), | 227 weak_ptr_factory_.GetWeakPtr(), |
214 file_system_it->second->GetFileSystemInfo())); | 228 file_system_it->second->GetFileSystemInfo())); |
215 return true; | 229 return true; |
216 } | 230 } |
217 | 231 |
218 std::vector<ProvidedFileSystemInfo> Service::GetProvidedFileSystemInfoList() { | 232 std::vector<ProvidedFileSystemInfo> Service::GetProvidedFileSystemInfoList() { |
219 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 233 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
220 | 234 |
221 std::vector<ProvidedFileSystemInfo> result; | 235 std::vector<ProvidedFileSystemInfo> result; |
222 for (ProvidedFileSystemMap::const_iterator it = file_system_map_.begin(); | 236 for (ProvidedFileSystemMap::const_iterator it = file_system_map_.begin(); |
223 it != file_system_map_.end(); | 237 it != file_system_map_.end(); |
224 ++it) { | 238 ++it) { |
225 result.push_back(it->second->GetFileSystemInfo()); | 239 result.push_back(it->second->GetFileSystemInfo()); |
226 } | 240 } |
227 return result; | 241 return result; |
228 } | 242 } |
229 | 243 |
230 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( | 244 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
231 const std::string& extension_id, | 245 const std::string& extension_id, |
232 int file_system_id) { | 246 const std::string& file_system_id) { |
233 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 247 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
234 | 248 |
235 const ProvidedFileSystemMap::const_iterator file_system_it = | 249 const ProvidedFileSystemMap::const_iterator file_system_it = |
236 file_system_map_.find(file_system_id); | 250 file_system_map_.find(FileSystemKey(extension_id, file_system_id)); |
237 if (file_system_it == file_system_map_.end() || | 251 if (file_system_it == file_system_map_.end()) |
238 file_system_it->second->GetFileSystemInfo().extension_id() != | |
239 extension_id) { | |
240 return NULL; | 252 return NULL; |
241 } | |
242 | 253 |
243 return file_system_it->second; | 254 return file_system_it->second; |
244 } | 255 } |
245 | 256 |
246 void Service::OnExtensionUnloaded( | 257 void Service::OnExtensionUnloaded( |
247 content::BrowserContext* browser_context, | 258 content::BrowserContext* browser_context, |
248 const extensions::Extension* extension, | 259 const extensions::Extension* extension, |
249 extensions::UnloadedExtensionInfo::Reason reason) { | 260 extensions::UnloadedExtensionInfo::Reason reason) { |
250 // Unmount all of the provided file systems associated with this extension. | 261 // Unmount all of the provided file systems associated with this extension. |
251 ProvidedFileSystemMap::iterator it = file_system_map_.begin(); | 262 ProvidedFileSystemMap::iterator it = file_system_map_.begin(); |
252 while (it != file_system_map_.end()) { | 263 while (it != file_system_map_.end()) { |
253 const ProvidedFileSystemInfo& file_system_info = | 264 const ProvidedFileSystemInfo& file_system_info = |
254 it->second->GetFileSystemInfo(); | 265 it->second->GetFileSystemInfo(); |
255 // Advance the iterator beforehand, otherwise it will become invalidated | 266 // Advance the iterator beforehand, otherwise it will become invalidated |
256 // by the UnmountFileSystem() call. | 267 // by the UnmountFileSystem() call. |
257 ++it; | 268 ++it; |
258 if (file_system_info.extension_id() == extension->id()) { | 269 if (file_system_info.extension_id() == extension->id()) { |
259 bool result = UnmountFileSystem(file_system_info.extension_id(), | 270 bool result = UnmountFileSystem(file_system_info.extension_id(), |
260 file_system_info.file_system_id()); | 271 file_system_info.file_system_id()); |
261 DCHECK(result); | 272 DCHECK(result); |
262 } | 273 } |
263 } | 274 } |
264 } | 275 } |
265 | 276 |
266 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( | 277 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
267 const std::string& mount_point_name) { | 278 const std::string& mount_point_name) { |
268 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 279 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
269 | 280 |
270 const MountPointNameToIdMap::const_iterator mapping_it = | 281 const MountPointNameToKeyMap::const_iterator mapping_it = |
271 mount_point_name_to_id_map_.find(mount_point_name); | 282 mount_point_name_to_key_map_.find(mount_point_name); |
272 if (mapping_it == mount_point_name_to_id_map_.end()) | 283 if (mapping_it == mount_point_name_to_key_map_.end()) |
273 return NULL; | 284 return NULL; |
274 | 285 |
275 const ProvidedFileSystemMap::const_iterator file_system_it = | 286 const ProvidedFileSystemMap::const_iterator file_system_it = |
276 file_system_map_.find(mapping_it->second); | 287 file_system_map_.find(mapping_it->second); |
277 if (file_system_it == file_system_map_.end()) | 288 if (file_system_it == file_system_map_.end()) |
278 return NULL; | 289 return NULL; |
279 | 290 |
280 return file_system_it->second; | 291 return file_system_it->second; |
281 } | 292 } |
282 | 293 |
283 void Service::OnRequestUnmountStatus( | 294 void Service::OnRequestUnmountStatus( |
284 const ProvidedFileSystemInfo& file_system_info, | 295 const ProvidedFileSystemInfo& file_system_info, |
285 base::File::Error error) { | 296 base::File::Error error) { |
286 // Notify observers about failure in unmounting, since mount() will not be | 297 // Notify observers about failure in unmounting, since mount() will not be |
287 // called by the provided file system. In case of success mount() will be | 298 // called by the provided file system. In case of success mount() will be |
288 // invoked, and observers notified, so there is no need to call them now. | 299 // invoked, and observers notified, so there is no need to call them now. |
289 if (error != base::File::FILE_OK) { | 300 if (error != base::File::FILE_OK) { |
290 FOR_EACH_OBSERVER(Observer, | 301 FOR_EACH_OBSERVER(Observer, |
291 observers_, | 302 observers_, |
292 OnProvidedFileSystemUnmount(file_system_info, error)); | 303 OnProvidedFileSystemUnmount(file_system_info, error)); |
293 } | 304 } |
294 } | 305 } |
295 | 306 |
296 } // namespace file_system_provider | 307 } // namespace file_system_provider |
297 } // namespace chromeos | 308 } // namespace chromeos |
OLD | NEW |