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"
30 bool LooksLikeAGuid(const std::string
& fsid
) {
31 const size_t kExpectedFsIdSize
= 32;
32 if (fsid
.size() != kExpectedFsIdSize
)
34 for (std::string::const_iterator it
= fsid
.begin(); it
!= fsid
.end(); ++it
) {
35 if (('A' <= *it
&& *it
<= 'F') ||
36 ('0' <= *it
&& *it
<= '9'))
45 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost
* host
,
48 PP_FileSystemType type
)
49 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
50 renderer_ppapi_host_(host
),
57 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost
* host
,
61 PP_FileSystemType type
)
62 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
63 renderer_ppapi_host_(host
),
71 PepperFileSystemHost::~PepperFileSystemHost() {
74 int32_t PepperFileSystemHost::OnResourceMessageReceived(
75 const IPC::Message
& msg
,
76 ppapi::host::HostMessageContext
* context
) {
77 IPC_BEGIN_MESSAGE_MAP(PepperFileSystemHost
, msg
)
78 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
79 PpapiHostMsg_FileSystem_Open
,
81 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
82 PpapiHostMsg_FileSystem_InitIsolatedFileSystem
,
83 OnHostMsgInitIsolatedFileSystem
)
85 return PP_ERROR_FAILED
;
88 bool PepperFileSystemHost::IsFileSystemHost() {
92 void PepperFileSystemHost::DidOpenFileSystem(
93 const std::string
& /* name_unused */,
97 reply_context_
.params
.set_result(PP_OK
);
98 host()->SendReply(reply_context_
, PpapiPluginMsg_FileSystem_OpenReply());
99 reply_context_
= ppapi::host::ReplyMessageContext();
102 void PepperFileSystemHost::DidFailOpenFileSystem(
103 base::PlatformFileError error
) {
104 int32 pp_error
= ppapi::PlatformFileErrorToPepperError(error
);
105 opened_
= (pp_error
== PP_OK
);
106 reply_context_
.params
.set_result(pp_error
);
107 host()->SendReply(reply_context_
, PpapiPluginMsg_FileSystem_OpenReply());
108 reply_context_
= ppapi::host::ReplyMessageContext();
111 int32_t PepperFileSystemHost::OnHostMsgOpen(
112 ppapi::host::HostMessageContext
* context
,
113 int64_t expected_size
) {
114 // Not allow multiple opens.
116 return PP_ERROR_INPROGRESS
;
119 fileapi::FileSystemType file_system_type
;
121 case PP_FILESYSTEMTYPE_LOCALTEMPORARY
:
122 file_system_type
= fileapi::kFileSystemTypeTemporary
;
124 case PP_FILESYSTEMTYPE_LOCALPERSISTENT
:
125 file_system_type
= fileapi::kFileSystemTypePersistent
;
127 case PP_FILESYSTEMTYPE_EXTERNAL
:
128 file_system_type
= fileapi::kFileSystemTypeExternal
;
131 return PP_ERROR_FAILED
;
134 PepperPluginInstance
* plugin_instance
=
135 renderer_ppapi_host_
->GetPluginInstance(pp_instance());
136 if (!plugin_instance
)
137 return PP_ERROR_FAILED
;
139 FileSystemDispatcher
* file_system_dispatcher
=
140 ChildThread::current()->file_system_dispatcher();
141 reply_context_
= context
->MakeReplyMessageContext();
142 file_system_dispatcher
->OpenFileSystem(
143 GURL(plugin_instance
->GetContainer()->element().document().url()).
146 base::Bind(&PepperFileSystemHost::DidOpenFileSystem
,
147 weak_factory_
.GetWeakPtr()),
148 base::Bind(&PepperFileSystemHost::DidFailOpenFileSystem
,
149 weak_factory_
.GetWeakPtr()));
150 return PP_OK_COMPLETIONPENDING
;
153 int32_t PepperFileSystemHost::OnHostMsgInitIsolatedFileSystem(
154 ppapi::host::HostMessageContext
* context
,
155 const std::string
& fsid
) {
156 // Do not allow multiple opens.
158 return PP_ERROR_INPROGRESS
;
160 // Do a sanity check.
161 if (!LooksLikeAGuid(fsid
))
162 return PP_ERROR_BADARGUMENT
;
164 renderer_ppapi_host_
->GetRenderViewForInstance(pp_instance());
166 return PP_ERROR_FAILED
;
167 const GURL
& url
= view
->GetWebView()->mainFrame()->document().url();
168 root_url_
= GURL(fileapi::GetIsolatedFileSystemRootURIString(
169 url
.GetOrigin(), fsid
, "crxfs"));
174 } // namespace content