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 "content/public/renderer/renderer_ppapi_host.h" |
| 8 #include "ppapi/c/pp_completion_callback.h" |
| 9 #include "ppapi/c/pp_errors.h" |
| 10 #include "ppapi/host/dispatch_host_message.h" |
| 11 #include "ppapi/host/ppapi_host.h" |
| 12 #include "ppapi/proxy/ppapi_messages.h" |
| 13 #include "ppapi/proxy/ppb_file_ref_proxy.h" |
| 14 |
| 15 using ppapi::thunk::EnterResource; |
| 16 using ppapi::thunk::EnterResourceCreation; |
| 17 using ppapi::thunk::PPB_DirectoryReader_API; |
| 18 using ppapi::thunk::PPB_FileRef_API; |
| 19 |
| 20 namespace content { |
| 21 |
| 22 PepperDirectoryReaderHost::PepperDirectoryReaderHost( |
| 23 RendererPpapiHost* host, |
| 24 PP_Instance instance, |
| 25 PP_Resource resource) |
| 26 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 27 instance_(instance), |
| 28 directory_reader_(0) { |
| 29 } |
| 30 |
| 31 PepperDirectoryReaderHost::~PepperDirectoryReaderHost() { |
| 32 } |
| 33 |
| 34 int32_t PepperDirectoryReaderHost::OnResourceMessageReceived( |
| 35 const IPC::Message& msg, |
| 36 ppapi::host::HostMessageContext* context) { |
| 37 IPC_BEGIN_MESSAGE_MAP(PepperDirectoryReaderHost, msg) |
| 38 PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
| 39 PpapiHostMsg_DirectoryReader_GetNextEntry, OnGetNextEntry) |
| 40 IPC_END_MESSAGE_MAP() |
| 41 return PP_ERROR_FAILED; |
| 42 } |
| 43 |
| 44 // static |
| 45 void PepperDirectoryReaderHost::OnGetNextEntryCallback( |
| 46 void* data, |
| 47 int32_t result) { |
| 48 PepperDirectoryReaderHost* self = |
| 49 static_cast<PepperDirectoryReaderHost*>(data); |
| 50 self->PushEntry(self->entry_); |
| 51 |
| 52 if (result != PP_OK || !self->entry_.file_ref) { |
| 53 self->SendGetNextEntryReply(result); |
| 54 return; |
| 55 } |
| 56 |
| 57 self->GetNextEntry(); |
| 58 } |
| 59 |
| 60 int32_t PepperDirectoryReaderHost::GetNextEntry() { |
| 61 EnterResource<PPB_DirectoryReader_API> enter(directory_reader_, false); |
| 62 if (enter.failed()) |
| 63 return PP_ERROR_FAILED; |
| 64 |
| 65 ppapi::ResourceTracker* tracker = |
| 66 ppapi::PpapiGlobals::Get()->GetResourceTracker(); |
| 67 ppapi::TrackedCallback* callback = new ppapi::TrackedCallback( |
| 68 tracker->GetResource(directory_reader_), |
| 69 PP_MakeCompletionCallback( |
| 70 &PepperDirectoryReaderHost::OnGetNextEntryCallback, |
| 71 AsWeakPtr())); |
| 72 |
| 73 int32_t result = enter.object()->GetNextEntry(&entry_, callback); |
| 74 while (result == PP_OK && entry_.file_ref) { |
| 75 PushEntry(entry_); |
| 76 result = enter.object()->GetNextEntry(&entry_, callback); |
| 77 } |
| 78 if (!entry_.file_ref) |
| 79 SendGetNextEntryReply(result); |
| 80 return result; |
| 81 } |
| 82 |
| 83 int32_t PepperDirectoryReaderHost::OnGetNextEntry( |
| 84 ppapi::host::HostMessageContext* host_context, |
| 85 ppapi::HostResource resource) { |
| 86 reply_context_ = host_context->MakeReplyMessageContext(); |
| 87 host_resource_ = resource; |
| 88 |
| 89 ppapi::thunk::EnterResourceCreation enter(pp_instance()); |
| 90 if (enter.failed()) |
| 91 return PP_ERROR_FAILED; |
| 92 |
| 93 if (!directory_reader_) |
| 94 directory_reader_ = |
| 95 enter.functions()->CreateDirectoryReader(resource.host_resource()); |
| 96 |
| 97 return GetNextEntry(); |
| 98 } |
| 99 |
| 100 void PepperDirectoryReaderHost::SendGetNextEntryReply(int32_t result) { |
| 101 reply_context_.params.set_result(result); |
| 102 host()->SendReply( |
| 103 reply_context_, |
| 104 PpapiPluginMsg_DirectoryReader_GetNextEntryReply(host_resources_, |
| 105 file_types_)); |
| 106 } |
| 107 |
| 108 void PepperDirectoryReaderHost::PushEntry(const PP_DirectoryEntry_Dev& entry) { |
| 109 ppapi::PPB_FileRef_CreateInfo info; |
| 110 ppapi::proxy::PPB_FileRef_Proxy::SerializeFileRef(entry.file_ref, &info); |
| 111 |
| 112 host_resources_.push_back(info); |
| 113 file_types_.push_back(entry.file_type); |
| 114 } |
| 115 |
| 116 } // namespace content |
OLD | NEW |