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 "ppapi/proxy/directory_reader_resource.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "ipc/ipc_message.h" |
| 11 #include "ppapi/c/pp_errors.h" |
| 12 #include "ppapi/proxy/dispatch_reply_message.h" |
| 13 #include "ppapi/proxy/plugin_globals.h" |
| 14 #include "ppapi/proxy/ppapi_messages.h" |
| 15 #include "ppapi/proxy/ppb_file_ref_proxy.h" |
| 16 #include "ppapi/shared_impl/ppapi_globals.h" |
| 17 #include "ppapi/shared_impl/resource.h" |
| 18 #include "ppapi/shared_impl/resource_tracker.h" |
| 19 #include "ppapi/thunk/enter.h" |
| 20 #include "ppapi/thunk/ppb_file_ref_api.h" |
| 21 |
| 22 using ppapi::proxy::PPB_FileRef_Proxy; |
| 23 |
| 24 namespace ppapi { |
| 25 namespace proxy { |
| 26 |
| 27 DirectoryReaderResource::DirectoryReaderResource( |
| 28 Connection connection, |
| 29 PP_Instance instance, |
| 30 PP_Resource directory_ref) |
| 31 : PluginResource(connection, instance), |
| 32 directory_ref_(directory_ref), |
| 33 has_more_(true) {} |
| 34 |
| 35 thunk::PPB_DirectoryReader_API* |
| 36 DirectoryReaderResource::AsPPB_DirectoryReader_API() { |
| 37 return this; |
| 38 } |
| 39 |
| 40 int32_t DirectoryReaderResource::GetNextEntry( |
| 41 PP_DirectoryEntry_Dev* entry, |
| 42 scoped_refptr<TrackedCallback> callback) { |
| 43 if (TrackedCallback::IsPending(callback_)) |
| 44 return PP_ERROR_INPROGRESS; |
| 45 |
| 46 entry_ = entry; |
| 47 callback_ = callback; |
| 48 |
| 49 if (FillUpEntry()) { |
| 50 callback_->Run(PP_OK); |
| 51 return PP_OK; |
| 52 } |
| 53 |
| 54 if (!sent_create_to_renderer()) |
| 55 SendCreate(RENDERER, PpapiHostMsg_DirectoryReader_Create()); |
| 56 |
| 57 ppapi::ResourceTracker* tracker = PpapiGlobals::Get()->GetResourceTracker(); |
| 58 PpapiHostMsg_DirectoryReader_GetNextEntry msg( |
| 59 tracker->GetResource(directory_ref_)->host_resource()); |
| 60 Call<PpapiPluginMsg_DirectoryReader_GetNextEntryReply>( |
| 61 RENDERER, msg, |
| 62 base::Bind(&DirectoryReaderResource::OnPluginMsgGetNextEntryReply, this)); |
| 63 return PP_OK_COMPLETIONPENDING; |
| 64 } |
| 65 |
| 66 void DirectoryReaderResource::OnPluginMsgGetNextEntryReply( |
| 67 const ResourceMessageReplyParams& params, |
| 68 const std::vector<ppapi::PPB_FileRef_CreateInfo> infos, |
| 69 const std::vector<PP_FileType> file_types) { |
| 70 DCHECK_EQ(infos.size(), file_types.size()); |
| 71 |
| 72 for (std::vector<ppapi::PPB_FileRef_CreateInfo>::size_type i = 0; |
| 73 i < infos.size(); ++i) { |
| 74 PP_DirectoryEntry_Dev entry; |
| 75 entry.file_ref = PPB_FileRef_Proxy::DeserializeFileRef(infos[i]); |
| 76 entry.file_type = file_types[i]; |
| 77 entries_.push(entry); |
| 78 } |
| 79 |
| 80 // Current implementation returns all entries at once. |
| 81 // TODO(nhiroki): DirectoryReader should return directory entries in multiple |
| 82 // chunks. |
| 83 has_more_ = false; |
| 84 |
| 85 FillUpEntry(); |
| 86 entry_ = NULL; |
| 87 |
| 88 callback_->Run(params.result()); |
| 89 } |
| 90 |
| 91 bool DirectoryReaderResource::FillUpEntry() { |
| 92 DCHECK(entry_); |
| 93 if (!entries_.empty()) { |
| 94 *entry_ = entries_.front(); |
| 95 entries_.pop(); |
| 96 return true; |
| 97 } |
| 98 |
| 99 if (!has_more_) { |
| 100 entry_->file_ref = 0; |
| 101 return true; |
| 102 } |
| 103 |
| 104 return false; |
| 105 } |
| 106 |
| 107 } // namespace proxy |
| 108 } // namespace ppapi |
OLD | NEW |