OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/chromeos/fileapi/cros_mount_point_provider.h" | |
6 | |
7 #include "base/chromeos/chromeos_version.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/stringprintf.h" | |
12 #include "base/synchronization/lock.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "chromeos/dbus/cros_disks_client.h" | |
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h" | |
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebFileSystem.h" | |
17 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | |
18 #include "webkit/browser/fileapi/async_file_util_adapter.h" | |
19 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | |
20 #include "webkit/browser/fileapi/external_mount_points.h" | |
21 #include "webkit/browser/fileapi/file_system_context.h" | |
22 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" | |
23 #include "webkit/browser/fileapi/file_system_operation_context.h" | |
24 #include "webkit/browser/fileapi/file_system_task_runners.h" | |
25 #include "webkit/browser/fileapi/file_system_url.h" | |
26 #include "webkit/browser/fileapi/isolated_context.h" | |
27 #include "webkit/browser/fileapi/isolated_file_util.h" | |
28 #include "webkit/browser/fileapi/local_file_stream_writer.h" | |
29 #include "webkit/browser/fileapi/local_file_system_operation.h" | |
30 #include "webkit/chromeos/fileapi/file_access_permissions.h" | |
31 #include "webkit/chromeos/fileapi/remote_file_stream_writer.h" | |
32 #include "webkit/chromeos/fileapi/remote_file_system_operation.h" | |
33 #include "webkit/glue/webkit_glue.h" | |
34 | |
35 namespace { | |
36 | |
37 const char kChromeUIScheme[] = "chrome"; | |
38 | |
39 } // namespace | |
40 | |
41 namespace chromeos { | |
42 | |
43 // static | |
44 bool CrosMountPointProvider::CanHandleURL(const fileapi::FileSystemURL& url) { | |
45 if (!url.is_valid()) | |
46 return false; | |
47 return url.type() == fileapi::kFileSystemTypeNativeLocal || | |
48 url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal || | |
49 url.type() == fileapi::kFileSystemTypeDrive; | |
50 } | |
51 | |
52 CrosMountPointProvider::CrosMountPointProvider( | |
53 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy, | |
54 scoped_refptr<fileapi::ExternalMountPoints> mount_points, | |
55 fileapi::ExternalMountPoints* system_mount_points) | |
56 : special_storage_policy_(special_storage_policy), | |
57 file_access_permissions_(new FileAccessPermissions()), | |
58 local_file_util_(new fileapi::AsyncFileUtilAdapter( | |
59 new fileapi::IsolatedFileUtil())), | |
60 mount_points_(mount_points), | |
61 system_mount_points_(system_mount_points) { | |
62 // Add default system mount points. | |
63 system_mount_points_->RegisterFileSystem( | |
64 "archive", | |
65 fileapi::kFileSystemTypeNativeLocal, | |
66 chromeos::CrosDisksClient::GetArchiveMountPoint()); | |
67 system_mount_points_->RegisterFileSystem( | |
68 "removable", | |
69 fileapi::kFileSystemTypeNativeLocal, | |
70 chromeos::CrosDisksClient::GetRemovableDiskMountPoint()); | |
71 system_mount_points_->RegisterFileSystem( | |
72 "oem", | |
73 fileapi::kFileSystemTypeRestrictedNativeLocal, | |
74 base::FilePath(FILE_PATH_LITERAL("/usr/share/oem"))); | |
75 } | |
76 | |
77 CrosMountPointProvider::~CrosMountPointProvider() { | |
78 } | |
79 | |
80 bool CrosMountPointProvider::CanHandleType(fileapi::FileSystemType type) const { | |
81 switch (type) { | |
82 case fileapi::kFileSystemTypeExternal: | |
83 case fileapi::kFileSystemTypeDrive: | |
84 case fileapi::kFileSystemTypeRestrictedNativeLocal: | |
85 case fileapi::kFileSystemTypeNativeLocal: | |
86 case fileapi::kFileSystemTypeNativeForPlatformApp: | |
87 return true; | |
88 default: | |
89 return false; | |
90 } | |
91 } | |
92 | |
93 void CrosMountPointProvider::ValidateFileSystemRoot( | |
94 const GURL& origin_url, | |
95 fileapi::FileSystemType type, | |
96 bool create, | |
97 const ValidateFileSystemCallback& callback) { | |
98 DCHECK(fileapi::IsolatedContext::IsIsolatedType(type)); | |
99 // Nothing to validate for external filesystem. | |
100 callback.Run(base::PLATFORM_FILE_OK); | |
101 } | |
102 | |
103 base::FilePath CrosMountPointProvider::GetFileSystemRootPathOnFileThread( | |
104 const fileapi::FileSystemURL& url, | |
105 bool create) { | |
106 DCHECK(fileapi::IsolatedContext::IsIsolatedType(url.mount_type())); | |
107 if (!url.is_valid()) | |
108 return base::FilePath(); | |
109 | |
110 base::FilePath root_path; | |
111 std::string mount_name = url.filesystem_id(); | |
112 if (!mount_points_->GetRegisteredPath(mount_name, &root_path) && | |
113 !system_mount_points_->GetRegisteredPath(mount_name, &root_path)) { | |
114 return base::FilePath(); | |
115 } | |
116 | |
117 return root_path.DirName(); | |
118 } | |
119 | |
120 fileapi::FileSystemQuotaUtil* CrosMountPointProvider::GetQuotaUtil() { | |
121 // No quota support. | |
122 return NULL; | |
123 } | |
124 | |
125 void CrosMountPointProvider::DeleteFileSystem( | |
126 const GURL& origin_url, | |
127 fileapi::FileSystemType type, | |
128 fileapi::FileSystemContext* context, | |
129 const DeleteFileSystemCallback& callback) { | |
130 NOTREACHED(); | |
131 callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); | |
132 } | |
133 | |
134 bool CrosMountPointProvider::IsAccessAllowed( | |
135 const fileapi::FileSystemURL& url) const { | |
136 if (!url.is_valid()) | |
137 return false; | |
138 | |
139 // Permit access to mount points from internal WebUI. | |
140 const GURL& origin_url = url.origin(); | |
141 if (origin_url.SchemeIs(kChromeUIScheme)) | |
142 return true; | |
143 | |
144 // No extra check is needed for isolated file systems. | |
145 if (url.mount_type() == fileapi::kFileSystemTypeIsolated) | |
146 return true; | |
147 | |
148 if (!CanHandleURL(url)) | |
149 return false; | |
150 | |
151 std::string extension_id = origin_url.host(); | |
152 // Check first to make sure this extension has fileBrowserHander permissions. | |
153 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
154 return false; | |
155 | |
156 return file_access_permissions_->HasAccessPermission(extension_id, | |
157 url.virtual_path()); | |
158 } | |
159 | |
160 void CrosMountPointProvider::GrantFullAccessToExtension( | |
161 const std::string& extension_id) { | |
162 DCHECK(special_storage_policy_->IsFileHandler(extension_id)); | |
163 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
164 return; | |
165 | |
166 std::vector<fileapi::MountPoints::MountPointInfo> files; | |
167 mount_points_->AddMountPointInfosTo(&files); | |
168 system_mount_points_->AddMountPointInfosTo(&files); | |
169 | |
170 for (size_t i = 0; i < files.size(); ++i) { | |
171 file_access_permissions_->GrantAccessPermission( | |
172 extension_id, | |
173 base::FilePath::FromUTF8Unsafe(files[i].name)); | |
174 } | |
175 } | |
176 | |
177 void CrosMountPointProvider::GrantFileAccessToExtension( | |
178 const std::string& extension_id, const base::FilePath& virtual_path) { | |
179 // All we care about here is access from extensions for now. | |
180 DCHECK(special_storage_policy_->IsFileHandler(extension_id)); | |
181 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
182 return; | |
183 | |
184 std::string id; | |
185 fileapi::FileSystemType type; | |
186 base::FilePath path; | |
187 if (!mount_points_->CrackVirtualPath(virtual_path, &id, &type, &path) && | |
188 !system_mount_points_->CrackVirtualPath(virtual_path, | |
189 &id, &type, &path)) { | |
190 return; | |
191 } | |
192 | |
193 if (type == fileapi::kFileSystemTypeRestrictedNativeLocal) { | |
194 LOG(ERROR) << "Can't grant access for restricted mount point"; | |
195 return; | |
196 } | |
197 | |
198 file_access_permissions_->GrantAccessPermission(extension_id, virtual_path); | |
199 } | |
200 | |
201 void CrosMountPointProvider::RevokeAccessForExtension( | |
202 const std::string& extension_id) { | |
203 file_access_permissions_->RevokePermissions(extension_id); | |
204 } | |
205 | |
206 std::vector<base::FilePath> CrosMountPointProvider::GetRootDirectories() const { | |
207 std::vector<fileapi::MountPoints::MountPointInfo> mount_points; | |
208 mount_points_->AddMountPointInfosTo(&mount_points); | |
209 system_mount_points_->AddMountPointInfosTo(&mount_points); | |
210 | |
211 std::vector<base::FilePath> root_dirs; | |
212 for (size_t i = 0; i < mount_points.size(); ++i) | |
213 root_dirs.push_back(mount_points[i].path); | |
214 return root_dirs; | |
215 } | |
216 | |
217 fileapi::FileSystemFileUtil* CrosMountPointProvider::GetFileUtil( | |
218 fileapi::FileSystemType type) { | |
219 DCHECK(type == fileapi::kFileSystemTypeNativeLocal || | |
220 type == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
221 return local_file_util_->sync_file_util(); | |
222 } | |
223 | |
224 fileapi::AsyncFileUtil* CrosMountPointProvider::GetAsyncFileUtil( | |
225 fileapi::FileSystemType type) { | |
226 DCHECK(type == fileapi::kFileSystemTypeNativeLocal || | |
227 type == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
228 return local_file_util_.get(); | |
229 } | |
230 | |
231 fileapi::CopyOrMoveFileValidatorFactory* | |
232 CrosMountPointProvider::GetCopyOrMoveFileValidatorFactory( | |
233 fileapi::FileSystemType type, base::PlatformFileError* error_code) { | |
234 DCHECK(error_code); | |
235 *error_code = base::PLATFORM_FILE_OK; | |
236 return NULL; | |
237 } | |
238 | |
239 void CrosMountPointProvider::InitializeCopyOrMoveFileValidatorFactory( | |
240 fileapi::FileSystemType type, | |
241 scoped_ptr<fileapi::CopyOrMoveFileValidatorFactory> factory) { | |
242 DCHECK(!factory); | |
243 } | |
244 | |
245 fileapi::FilePermissionPolicy CrosMountPointProvider::GetPermissionPolicy( | |
246 const fileapi::FileSystemURL& url, int permissions) const { | |
247 if (url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal && | |
248 (permissions & ~fileapi::kReadFilePermissions)) { | |
249 // Restricted file system is read-only. | |
250 return fileapi::FILE_PERMISSION_ALWAYS_DENY; | |
251 } | |
252 | |
253 if (!IsAccessAllowed(url)) | |
254 return fileapi::FILE_PERMISSION_ALWAYS_DENY; | |
255 | |
256 // Permit access to mount points from internal WebUI. | |
257 const GURL& origin_url = url.origin(); | |
258 if (origin_url.SchemeIs(kChromeUIScheme)) | |
259 return fileapi::FILE_PERMISSION_ALWAYS_ALLOW; | |
260 | |
261 if (url.mount_type() == fileapi::kFileSystemTypeIsolated) { | |
262 // Permissions in isolated filesystems should be examined with | |
263 // FileSystem permission. | |
264 return fileapi::FILE_PERMISSION_USE_FILESYSTEM_PERMISSION; | |
265 } | |
266 | |
267 // Also apply system's file permission by default. | |
268 return fileapi::FILE_PERMISSION_USE_FILE_PERMISSION; | |
269 } | |
270 | |
271 fileapi::FileSystemOperation* CrosMountPointProvider::CreateFileSystemOperation( | |
272 const fileapi::FileSystemURL& url, | |
273 fileapi::FileSystemContext* context, | |
274 base::PlatformFileError* error_code) const { | |
275 DCHECK(url.is_valid()); | |
276 | |
277 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
278 fileapi::RemoteFileSystemProxyInterface* remote_proxy = | |
279 GetRemoteProxy(url.filesystem_id()); | |
280 if (!remote_proxy) { | |
281 *error_code = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
282 return NULL; | |
283 } | |
284 return new chromeos::RemoteFileSystemOperation(remote_proxy); | |
285 } | |
286 | |
287 DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal || | |
288 url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
289 scoped_ptr<fileapi::FileSystemOperationContext> operation_context( | |
290 new fileapi::FileSystemOperationContext(context)); | |
291 return new fileapi::LocalFileSystemOperation(context, | |
292 operation_context.Pass()); | |
293 } | |
294 | |
295 scoped_ptr<webkit_blob::FileStreamReader> | |
296 CrosMountPointProvider::CreateFileStreamReader( | |
297 const fileapi::FileSystemURL& url, | |
298 int64 offset, | |
299 const base::Time& expected_modification_time, | |
300 fileapi::FileSystemContext* context) const { | |
301 DCHECK(url.is_valid()); | |
302 | |
303 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
304 fileapi::RemoteFileSystemProxyInterface* remote_proxy = | |
305 GetRemoteProxy(url.filesystem_id()); | |
306 if (!remote_proxy) | |
307 return scoped_ptr<webkit_blob::FileStreamReader>(); | |
308 return remote_proxy->CreateFileStreamReader( | |
309 context->task_runners()->file_task_runner(), | |
310 url, offset, expected_modification_time); | |
311 } | |
312 | |
313 return scoped_ptr<webkit_blob::FileStreamReader>( | |
314 new fileapi::FileSystemFileStreamReader( | |
315 context, url, offset, expected_modification_time)); | |
316 } | |
317 | |
318 scoped_ptr<fileapi::FileStreamWriter> | |
319 CrosMountPointProvider::CreateFileStreamWriter( | |
320 const fileapi::FileSystemURL& url, | |
321 int64 offset, | |
322 fileapi::FileSystemContext* context) const { | |
323 DCHECK(url.is_valid()); | |
324 | |
325 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
326 fileapi::RemoteFileSystemProxyInterface* remote_proxy = | |
327 GetRemoteProxy(url.filesystem_id()); | |
328 if (!remote_proxy) | |
329 return scoped_ptr<fileapi::FileStreamWriter>(); | |
330 return scoped_ptr<fileapi::FileStreamWriter>( | |
331 new fileapi::RemoteFileStreamWriter(remote_proxy, url, offset)); | |
332 } | |
333 | |
334 if (url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal) | |
335 return scoped_ptr<fileapi::FileStreamWriter>(); | |
336 | |
337 DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal); | |
338 return scoped_ptr<fileapi::FileStreamWriter>( | |
339 new fileapi::LocalFileStreamWriter(url.path(), offset)); | |
340 } | |
341 | |
342 bool CrosMountPointProvider::GetVirtualPath( | |
343 const base::FilePath& filesystem_path, | |
344 base::FilePath* virtual_path) { | |
345 return mount_points_->GetVirtualPath(filesystem_path, virtual_path) || | |
346 system_mount_points_->GetVirtualPath(filesystem_path, virtual_path); | |
347 } | |
348 | |
349 fileapi::RemoteFileSystemProxyInterface* CrosMountPointProvider::GetRemoteProxy( | |
350 const std::string& mount_name) const { | |
351 fileapi::RemoteFileSystemProxyInterface* proxy = | |
352 mount_points_->GetRemoteFileSystemProxy(mount_name); | |
353 if (proxy) | |
354 return proxy; | |
355 return system_mount_points_->GetRemoteFileSystemProxy(mount_name); | |
356 } | |
357 | |
358 } // namespace chromeos | |
OLD | NEW |