Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: ppapi/proxy/file_io_resource.cc

Issue 18063005: Do PPB_FileIO Query and Read in the plugin process. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add some comments explaining issues. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/proxy/file_io_resource.h ('k') | ppapi/proxy/ppapi_messages.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/files/file_util_proxy.h"
8 #include "ipc/ipc_message.h" 9 #include "ipc/ipc_message.h"
9 #include "ppapi/c/pp_errors.h" 10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/proxy/plugin_globals.h"
10 #include "ppapi/proxy/ppapi_messages.h" 12 #include "ppapi/proxy/ppapi_messages.h"
11 #include "ppapi/shared_impl/array_writer.h" 13 #include "ppapi/shared_impl/array_writer.h"
14 #include "ppapi/shared_impl/file_type_conversion.h"
12 #include "ppapi/shared_impl/ppapi_globals.h" 15 #include "ppapi/shared_impl/ppapi_globals.h"
16 #include "ppapi/shared_impl/proxy_lock.h"
13 #include "ppapi/shared_impl/resource_tracker.h" 17 #include "ppapi/shared_impl/resource_tracker.h"
14 #include "ppapi/thunk/enter.h" 18 #include "ppapi/thunk/enter.h"
15 #include "ppapi/thunk/ppb_file_ref_api.h" 19 #include "ppapi/thunk/ppb_file_ref_api.h"
16 20
17 using ppapi::thunk::EnterResourceNoLock; 21 using ppapi::thunk::EnterResourceNoLock;
18 using ppapi::thunk::PPB_FileIO_API; 22 using ppapi::thunk::PPB_FileIO_API;
19 using ppapi::thunk::PPB_FileRef_API; 23 using ppapi::thunk::PPB_FileRef_API;
20 24
21 namespace { 25 namespace {
22 26
23 // An adapter to let Read() share the same implementation with ReadToArray(). 27 // An adapter to let Read() share the same implementation with ReadToArray().
24 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { 28 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) {
25 return user_data; 29 return user_data;
26 } 30 }
27 31
32 // All potentially blocking file tasks should be done on the file thread. In
33 // most cases, we can use base::FileUtilProxy to do the task and callback to the
34 // original thread. This won't work for blocking PPAPI callbacks, since we
35 // block the calling thread, which will cause FileUtilProxy to hang. Instead,
dmichael (off chromium) 2013/08/02 15:44:29 how about instead of "which will cause FileUtilPro
bbudge 2013/08/02 20:56:44 n/a now that we've eliminated FileUtilProxy.
36 // do the same work in these tasks, which correctly wake the original thread.
37 // However, we can't use these tasks for non-blocking callbacks because the
38 // in-process proxy has no lock, which would lead to unsafe access to the
39 // resource by both threads.
40
41 void DoQuery(base::PlatformFile file,
42 PP_FileInfo* info,
43 const base::FileUtilProxy::GetFileInfoCallback& callback) {
44 base::PlatformFileError error = base::PLATFORM_FILE_OK;
45 base::PlatformFileInfo file_info;
46 if (!GetPlatformFileInfo(file, &file_info))
47 error = base::PLATFORM_FILE_ERROR_FAILED;
48
49 callback.Run(error, file_info);
50 }
51
52 void DoRead(base::PlatformFile file,
53 int64 offset,
54 int bytes_to_read,
55 const base::FileUtilProxy::ReadCallback& callback) {
56 scoped_ptr<char[]> buffer(new char[bytes_to_read]);
dmichael (off chromium) 2013/08/02 15:44:29 We shouldn't have to make a copy for the "Read" ca
bbudge 2013/08/02 20:56:44 Looks like we can't avoid the copy due to Abort se
57 int bytes_read =
58 base::ReadPlatformFile(file, offset, buffer.get(), bytes_to_read);
59 base::PlatformFileError error = (bytes_read < 0) ?
60 base::PLATFORM_FILE_ERROR_FAILED : base::PLATFORM_FILE_OK;
61
62 callback.Run(error, buffer.get(), bytes_read);
63 }
64
65 void DoClose(base::PlatformFile file) {
66 base::ClosePlatformFile(file);
67 }
68
28 } // namespace 69 } // namespace
29 70
30 namespace ppapi { 71 namespace ppapi {
31 namespace proxy { 72 namespace proxy {
32 73
33 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) 74 FileIOResource::FileIOResource(Connection connection, PP_Instance instance)
34 : PluginResource(connection, instance) { 75 : PluginResource(connection, instance),
76 file_handle_(PP_kInvalidFileHandle),
77 file_system_type_(PP_FILESYSTEMTYPE_INVALID) {
35 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); 78 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create());
36 } 79 }
37 80
38 FileIOResource::~FileIOResource() { 81 FileIOResource::~FileIOResource() {
82 CloseFileHandle();
39 } 83 }
40 84
41 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { 85 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() {
42 return this; 86 return this;
43 } 87 }
44 88
45 int32_t FileIOResource::Open(PP_Resource file_ref, 89 int32_t FileIOResource::Open(PP_Resource file_ref,
46 int32_t open_flags, 90 int32_t open_flags,
47 scoped_refptr<TrackedCallback> callback) { 91 scoped_refptr<TrackedCallback> callback) {
48 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); 92 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true);
49 if (enter.failed()) 93 if (enter.failed())
50 return PP_ERROR_BADRESOURCE; 94 return PP_ERROR_BADRESOURCE;
51 95
96 PPB_FileRef_API* file_ref_api = enter.object();
97 PP_FileSystemType type = file_ref_api->GetFileSystemType();
98 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT &&
99 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY &&
100 type != PP_FILESYSTEMTYPE_EXTERNAL &&
101 type != PP_FILESYSTEMTYPE_ISOLATED) {
102 NOTREACHED();
103 return PP_ERROR_FAILED;
104 }
105 file_system_type_ = type;
106
52 int32_t rv = state_manager_.CheckOperationState( 107 int32_t rv = state_manager_.CheckOperationState(
53 FileIOStateManager::OPERATION_EXCLUSIVE, false); 108 FileIOStateManager::OPERATION_EXCLUSIVE, false);
54 if (rv != PP_OK) 109 if (rv != PP_OK)
55 return rv; 110 return rv;
56 111
57 Call<PpapiPluginMsg_FileIO_OpenReply>(RENDERER, 112 Call<PpapiPluginMsg_FileIO_OpenReply>(RENDERER,
58 PpapiHostMsg_FileIO_Open( 113 PpapiHostMsg_FileIO_Open(
59 enter.resource()->host_resource().host_resource(), 114 enter.resource()->host_resource().host_resource(),
60 open_flags), 115 open_flags),
61 base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this, 116 base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this,
62 callback)); 117 callback));
63 118
64 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); 119 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
65 return PP_OK_COMPLETIONPENDING; 120 return PP_OK_COMPLETIONPENDING;
66 } 121 }
67 122
68 int32_t FileIOResource::Query(PP_FileInfo* info, 123 int32_t FileIOResource::Query(PP_FileInfo* info,
69 scoped_refptr<TrackedCallback> callback) { 124 scoped_refptr<TrackedCallback> callback) {
70 int32_t rv = state_manager_.CheckOperationState( 125 int32_t rv = state_manager_.CheckOperationState(
71 FileIOStateManager::OPERATION_EXCLUSIVE, true); 126 FileIOStateManager::OPERATION_EXCLUSIVE, true);
72 if (rv != PP_OK) 127 if (rv != PP_OK)
73 return rv; 128 return rv;
74 129
75 Call<PpapiPluginMsg_FileIO_QueryReply>(RENDERER, 130 if (file_handle_ == base::kInvalidPlatformFileValue)
76 PpapiHostMsg_FileIO_Query(), 131 return PP_ERROR_FAILED;
77 base::Bind(&FileIOResource::OnPluginMsgQueryComplete, this, 132
78 callback, info)); 133 base::TaskRunner* file_task_runner =
134 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance());
135 base::FileUtilProxy::GetFileInfoCallback file_task = RunWhileLocked(
136 base::Bind(&FileIOResource::OnQueryComplete, this, callback, info));
137 if (callback->is_blocking()) {
138 file_task_runner->PostTask(
139 FROM_HERE,
140 Bind(&DoQuery, file_handle_, info, file_task));
141 } else {
142 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile(file_task_runner,
143 file_handle_,
144 file_task)) {
145 return PP_ERROR_FAILED;
146 }
147 }
79 148
80 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); 149 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
81 return PP_OK_COMPLETIONPENDING; 150 return PP_OK_COMPLETIONPENDING;
82 } 151 }
83 152
84 int32_t FileIOResource::Touch(PP_Time last_access_time, 153 int32_t FileIOResource::Touch(PP_Time last_access_time,
85 PP_Time last_modified_time, 154 PP_Time last_modified_time,
86 scoped_refptr<TrackedCallback> callback) { 155 scoped_refptr<TrackedCallback> callback) {
87 int32_t rv = state_manager_.CheckOperationState( 156 int32_t rv = state_manager_.CheckOperationState(
88 FileIOStateManager::OPERATION_EXCLUSIVE, true); 157 FileIOStateManager::OPERATION_EXCLUSIVE, true);
(...skipping 14 matching lines...) Expand all
103 int32_t bytes_to_read, 172 int32_t bytes_to_read,
104 scoped_refptr<TrackedCallback> callback) { 173 scoped_refptr<TrackedCallback> callback) {
105 int32_t rv = state_manager_.CheckOperationState( 174 int32_t rv = state_manager_.CheckOperationState(
106 FileIOStateManager::OPERATION_READ, true); 175 FileIOStateManager::OPERATION_READ, true);
107 if (rv != PP_OK) 176 if (rv != PP_OK)
108 return rv; 177 return rv;
109 178
110 PP_ArrayOutput output_adapter; 179 PP_ArrayOutput output_adapter;
111 output_adapter.GetDataBuffer = &DummyGetDataBuffer; 180 output_adapter.GetDataBuffer = &DummyGetDataBuffer;
112 output_adapter.user_data = buffer; 181 output_adapter.user_data = buffer;
113 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ);
114 return ReadValidated(offset, bytes_to_read, output_adapter, callback); 182 return ReadValidated(offset, bytes_to_read, output_adapter, callback);
115 } 183 }
116 184
117 int32_t FileIOResource::ReadToArray(int64_t offset, 185 int32_t FileIOResource::ReadToArray(int64_t offset,
118 int32_t max_read_length, 186 int32_t max_read_length,
119 PP_ArrayOutput* array_output, 187 PP_ArrayOutput* array_output,
120 scoped_refptr<TrackedCallback> callback) { 188 scoped_refptr<TrackedCallback> callback) {
121 DCHECK(array_output); 189 DCHECK(array_output);
122 int32_t rv = state_manager_.CheckOperationState( 190 int32_t rv = state_manager_.CheckOperationState(
123 FileIOStateManager::OPERATION_READ, true); 191 FileIOStateManager::OPERATION_READ, true);
124 if (rv != PP_OK) 192 if (rv != PP_OK)
125 return rv; 193 return rv;
126 194
127 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ);
128 return ReadValidated(offset, max_read_length, *array_output, callback); 195 return ReadValidated(offset, max_read_length, *array_output, callback);
129 } 196 }
130 197
131 int32_t FileIOResource::Write(int64_t offset, 198 int32_t FileIOResource::Write(int64_t offset,
132 const char* buffer, 199 const char* buffer,
133 int32_t bytes_to_write, 200 int32_t bytes_to_write,
134 scoped_refptr<TrackedCallback> callback) { 201 scoped_refptr<TrackedCallback> callback) {
135 int32_t rv = state_manager_.CheckOperationState( 202 int32_t rv = state_manager_.CheckOperationState(
136 FileIOStateManager::OPERATION_WRITE, true); 203 FileIOStateManager::OPERATION_WRITE, true);
137 if (rv != PP_OK) 204 if (rv != PP_OK)
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, 241 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER,
175 PpapiHostMsg_FileIO_Flush(), 242 PpapiHostMsg_FileIO_Flush(),
176 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, 243 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this,
177 callback)); 244 callback));
178 245
179 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); 246 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
180 return PP_OK_COMPLETIONPENDING; 247 return PP_OK_COMPLETIONPENDING;
181 } 248 }
182 249
183 void FileIOResource::Close() { 250 void FileIOResource::Close() {
251 CloseFileHandle();
184 Post(RENDERER, PpapiHostMsg_FileIO_Close()); 252 Post(RENDERER, PpapiHostMsg_FileIO_Close());
185 } 253 }
186 254
187 int32_t FileIOResource::GetOSFileDescriptor() { 255 int32_t FileIOResource::GetOSFileDescriptor() {
188 int32_t file_descriptor; 256 int32_t file_descriptor;
189 // Only available when running in process. 257 // Only available when running in process.
190 SyncCall<PpapiPluginMsg_FileIO_GetOSFileDescriptorReply>( 258 SyncCall<PpapiPluginMsg_FileIO_GetOSFileDescriptorReply>(
191 RENDERER, PpapiHostMsg_FileIO_GetOSFileDescriptor(), &file_descriptor); 259 RENDERER, PpapiHostMsg_FileIO_GetOSFileDescriptor(), &file_descriptor);
192 return file_descriptor; 260 return file_descriptor;
193 } 261 }
194 262
263 int32_t FileIOResource::RequestOSFileHandle(
264 PP_FileHandle* handle,
265 scoped_refptr<TrackedCallback> callback) {
266 int32_t rv = state_manager_.CheckOperationState(
267 FileIOStateManager::OPERATION_EXCLUSIVE, true);
268 if (rv != PP_OK)
269 return rv;
270
271 Call<PpapiPluginMsg_FileIO_RequestOSFileHandleReply>(RENDERER,
272 PpapiHostMsg_FileIO_RequestOSFileHandle(),
273 base::Bind(&FileIOResource::OnPluginMsgRequestOSFileHandleComplete, this,
274 callback, handle));
275
276 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
277 return PP_OK_COMPLETIONPENDING;
278 }
279
195 int32_t FileIOResource::WillWrite(int64_t offset, 280 int32_t FileIOResource::WillWrite(int64_t offset,
196 int32_t bytes_to_write, 281 int32_t bytes_to_write,
197 scoped_refptr<TrackedCallback> callback) { 282 scoped_refptr<TrackedCallback> callback) {
198 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, 283 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER,
199 PpapiHostMsg_FileIO_WillWrite(offset, bytes_to_write), 284 PpapiHostMsg_FileIO_WillWrite(offset, bytes_to_write),
200 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, 285 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this,
201 callback)); 286 callback));
287
202 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); 288 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
203 return PP_OK_COMPLETIONPENDING; 289 return PP_OK_COMPLETIONPENDING;
204 } 290 }
205 291
206 int32_t FileIOResource::WillSetLength(int64_t length, 292 int32_t FileIOResource::WillSetLength(int64_t length,
207 scoped_refptr<TrackedCallback> callback) { 293 scoped_refptr<TrackedCallback> callback) {
208 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, 294 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER,
209 PpapiHostMsg_FileIO_WillSetLength(length), 295 PpapiHostMsg_FileIO_WillSetLength(length),
210 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, 296 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this,
211 callback)); 297 callback));
298
212 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); 299 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
213 return PP_OK_COMPLETIONPENDING; 300 return PP_OK_COMPLETIONPENDING;
214 } 301 }
215 302
216 int32_t FileIOResource::ReadValidated(int64_t offset, 303 int32_t FileIOResource::ReadValidated(int64_t offset,
217 int32_t bytes_to_read, 304 int32_t bytes_to_read,
218 const PP_ArrayOutput& array_output, 305 const PP_ArrayOutput& array_output,
219 scoped_refptr<TrackedCallback> callback) { 306 scoped_refptr<TrackedCallback> callback) {
220 Call<PpapiPluginMsg_FileIO_ReadReply>(RENDERER, 307 if (file_handle_ == base::kInvalidPlatformFileValue)
221 PpapiHostMsg_FileIO_Read(offset, bytes_to_read), 308 return PP_ERROR_FAILED;
222 base::Bind(&FileIOResource::OnPluginMsgReadComplete, this, 309 if (bytes_to_read < 0)
223 callback, array_output)); 310 return PP_ERROR_FAILED;
311
312 base::TaskRunner* file_task_runner =
313 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance());
314 base::FileUtilProxy::ReadCallback file_task = RunWhileLocked(
315 base::Bind(&FileIOResource::OnReadComplete, this, callback,
316 array_output));
317 if (callback->is_blocking()) {
318 file_task_runner->PostTask(
319 FROM_HERE,
320 Bind(&DoRead, file_handle_, offset, bytes_to_read, file_task));
321 } else {
322 if (!base::FileUtilProxy::Read(file_task_runner,
323 file_handle_,
324 offset,
325 bytes_to_read,
326 file_task)) {
327 return PP_ERROR_FAILED;
328 }
329 }
330
331 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ);
224 return PP_OK_COMPLETIONPENDING; 332 return PP_OK_COMPLETIONPENDING;
225 } 333 }
226 334
227 int32_t FileIOResource::RequestOSFileHandle( 335 void FileIOResource::CloseFileHandle() {
228 PP_FileHandle* handle, 336 if (file_handle_ != base::kInvalidPlatformFileValue) {
229 scoped_refptr<TrackedCallback> callback) { 337 base::TaskRunner* file_task_runner =
230 int32_t rv = state_manager_.CheckOperationState( 338 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance());
231 FileIOStateManager::OPERATION_EXCLUSIVE, true); 339 file_task_runner->PostTask(FROM_HERE,
232 if (rv != PP_OK) 340 base::Bind(&DoClose, file_handle_));
233 return rv;
234 341
235 Call<PpapiPluginMsg_FileIO_RequestOSFileHandleReply>(RENDERER, 342 file_handle_ = base::kInvalidPlatformFileValue;
236 PpapiHostMsg_FileIO_RequestOSFileHandle(), 343 }
237 base::Bind(&FileIOResource::OnPluginMsgRequestOSFileHandleComplete, this, 344 }
238 callback, handle));
239 345
240 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); 346 void FileIOResource::OnQueryComplete(
241 return PP_OK_COMPLETIONPENDING; 347 scoped_refptr<TrackedCallback> callback,
348 PP_FileInfo* output_info,
349 base::PlatformFileError error_code,
350 const base::PlatformFileInfo& file_info) {
351 DCHECK(state_manager_.get_pending_operation() ==
352 FileIOStateManager::OPERATION_EXCLUSIVE);
353
354 if (!TrackedCallback::IsPending(callback)) {
355 state_manager_.SetOperationFinished();
356 return;
357 }
358
359 int32_t result = ::ppapi::PlatformFileErrorToPepperError(error_code);
360 if (result == PP_OK) {
361 ppapi::PlatformFileInfoToPepperFileInfo(file_info, file_system_type_,
362 output_info);
363 }
364
365 // End this operation now, so the user's callback can execute another FileIO
366 // operation, assuming there are no other pending operations.
367 state_manager_.SetOperationFinished();
368 callback->Run(result);
369 }
370
371 void FileIOResource::OnReadComplete(
372 scoped_refptr<TrackedCallback> callback,
373 PP_ArrayOutput array_output,
374 base::PlatformFileError error_code,
375 const char* data, int bytes_read) {
376 DCHECK(state_manager_.get_pending_operation() ==
377 FileIOStateManager::OPERATION_READ);
378
379 if (!TrackedCallback::IsPending(callback)) {
380 state_manager_.SetOperationFinished();
381 return;
382 }
383
384 int32_t result = ::ppapi::PlatformFileErrorToPepperError(error_code);
385 if (result == PP_OK) {
386 result = std::max(0, bytes_read);
387 ArrayWriter output;
388 output.set_pp_array_output(array_output);
389 if (output.is_valid())
390 output.StoreArray(data, result);
391 else
392 result = PP_ERROR_FAILED;
393 }
394
395 // End this operation now, so the user's callback can execute another FileIO
396 // operation, assuming there are no other pending operations.
397 state_manager_.SetOperationFinished();
398 callback->Run(result);
242 } 399 }
243 400
244 void FileIOResource::OnPluginMsgGeneralComplete( 401 void FileIOResource::OnPluginMsgGeneralComplete(
245 scoped_refptr<TrackedCallback> callback, 402 scoped_refptr<TrackedCallback> callback,
246 const ResourceMessageReplyParams& params) { 403 const ResourceMessageReplyParams& params) {
247 DCHECK(state_manager_.get_pending_operation() == 404 DCHECK(state_manager_.get_pending_operation() ==
248 FileIOStateManager::OPERATION_EXCLUSIVE || 405 FileIOStateManager::OPERATION_EXCLUSIVE ||
249 state_manager_.get_pending_operation() == 406 state_manager_.get_pending_operation() ==
250 FileIOStateManager::OPERATION_WRITE); 407 FileIOStateManager::OPERATION_WRITE);
251 // End the operation now. The callback may perform another file operation. 408 // End this operation now, so the user's callback can execute another FileIO
409 // operation, assuming there are no other pending operations.
252 state_manager_.SetOperationFinished(); 410 state_manager_.SetOperationFinished();
253 callback->Run(params.result()); 411 callback->Run(params.result());
254 } 412 }
255 413
256 void FileIOResource::OnPluginMsgOpenFileComplete( 414 void FileIOResource::OnPluginMsgOpenFileComplete(
257 scoped_refptr<TrackedCallback> callback, 415 scoped_refptr<TrackedCallback> callback,
258 const ResourceMessageReplyParams& params) { 416 const ResourceMessageReplyParams& params) {
259 DCHECK(state_manager_.get_pending_operation() == 417 DCHECK(state_manager_.get_pending_operation() ==
260 FileIOStateManager::OPERATION_EXCLUSIVE); 418 FileIOStateManager::OPERATION_EXCLUSIVE);
261 if (params.result() == PP_OK) 419 if (params.result() == PP_OK)
262 state_manager_.SetOpenSucceed(); 420 state_manager_.SetOpenSucceed();
263 // End the operation now. The callback may perform another file operation. 421
422 int32_t result = params.result();
423 IPC::PlatformFileForTransit transit_file;
424 if (result == PP_OK && !params.TakeFileHandleAtIndex(0, &transit_file))
425 result = PP_ERROR_FAILED;
426 file_handle_ = IPC::PlatformFileForTransitToPlatformFile(transit_file);
427
428 // End this operation now, so the user's callback can execute another FileIO
429 // operation, assuming there are no other pending operations.
264 state_manager_.SetOperationFinished(); 430 state_manager_.SetOperationFinished();
265 callback->Run(params.result()); 431 callback->Run(params.result());
266 } 432 }
267 433
268 void FileIOResource::OnPluginMsgQueryComplete(
269 scoped_refptr<TrackedCallback> callback,
270 PP_FileInfo* output_info,
271 const ResourceMessageReplyParams& params,
272 const PP_FileInfo& info) {
273 DCHECK(state_manager_.get_pending_operation() ==
274 FileIOStateManager::OPERATION_EXCLUSIVE);
275 *output_info = info;
276 // End the operation now. The callback may perform another file operation.
277 state_manager_.SetOperationFinished();
278 callback->Run(params.result());
279 }
280
281 void FileIOResource::OnPluginMsgReadComplete(
282 scoped_refptr<TrackedCallback> callback,
283 PP_ArrayOutput array_output,
284 const ResourceMessageReplyParams& params,
285 const std::string& data) {
286 DCHECK(state_manager_.get_pending_operation() ==
287 FileIOStateManager::OPERATION_READ);
288
289 // The result code should contain the data size if it's positive.
290 int32_t result = params.result();
291 DCHECK((result < 0 && data.size() == 0) ||
292 result == static_cast<int32_t>(data.size()));
293
294 ArrayWriter output;
295 output.set_pp_array_output(array_output);
296 if (output.is_valid())
297 output.StoreArray(data.data(), std::max(0, result));
298 else
299 result = PP_ERROR_FAILED;
300
301 // End the operation now. The callback may perform another file operation.
302 state_manager_.SetOperationFinished();
303 callback->Run(result);
304 }
305
306 void FileIOResource::OnPluginMsgRequestOSFileHandleComplete( 434 void FileIOResource::OnPluginMsgRequestOSFileHandleComplete(
307 scoped_refptr<TrackedCallback> callback, 435 scoped_refptr<TrackedCallback> callback,
308 PP_FileHandle* output_handle, 436 PP_FileHandle* output_handle,
309 const ResourceMessageReplyParams& params) { 437 const ResourceMessageReplyParams& params) {
310 DCHECK(state_manager_.get_pending_operation() == 438 DCHECK(state_manager_.get_pending_operation() ==
311 FileIOStateManager::OPERATION_EXCLUSIVE); 439 FileIOStateManager::OPERATION_EXCLUSIVE);
312 440
313 if (!TrackedCallback::IsPending(callback)) { 441 if (!TrackedCallback::IsPending(callback)) {
314 state_manager_.SetOperationFinished(); 442 state_manager_.SetOperationFinished();
315 return; 443 return;
316 } 444 }
317 445
318 int32_t result = params.result(); 446 int32_t result = params.result();
319 IPC::PlatformFileForTransit transit_file; 447 IPC::PlatformFileForTransit transit_file;
320 if (!params.TakeFileHandleAtIndex(0, &transit_file)) 448 if (!params.TakeFileHandleAtIndex(0, &transit_file))
321 result = PP_ERROR_FAILED; 449 result = PP_ERROR_FAILED;
322 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); 450 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file);
323 451
324 // End the operation now. The callback may perform another file operation. 452 // End this operation now, so the user's callback can execute another FileIO
453 // operation, assuming there are no other pending operations.
325 state_manager_.SetOperationFinished(); 454 state_manager_.SetOperationFinished();
326 callback->Run(result); 455 callback->Run(result);
327 } 456 }
328 457
329 } // namespace proxy 458 } // namespace proxy
330 } // namespace ppapi 459 } // namespace ppapi
OLDNEW
« no previous file with comments | « ppapi/proxy/file_io_resource.h ('k') | ppapi/proxy/ppapi_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698