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