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/renderer/pepper/pepper_file_ref_renderer_host.h"
6
7#include <string>
8
9#include "net/base/escape.h"
10#include "ppapi/c/pp_errors.h"
11#include "ppapi/host/ppapi_host.h"
12#include "ppapi/shared_impl/file_ref_util.h"
13
14namespace content {
15
16PepperFileRefRendererHost::PepperFileRefRendererHost(
17    RendererPpapiHost* host,
18    PP_Instance instance,
19    PP_Resource resource,
20    PP_Resource file_system,
21    const std::string& internal_path)
22    : ResourceHost(host->GetPpapiHost(), instance, resource),
23      file_system_type_(PP_FILESYSTEMTYPE_INVALID),
24      internal_path_(internal_path) {
25  if (!ppapi::IsValidInternalPath(internal_path))
26    return;
27  ResourceHost* fs_host = host->GetPpapiHost()->GetResourceHost(file_system);
28  if (fs_host && fs_host->IsFileSystemHost()) {
29    fs_host_ = base::AsWeakPtr(static_cast<PepperFileSystemHost*>(fs_host));
30    file_system_type_ = fs_host_->GetType();
31  }
32}
33
34PepperFileRefRendererHost::PepperFileRefRendererHost(
35    RendererPpapiHost* host,
36    PP_Instance instance,
37    PP_Resource resource,
38    const base::FilePath& external_path)
39    : ResourceHost(host->GetPpapiHost(), instance, resource),
40      file_system_type_(PP_FILESYSTEMTYPE_EXTERNAL),
41      external_path_(external_path) {
42  if (!ppapi::IsValidExternalPath(external_path))
43    file_system_type_ = PP_FILESYSTEMTYPE_INVALID;
44}
45
46PepperFileRefRendererHost::~PepperFileRefRendererHost() {}
47
48PP_FileSystemType PepperFileRefRendererHost::GetFileSystemType() const {
49  return file_system_type_;
50}
51
52GURL PepperFileRefRendererHost::GetFileSystemURL() const {
53  if (fs_host_.get() && fs_host_->IsOpened() &&
54      fs_host_->GetRootUrl().is_valid()) {
55    CHECK(!internal_path_.empty() && internal_path_[0] == '/');
56    // We strip off the leading slash when passing the URL to Resolve().
57    // Internal paths are required to be absolute, so we can require this.
58    return fs_host_->GetRootUrl().Resolve(
59        net::EscapePath(internal_path_.substr(1)));
60  }
61  return GURL();
62}
63
64base::FilePath PepperFileRefRendererHost::GetExternalFilePath() const {
65  if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL)
66    return base::FilePath();
67  return external_path_;
68}
69
70int32_t PepperFileRefRendererHost::OnResourceMessageReceived(
71    const IPC::Message& msg,
72    ppapi::host::HostMessageContext* context) {
73  // We don't handle any messages from the plugin in this host.
74  NOTREACHED();
75  return PP_ERROR_FAILED;
76}
77
78bool PepperFileRefRendererHost::IsFileRefHost() { return true; }
79
80}  // namespace content
81