OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/file_io_resource.h" | 5 #include "ppapi/proxy/file_io_resource.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "ipc/ipc_message.h" | 8 #include "ipc/ipc_message.h" |
9 #include "ppapi/c/pp_errors.h" | 9 #include "ppapi/c/pp_errors.h" |
10 #include "ppapi/proxy/ppapi_messages.h" | 10 #include "ppapi/proxy/ppapi_messages.h" |
11 #include "ppapi/shared_impl/array_writer.h" | 11 #include "ppapi/shared_impl/array_writer.h" |
12 #include "ppapi/shared_impl/file_type_conversion.h" | |
12 #include "ppapi/shared_impl/ppapi_globals.h" | 13 #include "ppapi/shared_impl/ppapi_globals.h" |
14 #include "ppapi/shared_impl/proxy_lock.h" | |
13 #include "ppapi/shared_impl/resource_tracker.h" | 15 #include "ppapi/shared_impl/resource_tracker.h" |
14 #include "ppapi/thunk/enter.h" | 16 #include "ppapi/thunk/enter.h" |
15 #include "ppapi/thunk/ppb_file_ref_api.h" | 17 #include "ppapi/thunk/ppb_file_ref_api.h" |
16 | 18 |
17 using ppapi::thunk::EnterResourceNoLock; | 19 using ppapi::thunk::EnterResourceNoLock; |
18 using ppapi::thunk::PPB_FileIO_API; | 20 using ppapi::thunk::PPB_FileIO_API; |
19 using ppapi::thunk::PPB_FileRef_API; | 21 using ppapi::thunk::PPB_FileRef_API; |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 // An adapter to let Read() share the same implementation with ReadToArray(). | 25 // An adapter to let Read() share the same implementation with ReadToArray(). |
24 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { | 26 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { |
25 return user_data; | 27 return user_data; |
26 } | 28 } |
27 | 29 |
30 // File thread task to close the file handle. | |
31 void DoClose(base::PlatformFile file) { | |
32 base::ClosePlatformFile(file); | |
33 } | |
34 | |
28 } // namespace | 35 } // namespace |
29 | 36 |
30 namespace ppapi { | 37 namespace ppapi { |
31 namespace proxy { | 38 namespace proxy { |
32 | 39 |
33 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) | 40 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) |
34 : PluginResource(connection, instance) { | 41 : PluginResource(connection, instance), |
42 file_handle_(base::kInvalidPlatformFileValue), | |
43 file_system_type_(PP_FILESYSTEMTYPE_INVALID) { | |
35 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); | 44 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); |
36 } | 45 } |
37 | 46 |
38 FileIOResource::~FileIOResource() { | 47 FileIOResource::~FileIOResource() { |
48 CloseFileHandle(); | |
39 } | 49 } |
40 | 50 |
41 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { | 51 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { |
42 return this; | 52 return this; |
43 } | 53 } |
44 | 54 |
45 int32_t FileIOResource::Open(PP_Resource file_ref, | 55 int32_t FileIOResource::Open(PP_Resource file_ref, |
46 int32_t open_flags, | 56 int32_t open_flags, |
47 scoped_refptr<TrackedCallback> callback) { | 57 scoped_refptr<TrackedCallback> callback) { |
48 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); | 58 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); |
49 if (enter.failed()) | 59 if (enter.failed()) |
50 return PP_ERROR_BADRESOURCE; | 60 return PP_ERROR_BADRESOURCE; |
51 | 61 |
62 PPB_FileRef_API* file_ref_api = enter.object(); | |
63 PP_FileSystemType type = file_ref_api->GetFileSystemType(); | |
64 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | |
65 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY && | |
66 type != PP_FILESYSTEMTYPE_EXTERNAL && | |
67 type != PP_FILESYSTEMTYPE_ISOLATED) { | |
68 NOTREACHED(); | |
69 return PP_ERROR_FAILED; | |
70 } | |
71 file_system_type_ = type; | |
72 | |
52 int32_t rv = state_manager_.CheckOperationState( | 73 int32_t rv = state_manager_.CheckOperationState( |
53 FileIOStateManager::OPERATION_EXCLUSIVE, false); | 74 FileIOStateManager::OPERATION_EXCLUSIVE, false); |
54 if (rv != PP_OK) | 75 if (rv != PP_OK) |
55 return rv; | 76 return rv; |
56 | 77 |
57 Call<PpapiPluginMsg_FileIO_OpenReply>(RENDERER, | 78 Call<PpapiPluginMsg_FileIO_OpenReply>(RENDERER, |
58 PpapiHostMsg_FileIO_Open( | 79 PpapiHostMsg_FileIO_Open( |
59 enter.resource()->host_resource().host_resource(), | 80 enter.resource()->host_resource().host_resource(), |
60 open_flags), | 81 open_flags), |
61 base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this, | 82 base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this, |
62 callback)); | 83 callback)); |
63 | 84 |
64 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 85 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
65 return PP_OK_COMPLETIONPENDING; | 86 return PP_OK_COMPLETIONPENDING; |
66 } | 87 } |
67 | 88 |
68 int32_t FileIOResource::Query(PP_FileInfo* info, | 89 int32_t FileIOResource::Query(PP_FileInfo* info, |
69 scoped_refptr<TrackedCallback> callback) { | 90 scoped_refptr<TrackedCallback> callback) { |
70 int32_t rv = state_manager_.CheckOperationState( | 91 int32_t rv = state_manager_.CheckOperationState( |
71 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 92 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
72 if (rv != PP_OK) | 93 if (rv != PP_OK) |
73 return rv; | 94 return rv; |
74 | 95 |
96 if (callback->is_blocking() && | |
97 file_handle_ != base::kInvalidPlatformFileValue) { | |
98 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
99 int32_t result = PP_ERROR_FAILED; | |
100 base::PlatformFileInfo file_info; | |
101 // Release the proxy lock while making a potentially blocking file call. | |
102 { | |
103 ProxyAutoUnlock unlock; | |
104 result = base::GetPlatformFileInfo(file_handle_, &file_info) ? | |
105 PP_OK : PP_ERROR_FAILED; | |
106 } | |
107 if ((result == PP_OK) && TrackedCallback::IsPending(callback)) { | |
108 ppapi::PlatformFileInfoToPepperFileInfo(file_info, file_system_type_, | |
109 info); | |
110 } | |
111 | |
112 state_manager_.SetOperationFinished(); | |
113 return result; | |
114 } | |
115 | |
75 Call<PpapiPluginMsg_FileIO_QueryReply>(RENDERER, | 116 Call<PpapiPluginMsg_FileIO_QueryReply>(RENDERER, |
76 PpapiHostMsg_FileIO_Query(), | 117 PpapiHostMsg_FileIO_Query(), |
77 base::Bind(&FileIOResource::OnPluginMsgQueryComplete, this, | 118 base::Bind(&FileIOResource::OnPluginMsgQueryComplete, this, |
78 callback, info)); | 119 callback, info)); |
79 | 120 |
80 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 121 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
81 return PP_OK_COMPLETIONPENDING; | 122 return PP_OK_COMPLETIONPENDING; |
82 } | 123 } |
83 | 124 |
84 int32_t FileIOResource::Touch(PP_Time last_access_time, | 125 int32_t FileIOResource::Touch(PP_Time last_access_time, |
(...skipping 18 matching lines...) Expand all Loading... | |
103 int32_t bytes_to_read, | 144 int32_t bytes_to_read, |
104 scoped_refptr<TrackedCallback> callback) { | 145 scoped_refptr<TrackedCallback> callback) { |
105 int32_t rv = state_manager_.CheckOperationState( | 146 int32_t rv = state_manager_.CheckOperationState( |
106 FileIOStateManager::OPERATION_READ, true); | 147 FileIOStateManager::OPERATION_READ, true); |
107 if (rv != PP_OK) | 148 if (rv != PP_OK) |
108 return rv; | 149 return rv; |
109 | 150 |
110 PP_ArrayOutput output_adapter; | 151 PP_ArrayOutput output_adapter; |
111 output_adapter.GetDataBuffer = &DummyGetDataBuffer; | 152 output_adapter.GetDataBuffer = &DummyGetDataBuffer; |
112 output_adapter.user_data = buffer; | 153 output_adapter.user_data = buffer; |
113 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); | |
114 return ReadValidated(offset, bytes_to_read, output_adapter, callback); | 154 return ReadValidated(offset, bytes_to_read, output_adapter, callback); |
115 } | 155 } |
116 | 156 |
117 int32_t FileIOResource::ReadToArray(int64_t offset, | 157 int32_t FileIOResource::ReadToArray(int64_t offset, |
118 int32_t max_read_length, | 158 int32_t max_read_length, |
119 PP_ArrayOutput* array_output, | 159 PP_ArrayOutput* array_output, |
120 scoped_refptr<TrackedCallback> callback) { | 160 scoped_refptr<TrackedCallback> callback) { |
121 DCHECK(array_output); | 161 DCHECK(array_output); |
122 int32_t rv = state_manager_.CheckOperationState( | 162 int32_t rv = state_manager_.CheckOperationState( |
123 FileIOStateManager::OPERATION_READ, true); | 163 FileIOStateManager::OPERATION_READ, true); |
124 if (rv != PP_OK) | 164 if (rv != PP_OK) |
125 return rv; | 165 return rv; |
126 | 166 |
127 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); | |
128 return ReadValidated(offset, max_read_length, *array_output, callback); | 167 return ReadValidated(offset, max_read_length, *array_output, callback); |
129 } | 168 } |
130 | 169 |
131 int32_t FileIOResource::Write(int64_t offset, | 170 int32_t FileIOResource::Write(int64_t offset, |
132 const char* buffer, | 171 const char* buffer, |
133 int32_t bytes_to_write, | 172 int32_t bytes_to_write, |
134 scoped_refptr<TrackedCallback> callback) { | 173 scoped_refptr<TrackedCallback> callback) { |
135 int32_t rv = state_manager_.CheckOperationState( | 174 int32_t rv = state_manager_.CheckOperationState( |
136 FileIOStateManager::OPERATION_WRITE, true); | 175 FileIOStateManager::OPERATION_WRITE, true); |
137 if (rv != PP_OK) | 176 if (rv != PP_OK) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, | 213 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, |
175 PpapiHostMsg_FileIO_Flush(), | 214 PpapiHostMsg_FileIO_Flush(), |
176 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, | 215 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
177 callback)); | 216 callback)); |
178 | 217 |
179 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 218 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
180 return PP_OK_COMPLETIONPENDING; | 219 return PP_OK_COMPLETIONPENDING; |
181 } | 220 } |
182 | 221 |
183 void FileIOResource::Close() { | 222 void FileIOResource::Close() { |
223 CloseFileHandle(); | |
184 Post(RENDERER, PpapiHostMsg_FileIO_Close()); | 224 Post(RENDERER, PpapiHostMsg_FileIO_Close()); |
185 } | 225 } |
186 | 226 |
187 int32_t FileIOResource::GetOSFileDescriptor() { | 227 int32_t FileIOResource::GetOSFileDescriptor() { |
188 int32_t file_descriptor; | 228 int32_t file_descriptor; |
189 // Only available when running in process. | 229 // Only available when running in process. |
190 SyncCall<PpapiPluginMsg_FileIO_GetOSFileDescriptorReply>( | 230 SyncCall<PpapiPluginMsg_FileIO_GetOSFileDescriptorReply>( |
191 RENDERER, PpapiHostMsg_FileIO_GetOSFileDescriptor(), &file_descriptor); | 231 RENDERER, PpapiHostMsg_FileIO_GetOSFileDescriptor(), &file_descriptor); |
192 return file_descriptor; | 232 return file_descriptor; |
193 } | 233 } |
194 | 234 |
235 int32_t FileIOResource::RequestOSFileHandle( | |
236 PP_FileHandle* handle, | |
237 scoped_refptr<TrackedCallback> callback) { | |
238 int32_t rv = state_manager_.CheckOperationState( | |
239 FileIOStateManager::OPERATION_EXCLUSIVE, true); | |
240 if (rv != PP_OK) | |
241 return rv; | |
242 | |
243 Call<PpapiPluginMsg_FileIO_RequestOSFileHandleReply>(RENDERER, | |
244 PpapiHostMsg_FileIO_RequestOSFileHandle(), | |
245 base::Bind(&FileIOResource::OnPluginMsgRequestOSFileHandleComplete, this, | |
246 callback, handle)); | |
247 | |
248 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
249 return PP_OK_COMPLETIONPENDING; | |
250 } | |
251 | |
195 int32_t FileIOResource::WillWrite(int64_t offset, | 252 int32_t FileIOResource::WillWrite(int64_t offset, |
196 int32_t bytes_to_write, | 253 int32_t bytes_to_write, |
197 scoped_refptr<TrackedCallback> callback) { | 254 scoped_refptr<TrackedCallback> callback) { |
198 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, | 255 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, |
199 PpapiHostMsg_FileIO_WillWrite(offset, bytes_to_write), | 256 PpapiHostMsg_FileIO_WillWrite(offset, bytes_to_write), |
200 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, | 257 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, callback)); |
201 callback)); | 258 |
202 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 259 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
203 return PP_OK_COMPLETIONPENDING; | 260 return PP_OK_COMPLETIONPENDING; |
204 } | 261 } |
205 | 262 |
206 int32_t FileIOResource::WillSetLength(int64_t length, | 263 int32_t FileIOResource::WillSetLength(int64_t length, |
207 scoped_refptr<TrackedCallback> callback) { | 264 scoped_refptr<TrackedCallback> callback) { |
208 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, | 265 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, |
209 PpapiHostMsg_FileIO_WillSetLength(length), | 266 PpapiHostMsg_FileIO_WillSetLength(length), |
210 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, | 267 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, callback)); |
211 callback)); | 268 |
212 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 269 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
213 return PP_OK_COMPLETIONPENDING; | 270 return PP_OK_COMPLETIONPENDING; |
214 } | 271 } |
215 | 272 |
216 int32_t FileIOResource::ReadValidated(int64_t offset, | 273 int32_t FileIOResource::ReadValidated(int64_t offset, |
217 int32_t bytes_to_read, | 274 int32_t bytes_to_read, |
218 const PP_ArrayOutput& array_output, | 275 const PP_ArrayOutput& array_output, |
219 scoped_refptr<TrackedCallback> callback) { | 276 scoped_refptr<TrackedCallback> callback) { |
277 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); | |
278 | |
279 if (callback->is_blocking() && | |
280 file_handle_ != base::kInvalidPlatformFileValue) { | |
281 int32_t result = PP_ERROR_FAILED; | |
282 if (bytes_to_read >= 0) { | |
283 // We can't read directly into the plugin's buffer since we don't know if | |
284 // we should write to it until after we re-acquire the lock. | |
285 scoped_ptr<char[]> buffer(new char[bytes_to_read]); | |
dmichael (off chromium)
2013/08/08 17:44:44
It would probably be better to do the allocation a
bbudge
2013/08/08 18:44:31
Done.
| |
286 // Release the proxy lock while making a potentially blocking file call. | |
287 { | |
288 ProxyAutoUnlock unlock; | |
289 int32_t bytes_read = base::ReadPlatformFile( | |
290 file_handle_, offset, buffer.get(), bytes_to_read); | |
291 result = (bytes_read >= 0) ? bytes_read : PP_ERROR_FAILED; | |
dmichael (off chromium)
2013/08/08 17:44:44
I may be reading it wrong, but it looks in the hos
bbudge
2013/08/08 18:44:31
That would cause a result of 0 to return PP_ERROR_
dmichael (off chromium)
2013/08/08 19:51:38
My bad, thanks
| |
292 } | |
293 if ((result >= 0) && TrackedCallback::IsPending(callback)) { | |
294 ArrayWriter output; | |
295 output.set_pp_array_output(array_output); | |
296 if (output.is_valid()) | |
297 output.StoreArray(buffer.get(), result); | |
298 else | |
299 result = PP_ERROR_FAILED; | |
300 } | |
301 } | |
302 | |
303 state_manager_.SetOperationFinished(); | |
304 return result; | |
305 } | |
306 | |
220 Call<PpapiPluginMsg_FileIO_ReadReply>(RENDERER, | 307 Call<PpapiPluginMsg_FileIO_ReadReply>(RENDERER, |
221 PpapiHostMsg_FileIO_Read(offset, bytes_to_read), | 308 PpapiHostMsg_FileIO_Read(offset, bytes_to_read), |
222 base::Bind(&FileIOResource::OnPluginMsgReadComplete, this, | 309 base::Bind(&FileIOResource::OnPluginMsgReadComplete, this, |
223 callback, array_output)); | 310 callback, array_output)); |
224 return PP_OK_COMPLETIONPENDING; | 311 return PP_OK_COMPLETIONPENDING; |
225 } | 312 } |
226 | 313 |
227 int32_t FileIOResource::RequestOSFileHandle( | 314 void FileIOResource::CloseFileHandle() { |
228 PP_FileHandle* handle, | 315 if (file_handle_ != base::kInvalidPlatformFileValue) { |
229 scoped_refptr<TrackedCallback> callback) { | 316 // Close our local fd on the file thread. |
230 int32_t rv = state_manager_.CheckOperationState( | 317 base::TaskRunner* file_task_runner = |
231 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 318 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()); |
232 if (rv != PP_OK) | 319 file_task_runner->PostTask(FROM_HERE, |
233 return rv; | 320 base::Bind(&DoClose, file_handle_)); |
234 | 321 |
235 Call<PpapiPluginMsg_FileIO_RequestOSFileHandleReply>(RENDERER, | 322 file_handle_ = base::kInvalidPlatformFileValue; |
236 PpapiHostMsg_FileIO_RequestOSFileHandle(), | 323 } |
237 base::Bind(&FileIOResource::OnPluginMsgRequestOSFileHandleComplete, this, | |
238 callback, handle)); | |
239 | |
240 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
241 return PP_OK_COMPLETIONPENDING; | |
242 } | 324 } |
243 | 325 |
244 void FileIOResource::OnPluginMsgGeneralComplete( | 326 void FileIOResource::OnPluginMsgGeneralComplete( |
245 scoped_refptr<TrackedCallback> callback, | 327 scoped_refptr<TrackedCallback> callback, |
246 const ResourceMessageReplyParams& params) { | 328 const ResourceMessageReplyParams& params) { |
247 DCHECK(state_manager_.get_pending_operation() == | 329 DCHECK(state_manager_.get_pending_operation() == |
248 FileIOStateManager::OPERATION_EXCLUSIVE || | 330 FileIOStateManager::OPERATION_EXCLUSIVE || |
249 state_manager_.get_pending_operation() == | 331 state_manager_.get_pending_operation() == |
250 FileIOStateManager::OPERATION_WRITE); | 332 FileIOStateManager::OPERATION_WRITE); |
251 // End the operation now. The callback may perform another file operation. | 333 // End this operation now, so the user's callback can execute another FileIO |
334 // operation, assuming there are no other pending operations. | |
252 state_manager_.SetOperationFinished(); | 335 state_manager_.SetOperationFinished(); |
253 callback->Run(params.result()); | 336 callback->Run(params.result()); |
254 } | 337 } |
255 | 338 |
256 void FileIOResource::OnPluginMsgOpenFileComplete( | 339 void FileIOResource::OnPluginMsgOpenFileComplete( |
257 scoped_refptr<TrackedCallback> callback, | 340 scoped_refptr<TrackedCallback> callback, |
258 const ResourceMessageReplyParams& params) { | 341 const ResourceMessageReplyParams& params) { |
259 DCHECK(state_manager_.get_pending_operation() == | 342 DCHECK(state_manager_.get_pending_operation() == |
260 FileIOStateManager::OPERATION_EXCLUSIVE); | 343 FileIOStateManager::OPERATION_EXCLUSIVE); |
261 if (params.result() == PP_OK) | 344 if (params.result() == PP_OK) |
262 state_manager_.SetOpenSucceed(); | 345 state_manager_.SetOpenSucceed(); |
263 // End the operation now. The callback may perform another file operation. | 346 |
347 int32_t result = params.result(); | |
348 IPC::PlatformFileForTransit transit_file; | |
349 if ((result == PP_OK) && params.TakeFileHandleAtIndex(0, &transit_file)) | |
350 file_handle_ = IPC::PlatformFileForTransitToPlatformFile(transit_file); | |
351 // End this operation now, so the user's callback can execute another FileIO | |
352 // operation, assuming there are no other pending operations. | |
264 state_manager_.SetOperationFinished(); | 353 state_manager_.SetOperationFinished(); |
265 callback->Run(params.result()); | 354 callback->Run(result); |
266 } | 355 } |
267 | 356 |
268 void FileIOResource::OnPluginMsgQueryComplete( | 357 void FileIOResource::OnPluginMsgQueryComplete( |
269 scoped_refptr<TrackedCallback> callback, | 358 scoped_refptr<TrackedCallback> callback, |
270 PP_FileInfo* output_info, | 359 PP_FileInfo* output_info, |
271 const ResourceMessageReplyParams& params, | 360 const ResourceMessageReplyParams& params, |
272 const PP_FileInfo& info) { | 361 const PP_FileInfo& info) { |
273 DCHECK(state_manager_.get_pending_operation() == | 362 DCHECK(state_manager_.get_pending_operation() == |
274 FileIOStateManager::OPERATION_EXCLUSIVE); | 363 FileIOStateManager::OPERATION_EXCLUSIVE); |
275 *output_info = info; | 364 *output_info = info; |
276 // End the operation now. The callback may perform another file operation. | 365 // End this operation now, so the user's callback can execute another FileIO |
366 // operation, assuming there are no other pending operations. | |
277 state_manager_.SetOperationFinished(); | 367 state_manager_.SetOperationFinished(); |
278 callback->Run(params.result()); | 368 callback->Run(params.result()); |
279 } | 369 } |
280 | 370 |
281 void FileIOResource::OnPluginMsgReadComplete( | 371 void FileIOResource::OnPluginMsgReadComplete( |
282 scoped_refptr<TrackedCallback> callback, | 372 scoped_refptr<TrackedCallback> callback, |
283 PP_ArrayOutput array_output, | 373 PP_ArrayOutput array_output, |
284 const ResourceMessageReplyParams& params, | 374 const ResourceMessageReplyParams& params, |
285 const std::string& data) { | 375 const std::string& data) { |
286 DCHECK(state_manager_.get_pending_operation() == | 376 DCHECK(state_manager_.get_pending_operation() == |
287 FileIOStateManager::OPERATION_READ); | 377 FileIOStateManager::OPERATION_READ); |
288 | 378 |
289 // The result code should contain the data size if it's positive. | 379 // The result code should contain the data size if it's positive. |
290 int32_t result = params.result(); | 380 int32_t result = params.result(); |
291 DCHECK((result < 0 && data.size() == 0) || | 381 DCHECK((result < 0 && data.size() == 0) || |
292 result == static_cast<int32_t>(data.size())); | 382 result == static_cast<int32_t>(data.size())); |
293 | 383 |
294 ArrayWriter output; | 384 ArrayWriter output; |
295 output.set_pp_array_output(array_output); | 385 output.set_pp_array_output(array_output); |
296 if (output.is_valid()) | 386 if (output.is_valid()) |
297 output.StoreArray(data.data(), std::max(0, result)); | 387 output.StoreArray(data.data(), std::max(0, result)); |
298 else | 388 else |
299 result = PP_ERROR_FAILED; | 389 result = PP_ERROR_FAILED; |
300 | 390 |
301 // End the operation now. The callback may perform another file operation. | 391 // End this operation now, so the user's callback can execute another FileIO |
392 // operation, assuming there are no other pending operations. | |
302 state_manager_.SetOperationFinished(); | 393 state_manager_.SetOperationFinished(); |
303 callback->Run(result); | 394 callback->Run(result); |
304 } | 395 } |
305 | 396 |
306 void FileIOResource::OnPluginMsgRequestOSFileHandleComplete( | 397 void FileIOResource::OnPluginMsgRequestOSFileHandleComplete( |
307 scoped_refptr<TrackedCallback> callback, | 398 scoped_refptr<TrackedCallback> callback, |
308 PP_FileHandle* output_handle, | 399 PP_FileHandle* output_handle, |
309 const ResourceMessageReplyParams& params) { | 400 const ResourceMessageReplyParams& params) { |
310 DCHECK(state_manager_.get_pending_operation() == | 401 DCHECK(state_manager_.get_pending_operation() == |
311 FileIOStateManager::OPERATION_EXCLUSIVE); | 402 FileIOStateManager::OPERATION_EXCLUSIVE); |
312 | 403 |
313 if (!TrackedCallback::IsPending(callback)) { | 404 if (!TrackedCallback::IsPending(callback)) { |
314 state_manager_.SetOperationFinished(); | 405 state_manager_.SetOperationFinished(); |
315 return; | 406 return; |
316 } | 407 } |
317 | 408 |
318 int32_t result = params.result(); | 409 int32_t result = params.result(); |
319 IPC::PlatformFileForTransit transit_file; | 410 IPC::PlatformFileForTransit transit_file; |
320 if (!params.TakeFileHandleAtIndex(0, &transit_file)) | 411 if (!params.TakeFileHandleAtIndex(0, &transit_file)) |
321 result = PP_ERROR_FAILED; | 412 result = PP_ERROR_FAILED; |
322 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); | 413 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
323 | 414 |
324 // End the operation now. The callback may perform another file operation. | 415 // End this operation now, so the user's callback can execute another FileIO |
416 // operation, assuming there are no other pending operations. | |
325 state_manager_.SetOperationFinished(); | 417 state_manager_.SetOperationFinished(); |
326 callback->Run(result); | 418 callback->Run(result); |
327 } | 419 } |
328 | 420 |
329 } // namespace proxy | 421 } // namespace proxy |
330 } // namespace ppapi | 422 } // namespace ppapi |
OLD | NEW |