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_system_util.h"
19 #include "ppapi/shared_impl/file_type_conversion.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebFrame.h"
22 #include "third_party/WebKit/public/web/WebView.h"
23 #include "webkit/common/fileapi/file_system_util.h"
27 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
30 PP_FileSystemType type)
31 : ResourceHost(host->GetPpapiHost(), instance, resource),
32 renderer_ppapi_host_(host),
36 weak_factory_(this) {}
38 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
42 PP_FileSystemType type)
43 : ResourceHost(host->GetPpapiHost(), instance, resource),
44 renderer_ppapi_host_(host),
49 weak_factory_(this) {}
51 PepperFileSystemHost::~PepperFileSystemHost() {}
53 int32_t PepperFileSystemHost::OnResourceMessageReceived(
54 const IPC::Message& msg,
55 ppapi::host::HostMessageContext* context) {
56 IPC_BEGIN_MESSAGE_MAP(PepperFileSystemHost, msg)
57 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileSystem_Open, OnHostMsgOpen)
58 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
59 PpapiHostMsg_FileSystem_InitIsolatedFileSystem,
60 OnHostMsgInitIsolatedFileSystem)
62 return PP_ERROR_FAILED;
65 bool PepperFileSystemHost::IsFileSystemHost() { return true; }
67 void PepperFileSystemHost::DidOpenFileSystem(
68 const std::string& /* name_unused */,
72 reply_context_.params.set_result(PP_OK);
73 host()->SendReply(reply_context_, PpapiPluginMsg_FileSystem_OpenReply());
74 reply_context_ = ppapi::host::ReplyMessageContext();
77 void PepperFileSystemHost::DidFailOpenFileSystem(base::File::Error error) {
78 int32 pp_error = ppapi::FileErrorToPepperError(error);
79 opened_ = (pp_error == PP_OK);
80 reply_context_.params.set_result(pp_error);
81 host()->SendReply(reply_context_, PpapiPluginMsg_FileSystem_OpenReply());
82 reply_context_ = ppapi::host::ReplyMessageContext();
85 int32_t PepperFileSystemHost::OnHostMsgOpen(
86 ppapi::host::HostMessageContext* context,
87 int64_t expected_size) {
88 // Not allow multiple opens.
90 return PP_ERROR_INPROGRESS;
93 fileapi::FileSystemType file_system_type =
94 ppapi::PepperFileSystemTypeToFileSystemType(type_);
95 if (file_system_type == fileapi::kFileSystemTypeUnknown)
96 return PP_ERROR_FAILED;
98 GURL document_url = renderer_ppapi_host_->GetDocumentURL(pp_instance());
99 if (!document_url.is_valid())
100 return PP_ERROR_FAILED;
102 FileSystemDispatcher* file_system_dispatcher =
103 ChildThread::current()->file_system_dispatcher();
104 reply_context_ = context->MakeReplyMessageContext();
105 file_system_dispatcher->OpenFileSystem(
106 document_url.GetOrigin(),
108 base::Bind(&PepperFileSystemHost::DidOpenFileSystem,
109 weak_factory_.GetWeakPtr()),
110 base::Bind(&PepperFileSystemHost::DidFailOpenFileSystem,
111 weak_factory_.GetWeakPtr()));
112 return PP_OK_COMPLETIONPENDING;
115 int32_t PepperFileSystemHost::OnHostMsgInitIsolatedFileSystem(
116 ppapi::host::HostMessageContext* context,
117 const std::string& fsid,
118 PP_IsolatedFileSystemType_Private type) {
119 // Do not allow multiple opens.
121 return PP_ERROR_INPROGRESS;
124 // Do a sanity check.
125 if (!fileapi::ValidateIsolatedFileSystemId(fsid))
126 return PP_ERROR_BADARGUMENT;
129 renderer_ppapi_host_->GetRenderViewForInstance(pp_instance());
131 return PP_ERROR_FAILED;
133 const GURL& url = view->GetWebView()->mainFrame()->document().url();
134 const std::string root_name = ppapi::IsolatedFileSystemTypeToRootName(type);
135 if (root_name.empty())
136 return PP_ERROR_BADARGUMENT;
137 root_url_ = GURL(fileapi::GetIsolatedFileSystemRootURIString(
138 url.GetOrigin(), fsid, root_name));
143 } // namespace content