Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / fileapi / file_system_backend.cc
1 // Copyright 2013 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 "chrome/browser/chromeos/fileapi/file_system_backend.h"
6
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/synchronization/lock.h"
13 #include "chrome/browser/chromeos/fileapi/file_access_permissions.h"
14 #include "chrome/browser/chromeos/fileapi/file_system_backend_delegate.h"
15 #include "chromeos/dbus/cros_disks_client.h"
16 #include "webkit/browser/blob/file_stream_reader.h"
17 #include "webkit/browser/fileapi/async_file_util.h"
18 #include "webkit/browser/fileapi/external_mount_points.h"
19 #include "webkit/browser/fileapi/file_stream_writer.h"
20 #include "webkit/browser/fileapi/file_system_context.h"
21 #include "webkit/browser/fileapi/file_system_operation.h"
22 #include "webkit/browser/fileapi/file_system_operation_context.h"
23 #include "webkit/browser/fileapi/file_system_url.h"
24 #include "webkit/browser/fileapi/isolated_context.h"
25
26 namespace {
27
28 const char kChromeUIScheme[] = "chrome";
29
30 }  // namespace
31
32 namespace chromeos {
33
34 // static
35 bool FileSystemBackend::CanHandleURL(const fileapi::FileSystemURL& url) {
36   if (!url.is_valid())
37     return false;
38   return url.type() == fileapi::kFileSystemTypeNativeLocal ||
39          url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal ||
40          url.type() == fileapi::kFileSystemTypeDrive;
41 }
42
43 FileSystemBackend::FileSystemBackend(
44     FileSystemBackendDelegate* drive_delegate,
45     scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy,
46     scoped_refptr<fileapi::ExternalMountPoints> mount_points,
47     fileapi::ExternalMountPoints* system_mount_points)
48     : special_storage_policy_(special_storage_policy),
49       file_access_permissions_(new FileAccessPermissions()),
50       local_file_util_(fileapi::AsyncFileUtil::CreateForLocalFileSystem()),
51       drive_delegate_(drive_delegate),
52       mount_points_(mount_points),
53       system_mount_points_(system_mount_points) {
54 }
55
56 FileSystemBackend::~FileSystemBackend() {
57 }
58
59 void FileSystemBackend::AddSystemMountPoints() {
60   // RegisterFileSystem() is no-op if the mount point with the same name
61   // already exists, hence it's safe to call without checking if a mount
62   // point already exists or not.
63   system_mount_points_->RegisterFileSystem(
64       "archive",
65       fileapi::kFileSystemTypeNativeLocal,
66       fileapi::FileSystemMountOption(),
67       chromeos::CrosDisksClient::GetArchiveMountPoint());
68   system_mount_points_->RegisterFileSystem(
69       "removable",
70       fileapi::kFileSystemTypeNativeLocal,
71       fileapi::FileSystemMountOption(fileapi::COPY_SYNC_OPTION_SYNC),
72       chromeos::CrosDisksClient::GetRemovableDiskMountPoint());
73   system_mount_points_->RegisterFileSystem(
74       "oem",
75       fileapi::kFileSystemTypeRestrictedNativeLocal,
76       fileapi::FileSystemMountOption(),
77       base::FilePath(FILE_PATH_LITERAL("/usr/share/oem")));
78 }
79
80 bool FileSystemBackend::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 FileSystemBackend::Initialize(fileapi::FileSystemContext* context) {
94 }
95
96 void FileSystemBackend::OpenFileSystem(
97     const GURL& origin_url,
98     fileapi::FileSystemType type,
99     fileapi::OpenFileSystemMode mode,
100     const OpenFileSystemCallback& callback) {
101   // TODO(nhiroki): Deprecate OpenFileSystem for non-sandboxed filesystem.
102   // (http://crbug.com/297412)
103   NOTREACHED();
104   callback.Run(GURL(), std::string(), base::File::FILE_ERROR_SECURITY);
105 }
106
107 fileapi::FileSystemQuotaUtil* FileSystemBackend::GetQuotaUtil() {
108   // No quota support.
109   return NULL;
110 }
111
112 bool FileSystemBackend::IsAccessAllowed(
113     const fileapi::FileSystemURL& url) const {
114   if (!url.is_valid())
115     return false;
116
117   // Permit access to mount points from internal WebUI.
118   const GURL& origin_url = url.origin();
119   if (origin_url.SchemeIs(kChromeUIScheme))
120     return true;
121
122   // No extra check is needed for isolated file systems.
123   if (url.mount_type() == fileapi::kFileSystemTypeIsolated)
124     return true;
125
126   if (!CanHandleURL(url))
127     return false;
128
129   std::string extension_id = origin_url.host();
130   // TODO(mtomasz): Temporarily whitelist TimeScapes. Remove this in M-31.
131   // See: crbug.com/271946
132   if (extension_id == "mlbmkoenclnokonejhlfakkeabdlmpek" &&
133       url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal) {
134     return true;
135   }
136
137   // Check first to make sure this extension has fileBrowserHander permissions.
138   if (!special_storage_policy_->IsFileHandler(extension_id))
139     return false;
140
141   return file_access_permissions_->HasAccessPermission(extension_id,
142                                                        url.virtual_path());
143 }
144
145 void FileSystemBackend::GrantFullAccessToExtension(
146     const std::string& extension_id) {
147   DCHECK(special_storage_policy_->IsFileHandler(extension_id));
148   if (!special_storage_policy_->IsFileHandler(extension_id))
149     return;
150   file_access_permissions_->GrantFullAccessPermission(extension_id);
151 }
152
153 void FileSystemBackend::GrantFileAccessToExtension(
154     const std::string& extension_id, const base::FilePath& virtual_path) {
155   // All we care about here is access from extensions for now.
156   DCHECK(special_storage_policy_->IsFileHandler(extension_id));
157   if (!special_storage_policy_->IsFileHandler(extension_id))
158     return;
159
160   std::string id;
161   fileapi::FileSystemType type;
162   base::FilePath path;
163   fileapi::FileSystemMountOption option;
164   if (!mount_points_->CrackVirtualPath(virtual_path,
165                                        &id, &type, &path, &option) &&
166       !system_mount_points_->CrackVirtualPath(virtual_path,
167                                               &id, &type, &path, &option)) {
168     return;
169   }
170
171   if (type == fileapi::kFileSystemTypeRestrictedNativeLocal) {
172     LOG(ERROR) << "Can't grant access for restricted mount point";
173     return;
174   }
175
176   file_access_permissions_->GrantAccessPermission(extension_id, virtual_path);
177 }
178
179 void FileSystemBackend::RevokeAccessForExtension(
180       const std::string& extension_id) {
181   file_access_permissions_->RevokePermissions(extension_id);
182 }
183
184 std::vector<base::FilePath> FileSystemBackend::GetRootDirectories() const {
185   std::vector<fileapi::MountPoints::MountPointInfo> mount_points;
186   mount_points_->AddMountPointInfosTo(&mount_points);
187   system_mount_points_->AddMountPointInfosTo(&mount_points);
188
189   std::vector<base::FilePath> root_dirs;
190   for (size_t i = 0; i < mount_points.size(); ++i)
191     root_dirs.push_back(mount_points[i].path);
192   return root_dirs;
193 }
194
195 fileapi::AsyncFileUtil* FileSystemBackend::GetAsyncFileUtil(
196     fileapi::FileSystemType type) {
197   if (type == fileapi::kFileSystemTypeDrive)
198     return drive_delegate_->GetAsyncFileUtil(type);
199
200   DCHECK(type == fileapi::kFileSystemTypeNativeLocal ||
201          type == fileapi::kFileSystemTypeRestrictedNativeLocal);
202   return local_file_util_.get();
203 }
204
205 fileapi::CopyOrMoveFileValidatorFactory*
206 FileSystemBackend::GetCopyOrMoveFileValidatorFactory(
207     fileapi::FileSystemType type, base::File::Error* error_code) {
208   DCHECK(error_code);
209   *error_code = base::File::FILE_OK;
210   return NULL;
211 }
212
213 fileapi::FileSystemOperation* FileSystemBackend::CreateFileSystemOperation(
214     const fileapi::FileSystemURL& url,
215     fileapi::FileSystemContext* context,
216     base::File::Error* error_code) const {
217   DCHECK(url.is_valid());
218
219   if (!IsAccessAllowed(url)) {
220     *error_code = base::File::FILE_ERROR_SECURITY;
221     return NULL;
222   }
223
224   DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal ||
225          url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal ||
226          url.type() == fileapi::kFileSystemTypeDrive);
227   return fileapi::FileSystemOperation::Create(
228       url, context,
229       make_scoped_ptr(new fileapi::FileSystemOperationContext(context)));
230 }
231
232 scoped_ptr<webkit_blob::FileStreamReader>
233 FileSystemBackend::CreateFileStreamReader(
234     const fileapi::FileSystemURL& url,
235     int64 offset,
236     const base::Time& expected_modification_time,
237     fileapi::FileSystemContext* context) const {
238   DCHECK(url.is_valid());
239
240   if (!IsAccessAllowed(url))
241     return scoped_ptr<webkit_blob::FileStreamReader>();
242
243   if (url.type() == fileapi::kFileSystemTypeDrive) {
244     return drive_delegate_->CreateFileStreamReader(
245         url, offset, expected_modification_time, context);
246   }
247
248   return scoped_ptr<webkit_blob::FileStreamReader>(
249       webkit_blob::FileStreamReader::CreateForFileSystemFile(
250           context, url, offset, expected_modification_time));
251 }
252
253 scoped_ptr<fileapi::FileStreamWriter>
254 FileSystemBackend::CreateFileStreamWriter(
255     const fileapi::FileSystemURL& url,
256     int64 offset,
257     fileapi::FileSystemContext* context) const {
258   DCHECK(url.is_valid());
259
260   if (!IsAccessAllowed(url))
261     return scoped_ptr<fileapi::FileStreamWriter>();
262
263   if (url.type() == fileapi::kFileSystemTypeDrive)
264     return drive_delegate_->CreateFileStreamWriter(url, offset, context);
265
266   if (url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal)
267     return scoped_ptr<fileapi::FileStreamWriter>();
268
269   DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal);
270   return scoped_ptr<fileapi::FileStreamWriter>(
271       fileapi::FileStreamWriter::CreateForLocalFile(
272           context->default_file_task_runner(), url.path(), offset));
273 }
274
275 bool FileSystemBackend::GetVirtualPath(
276     const base::FilePath& filesystem_path,
277     base::FilePath* virtual_path) {
278   return mount_points_->GetVirtualPath(filesystem_path, virtual_path) ||
279          system_mount_points_->GetVirtualPath(filesystem_path, virtual_path);
280 }
281
282 }  // namespace chromeos