- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / pepper / pepper_internal_file_ref_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 "content/browser/renderer_host/pepper/pepper_internal_file_ref_backend.h"
6
7 #include <string>
8
9 #include "base/callback.h"
10 #include "base/file_util.h"
11 #include "base/files/file_util_proxy.h"
12 #include "content/browser/child_process_security_policy_impl.h"
13 #include "content/browser/fileapi/browser_file_system_helper.h"
14 #include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/storage_partition.h"
19 #include "net/base/escape.h"
20 #include "ppapi/c/pp_errors.h"
21 #include "ppapi/c/pp_file_info.h"
22 #include "ppapi/c/pp_instance.h"
23 #include "ppapi/c/pp_resource.h"
24 #include "ppapi/host/dispatch_host_message.h"
25 #include "ppapi/host/ppapi_host.h"
26 #include "ppapi/proxy/ppapi_messages.h"
27 #include "ppapi/shared_impl/file_ref_create_info.h"
28 #include "ppapi/shared_impl/file_ref_util.h"
29 #include "ppapi/shared_impl/file_type_conversion.h"
30 #include "ppapi/shared_impl/scoped_pp_var.h"
31 #include "ppapi/shared_impl/time_conversion.h"
32 #include "ppapi/shared_impl/var.h"
33 #include "ppapi/thunk/enter.h"
34 #include "ppapi/thunk/ppb_file_ref_api.h"
35 #include "ppapi/thunk/ppb_file_system_api.h"
36 #include "webkit/browser/fileapi/file_system_operation.h"
37 #include "webkit/browser/fileapi/file_system_operation_runner.h"
38 #include "webkit/browser/fileapi/file_system_url.h"
39 #include "webkit/common/fileapi/file_system_util.h"
40
41 using ppapi::host::PpapiHost;
42 using ppapi::host::ResourceHost;
43
44 namespace content {
45
46 PepperInternalFileRefBackend::PepperInternalFileRefBackend(
47     PpapiHost* host,
48     int render_process_id,
49     base::WeakPtr<PepperFileSystemBrowserHost> fs_host,
50     const std::string& path) : host_(host),
51                                render_process_id_(render_process_id),
52                                fs_host_(fs_host),
53                                fs_type_(fs_host->GetType()),
54                                path_(path),
55                                weak_factory_(this) {
56   ppapi::NormalizeInternalPath(&path_);
57 }
58
59 PepperInternalFileRefBackend::~PepperInternalFileRefBackend() {
60 }
61
62 fileapi::FileSystemURL PepperInternalFileRefBackend::GetFileSystemURL() const {
63   if (!fs_url_.is_valid() && fs_host_.get() && fs_host_->IsOpened()) {
64     GURL fs_path = fs_host_->GetRootUrl().Resolve(
65         net::EscapePath(path_.substr(1)));
66     scoped_refptr<fileapi::FileSystemContext> fs_context =
67         GetFileSystemContext();
68     if (fs_context.get())
69       fs_url_ = fs_context->CrackURL(fs_path);
70   }
71   return fs_url_;
72 }
73
74 base::FilePath PepperInternalFileRefBackend::GetExternalFilePath() const {
75   return base::FilePath();
76 }
77
78 scoped_refptr<fileapi::FileSystemContext>
79 PepperInternalFileRefBackend::GetFileSystemContext() const {
80   if (!fs_host_.get())
81     return NULL;
82   return fs_host_->GetFileSystemContext();
83 }
84
85 void PepperInternalFileRefBackend::DidFinish(
86     ppapi::host::ReplyMessageContext context,
87     const IPC::Message& msg,
88     base::PlatformFileError error) {
89   context.params.set_result(ppapi::PlatformFileErrorToPepperError(error));
90   host_->SendReply(context, msg);
91 }
92
93 int32_t PepperInternalFileRefBackend::MakeDirectory(
94     ppapi::host::ReplyMessageContext reply_context,
95     bool make_ancestors) {
96   if (!GetFileSystemURL().is_valid())
97     return PP_ERROR_FAILED;
98
99   GetFileSystemContext()->operation_runner()->CreateDirectory(
100       GetFileSystemURL(),
101       false,
102       make_ancestors,
103       base::Bind(&PepperInternalFileRefBackend::DidFinish,
104                  weak_factory_.GetWeakPtr(),
105                  reply_context,
106                  PpapiPluginMsg_FileRef_MakeDirectoryReply()));
107   return PP_OK_COMPLETIONPENDING;
108 }
109
110 int32_t PepperInternalFileRefBackend::Touch(
111     ppapi::host::ReplyMessageContext reply_context,
112     PP_Time last_access_time,
113     PP_Time last_modified_time) {
114   if (!GetFileSystemURL().is_valid())
115     return PP_ERROR_FAILED;
116
117   GetFileSystemContext()->operation_runner()->TouchFile(
118       GetFileSystemURL(),
119       ppapi::PPTimeToTime(last_access_time),
120       ppapi::PPTimeToTime(last_modified_time),
121       base::Bind(&PepperInternalFileRefBackend::DidFinish,
122                  weak_factory_.GetWeakPtr(),
123                  reply_context,
124                  PpapiPluginMsg_FileRef_TouchReply()));
125   return PP_OK_COMPLETIONPENDING;
126 }
127
128 int32_t PepperInternalFileRefBackend::Delete(
129     ppapi::host::ReplyMessageContext reply_context) {
130   if (!GetFileSystemURL().is_valid())
131     return PP_ERROR_FAILED;
132
133   GetFileSystemContext()->operation_runner()->Remove(
134       GetFileSystemURL(),
135       false,
136       base::Bind(&PepperInternalFileRefBackend::DidFinish,
137                  weak_factory_.GetWeakPtr(),
138                  reply_context,
139                  PpapiPluginMsg_FileRef_DeleteReply()));
140   return PP_OK_COMPLETIONPENDING;
141 }
142
143 int32_t PepperInternalFileRefBackend::Rename(
144     ppapi::host::ReplyMessageContext reply_context,
145     PepperFileRefHost* new_file_ref) {
146   if (!GetFileSystemURL().is_valid())
147     return PP_ERROR_FAILED;
148
149   fileapi::FileSystemURL new_url = new_file_ref->GetFileSystemURL();
150   if (!new_url.is_valid())
151     return PP_ERROR_FAILED;
152   if (!new_url.IsInSameFileSystem(GetFileSystemURL()))
153     return PP_ERROR_FAILED;
154
155   GetFileSystemContext()->operation_runner()->Move(
156       GetFileSystemURL(),
157       new_url,
158       fileapi::FileSystemOperation::OPTION_NONE,
159       base::Bind(&PepperInternalFileRefBackend::DidFinish,
160                  weak_factory_.GetWeakPtr(),
161                  reply_context,
162                  PpapiPluginMsg_FileRef_RenameReply()));
163   return PP_OK_COMPLETIONPENDING;
164 }
165
166 int32_t PepperInternalFileRefBackend::Query(
167     ppapi::host::ReplyMessageContext reply_context) {
168   if (!GetFileSystemURL().is_valid())
169     return PP_ERROR_FAILED;
170
171   GetFileSystemContext()->operation_runner()->GetMetadata(
172       GetFileSystemURL(),
173       base::Bind(&PepperInternalFileRefBackend::GetMetadataComplete,
174                  weak_factory_.GetWeakPtr(),
175                  reply_context));
176   return PP_OK_COMPLETIONPENDING;
177 }
178
179 void PepperInternalFileRefBackend::GetMetadataComplete(
180     ppapi::host::ReplyMessageContext reply_context,
181     base::PlatformFileError error,
182     const base::PlatformFileInfo& file_info) {
183   reply_context.params.set_result(ppapi::PlatformFileErrorToPepperError(error));
184
185   PP_FileInfo pp_file_info;
186   if (error == base::PLATFORM_FILE_OK)
187     ppapi::PlatformFileInfoToPepperFileInfo(file_info, fs_type_, &pp_file_info);
188   else
189     memset(&pp_file_info, 0, sizeof(pp_file_info));
190
191   host_->SendReply(reply_context,
192                    PpapiPluginMsg_FileRef_QueryReply(pp_file_info));
193 }
194
195 int32_t PepperInternalFileRefBackend::ReadDirectoryEntries(
196     ppapi::host::ReplyMessageContext reply_context) {
197   if (!GetFileSystemURL().is_valid())
198     return PP_ERROR_FAILED;
199
200   GetFileSystemContext()->operation_runner()->ReadDirectory(
201       GetFileSystemURL(),
202       base::Bind(&PepperInternalFileRefBackend::ReadDirectoryComplete,
203                  weak_factory_.GetWeakPtr(),
204                  reply_context));
205   return PP_OK_COMPLETIONPENDING;
206 }
207
208 void PepperInternalFileRefBackend::ReadDirectoryComplete(
209     ppapi::host::ReplyMessageContext context,
210     base::PlatformFileError error,
211     const fileapi::FileSystemOperation::FileEntryList& file_list,
212     bool has_more) {
213   // The current filesystem backend always returns false.
214   DCHECK(!has_more);
215
216   context.params.set_result(ppapi::PlatformFileErrorToPepperError(error));
217
218   std::vector<ppapi::FileRefCreateInfo> infos;
219   std::vector<PP_FileType> file_types;
220   if (error == base::PLATFORM_FILE_OK && fs_host_.get()) {
221     std::string dir_path = path_;
222     if (dir_path.empty() || dir_path[dir_path.size() - 1] != '/')
223       dir_path += '/';
224
225     for (fileapi::FileSystemOperation::FileEntryList::const_iterator it =
226          file_list.begin(); it != file_list.end(); ++it) {
227       if (it->is_directory)
228         file_types.push_back(PP_FILETYPE_DIRECTORY);
229       else
230         file_types.push_back(PP_FILETYPE_REGULAR);
231
232       ppapi::FileRefCreateInfo info;
233       info.file_system_type = fs_type_;
234       info.file_system_plugin_resource = fs_host_->pp_resource();
235       std::string path =
236           dir_path + fileapi::FilePathToString(base::FilePath(it->name));
237       info.internal_path = path;
238       info.display_name = ppapi::GetNameForInternalFilePath(path);
239       infos.push_back(info);
240     }
241   }
242
243   host_->SendReply(context,
244       PpapiPluginMsg_FileRef_ReadDirectoryEntriesReply(infos, file_types));
245 }
246
247 int32_t PepperInternalFileRefBackend::GetAbsolutePath(
248     ppapi::host::ReplyMessageContext reply_context) {
249   host_->SendReply(reply_context,
250       PpapiPluginMsg_FileRef_GetAbsolutePathReply(path_));
251   return PP_OK_COMPLETIONPENDING;
252 }
253
254 int32_t PepperInternalFileRefBackend::CanRead() const {
255   fileapi::FileSystemURL url = GetFileSystemURL();
256   if (!FileSystemURLIsValid(GetFileSystemContext().get(), url))
257     return PP_ERROR_FAILED;
258   if (!ChildProcessSecurityPolicyImpl::GetInstance()->
259           CanReadFileSystemFile(render_process_id_, url)) {
260     return PP_ERROR_NOACCESS;
261   }
262   return PP_OK;
263 }
264
265 int32_t PepperInternalFileRefBackend::CanWrite() const {
266   fileapi::FileSystemURL url = GetFileSystemURL();
267   if (!FileSystemURLIsValid(GetFileSystemContext().get(), url))
268     return PP_ERROR_FAILED;
269   if (!ChildProcessSecurityPolicyImpl::GetInstance()->
270           CanWriteFileSystemFile(render_process_id_, url)) {
271     return PP_ERROR_NOACCESS;
272   }
273   return PP_OK;
274 }
275
276 int32_t PepperInternalFileRefBackend::CanCreate() const {
277   fileapi::FileSystemURL url = GetFileSystemURL();
278   if (!FileSystemURLIsValid(GetFileSystemContext().get(), url))
279     return PP_ERROR_FAILED;
280   if (!ChildProcessSecurityPolicyImpl::GetInstance()->
281           CanCreateFileSystemFile(render_process_id_, url)) {
282     return PP_ERROR_NOACCESS;
283   }
284   return PP_OK;
285 }
286
287 int32_t PepperInternalFileRefBackend::CanReadWrite() const {
288   fileapi::FileSystemURL url = GetFileSystemURL();
289   if (!FileSystemURLIsValid(GetFileSystemContext().get(), url))
290     return PP_ERROR_FAILED;
291   ChildProcessSecurityPolicyImpl* policy =
292       ChildProcessSecurityPolicyImpl::GetInstance();
293   if (!policy->CanReadFileSystemFile(render_process_id_, url) ||
294       !policy->CanWriteFileSystemFile(render_process_id_, url)) {
295     return PP_ERROR_NOACCESS;
296   }
297   return PP_OK;
298 }
299
300 }  // namespace content