1 // Copyright (c) 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/renderer/pepper/pepper_file_system_host.h"
8 #include "base/callback.h"
9 #include "content/child/child_thread.h"
10 #include "content/child/fileapi/file_system_dispatcher.h"
11 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
12 #include "content/public/renderer/render_view.h"
13 #include "content/public/renderer/renderer_ppapi_host.h"
14 #include "ppapi/c/pp_errors.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_type_conversion.h"
19 #include "third_party/WebKit/public/web/WebDocument.h"
20 #include "third_party/WebKit/public/web/WebElement.h"
21 #include "third_party/WebKit/public/web/WebFrame.h"
22 #include "third_party/WebKit/public/web/WebPluginContainer.h"
23 #include "third_party/WebKit/public/web/WebView.h"
24 #include "webkit/common/fileapi/file_system_util.h"
28 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
31 PP_FileSystemType type)
32 : ResourceHost(host->GetPpapiHost(), instance, resource),
33 renderer_ppapi_host_(host),
40 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
44 PP_FileSystemType type)
45 : ResourceHost(host->GetPpapiHost(), instance, resource),
46 renderer_ppapi_host_(host),
54 PepperFileSystemHost::~PepperFileSystemHost() {
57 int32_t PepperFileSystemHost::OnResourceMessageReceived(
58 const IPC::Message& msg,
59 ppapi::host::HostMessageContext* context) {
60 IPC_BEGIN_MESSAGE_MAP(PepperFileSystemHost, msg)
61 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
62 PpapiHostMsg_FileSystem_Open,
64 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
65 PpapiHostMsg_FileSystem_InitIsolatedFileSystem,
66 OnHostMsgInitIsolatedFileSystem)
68 return PP_ERROR_FAILED;
71 bool PepperFileSystemHost::IsFileSystemHost() {
75 void PepperFileSystemHost::DidOpenFileSystem(
76 const std::string& /* name_unused */,
80 reply_context_.params.set_result(PP_OK);
81 host()->SendReply(reply_context_, PpapiPluginMsg_FileSystem_OpenReply());
82 reply_context_ = ppapi::host::ReplyMessageContext();
85 void PepperFileSystemHost::DidFailOpenFileSystem(
86 base::PlatformFileError error) {
87 int32 pp_error = ppapi::PlatformFileErrorToPepperError(error);
88 opened_ = (pp_error == PP_OK);
89 reply_context_.params.set_result(pp_error);
90 host()->SendReply(reply_context_, PpapiPluginMsg_FileSystem_OpenReply());
91 reply_context_ = ppapi::host::ReplyMessageContext();
94 int32_t PepperFileSystemHost::OnHostMsgOpen(
95 ppapi::host::HostMessageContext* context,
96 int64_t expected_size) {
97 // Not allow multiple opens.
99 return PP_ERROR_INPROGRESS;
102 fileapi::FileSystemType file_system_type;
104 case PP_FILESYSTEMTYPE_LOCALTEMPORARY:
105 file_system_type = fileapi::kFileSystemTypeTemporary;
107 case PP_FILESYSTEMTYPE_LOCALPERSISTENT:
108 file_system_type = fileapi::kFileSystemTypePersistent;
110 case PP_FILESYSTEMTYPE_EXTERNAL:
111 file_system_type = fileapi::kFileSystemTypeExternal;
114 return PP_ERROR_FAILED;
117 PepperPluginInstance* plugin_instance =
118 renderer_ppapi_host_->GetPluginInstance(pp_instance());
119 if (!plugin_instance)
120 return PP_ERROR_FAILED;
122 FileSystemDispatcher* file_system_dispatcher =
123 ChildThread::current()->file_system_dispatcher();
124 reply_context_ = context->MakeReplyMessageContext();
125 file_system_dispatcher->OpenFileSystem(
126 GURL(plugin_instance->GetContainer()->element().document().url()).
129 base::Bind(&PepperFileSystemHost::DidOpenFileSystem,
130 weak_factory_.GetWeakPtr()),
131 base::Bind(&PepperFileSystemHost::DidFailOpenFileSystem,
132 weak_factory_.GetWeakPtr()));
133 return PP_OK_COMPLETIONPENDING;
136 int32_t PepperFileSystemHost::OnHostMsgInitIsolatedFileSystem(
137 ppapi::host::HostMessageContext* context,
138 const std::string& fsid) {
139 // Do not allow multiple opens.
141 return PP_ERROR_INPROGRESS;
143 // Do a sanity check.
144 if (!fileapi::ValidateIsolatedFileSystemId(fsid))
145 return PP_ERROR_BADARGUMENT;
147 renderer_ppapi_host_->GetRenderViewForInstance(pp_instance());
149 return PP_ERROR_FAILED;
150 const GURL& url = view->GetWebView()->mainFrame()->document().url();
151 root_url_ = GURL(fileapi::GetIsolatedFileSystemRootURIString(
152 url.GetOrigin(), fsid, "crxfs"));
157 } // namespace content