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..b03081093627f9ae1b03736482f2142d819fe920 |
--- /dev/null |
+++ b/content/renderer/pepper/pepper_directory_reader_host.cc |
@@ -0,0 +1,212 @@ |
+// 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 "base/compiler_specific.h" |
+#include "base/utf_string_conversions.h" |
+#include "content/public/renderer/renderer_ppapi_host.h" |
+#include "ppapi/c/pp_errors.h" |
+#include "ppapi/host/dispatch_host_message.h" |
+#include "ppapi/host/ppapi_host.h" |
+#include "ppapi/proxy/ppapi_messages.h" |
+#include "ppapi/shared_impl/file_type_conversion.h" |
+#include "ppapi/shared_impl/ppb_file_ref_shared.h" |
+#include "ppapi/thunk/enter.h" |
+#include "webkit/fileapi/file_system_callback_dispatcher.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::thunk::EnterResource; |
+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: |
+ typedef base::Callback<void (const PepperDirectoryReaderHost::Entries&, |
+ bool, int32_t)> |
+ OnReadDirectoryCallback; |
+ |
+ explicit ReadDirectoryCallback(const OnReadDirectoryCallback& callback) |
+ : callback_(callback) {} |
+ virtual ~ReadDirectoryCallback() {} |
+ |
+ virtual void DidSucceed() OVERRIDE { |
+ NOTREACHED(); |
+ } |
+ |
+ 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 { |
+ callback_.Run(entries, has_more, PP_OK); |
+ } |
+ |
+ virtual void DidOpenFileSystem(const std::string& name, |
+ const GURL& root) OVERRIDE { |
+ NOTREACHED(); |
+ } |
+ |
+ virtual void DidFail(base::PlatformFileError error) OVERRIDE { |
+ callback_.Run(PepperDirectoryReaderHost::Entries(), |
+ false, |
+ ppapi::PlatformFileErrorToPepperError(error)); |
+ } |
+ |
+ virtual void DidWrite(int64 bytes, bool complete) OVERRIDE { |
+ NOTREACHED(); |
+ } |
+ |
+ virtual void DidOpenFile(base::PlatformFile file) OVERRIDE { |
+ NOTREACHED(); |
+ } |
+ |
+ private: |
+ OnReadDirectoryCallback callback_; |
+}; |
+ |
+} // namespace |
+ |
+PepperDirectoryReaderHost::PepperDirectoryReaderHost( |
+ RendererPpapiHost* host, |
+ PP_Instance instance, |
+ PP_Resource resource) |
+ : ResourceHost(host->GetPpapiHost(), instance, resource), |
+ renderer_ppapi_host_(host), |
+ weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
+} |
+ |
+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; |
+} |
+ |
+int32_t PepperDirectoryReaderHost::OnGetEntries( |
+ ppapi::host::HostMessageContext* host_context, |
+ const 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; |
+ |
+ if (!plugin_instance->delegate()->ReadDirectory( |
+ directory_ref_->GetFileSystemURL(), |
+ new ReadDirectoryCallback( |
+ base::Bind(&PepperDirectoryReaderHost::OnReadDirectory, |
+ weak_factory_.GetWeakPtr())))) |
+ return PP_ERROR_FAILED; |
+ return PP_OK_COMPLETIONPENDING; |
+} |
+ |
+void PepperDirectoryReaderHost::OnReadDirectory(const Entries& entries, |
+ bool has_more, |
+ int32_t result) { |
+ // The current filesystem backend always returns false. |
+ DCHECK(!has_more); |
+ if (result == PP_OK && !AddNewEntries(entries)) |
+ result = PP_ERROR_FAILED; |
+ SendGetEntriesReply(result); |
+} |
+ |
+bool PepperDirectoryReaderHost::AddNewEntries(const Entries& entries) { |
+ 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); |
+ |
+ for (Entries::const_iterator it = entries.begin(); |
+ it != entries.end(); |
+ ++it) { |
+ scoped_refptr<PPB_FileRef_Impl> file_ref = PPB_FileRef_Impl::CreateInternal( |
+ directory_ref_->file_system()->pp_resource(), |
+ FilePathStringToUTF8String(dir_file_path + it->name)); |
+ file_refs_.push_back(file_ref); |
+ |
+ if (!file_ref) { |
+ host_resources_.clear(); |
+ file_types_.clear(); |
+ ReleaseResources(); |
+ return false; |
+ } |
+ |
+ // Add a ref count on behalf of the plugin side. If the resources are not |
+ // sent for some reason, we should ensure the release of them. |
+ file_ref->GetReference(); |
yzshen1
2013/02/01 23:57:47
Is it better to do line 181-186 in SendGetEntriesR
nhiroki
2013/02/02 05:14:58
Done.
|
+ |
+ host_resources_.push_back(file_ref->GetCreateInfo()); |
+ file_types_.push_back(it->is_directory ? |
+ PP_FILETYPE_DIRECTORY : PP_FILETYPE_REGULAR); |
+ } |
+ |
+ return true; |
+} |
+ |
+void PepperDirectoryReaderHost::ReleaseResources() { |
+ ppapi::ResourceTracker* tracker = |
+ ppapi::PpapiGlobals::Get()->GetResourceTracker(); |
+ for (FileRefs::iterator it = file_refs_.begin(); |
+ it != file_refs_.end(); ++it) |
+ tracker->ReleaseResource((*it)->pp_resource()); |
+ file_refs_.clear(); |
+} |
+ |
+void PepperDirectoryReaderHost::SendGetEntriesReply(int32_t result) { |
+ reply_context_.params.set_result(result); |
+ host()->SendReply( |
+ reply_context_, |
+ PpapiPluginMsg_DirectoryReader_GetEntriesReply(host_resources_, |
+ file_types_)); |
+ host_resources_.clear(); |
+ file_types_.clear(); |
+} |
+ |
+} // namespace content |