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.
5 #include "content/browser/renderer_host/pepper/pepper_file_ref_host.h"
9 #include "content/browser/renderer_host/pepper/pepper_external_file_ref_backend.h"
10 #include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h"
11 #include "content/browser/renderer_host/pepper/pepper_internal_file_ref_backend.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/c/pp_file_info.h"
14 #include "ppapi/c/pp_instance.h"
15 #include "ppapi/c/pp_resource.h"
16 #include "ppapi/host/dispatch_host_message.h"
17 #include "ppapi/host/ppapi_host.h"
18 #include "ppapi/proxy/ppapi_messages.h"
19 #include "ppapi/shared_impl/file_ref_util.h"
20 #include "webkit/browser/fileapi/file_permission_policy.h"
22 using ppapi::host::ResourceHost
;
26 PepperFileRefBackend::~PepperFileRefBackend() {
29 PepperFileRefHost::PepperFileRefHost(BrowserPpapiHost
* host
,
32 PP_Resource file_system
,
33 const std::string
& path
)
34 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
36 fs_type_(PP_FILESYSTEMTYPE_INVALID
) {
37 if (!ppapi::IsValidInternalPath(path
))
40 int render_process_id
;
42 if (!host
->GetRenderFrameIDsForInstance(instance
,
48 ResourceHost
* fs_resource_host
=
49 host
->GetPpapiHost()->GetResourceHost(file_system
);
50 if (fs_resource_host
== NULL
) {
51 DLOG(ERROR
) << "Couldn't find FileSystem host: " << resource
56 if (!fs_resource_host
->IsFileSystemHost()) {
57 DLOG(ERROR
) << "Filesystem PP_Resource is not PepperFileSystemBrowserHost";
61 PepperFileSystemBrowserHost
* file_system_host
=
62 static_cast<PepperFileSystemBrowserHost
*>(fs_resource_host
);
63 file_system_host_
= file_system_host
->AsWeakPtr();
64 fs_type_
= file_system_host
->GetType();
65 if ((fs_type_
!= PP_FILESYSTEMTYPE_LOCALPERSISTENT
) &&
66 (fs_type_
!= PP_FILESYSTEMTYPE_LOCALTEMPORARY
) &&
67 (fs_type_
!= PP_FILESYSTEMTYPE_ISOLATED
)) {
68 DLOG(ERROR
) << "Unsupported filesystem type: " << fs_type_
;
72 backend_
.reset(new PepperInternalFileRefBackend(
75 file_system_host
->AsWeakPtr(),
79 PepperFileRefHost::PepperFileRefHost(BrowserPpapiHost
* host
,
82 const base::FilePath
& external_path
)
83 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
85 fs_type_(PP_FILESYSTEMTYPE_EXTERNAL
) {
86 if (!ppapi::IsValidExternalPath(external_path
))
89 int render_process_id
;
91 if (!host
->GetRenderFrameIDsForInstance(instance
,
97 backend_
.reset(new PepperExternalFileRefBackend(host
->GetPpapiHost(),
102 PepperFileRefHost::~PepperFileRefHost() {
105 bool PepperFileRefHost::IsFileRefHost() {
109 PP_FileSystemType
PepperFileRefHost::GetFileSystemType() const {
113 fileapi::FileSystemURL
PepperFileRefHost::GetFileSystemURL() const {
115 return backend_
->GetFileSystemURL();
116 return fileapi::FileSystemURL();
119 base::FilePath
PepperFileRefHost::GetExternalFilePath() const {
121 return backend_
->GetExternalFilePath();
122 return base::FilePath();
125 base::WeakPtr
<PepperFileSystemBrowserHost
>
126 PepperFileRefHost::GetFileSystemHost() const {
127 return file_system_host_
;
130 int32_t PepperFileRefHost::CanRead() const {
132 return backend_
->CanRead();
133 return PP_ERROR_FAILED
;
136 int32_t PepperFileRefHost::CanWrite() const {
138 return backend_
->CanWrite();
139 return PP_ERROR_FAILED
;
142 int32_t PepperFileRefHost::CanCreate() const {
144 return backend_
->CanCreate();
145 return PP_ERROR_FAILED
;
148 int32_t PepperFileRefHost::CanReadWrite() const {
150 return backend_
->CanReadWrite();
151 return PP_ERROR_FAILED
;
154 int32_t PepperFileRefHost::OnResourceMessageReceived(
155 const IPC::Message
& msg
,
156 ppapi::host::HostMessageContext
* context
) {
158 return PP_ERROR_FAILED
;
160 IPC_BEGIN_MESSAGE_MAP(PepperFileRefHost
, msg
)
161 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileRef_MakeDirectory
,
163 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileRef_Touch
,
165 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileRef_Delete
,
167 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileRef_Rename
,
169 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileRef_Query
,
171 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
172 PpapiHostMsg_FileRef_ReadDirectoryEntries
,
173 OnReadDirectoryEntries
);
174 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileRef_GetAbsolutePath
,
177 IPC_END_MESSAGE_MAP()
178 return PP_ERROR_FAILED
;
181 int32_t PepperFileRefHost::OnMakeDirectory(
182 ppapi::host::HostMessageContext
* context
,
183 int32_t make_directory_flags
) {
184 int32_t rv
= CanCreate();
187 return backend_
->MakeDirectory(
188 context
->MakeReplyMessageContext(), make_directory_flags
);
191 int32_t PepperFileRefHost::OnTouch(ppapi::host::HostMessageContext
* context
,
192 PP_Time last_access_time
,
193 PP_Time last_modified_time
) {
194 // TODO(teravest): Change this to be kWriteFilePermissions here and in
195 // fileapi_message_filter.
196 int32_t rv
= CanCreate();
199 return backend_
->Touch(context
->MakeReplyMessageContext(),
204 int32_t PepperFileRefHost::OnDelete(ppapi::host::HostMessageContext
* context
) {
205 int32_t rv
= CanWrite();
208 return backend_
->Delete(context
->MakeReplyMessageContext());
211 int32_t PepperFileRefHost::OnRename(ppapi::host::HostMessageContext
* context
,
212 PP_Resource new_file_ref
) {
213 int32_t rv
= CanReadWrite();
217 ResourceHost
* resource_host
=
218 host_
->GetPpapiHost()->GetResourceHost(new_file_ref
);
220 return PP_ERROR_BADRESOURCE
;
222 PepperFileRefHost
* file_ref_host
= NULL
;
223 if (resource_host
->IsFileRefHost())
224 file_ref_host
= static_cast<PepperFileRefHost
*>(resource_host
);
226 return PP_ERROR_BADRESOURCE
;
228 rv
= file_ref_host
->CanCreate();
232 return backend_
->Rename(context
->MakeReplyMessageContext(),
236 int32_t PepperFileRefHost::OnQuery(ppapi::host::HostMessageContext
* context
) {
237 int32_t rv
= CanRead();
240 return backend_
->Query(context
->MakeReplyMessageContext());
243 int32_t PepperFileRefHost::OnReadDirectoryEntries(
244 ppapi::host::HostMessageContext
* context
) {
245 int32_t rv
= CanRead();
248 return backend_
->ReadDirectoryEntries(context
->MakeReplyMessageContext());
251 int32_t PepperFileRefHost::OnGetAbsolutePath(
252 ppapi::host::HostMessageContext
* context
) {
253 if (!host_
->GetPpapiHost()->permissions().HasPermission(
254 ppapi::PERMISSION_PRIVATE
))
255 return PP_ERROR_NOACCESS
;
256 return backend_
->GetAbsolutePath(context
->MakeReplyMessageContext());
259 } // namespace content