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