Chromium Code Reviews| Index: content/renderer/pepper/pepper_directory_reader_host.cc |
| diff --git a/content/renderer/pepper/pepper_directory_reader_host.cc b/content/renderer/pepper/pepper_directory_reader_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..79759264f56421368b81db641132d05037f2fa2a |
| --- /dev/null |
| +++ b/content/renderer/pepper/pepper_directory_reader_host.cc |
| @@ -0,0 +1,218 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/pepper/pepper_directory_reader_host.h" |
| + |
| +#include "content/public/renderer/renderer_ppapi_host.h" |
| +#include "ppapi/c/pp_completion_callback.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/host/dispatch_host_message.h" |
| +#include "ppapi/host/ppapi_host.h" |
| +#include "ppapi/proxy/enter_proxy.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| +#include "ppapi/proxy/ppb_file_ref_proxy.h" |
| +#include "ppapi/shared_impl/file_type_conversion.h" |
| +#include "ppapi/shared_impl/ppb_file_ref_shared.h" |
| +#include "webkit/plugins/ppapi/file_callbacks.h" |
| +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| +#include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
| +#include "webkit/plugins/ppapi/ppb_file_system_impl.h" |
| +#include "webkit/plugins/ppapi/resource_helper.h" |
| + |
| +using ppapi::proxy::PPB_FileRef_Proxy; |
| +using ppapi::thunk::EnterResource; |
| +using ppapi::thunk::EnterResourceCreation; |
| +using ppapi::thunk::PPB_DirectoryReader_API; |
| +using ppapi::thunk::PPB_FileRef_API; |
| +using webkit::ppapi::PPB_FileRef_Impl; |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +std::string FilePathStringToUTF8String(const FilePath::StringType& str) { |
| +#if defined(OS_WIN) |
| + return WideToUTF8(str); |
| +#elif defined(OS_POSIX) |
| + return str; |
| +#else |
| +#error "Unsupported platform." |
| +#endif |
| +} |
| + |
| +FilePath::StringType UTF8StringToFilePathString(const std::string& str) { |
| +#if defined(OS_WIN) |
| + return UTF8ToWide(str); |
| +#elif defined(OS_POSIX) |
| + return str; |
| +#else |
| +#error "Unsupported platform." |
| +#endif |
| +} |
| + |
| +class ReadDirectoryCallback : public fileapi::FileSystemCallbackDispatcher { |
| + public: |
| + ReadDirectoryCallback( |
| + scoped_refptr<ppapi::TrackedCallback> callback, |
| + const base::WeakPtr<content::PepperDirectoryReaderHost>& host) |
| + : callback_(callback), |
| + host_(host) {} |
|
dmichael (off chromium)
2013/01/29 21:22:42
^ nit: line up host_ with callback_
nhiroki
2013/01/30 09:24:51
Removed host_.
|
| + virtual ~ReadDirectoryCallback() {} |
| + |
| + virtual void DidSucceed() OVERRIDE { |
| + if (callback_->completed()) |
| + return; |
| + callback_->Run(PP_OK); |
| + } |
| + |
| + virtual void DidReadMetadata(const base::PlatformFileInfo& file_info, |
| + const FilePath& platform_path) OVERRIDE { |
| + NOTREACHED(); |
| + } |
| + |
| + virtual void DidReadDirectory( |
| + const std::vector<base::FileUtilProxy::Entry>& entries, |
| + bool has_more) OVERRIDE { |
| + if (callback_->completed()) |
| + return; |
| + if (host_ && host_->AddNewEntries(entries, has_more)) |
| + callback_->Run(PP_OK); |
| + else |
| + callback_->Run(PP_ERROR_FAILED); |
| + } |
| + |
| + virtual void DidOpenFileSystem(const std::string& name, |
| + const GURL& root) OVERRIDE { |
| + NOTREACHED(); |
| + } |
| + |
| + virtual void DidFail(base::PlatformFileError error) OVERRIDE { |
| + callback_->Run(ppapi::PlatformFileErrorToPepperError(error)); |
| + } |
| + |
| + virtual void DidWrite(int64 bytes, bool complete) OVERRIDE { |
| + NOTREACHED(); |
| + } |
| + |
| + virtual void DidOpenFile(base::PlatformFile file) OVERRIDE { |
| + NOTREACHED(); |
| + } |
| + |
| + private: |
| + scoped_refptr<ppapi::TrackedCallback> callback_; |
| + base::WeakPtr<content::PepperDirectoryReaderHost> host_; |
| +}; |
| + |
| +} // namespace |
| + |
| +PepperDirectoryReaderHost::PepperDirectoryReaderHost( |
| + RendererPpapiHost* host, |
| + PP_Instance instance, |
| + PP_Resource resource) |
| + : ResourceHost(host->GetPpapiHost(), instance, resource), |
| + renderer_ppapi_host_(host), |
| + directory_ref_(0), |
| + has_more_(true) { |
| +} |
| + |
| +PepperDirectoryReaderHost::~PepperDirectoryReaderHost() { |
| +} |
| + |
| +int32_t PepperDirectoryReaderHost::OnResourceMessageReceived( |
| + const IPC::Message& msg, |
| + ppapi::host::HostMessageContext* context) { |
| + IPC_BEGIN_MESSAGE_MAP(PepperDirectoryReaderHost, msg) |
| + PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
| + PpapiHostMsg_DirectoryReader_GetEntries, OnGetEntries) |
| + IPC_END_MESSAGE_MAP() |
| + return PP_ERROR_FAILED; |
| +} |
| + |
| +bool PepperDirectoryReaderHost::AddNewEntries( |
| + const std::vector<base::FileUtilProxy::Entry>& entries, |
| + bool has_more) { |
| + DCHECK(!entries.empty() || !has_more); |
| + has_more_ = has_more; |
| + |
| + std::string dir_path = directory_ref_->GetCreateInfo().path; |
| + if (dir_path[dir_path.size() - 1] != '/') |
| + dir_path += '/'; |
| + FilePath::StringType dir_file_path = UTF8StringToFilePathString(dir_path); |
| + DCHECK(!entries.empty()); |
| + |
| + for (std::vector<base::FileUtilProxy::Entry>::const_iterator it = |
| + entries.begin(); it != entries.end(); ++it) { |
|
dmichael (off chromium)
2013/01/29 21:22:42
nit: entries.begin() should be indented. I'd sugge
nhiroki
2013/01/30 09:24:51
Done.
|
| + PPB_FileRef_Impl* file_ref = PPB_FileRef_Impl::CreateInternal( |
|
dmichael (off chromium)
2013/01/29 21:22:42
You should usually wrap this immediately in a scop
nhiroki
2013/01/30 09:24:51
Done.
|
| + directory_ref_->file_system()->pp_resource(), |
| + FilePathStringToUTF8String(dir_file_path + it->name)); |
| + |
| + if (!file_ref) { |
| + host_resources_.clear(); |
| + file_types_.clear(); |
| + return false; |
| + } |
| + |
| + ppapi::PPB_FileRef_CreateInfo info; |
| + PPB_FileRef_Proxy::SerializeFileRef(file_ref->GetReference(), &info); |
|
dmichael (off chromium)
2013/01/29 21:22:42
We generally shouldn't be using stuff from ppapi/p
nhiroki
2013/01/30 09:24:51
Done.
|
| + host_resources_.push_back(info); |
| + file_types_.push_back(it->is_directory ? |
| + PP_FILETYPE_DIRECTORY : PP_FILETYPE_REGULAR); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +// static |
| +void PepperDirectoryReaderHost::OnGetEntriesCallback( |
| + void* data, |
| + int32_t result) { |
| + PepperDirectoryReaderHost* self = |
| + static_cast<PepperDirectoryReaderHost*>(data); |
| + self->SendGetEntriesReply(result); |
| +} |
| + |
| +int32_t PepperDirectoryReaderHost::OnGetEntries( |
| + ppapi::host::HostMessageContext* host_context, |
| + ppapi::HostResource resource) { |
| + reply_context_ = host_context->MakeReplyMessageContext(); |
| + |
| + EnterResource<PPB_FileRef_API> enter(resource.host_resource(), true); |
| + if (enter.failed()) |
| + return PP_ERROR_FAILED; |
| + directory_ref_ = static_cast<PPB_FileRef_Impl*>(enter.object()); |
| + |
| + if (directory_ref_->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) |
| + return PP_ERROR_FAILED; |
| + |
| + webkit::ppapi::PluginInstance* plugin_instance = |
| + renderer_ppapi_host_->GetPluginInstance(pp_instance()); |
| + if (!plugin_instance) |
| + return PP_ERROR_FAILED; |
| + |
| + ppapi::TrackedCallback* callback = new ppapi::TrackedCallback( |
| + ppapi::PpapiGlobals::Get()->GetResourceTracker()->GetResource( |
| + resource.host_resource()), |
| + PP_MakeCompletionCallback( |
| + &PepperDirectoryReaderHost::OnGetEntriesCallback, |
| + AsWeakPtr())); |
|
dmichael (off chromium)
2013/01/29 21:22:42
Why are you using completion callbacks instead of
nhiroki
2013/01/30 09:24:51
Done.
|
| + |
| + if (!plugin_instance->delegate()->ReadDirectory( |
| + directory_ref_->GetFileSystemURL(), |
| + new ReadDirectoryCallback(callback, AsWeakPtr()))) |
| + return PP_ERROR_FAILED; |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +void PepperDirectoryReaderHost::SendGetEntriesReply(int32_t result) { |
| + reply_context_.params.set_result(result); |
| + host()->SendReply( |
| + reply_context_, |
| + PpapiPluginMsg_DirectoryReader_GetEntriesReply(host_resources_, |
| + file_types_, |
| + has_more_)); |
| + host_resources_.clear(); |
| + file_types_.clear(); |
| +} |
| + |
| +} // namespace content |