OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/file_io_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/ppapi_messages.h" |
| 11 #include "ppapi/shared_impl/array_writer.h" |
| 12 #include "ppapi/shared_impl/ppapi_globals.h" |
| 13 #include "ppapi/shared_impl/resource_tracker.h" |
| 14 #include "ppapi/thunk/ppb_file_ref_api.h" |
| 15 |
| 16 using ppapi::thunk::PPB_FileRef_API; |
| 17 |
| 18 namespace { |
| 19 |
| 20 // An adapter to let Read() share the same implementation with ReadToArray(). |
| 21 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { |
| 22 return user_data; |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 namespace ppapi { |
| 28 namespace proxy { |
| 29 |
| 30 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) |
| 31 : PluginResource(connection, instance), |
| 32 PPB_FileIO_Shared(), |
| 33 num_pending_ops_(0), |
| 34 pending_op_(OPERATION_NONE) { |
| 35 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); |
| 36 } |
| 37 |
| 38 FileIOResource::~FileIOResource() { |
| 39 } |
| 40 |
| 41 thunk::PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { |
| 42 return this; |
| 43 } |
| 44 |
| 45 int32_t FileIOResource::Open(PP_Resource file_ref, |
| 46 int32_t open_flags, |
| 47 scoped_refptr<TrackedCallback> callback) { |
| 48 temp_callback_ = callback; |
| 49 return PPB_FileIO_Shared::DoOpen(file_ref, open_flags); |
| 50 } |
| 51 |
| 52 int32_t FileIOResource::Query(PP_FileInfo* info, |
| 53 scoped_refptr<TrackedCallback> callback) { |
| 54 temp_callback_ = callback; |
| 55 temp_info_ = info; |
| 56 return DoQuery(); |
| 57 } |
| 58 |
| 59 int32_t FileIOResource::Touch(PP_Time last_access_time, |
| 60 PP_Time last_modified_time, |
| 61 scoped_refptr<TrackedCallback> callback) { |
| 62 temp_callback_ = callback; |
| 63 return DoTouch(last_access_time, last_modified_time); |
| 64 } |
| 65 |
| 66 int32_t FileIOResource::Read(int64_t offset, |
| 67 char* buffer, |
| 68 int32_t bytes_to_read, |
| 69 scoped_refptr<TrackedCallback> callback) { |
| 70 temp_callback_ = callback; |
| 71 temp_array_output_.GetDataBuffer = &DummyGetDataBuffer; |
| 72 temp_array_output_.user_data = buffer; |
| 73 return PPB_FileIO_Shared::DoRead(offset, bytes_to_read); |
| 74 } |
| 75 |
| 76 int32_t FileIOResource::ReadToArray(int64_t offset, |
| 77 int32_t max_read_length, |
| 78 PP_ArrayOutput* array_output, |
| 79 scoped_refptr<TrackedCallback> callback) { |
| 80 DCHECK(array_output); |
| 81 temp_callback_ = callback; |
| 82 temp_array_output_ = *array_output; |
| 83 return PPB_FileIO_Shared::DoRead(offset, max_read_length); |
| 84 } |
| 85 |
| 86 int32_t FileIOResource::Write(int64_t offset, |
| 87 const char* buffer, |
| 88 int32_t bytes_to_write, |
| 89 scoped_refptr<TrackedCallback> callback) { |
| 90 temp_callback_ = callback; |
| 91 return PPB_FileIO_Shared::DoWrite(offset, buffer, bytes_to_write); |
| 92 } |
| 93 |
| 94 int32_t FileIOResource::SetLength(int64_t length, |
| 95 scoped_refptr<TrackedCallback> callback) { |
| 96 temp_callback_ = callback; |
| 97 return PPB_FileIO_Shared::DoSetLength(length); |
| 98 } |
| 99 |
| 100 int32_t FileIOResource::Flush(scoped_refptr<TrackedCallback> callback) { |
| 101 temp_callback_ = callback; |
| 102 return PPB_FileIO_Shared::DoFlush(); |
| 103 } |
| 104 |
| 105 void FileIOResource::Close() { |
| 106 Post(RENDERER, PpapiHostMsg_FileIO_Close()); |
| 107 } |
| 108 |
| 109 int32_t FileIOResource::GetOSFileDescriptor() { |
| 110 return -1; // FIXME: deprecated |
| 111 } |
| 112 |
| 113 int32_t FileIOResource::WillWrite(int64_t offset, |
| 114 int32_t bytes_to_write, |
| 115 scoped_refptr<TrackedCallback> callback) { |
| 116 Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| 117 PpapiHostMsg_FileIO_WillWrite(offset, bytes_to_write), |
| 118 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| 119 callback)); |
| 120 SetOpInProgress(OPERATION_EXCLUSIVE); |
| 121 return PP_OK_COMPLETIONPENDING; |
| 122 } |
| 123 |
| 124 int32_t FileIOResource::WillSetLength(int64_t length, |
| 125 scoped_refptr<TrackedCallback> callback) { |
| 126 Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| 127 PpapiHostMsg_FileIO_WillSetLength(length), |
| 128 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| 129 callback)); |
| 130 SetOpInProgress(OPERATION_EXCLUSIVE); |
| 131 return PP_OK_COMPLETIONPENDING; |
| 132 } |
| 133 |
| 134 int32_t FileIOResource::CommonPreCondition(bool should_be_open, |
| 135 OperationType new_op) { |
| 136 if (!CheckOpenState(should_be_open)) |
| 137 return PP_ERROR_FAILED; |
| 138 |
| 139 if (pending_op_ != OPERATION_NONE && |
| 140 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) |
| 141 return PP_ERROR_INPROGRESS; |
| 142 |
| 143 return PP_OK; |
| 144 } |
| 145 |
| 146 void FileIOResource::CommonPostCondition(OperationType new_op) { |
| 147 SetOpInProgress(new_op); |
| 148 } |
| 149 |
| 150 int32_t FileIOResource::OpenValidated(PP_Resource file_ref_resource, |
| 151 PPB_FileRef_API* file_ref_api, |
| 152 int32_t open_flags) { |
| 153 Resource* file_ref_object = |
| 154 PpapiGlobals::Get()->GetResourceTracker()->GetResource(file_ref_resource); |
| 155 |
| 156 Call<PpapiPluginMsg_FileIO_OpenFileComplete>(RENDERER, |
| 157 PpapiHostMsg_FileIO_Open( |
| 158 file_ref_object->host_resource().host_resource(), |
| 159 open_flags), |
| 160 base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this, |
| 161 temp_callback_)); |
| 162 return PP_OK_COMPLETIONPENDING; |
| 163 } |
| 164 |
| 165 int32_t FileIOResource::QueryValidated() { |
| 166 Call<PpapiPluginMsg_FileIO_QueryComplete>(RENDERER, |
| 167 PpapiHostMsg_FileIO_Query(), |
| 168 base::Bind(&FileIOResource::OnPluginMsgQueryComplete, this, |
| 169 temp_callback_, temp_info_)); |
| 170 return PP_OK_COMPLETIONPENDING; |
| 171 } |
| 172 |
| 173 int32_t FileIOResource::TouchValidated(PP_Time last_access_time, |
| 174 PP_Time last_modified_time) { |
| 175 Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| 176 PpapiHostMsg_FileIO_Touch(last_access_time, last_modified_time), |
| 177 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| 178 temp_callback_)); |
| 179 return PP_OK_COMPLETIONPENDING; |
| 180 } |
| 181 |
| 182 int32_t FileIOResource::ReadValidated(int64_t offset, |
| 183 int32_t bytes_to_read) { |
| 184 Call<PpapiPluginMsg_FileIO_ReadComplete>(RENDERER, |
| 185 PpapiHostMsg_FileIO_Read(offset, bytes_to_read), |
| 186 base::Bind(&FileIOResource::OnPluginMsgReadComplete, this, |
| 187 temp_callback_, temp_array_output_)); |
| 188 return PP_OK_COMPLETIONPENDING; |
| 189 } |
| 190 |
| 191 int32_t FileIOResource::WriteValidated(int64_t offset, |
| 192 const char* buffer, |
| 193 int32_t bytes_to_write) { |
| 194 // TODO(brettw) it would be nice to use a shared memory buffer for large |
| 195 // writes rather than having to copy to a string (which will involve a number |
| 196 // of extra copies to serialize over IPC). |
| 197 Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| 198 PpapiHostMsg_FileIO_Write(offset, std::string(buffer, bytes_to_write)), |
| 199 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| 200 temp_callback_)); |
| 201 return PP_OK_COMPLETIONPENDING; |
| 202 } |
| 203 |
| 204 int32_t FileIOResource::SetLengthValidated(int64_t length) { |
| 205 Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| 206 PpapiHostMsg_FileIO_SetLength(length), |
| 207 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| 208 temp_callback_)); |
| 209 return PP_OK_COMPLETIONPENDING; |
| 210 } |
| 211 |
| 212 int32_t FileIOResource::FlushValidated() { |
| 213 Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| 214 PpapiHostMsg_FileIO_Flush(), |
| 215 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| 216 temp_callback_)); |
| 217 return PP_OK_COMPLETIONPENDING; |
| 218 } |
| 219 |
| 220 void FileIOResource::OnPluginMsgGeneralComplete( |
| 221 scoped_refptr<TrackedCallback> callback, |
| 222 const ResourceMessageReplyParams& params) { |
| 223 DCHECK(pending_op_ == OPERATION_EXCLUSIVE || |
| 224 pending_op_ == OPERATION_WRITE); |
| 225 callback->Run(params.result()); |
| 226 SetOpFinished(); |
| 227 } |
| 228 |
| 229 void FileIOResource::OnPluginMsgOpenFileComplete( |
| 230 scoped_refptr<TrackedCallback> callback, |
| 231 const ResourceMessageReplyParams& params) { |
| 232 DCHECK(pending_op_ == OPERATION_EXCLUSIVE); |
| 233 if (params.result() == PP_OK) |
| 234 SetOpenSucceed(); |
| 235 callback->Run(params.result()); |
| 236 SetOpFinished(); |
| 237 } |
| 238 |
| 239 void FileIOResource::OnPluginMsgQueryComplete( |
| 240 scoped_refptr<TrackedCallback> callback, |
| 241 PP_FileInfo* output_info, |
| 242 const ResourceMessageReplyParams& params, |
| 243 const PP_FileInfo& info) { |
| 244 DCHECK(pending_op_ == OPERATION_EXCLUSIVE); |
| 245 *output_info = info; |
| 246 callback->Run(params.result()); |
| 247 SetOpFinished(); |
| 248 } |
| 249 |
| 250 void FileIOResource::OnPluginMsgReadComplete( |
| 251 scoped_refptr<TrackedCallback> callback, |
| 252 PP_ArrayOutput array_output, |
| 253 const ResourceMessageReplyParams& params, |
| 254 const std::string& data) { |
| 255 DCHECK(pending_op_ == OPERATION_READ); |
| 256 |
| 257 // The result code should contain the data size if it's positive. |
| 258 int32_t result = params.result(); |
| 259 DCHECK((result < 0 && data.size() == 0) || |
| 260 result == static_cast<int32_t>(data.size())); |
| 261 |
| 262 ArrayWriter output; |
| 263 output.set_pp_array_output(array_output); |
| 264 if (output.is_valid()) |
| 265 output.StoreArray(data.data(), std::max(0, result)); |
| 266 |
| 267 callback->Run(result); |
| 268 SetOpFinished(); |
| 269 } |
| 270 |
| 271 void FileIOResource::SetOpInProgress(OperationType new_op) { |
| 272 DCHECK(pending_op_ == OPERATION_NONE || |
| 273 (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == new_op)); |
| 274 pending_op_ = new_op; |
| 275 num_pending_ops_++; |
| 276 } |
| 277 |
| 278 void FileIOResource::SetOpFinished() { |
| 279 DCHECK(num_pending_ops_ > 0); |
| 280 if (--num_pending_ops_ == 0) |
| 281 pending_op_ = OPERATION_NONE; |
| 282 } |
| 283 |
| 284 } // namespace proxy |
| 285 } // namespace ppapi |
OLD | NEW |