Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / renderer / pepper / pepper_file_system_host.cc
blob1944ee3662c747f28e3ce70a306ae10b5e2b1a5c
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"
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"
26 namespace content {
28 namespace {
30 bool LooksLikeAGuid(const std::string& fsid) {
31 const size_t kExpectedFsIdSize = 32;
32 if (fsid.size() != kExpectedFsIdSize)
33 return false;
34 for (std::string::const_iterator it = fsid.begin(); it != fsid.end(); ++it) {
35 if (('A' <= *it && *it <= 'F') ||
36 ('0' <= *it && *it <= '9'))
37 continue;
38 return false;
40 return true;
43 } // namespace
45 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
46 PP_Instance instance,
47 PP_Resource resource,
48 PP_FileSystemType type)
49 : ResourceHost(host->GetPpapiHost(), instance, resource),
50 renderer_ppapi_host_(host),
51 type_(type),
52 opened_(false),
53 called_open_(false),
54 weak_factory_(this) {
57 PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host,
58 PP_Instance instance,
59 PP_Resource resource,
60 const GURL& root_url,
61 PP_FileSystemType type)
62 : ResourceHost(host->GetPpapiHost(), instance, resource),
63 renderer_ppapi_host_(host),
64 type_(type),
65 opened_(true),
66 root_url_(root_url),
67 called_open_(true),
68 weak_factory_(this) {
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,
80 OnHostMsgOpen)
81 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
82 PpapiHostMsg_FileSystem_InitIsolatedFileSystem,
83 OnHostMsgInitIsolatedFileSystem)
84 IPC_END_MESSAGE_MAP()
85 return PP_ERROR_FAILED;
88 bool PepperFileSystemHost::IsFileSystemHost() {
89 return true;
92 void PepperFileSystemHost::DidOpenFileSystem(
93 const std::string& /* name_unused */,
94 const GURL& root) {
95 opened_ = true;
96 root_url_ = root;
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.
115 if (called_open_)
116 return PP_ERROR_INPROGRESS;
117 called_open_ = true;
119 fileapi::FileSystemType file_system_type;
120 switch (type_) {
121 case PP_FILESYSTEMTYPE_LOCALTEMPORARY:
122 file_system_type = fileapi::kFileSystemTypeTemporary;
123 break;
124 case PP_FILESYSTEMTYPE_LOCALPERSISTENT:
125 file_system_type = fileapi::kFileSystemTypePersistent;
126 break;
127 case PP_FILESYSTEMTYPE_EXTERNAL:
128 file_system_type = fileapi::kFileSystemTypeExternal;
129 break;
130 default:
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()).
144 GetOrigin(),
145 file_system_type,
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.
157 if (called_open_)
158 return PP_ERROR_INPROGRESS;
159 called_open_ = true;
160 // Do a sanity check.
161 if (!LooksLikeAGuid(fsid))
162 return PP_ERROR_BADARGUMENT;
163 RenderView* view =
164 renderer_ppapi_host_->GetRenderViewForInstance(pp_instance());
165 if (!view)
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"));
170 opened_ = true;
171 return PP_OK;
174 } // namespace content