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