- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_file_system_host.cc
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.
4
5 #include "content/renderer/pepper/pepper_file_system_host.h"
6
7 #include "base/bind.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"
25
26 namespace content {
27
28 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
29                                            PP_Instance instance,
30                                            PP_Resource resource,
31                                            PP_FileSystemType type)
32     : ResourceHost(host->GetPpapiHost(), instance, resource),
33       renderer_ppapi_host_(host),
34       type_(type),
35       opened_(false),
36       called_open_(false),
37       weak_factory_(this) {
38 }
39
40 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
41                                            PP_Instance instance,
42                                            PP_Resource resource,
43                                            const GURL& root_url,
44                                            PP_FileSystemType type)
45     : ResourceHost(host->GetPpapiHost(), instance, resource),
46       renderer_ppapi_host_(host),
47       type_(type),
48       opened_(true),
49       root_url_(root_url),
50       called_open_(true),
51       weak_factory_(this) {
52 }
53
54 PepperFileSystemHost::~PepperFileSystemHost() {
55 }
56
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,
63         OnHostMsgOpen)
64     PPAPI_DISPATCH_HOST_RESOURCE_CALL(
65         PpapiHostMsg_FileSystem_InitIsolatedFileSystem,
66         OnHostMsgInitIsolatedFileSystem)
67   IPC_END_MESSAGE_MAP()
68   return PP_ERROR_FAILED;
69 }
70
71 bool PepperFileSystemHost::IsFileSystemHost() {
72   return true;
73 }
74
75 void PepperFileSystemHost::DidOpenFileSystem(
76     const std::string& /* name_unused */,
77     const GURL& root) {
78   opened_ = true;
79   root_url_ = root;
80   reply_context_.params.set_result(PP_OK);
81   host()->SendReply(reply_context_, PpapiPluginMsg_FileSystem_OpenReply());
82   reply_context_ = ppapi::host::ReplyMessageContext();
83 }
84
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();
92 }
93
94 int32_t PepperFileSystemHost::OnHostMsgOpen(
95     ppapi::host::HostMessageContext* context,
96     int64_t expected_size) {
97   // Not allow multiple opens.
98   if (called_open_)
99     return PP_ERROR_INPROGRESS;
100   called_open_ = true;
101
102   fileapi::FileSystemType file_system_type;
103   switch (type_) {
104     case PP_FILESYSTEMTYPE_LOCALTEMPORARY:
105       file_system_type = fileapi::kFileSystemTypeTemporary;
106       break;
107     case PP_FILESYSTEMTYPE_LOCALPERSISTENT:
108       file_system_type = fileapi::kFileSystemTypePersistent;
109       break;
110     case PP_FILESYSTEMTYPE_EXTERNAL:
111       file_system_type = fileapi::kFileSystemTypeExternal;
112       break;
113     default:
114       return PP_ERROR_FAILED;
115   }
116
117   PepperPluginInstance* plugin_instance =
118       renderer_ppapi_host_->GetPluginInstance(pp_instance());
119   if (!plugin_instance)
120     return PP_ERROR_FAILED;
121
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()).
127           GetOrigin(),
128       file_system_type,
129       base::Bind(&PepperFileSystemHost::DidOpenFileSystem,
130                  weak_factory_.GetWeakPtr()),
131       base::Bind(&PepperFileSystemHost::DidFailOpenFileSystem,
132                  weak_factory_.GetWeakPtr()));
133   return PP_OK_COMPLETIONPENDING;
134 }
135
136 int32_t PepperFileSystemHost::OnHostMsgInitIsolatedFileSystem(
137     ppapi::host::HostMessageContext* context,
138     const std::string& fsid) {
139   // Do not allow multiple opens.
140   if (called_open_)
141     return PP_ERROR_INPROGRESS;
142   called_open_ = true;
143   // Do a sanity check.
144   if (!fileapi::ValidateIsolatedFileSystemId(fsid))
145     return PP_ERROR_BADARGUMENT;
146   RenderView* view =
147       renderer_ppapi_host_->GetRenderViewForInstance(pp_instance());
148   if (!view)
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"));
153   opened_ = true;
154   return PP_OK;
155 }
156
157 }  // namespace content