pepper_file_ref_host.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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_file_ref_host.h"
6
7#include <string>
8
9#include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h"
10#include "content/browser/renderer_host/pepper/pepper_internal_file_ref_backend.h"
11#include "ppapi/c/pp_errors.h"
12#include "ppapi/c/pp_file_info.h"
13#include "ppapi/c/pp_instance.h"
14#include "ppapi/c/pp_resource.h"
15#include "ppapi/host/dispatch_host_message.h"
16#include "ppapi/host/ppapi_host.h"
17#include "ppapi/proxy/ppapi_messages.h"
18#include "ppapi/shared_impl/file_ref_util.h"
19
20using ppapi::host::ResourceHost;
21
22namespace content {
23
24PepperFileRefBackend::~PepperFileRefBackend() {
25}
26
27PepperFileRefHost::PepperFileRefHost(BrowserPpapiHost* host,
28                                     PP_Instance instance,
29                                     PP_Resource resource,
30                                     PP_Resource file_system,
31                                     const std::string& path)
32    : ResourceHost(host->GetPpapiHost(), instance, resource),
33      host_(host),
34      fs_type_(PP_FILESYSTEMTYPE_INVALID) {
35  if (!ppapi::IsValidInternalPath(path))
36    return;
37
38  int render_process_id;
39  int unused;
40  if (!host->GetRenderViewIDsForInstance(instance,
41                                         &render_process_id,
42                                         &unused)) {
43    return;
44  }
45
46  ResourceHost* fs_resource_host =
47      host->GetPpapiHost()->GetResourceHost(file_system);
48  if (fs_resource_host == NULL) {
49    DLOG(ERROR) << "Couldn't find FileSystem host: " << resource
50                << " path: " << path;
51    return;
52  }
53
54  PepperFileSystemBrowserHost* fs_host =
55      fs_resource_host->AsPepperFileSystemBrowserHost();
56  if (fs_host == NULL) {
57    DLOG(ERROR) << "Filesystem PP_Resource is not PepperFileSystemBrowserHost";
58    return;
59  }
60
61  fs_type_ = fs_host->GetType();
62  // TODO(teravest): Add support for isolated filesystems.
63  if ((fs_type_ != PP_FILESYSTEMTYPE_LOCALPERSISTENT) &&
64      (fs_type_ != PP_FILESYSTEMTYPE_LOCALTEMPORARY)) {
65    DLOG(ERROR) << "Unsupported filesystem type: " << fs_type_;
66    return;
67  }
68
69  backend_.reset(new PepperInternalFileRefBackend(
70      host->GetPpapiHost(),
71      render_process_id,
72      base::AsWeakPtr(fs_host),
73      path));
74}
75
76PepperFileRefHost::~PepperFileRefHost() {
77}
78
79PepperFileRefHost* PepperFileRefHost::AsPepperFileRefHost() {
80  return this;
81}
82
83PP_FileSystemType PepperFileRefHost::GetFileSystemType() const {
84  return fs_type_;
85}
86
87fileapi::FileSystemURL PepperFileRefHost::GetFileSystemURL() const {
88  if (backend_)
89    return backend_->GetFileSystemURL();
90  return fileapi::FileSystemURL();
91}
92
93int32_t PepperFileRefHost::OnResourceMessageReceived(
94    const IPC::Message& msg,
95    ppapi::host::HostMessageContext* context) {
96  if (!backend_)
97    return PP_ERROR_FAILED;
98
99  IPC_BEGIN_MESSAGE_MAP(PepperFileRefHost, msg)
100    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileRef_MakeDirectory,
101                                      OnMakeDirectory);
102    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileRef_Touch,
103                                      OnTouch);
104    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileRef_Delete,
105                                        OnDelete);
106    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileRef_Rename,
107                                      OnRename);
108    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileRef_Query,
109                                        OnQuery);
110    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
111        PpapiHostMsg_FileRef_ReadDirectoryEntries,
112        OnReadDirectoryEntries);
113    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileRef_GetAbsolutePath,
114                                        OnGetAbsolutePath);
115
116  IPC_END_MESSAGE_MAP()
117  return PP_ERROR_FAILED;
118}
119
120int32_t PepperFileRefHost::OnMakeDirectory(
121    ppapi::host::HostMessageContext* context,
122    bool make_ancestors) {
123  return backend_->MakeDirectory(context->MakeReplyMessageContext(),
124                                 make_ancestors);
125}
126
127int32_t PepperFileRefHost::OnTouch(ppapi::host::HostMessageContext* context,
128                                   PP_Time last_access_time,
129                                   PP_Time last_modified_time) {
130  return backend_->Touch(context->MakeReplyMessageContext(),
131                         last_access_time,
132                         last_modified_time);
133}
134
135int32_t PepperFileRefHost::OnDelete(ppapi::host::HostMessageContext* context) {
136  return backend_->Delete(context->MakeReplyMessageContext());
137}
138
139int32_t PepperFileRefHost::OnRename(ppapi::host::HostMessageContext* context,
140                                    PP_Resource new_file_ref) {
141  return backend_->Rename(context->MakeReplyMessageContext(),
142                          new_file_ref);
143}
144
145int32_t PepperFileRefHost::OnQuery(ppapi::host::HostMessageContext* context) {
146  return backend_->Query(context->MakeReplyMessageContext());
147}
148
149int32_t PepperFileRefHost::OnReadDirectoryEntries(
150    ppapi::host::HostMessageContext* context) {
151  return backend_->ReadDirectoryEntries(context->MakeReplyMessageContext());
152}
153
154int32_t PepperFileRefHost::OnGetAbsolutePath(
155    ppapi::host::HostMessageContext* context) {
156  if (!host_->GetPpapiHost()->permissions().HasPermission(
157      ppapi::PERMISSION_PRIVATE))
158    return PP_ERROR_NOACCESS;
159  return backend_->GetAbsolutePath(context->MakeReplyMessageContext());
160}
161
162}  // namespace content
163