| OLD | NEW |
| (Empty) |
| 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_directory_reader_host.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/utf_string_conversions.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 "ppapi/shared_impl/ppb_file_ref_shared.h" | |
| 19 #include "ppapi/thunk/enter.h" | |
| 20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
| 21 #include "webkit/plugins/ppapi/resource_helper.h" | |
| 22 | |
| 23 using ppapi::thunk::EnterResource; | |
| 24 using ppapi::thunk::PPB_FileRef_API; | |
| 25 using webkit::ppapi::PPB_FileRef_Impl; | |
| 26 | |
| 27 namespace content { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 std::string FilePathStringToUTF8String(const base::FilePath::StringType& str) { | |
| 32 #if defined(OS_WIN) | |
| 33 return WideToUTF8(str); | |
| 34 #elif defined(OS_POSIX) | |
| 35 return str; | |
| 36 #else | |
| 37 #error "Unsupported platform." | |
| 38 #endif | |
| 39 } | |
| 40 | |
| 41 base::FilePath::StringType UTF8StringToFilePathString(const std::string& str) { | |
| 42 #if defined(OS_WIN) | |
| 43 return UTF8ToWide(str); | |
| 44 #elif defined(OS_POSIX) | |
| 45 return str; | |
| 46 #else | |
| 47 #error "Unsupported platform." | |
| 48 #endif | |
| 49 } | |
| 50 | |
| 51 class ReadDirectoryCallback : public NullFileSystemCallbackDispatcher { | |
| 52 public: | |
| 53 typedef base::Callback<void (const PepperDirectoryReaderHost::Entries&, | |
| 54 bool, int32_t)> | |
| 55 OnReadDirectoryCallback; | |
| 56 | |
| 57 explicit ReadDirectoryCallback(const OnReadDirectoryCallback& callback) | |
| 58 : callback_(callback) {} | |
| 59 virtual ~ReadDirectoryCallback() {} | |
| 60 | |
| 61 virtual void DidReadDirectory( | |
| 62 const std::vector<base::FileUtilProxy::Entry>& entries, | |
| 63 bool has_more) OVERRIDE { | |
| 64 callback_.Run(entries, has_more, PP_OK); | |
| 65 } | |
| 66 | |
| 67 virtual void DidFail(base::PlatformFileError platform_error) OVERRIDE { | |
| 68 callback_.Run(PepperDirectoryReaderHost::Entries(), false, | |
| 69 ppapi::PlatformFileErrorToPepperError(platform_error)); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 OnReadDirectoryCallback callback_; | |
| 74 }; | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 PepperDirectoryReaderHost::PepperDirectoryReaderHost( | |
| 79 RendererPpapiHost* host, | |
| 80 PP_Instance instance, | |
| 81 PP_Resource resource) | |
| 82 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
| 83 renderer_ppapi_host_(host), | |
| 84 weak_factory_(this) { | |
| 85 } | |
| 86 | |
| 87 PepperDirectoryReaderHost::~PepperDirectoryReaderHost() { | |
| 88 } | |
| 89 | |
| 90 int32_t PepperDirectoryReaderHost::OnResourceMessageReceived( | |
| 91 const IPC::Message& msg, | |
| 92 ppapi::host::HostMessageContext* context) { | |
| 93 IPC_BEGIN_MESSAGE_MAP(PepperDirectoryReaderHost, msg) | |
| 94 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
| 95 PpapiHostMsg_DirectoryReader_GetEntries, OnGetEntries) | |
| 96 IPC_END_MESSAGE_MAP() | |
| 97 return PP_ERROR_FAILED; | |
| 98 } | |
| 99 | |
| 100 int32_t PepperDirectoryReaderHost::OnGetEntries( | |
| 101 ppapi::host::HostMessageContext* host_context, | |
| 102 const ppapi::HostResource& resource) { | |
| 103 reply_context_ = host_context->MakeReplyMessageContext(); | |
| 104 | |
| 105 EnterResource<PPB_FileRef_API> enter(resource.host_resource(), true); | |
| 106 if (enter.failed()) | |
| 107 return PP_ERROR_FAILED; | |
| 108 directory_ref_ = static_cast<PPB_FileRef_Impl*>(enter.object()); | |
| 109 | |
| 110 if (directory_ref_->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | |
| 111 return PP_ERROR_FAILED; | |
| 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 if (!plugin_instance->delegate()->ReadDirectory( | |
| 119 directory_ref_->GetFileSystemURL(), | |
| 120 new ReadDirectoryCallback( | |
| 121 base::Bind(&PepperDirectoryReaderHost::OnReadDirectory, | |
| 122 weak_factory_.GetWeakPtr())))) | |
| 123 return PP_ERROR_FAILED; | |
| 124 return PP_OK_COMPLETIONPENDING; | |
| 125 } | |
| 126 | |
| 127 void PepperDirectoryReaderHost::OnReadDirectory(const Entries& entries, | |
| 128 bool has_more, | |
| 129 int32_t result) { | |
| 130 // The current filesystem backend always returns false. | |
| 131 DCHECK(!has_more); | |
| 132 if (result == PP_OK && !AddNewEntries(entries)) | |
| 133 result = PP_ERROR_FAILED; | |
| 134 SendGetEntriesReply(result); | |
| 135 } | |
| 136 | |
| 137 bool PepperDirectoryReaderHost::AddNewEntries(const Entries& entries) { | |
| 138 std::string dir_path = directory_ref_->GetCreateInfo().path; | |
| 139 if (dir_path[dir_path.size() - 1] != '/') | |
| 140 dir_path += '/'; | |
| 141 base::FilePath::StringType dir_file_path = | |
| 142 UTF8StringToFilePathString(dir_path); | |
| 143 | |
| 144 for (Entries::const_iterator it = entries.begin(); | |
| 145 it != entries.end(); ++it) { | |
| 146 EntryData data; | |
| 147 data.file_ref = PPB_FileRef_Impl::CreateInternal( | |
| 148 pp_instance(), | |
| 149 directory_ref_->file_system_resource(), | |
| 150 FilePathStringToUTF8String(dir_file_path + it->name)); | |
| 151 if (!data.file_ref) { | |
| 152 entry_data_.clear(); | |
| 153 return false; | |
| 154 } | |
| 155 data.file_type = it->is_directory ? | |
| 156 PP_FILETYPE_DIRECTORY : PP_FILETYPE_REGULAR; | |
| 157 entry_data_.push_back(data); | |
| 158 } | |
| 159 | |
| 160 return true; | |
| 161 } | |
| 162 | |
| 163 void PepperDirectoryReaderHost::SendGetEntriesReply(int32_t result) { | |
| 164 std::vector<ppapi::PPB_FileRef_CreateInfo> host_resources; | |
| 165 std::vector<PP_FileType> file_types; | |
| 166 | |
| 167 for (std::vector<EntryData>::iterator it = entry_data_.begin(); | |
| 168 it != entry_data_.end(); ++it) { | |
| 169 // Add a ref count on behalf of the plugin side. | |
| 170 it->file_ref->GetReference(); | |
| 171 host_resources.push_back(it->file_ref->GetCreateInfo()); | |
| 172 file_types.push_back(it->file_type); | |
| 173 } | |
| 174 entry_data_.clear(); | |
| 175 | |
| 176 reply_context_.params.set_result(result); | |
| 177 host()->SendReply( | |
| 178 reply_context_, | |
| 179 PpapiPluginMsg_DirectoryReader_GetEntriesReply(host_resources, | |
| 180 file_types)); | |
| 181 } | |
| 182 | |
| 183 PepperDirectoryReaderHost::EntryData::EntryData() { | |
| 184 } | |
| 185 | |
| 186 PepperDirectoryReaderHost::EntryData::~EntryData() { | |
| 187 } | |
| 188 | |
| 189 } // namespace content | |
| OLD | NEW |