1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250

content / browser / renderer_host / pepper / pepper_file_ref_host.cc [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/pepper/pepper_file_ref_host.h"

#include <memory>
#include <string>

#include "content/browser/renderer_host/pepper/pepper_external_file_ref_backend.h"
#include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h"
#include "content/browser/renderer_host/pepper/pepper_internal_file_ref_backend.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_file_info.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/host/dispatch_host_message.h"
#include "ppapi/host/ppapi_host.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/file_ref_util.h"
#include "storage/browser/file_system/file_permission_policy.h"

using ppapi::host::ResourceHost;

namespace content {

PepperFileRefBackend::~PepperFileRefBackend() {}

PepperFileRefHost::PepperFileRefHost(BrowserPpapiHost* host,
                                     PP_Instance instance,
                                     PP_Resource resource,
                                     PP_Resource file_system,
                                     const std::string& path)
    : ResourceHost(host->GetPpapiHost(), instance, resource),
      host_(host),
      fs_type_(PP_FILESYSTEMTYPE_INVALID) {
  if (!ppapi::IsValidInternalPath(path))
    return;

  int render_process_id;
  int unused;
  if (!host->GetRenderFrameIDsForInstance(
          instance, &render_process_id, &unused)) {
    return;
  }

  ResourceHost* fs_resource_host =
      host->GetPpapiHost()->GetResourceHost(file_system);
  if (fs_resource_host == nullptr) {
    DLOG(ERROR) << "Couldn't find FileSystem host: " << resource
                << " path: " << path;
    return;
  }

  if (!fs_resource_host->IsFileSystemHost()) {
    DLOG(ERROR) << "Filesystem PP_Resource is not PepperFileSystemBrowserHost";
    return;
  }

  PepperFileSystemBrowserHost* file_system_host =
      static_cast<PepperFileSystemBrowserHost*>(fs_resource_host);
  file_system_host_ = file_system_host->AsWeakPtr();
  fs_type_ = file_system_host->GetType();
  if ((fs_type_ != PP_FILESYSTEMTYPE_LOCALPERSISTENT) &&
      (fs_type_ != PP_FILESYSTEMTYPE_LOCALTEMPORARY) &&
      (fs_type_ != PP_FILESYSTEMTYPE_EXTERNAL) &&
      (fs_type_ != PP_FILESYSTEMTYPE_ISOLATED)) {
    DLOG(ERROR) << "Unsupported filesystem type: " << fs_type_;
    return;
  }
  if ((fs_type_ == PP_FILESYSTEMTYPE_EXTERNAL) &&
      (!file_system_host->GetRootUrl().is_valid())) {
    DLOG(ERROR) << "Native external filesystems are not supported by this "
                << "constructor.";
    return;
  }

  backend_ = std::make_unique<PepperInternalFileRefBackend>(
      host->GetPpapiHost(), render_process_id, file_system_host->AsWeakPtr(),
      path);
}

PepperFileRefHost::PepperFileRefHost(BrowserPpapiHost* host,
                                     PP_Instance instance,
                                     PP_Resource resource,
                                     const base::FilePath& external_path)
    : ResourceHost(host->GetPpapiHost(), instance, resource),
      host_(host),
      fs_type_(PP_FILESYSTEMTYPE_EXTERNAL) {
  if (!ppapi::IsValidExternalPath(external_path))
    return;

  int render_process_id;
  int unused;
  if (!host->GetRenderFrameIDsForInstance(
          instance, &render_process_id, &unused)) {
    return;
  }

  backend_ = std::make_unique<PepperExternalFileRefBackend>(
      host->GetPpapiHost(), render_process_id, external_path);
}

PepperFileRefHost::~PepperFileRefHost() {}

bool PepperFileRefHost::IsFileRefHost() { return true; }

PP_FileSystemType PepperFileRefHost::GetFileSystemType() const {
  return fs_type_;
}

storage::FileSystemURL PepperFileRefHost::GetFileSystemURL() const {
  if (backend_)
    return backend_->GetFileSystemURL();
  return storage::FileSystemURL();
}

base::FilePath PepperFileRefHost::GetExternalFilePath() const {
  if (backend_)
    return backend_->GetExternalFilePath();
  return base::FilePath();
}

base::WeakPtr<PepperFileSystemBrowserHost>
PepperFileRefHost::GetFileSystemHost() const {
  return file_system_host_;
}

int32_t PepperFileRefHost::CanRead() const {
  if (backend_)
    return backend_->CanRead();
  return PP_ERROR_FAILED;
}

int32_t PepperFileRefHost::CanWrite() const {
  if (backend_)
    return backend_->CanWrite();
  return PP_ERROR_FAILED;
}

int32_t PepperFileRefHost::CanCreate() const {
  if (backend_)
    return backend_->CanCreate();
  return PP_ERROR_FAILED;
}

int32_t PepperFileRefHost::CanReadWrite() const {
  if (backend_)
    return backend_->CanReadWrite();
  return PP_ERROR_FAILED;
}

int32_t PepperFileRefHost::OnResourceMessageReceived(
    const IPC::Message& msg,
    ppapi::host::HostMessageContext* context) {
  if (!backend_)
    return PP_ERROR_FAILED;

  PPAPI_BEGIN_MESSAGE_MAP(PepperFileRefHost, msg)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileRef_MakeDirectory,
                                      OnMakeDirectory)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileRef_Touch, OnTouch)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileRef_Delete, OnDelete)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileRef_Rename, OnRename)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileRef_Query, OnQuery)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
        PpapiHostMsg_FileRef_ReadDirectoryEntries, OnReadDirectoryEntries)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileRef_GetAbsolutePath,
                                        OnGetAbsolutePath)
  PPAPI_END_MESSAGE_MAP()
  return PP_ERROR_FAILED;
}

