pepper_file_system_host.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/common/child_thread.h" 10#include "content/common/fileapi/file_system_dispatcher.h" 11#include "content/public/renderer/renderer_ppapi_host.h" 12#include "content/renderer/pepper/null_file_system_callback_dispatcher.h" 13#include "ppapi/c/pp_errors.h" 14#include "ppapi/host/dispatch_host_message.h" 15#include "ppapi/host/ppapi_host.h" 16#include "ppapi/proxy/ppapi_messages.h" 17#include "ppapi/shared_impl/file_type_conversion.h" 18#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 19#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" 20#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 21#include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" 22#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" 23 24namespace content { 25 26namespace { 27 28class PlatformCallbackAdaptor : public NullFileSystemCallbackDispatcher { 29 public: 30 explicit PlatformCallbackAdaptor( 31 const base::WeakPtr<PepperFileSystemHost>& weak_host) 32 : weak_host_(weak_host) {} 33 34 virtual ~PlatformCallbackAdaptor() {} 35 36 virtual void DidOpenFileSystem(const std::string& /* unused */, 37 const GURL& root) OVERRIDE { 38 if (weak_host_) 39 weak_host_->OpenFileSystemReply(PP_OK, root); 40 } 41 42 virtual void DidFail(base::PlatformFileError platform_error) OVERRIDE { 43 if (weak_host_) { 44 weak_host_->OpenFileSystemReply( 45 ppapi::PlatformFileErrorToPepperError(platform_error), GURL()); 46 } 47 } 48 49 private: 50 base::WeakPtr<PepperFileSystemHost> weak_host_; 51}; 52 53} // namespace 54 55PepperFileSystemHost::PepperFileSystemHost(RendererPpapiHost* host, 56 PP_Instance instance, 57 PP_Resource resource, 58 PP_FileSystemType type) 59 : ResourceHost(host->GetPpapiHost(), instance, resource), 60 renderer_ppapi_host_(host), 61 weak_factory_(this), 62 type_(type), 63 opened_(false), 64 called_open_(false) { 65} 66 67PepperFileSystemHost::~PepperFileSystemHost() { 68} 69 70int32_t PepperFileSystemHost::OnResourceMessageReceived( 71 const IPC::Message& msg, 72 ppapi::host::HostMessageContext* context) { 73 IPC_BEGIN_MESSAGE_MAP(PepperFileSystemHost, msg) 74 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileSystem_Open, 75 OnHostMsgOpen) 76 IPC_END_MESSAGE_MAP() 77 return PP_ERROR_FAILED; 78} 79 80void PepperFileSystemHost::OpenFileSystemReply(int32_t pp_error, 81 const GURL& root) { 82 opened_ = (pp_error == PP_OK); 83 root_url_ = root; 84 reply_context_.params.set_result(pp_error); 85 host()->SendReply(reply_context_, 86 PpapiPluginMsg_FileSystem_OpenReply()); 87 reply_context_ = ppapi::host::ReplyMessageContext(); 88} 89 90int32_t PepperFileSystemHost::OnHostMsgOpen( 91 ppapi::host::HostMessageContext* context, 92 int64_t expected_size) { 93 // Not allow multiple opens. 94 if (called_open_) 95 return PP_ERROR_INPROGRESS; 96 called_open_ = true; 97 98 fileapi::FileSystemType file_system_type; 99 switch (type_) { 100 case PP_FILESYSTEMTYPE_LOCALTEMPORARY: 101 file_system_type = fileapi::kFileSystemTypeTemporary; 102 break; 103 case PP_FILESYSTEMTYPE_LOCALPERSISTENT: 104 file_system_type = fileapi::kFileSystemTypePersistent; 105 break; 106 case PP_FILESYSTEMTYPE_EXTERNAL: 107 file_system_type = fileapi::kFileSystemTypeExternal; 108 break; 109 default: 110 return PP_ERROR_FAILED; 111 } 112 113 webkit::ppapi::PluginInstance* plugin_instance = 114 renderer_ppapi_host_->GetPluginInstance(pp_instance()); 115 if (!plugin_instance) 116 return PP_ERROR_FAILED; 117 118 FileSystemDispatcher* file_system_dispatcher = 119 ChildThread::current()->file_system_dispatcher(); 120 reply_context_ = context->MakeReplyMessageContext(); 121 if (!file_system_dispatcher->OpenFileSystem( 122 GURL(plugin_instance->container()->element().document().url()). 123 GetOrigin(), 124 file_system_type, expected_size, true /* create */, 125 new PlatformCallbackAdaptor(weak_factory_.GetWeakPtr()))) { 126 return PP_ERROR_FAILED; 127 } 128 129 return PP_OK_COMPLETIONPENDING; 130} 131 132} // namespace content 133