Upstream version 7.36.149.0
[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_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"
24
25 namespace content {
26
27 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
28                                            PP_Instance instance,
29                                            PP_Resource resource,
30                                            PP_FileSystemType type)
31     : ResourceHost(host->GetPpapiHost(), instance, resource),
32       renderer_ppapi_host_(host),
33       type_(type),
34       opened_(false),
35       called_open_(false),
36       weak_factory_(this) {}
37
38 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
39                                            PP_Instance instance,
40                                            PP_Resource resource,
41                                            const GURL& root_url,
42                                            PP_FileSystemType type)
43     : ResourceHost(host->GetPpapiHost(), instance, resource),
44       renderer_ppapi_host_(host),
45       type_(type),
46       opened_(true),
47       root_url_(root_url),
48       called_open_(true),
49       weak_factory_(this) {}
50
51 PepperFileSystemHost::~PepperFileSystemHost() {}
52
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)
61   IPC_END_MESSAGE_MAP()
62   return PP_ERROR_FAILED;
63 }
64
65 bool PepperFileSystemHost::IsFileSystemHost() { return true; }
66
67 void PepperFileSystemHost::DidOpenFileSystem(
68     const std::string& /* name_unused */,
69     const GURL& root) {
70   opened_ = true;
71   root_url_ = root;
72   reply_context_.params.set_result(PP_OK);
73   host()->SendReply(reply_context_, PpapiPluginMsg_FileSystem_OpenReply());
74   reply_context_ = ppapi::host::ReplyMessageContext();
75 }
76
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();
83 }
84
85 int32_t PepperFileSystemHost::OnHostMsgOpen(
86     ppapi::host::HostMessageContext* context,
87     int64_t expected_size) {
88   // Not allow multiple opens.
89   if (called_open_)
90     return PP_ERROR_INPROGRESS;
91   called_open_ = true;
92
93   fileapi::FileSystemType file_system_type =
94       ppapi::PepperFileSystemTypeToFileSystemType(type_);
95   if (file_system_type == fileapi::kFileSystemTypeUnknown)
96     return PP_ERROR_FAILED;
97
98   GURL document_url = renderer_ppapi_host_->GetDocumentURL(pp_instance());
99   if (!document_url.is_valid())
100     return PP_ERROR_FAILED;
101
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(),
107       file_system_type,
108       base::Bind(&PepperFileSystemHost::DidOpenFileSystem,
109                  weak_factory_.GetWeakPtr()),
110       base::Bind(&PepperFileSystemHost::DidFailOpenFileSystem,
111                  weak_factory_.GetWeakPtr()));
112   return PP_OK_COMPLETIONPENDING;
113 }
114
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.
120   if (called_open_)
121     return PP_ERROR_INPROGRESS;
122   called_open_ = true;
123
124   // Do a sanity check.
125   if (!fileapi::ValidateIsolatedFileSystemId(fsid))
126     return PP_ERROR_BADARGUMENT;
127
128   RenderView* view =
129       renderer_ppapi_host_->GetRenderViewForInstance(pp_instance());
130   if (!view)
131     return PP_ERROR_FAILED;
132
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));
139   opened_ = true;
140   return PP_OK;
141 }
142
143 }  // namespace content