int32_t PepperFileRefHost::OnMakeDirectory(
    ppapi::host::HostMessageContext* context,
    int32_t make_directory_flags) {
  int32_t rv = CanCreate();
  if (rv != PP_OK)
    return rv;
  return backend_->MakeDirectory(context->MakeReplyMessageContext(),
                                 make_directory_flags);
}

int32_t PepperFileRefHost::OnTouch(ppapi::host::HostMessageContext* context,
                                   PP_Time last_access_time,
                                   PP_Time last_modified_time) {
  // TODO(teravest): Change this to be kWriteFilePermissions here and in
  // fileapi_message_filter.
  int32_t rv = CanCreate();
  if (rv != PP_OK)
    return rv;
  return backend_->Touch(
      context->MakeReplyMessageContext(), last_access_time, last_modified_time);
}

int32_t PepperFileRefHost::OnDelete(ppapi::host::HostMessageContext* context) {
  int32_t rv = CanWrite();
  if (rv != PP_OK)
    return rv;
  return backend_->Delete(context->MakeReplyMessageContext());
}

int32_t PepperFileRefHost::OnRename(ppapi::host::HostMessageContext* context,
                                    PP_Resource new_file_ref) {
  int32_t rv = CanReadWrite();
  if (rv != PP_OK)
    return rv;

  ResourceHost* resource_host =
      host_->GetPpapiHost()->GetResourceHost(new_file_ref);
  if (!resource_host)
    return PP_ERROR_BADRESOURCE;

  PepperFileRefHost* file_ref_host = nullptr;
  if (resource_host->IsFileRefHost())
    file_ref_host = static_cast<PepperFileRefHost*>(resource_host);
  if (!file_ref_host)
    return PP_ERROR_BADRESOURCE;

  rv = file_ref_host->CanCreate();
  if (rv != PP_OK)
    return rv;

  return backend_->Rename(context->MakeReplyMessageContext(), file_ref_host);
}

int32_t PepperFileRefHost::OnQuery(ppapi::host::HostMessageContext* context) {
  int32_t rv = CanRead();
  if (rv != PP_OK)
    return rv;
  return backend_->Query(context->MakeReplyMessageContext());
}

int32_t PepperFileRefHost::OnReadDirectoryEntries(
    ppapi::host::HostMessageContext* context) {
  int32_t rv = CanRead();
  if (rv != PP_OK)
    return rv;
  return backend_->ReadDirectoryEntries(context->MakeReplyMessageContext());
}

int32_t PepperFileRefHost::OnGetAbsolutePath(
    ppapi::host::HostMessageContext* context) {
  if (!host_->GetPpapiHost()->permissions().HasPermission(
          ppapi::PERMISSION_PRIVATE))
    return PP_ERROR_NOACCESS;
  return backend_->GetAbsolutePath(context->MakeReplyMessageContext());
}

}  // namespace content