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/browser/chromeos/fileapi/cros_mount_point_provider.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/path_service.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "base/synchronization/lock.h" | |
14 #include "chromeos/dbus/cros_disks_client.h" | |
15 #include "third_party/WebKit/public/platform/WebCString.h" | |
16 #include "third_party/WebKit/public/platform/WebFileSystem.h" | |
17 #include "third_party/WebKit/public/platform/WebString.h" | |
18 #include "webkit/browser/chromeos/fileapi/file_access_permissions.h" | |
19 #include "webkit/browser/chromeos/fileapi/remote_file_stream_writer.h" | |
20 #include "webkit/browser/chromeos/fileapi/remote_file_system_operation.h" | |
21 #include "webkit/browser/fileapi/async_file_util_adapter.h" | |
22 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | |
23 #include "webkit/browser/fileapi/external_mount_points.h" | |
24 #include "webkit/browser/fileapi/file_system_context.h" | |
25 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" | |
26 #include "webkit/browser/fileapi/file_system_operation_context.h" | |
27 #include "webkit/browser/fileapi/file_system_task_runners.h" | |
28 #include "webkit/browser/fileapi/file_system_url.h" | |
29 #include "webkit/browser/fileapi/isolated_context.h" | |
30 #include "webkit/browser/fileapi/isolated_file_util.h" | |
31 #include "webkit/browser/fileapi/local_file_stream_writer.h" | |
32 #include "webkit/browser/fileapi/local_file_system_operation.h" | |
33 | |
34 namespace { | |
35 | |
36 const char kChromeUIScheme[] = "chrome"; | |
37 | |
38 } // namespace | |
39 | |
40 namespace chromeos { | |
41 | |
42 // static | |
43 bool CrosMountPointProvider::CanHandleURL(const fileapi::FileSystemURL& url) { | |
44 if (!url.is_valid()) | |
45 return false; | |
46 return url.type() == fileapi::kFileSystemTypeNativeLocal || | |
47 url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal || | |
48 url.type() == fileapi::kFileSystemTypeDrive; | |
49 } | |
50 | |
51 CrosMountPointProvider::CrosMountPointProvider( | |
52 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy, | |
53 scoped_refptr<fileapi::ExternalMountPoints> mount_points, | |
54 fileapi::ExternalMountPoints* system_mount_points) | |
55 : special_storage_policy_(special_storage_policy), | |
56 file_access_permissions_(new FileAccessPermissions()), | |
57 local_file_util_(new fileapi::AsyncFileUtilAdapter( | |
58 new fileapi::IsolatedFileUtil())), | |
59 mount_points_(mount_points), | |
60 system_mount_points_(system_mount_points) { | |
61 } | |
62 | |
63 CrosMountPointProvider::~CrosMountPointProvider() { | |
64 } | |
65 | |
66 void CrosMountPointProvider::AddSystemMountPoints() { | |
67 // RegisterFileSystem() is no-op if the mount point with the same name | |
68 // already exists, hence it's safe to call without checking if a mount | |
69 // point already exists or not. | |
70 | |
71 // TODO(satorux): "Downloads" directory should probably be per-profile. For | |
72 // this to be per-profile, a unique directory path should be chosen per | |
73 // profile, and the mount point should be added to | |
74 // mount_points_. crbug.com/247236 | |
75 base::FilePath home_path; | |
76 if (PathService::Get(base::DIR_HOME, &home_path)) { | |
77 system_mount_points_->RegisterFileSystem( | |
78 "Downloads", | |
79 fileapi::kFileSystemTypeNativeLocal, | |
80 home_path.AppendASCII("Downloads")); | |
81 } | |
82 | |
83 system_mount_points_->RegisterFileSystem( | |
84 "archive", | |
85 fileapi::kFileSystemTypeNativeLocal, | |
86 chromeos::CrosDisksClient::GetArchiveMountPoint()); | |
87 system_mount_points_->RegisterFileSystem( | |
88 "removable", | |
89 fileapi::kFileSystemTypeNativeLocal, | |
90 chromeos::CrosDisksClient::GetRemovableDiskMountPoint()); | |
91 system_mount_points_->RegisterFileSystem( | |
92 "oem", | |
93 fileapi::kFileSystemTypeRestrictedNativeLocal, | |
94 base::FilePath(FILE_PATH_LITERAL("/usr/share/oem"))); | |
95 } | |
96 | |
97 bool CrosMountPointProvider::CanHandleType(fileapi::FileSystemType type) const { | |
98 switch (type) { | |
99 case fileapi::kFileSystemTypeExternal: | |
100 case fileapi::kFileSystemTypeDrive: | |
101 case fileapi::kFileSystemTypeRestrictedNativeLocal: | |
102 case fileapi::kFileSystemTypeNativeLocal: | |
103 case fileapi::kFileSystemTypeNativeForPlatformApp: | |
104 return true; | |
105 default: | |
106 return false; | |
107 } | |
108 } | |
109 | |
110 void CrosMountPointProvider::OpenFileSystem( | |
111 const GURL& origin_url, | |
112 fileapi::FileSystemType type, | |
113 fileapi::OpenFileSystemMode mode, | |
114 const OpenFileSystemCallback& callback) { | |
115 DCHECK(fileapi::IsolatedContext::IsIsolatedType(type)); | |
116 // Nothing to validate for external filesystem. | |
117 callback.Run(base::PLATFORM_FILE_OK); | |
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 fileapi::FilePermissionPolicy CrosMountPointProvider::GetPermissionPolicy( | |
240 const fileapi::FileSystemURL& url, int permissions) const { | |
241 if (url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal && | |
242 (permissions & ~fileapi::kReadFilePermissions)) { | |
243 // Restricted file system is read-only. | |
244 return fileapi::FILE_PERMISSION_ALWAYS_DENY; | |
245 } | |
246 | |
247 if (!IsAccessAllowed(url)) | |
248 return fileapi::FILE_PERMISSION_ALWAYS_DENY; | |
249 | |
250 // Permit access to mount points from internal WebUI. | |
251 const GURL& origin_url = url.origin(); | |
252 if (origin_url.SchemeIs(kChromeUIScheme)) | |
253 return fileapi::FILE_PERMISSION_ALWAYS_ALLOW; | |
254 | |
255 if (url.mount_type() == fileapi::kFileSystemTypeIsolated) { | |
256 // Permissions in isolated filesystems should be examined with | |
257 // FileSystem permission. | |
258 return fileapi::FILE_PERMISSION_USE_FILESYSTEM_PERMISSION; | |
259 } | |
260 | |
261 // Also apply system's file permission by default. | |
262 return fileapi::FILE_PERMISSION_USE_FILE_PERMISSION; | |
263 } | |
264 | |
265 fileapi::FileSystemOperation* CrosMountPointProvider::CreateFileSystemOperation( | |
266 const fileapi::FileSystemURL& url, | |
267 fileapi::FileSystemContext* context, | |
268 base::PlatformFileError* error_code) const { | |
269 DCHECK(url.is_valid()); | |
270 | |
271 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
272 fileapi::RemoteFileSystemProxyInterface* remote_proxy = | |
273 GetRemoteProxy(url.filesystem_id()); | |
274 if (!remote_proxy) { | |
275 *error_code = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
276 return NULL; | |
277 } | |
278 return new chromeos::RemoteFileSystemOperation(remote_proxy); | |
279 } | |
280 | |
281 DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal || | |
282 url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
283 scoped_ptr<fileapi::FileSystemOperationContext> operation_context( | |
284 new fileapi::FileSystemOperationContext(context)); | |
285 operation_context->set_root_path(GetFileSystemRootPath(url)); | |
286 return new fileapi::LocalFileSystemOperation(url, context, | |
287 operation_context.Pass()); | |
288 } | |
289 | |
290 scoped_ptr<webkit_blob::FileStreamReader> | |
291 CrosMountPointProvider::CreateFileStreamReader( | |
292 const fileapi::FileSystemURL& url, | |
293 int64 offset, | |
294 const base::Time& expected_modification_time, | |
295 fileapi::FileSystemContext* context) const { | |
296 DCHECK(url.is_valid()); | |
297 | |
298 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
299 fileapi::RemoteFileSystemProxyInterface* remote_proxy = | |
300 GetRemoteProxy(url.filesystem_id()); | |
301 if (!remote_proxy) | |
302 return scoped_ptr<webkit_blob::FileStreamReader>(); | |
303 return remote_proxy->CreateFileStreamReader( | |
304 context->task_runners()->file_task_runner(), | |
305 url, offset, expected_modification_time); | |
306 } | |
307 | |
308 return scoped_ptr<webkit_blob::FileStreamReader>( | |
309 new fileapi::FileSystemFileStreamReader( | |
310 context, url, offset, expected_modification_time)); | |
311 } | |
312 | |
313 scoped_ptr<fileapi::FileStreamWriter> | |
314 CrosMountPointProvider::CreateFileStreamWriter( | |
315 const fileapi::FileSystemURL& url, | |
316 int64 offset, | |
317 fileapi::FileSystemContext* context) const { | |
318 DCHECK(url.is_valid()); | |
319 | |
320 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
321 fileapi::RemoteFileSystemProxyInterface* remote_proxy = | |
322 GetRemoteProxy(url.filesystem_id()); | |
323 if (!remote_proxy) | |
324 return scoped_ptr<fileapi::FileStreamWriter>(); | |
325 return scoped_ptr<fileapi::FileStreamWriter>( | |
326 new fileapi::RemoteFileStreamWriter( | |
327 remote_proxy, url, offset, | |
328 context->task_runners()->file_task_runner())); | |
329 } | |
330 | |
331 if (url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal) | |
332 return scoped_ptr<fileapi::FileStreamWriter>(); | |
333 | |
334 DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal); | |
335 return scoped_ptr<fileapi::FileStreamWriter>( | |
336 new fileapi::LocalFileStreamWriter( | |
337 context->task_runners()->file_task_runner(), url.path(), offset)); | |
338 } | |
339 | |
340 bool CrosMountPointProvider::GetVirtualPath( | |
341 const base::FilePath& filesystem_path, | |
342 base::FilePath* virtual_path) { | |
343 return mount_points_->GetVirtualPath(filesystem_path, virtual_path) || | |
344 system_mount_points_->GetVirtualPath(filesystem_path, virtual_path); | |
345 } | |
346 | |
347 fileapi::RemoteFileSystemProxyInterface* CrosMountPointProvider::GetRemoteProxy( | |
348 const std::string& mount_name) const { | |
349 fileapi::RemoteFileSystemProxyInterface* proxy = | |
350 mount_points_->GetRemoteFileSystemProxy(mount_name); | |
351 if (proxy) | |
352 return proxy; | |
353 return system_mount_points_->GetRemoteFileSystemProxy(mount_name); | |
354 } | |
355 | |
356 base::FilePath CrosMountPointProvider::GetFileSystemRootPath( | |
357 const fileapi::FileSystemURL& url) const { | |
358 DCHECK(fileapi::IsolatedContext::IsIsolatedType(url.mount_type())); | |
359 if (!url.is_valid()) | |
360 return base::FilePath(); | |
361 | |
362 base::FilePath root_path; | |
363 std::string mount_name = url.filesystem_id(); | |
364 if (!mount_points_->GetRegisteredPath(mount_name, &root_path) && | |
365 !system_mount_points_->GetRegisteredPath(mount_name, &root_path)) { | |
366 return base::FilePath(); | |
367 } | |
368 | |
369 return root_path.DirName(); | |
370 } | |
371 | |
372 } // namespace chromeos | |
OLD | NEW |