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

Side by Side Diff: ppapi/shared_impl/ppb_file_io_shared.cc

Issue 11419131: Refactor FileIO to the new design (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years 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
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/shared_impl/ppb_file_io_shared.h" 5 #include "ppapi/shared_impl/ppb_file_io_shared.h"
6 6
7 #include <string.h>
8
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/message_loop.h"
12 #include "ppapi/c/pp_errors.h" 7 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/shared_impl/array_writer.h"
14 #include "ppapi/shared_impl/file_type_conversion.h"
15 #include "ppapi/shared_impl/time_conversion.h"
16 #include "ppapi/thunk/enter.h" 8 #include "ppapi/thunk/enter.h"
17 #include "ppapi/thunk/ppb_file_ref_api.h" 9 #include "ppapi/thunk/ppb_file_ref_api.h"
18 10
19 namespace ppapi { 11 namespace ppapi {
20 12
21 using thunk::EnterResourceNoLock; 13 PPB_FileIO_Shared::PPB_FileIO_Shared()
22 using thunk::PPB_FileIO_API; 14 : num_pending_ops_(0),
23 using thunk::PPB_FileRef_API; 15 pending_op_(OPERATION_NONE),
24 16 file_open_(false) {
25 namespace {
26
27 // An adapter to let Read() share the same implementation with ReadToArray().
28 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) {
29 return user_data;
30 }
31
32 } // namespace
33
34 PPB_FileIO_Shared::CallbackEntry::CallbackEntry()
35 : info(NULL) {
36 }
37
38 PPB_FileIO_Shared::CallbackEntry::CallbackEntry(const CallbackEntry& entry)
39 : callback(entry.callback),
40 read_buffer(entry.read_buffer),
41 info(entry.info) {
42 }
43
44 PPB_FileIO_Shared::CallbackEntry::~CallbackEntry() {
45 }
46
47 PPB_FileIO_Shared::PPB_FileIO_Shared(PP_Instance instance)
48 : Resource(OBJECT_IS_IMPL, instance),
49 file_system_type_(PP_FILESYSTEMTYPE_INVALID),
50 file_open_(false),
51 pending_op_(OPERATION_NONE) {
52 }
53
54 PPB_FileIO_Shared::PPB_FileIO_Shared(const HostResource& host_resource)
55 : Resource(OBJECT_IS_PROXY, host_resource),
56 file_system_type_(PP_FILESYSTEMTYPE_INVALID),
57 file_open_(false),
58 pending_op_(OPERATION_NONE) {
59 } 17 }
60 18
61 PPB_FileIO_Shared::~PPB_FileIO_Shared() { 19 PPB_FileIO_Shared::~PPB_FileIO_Shared() {
62 } 20 }
63 21
64 thunk::PPB_FileIO_API* PPB_FileIO_Shared::AsPPB_FileIO_API() { 22 int32_t PPB_FileIO_Shared::DoOpen(PP_Resource file_ref,
65 return this; 23 int32_t open_flags) {
66 } 24 thunk::EnterResourceNoLock<thunk::PPB_FileRef_API> enter(file_ref, true);
67
68 int32_t PPB_FileIO_Shared::Open(PP_Resource file_ref,
69 int32_t open_flags,
70 scoped_refptr<TrackedCallback> callback) {
71 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true);
72 if (enter.failed()) 25 if (enter.failed())
73 return PP_ERROR_BADRESOURCE; 26 return PP_ERROR_BADRESOURCE;
74 27
75 int32_t rv = CommonCallValidation(false, OPERATION_EXCLUSIVE); 28 int32_t rv = CommonPreCondition(false, OPERATION_EXCLUSIVE);
76 if (rv != PP_OK) 29 if (rv != PP_OK)
77 return rv; 30 return rv;
78 31 rv = OpenValidated(file_ref, enter.object(), open_flags);
79 PP_FileSystemType type = enter.object()->GetFileSystemType(); 32 CommonPostCondition(OPERATION_EXCLUSIVE);
80 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && 33 return rv;
81 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY &&
82 type != PP_FILESYSTEMTYPE_EXTERNAL)
83 return PP_ERROR_FAILED;
84 file_system_type_ = type;
85
86 return OpenValidated(file_ref, enter.object(), open_flags, callback);
87 } 34 }
88 35
89 int32_t PPB_FileIO_Shared::Query(PP_FileInfo* info, 36 int32_t PPB_FileIO_Shared::DoQuery() {
90 scoped_refptr<TrackedCallback> callback) { 37 int32_t rv = CommonPreCondition(true, OPERATION_EXCLUSIVE);
91 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE);
92 if (rv != PP_OK) 38 if (rv != PP_OK)
93 return rv; 39 return rv;
94 if (!info) 40 rv = QueryValidated();
95 return PP_ERROR_BADARGUMENT; 41 CommonPostCondition(OPERATION_EXCLUSIVE);
96 return QueryValidated(info, callback); 42 return rv;
97 } 43 }
98 44
99 int32_t PPB_FileIO_Shared::Touch(PP_Time last_access_time, 45 int32_t PPB_FileIO_Shared::DoTouch(PP_Time last_access_time,
100 PP_Time last_modified_time, 46 PP_Time last_modified_time) {
101 scoped_refptr<TrackedCallback> callback) { 47 int32_t rv = CommonPreCondition(true, OPERATION_EXCLUSIVE);
102 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE);
103 if (rv != PP_OK) 48 if (rv != PP_OK)
104 return rv; 49 return rv;
105 return TouchValidated(last_access_time, last_modified_time, callback); 50 rv = TouchValidated(last_access_time, last_modified_time);
51 CommonPostCondition(OPERATION_EXCLUSIVE);
52 return rv;
106 } 53 }
107 54
108 int32_t PPB_FileIO_Shared::Read(int64_t offset, 55 int32_t PPB_FileIO_Shared::DoRead(int64_t offset,
109 char* buffer, 56 int32_t bytes_to_read) {
110 int32_t bytes_to_read, 57 int32_t rv = CommonPreCondition(true, OPERATION_READ);
111 scoped_refptr<TrackedCallback> callback) {
112 int32_t rv = CommonCallValidation(true, OPERATION_READ);
113 if (rv != PP_OK) 58 if (rv != PP_OK)
114 return rv; 59 return rv;
115 PP_ArrayOutput buffer_adapter = { &DummyGetDataBuffer, buffer }; 60 rv = ReadValidated(offset, bytes_to_read);
116 return ReadValidated(offset, buffer_adapter, bytes_to_read, callback); 61 CommonPostCondition(OPERATION_READ);
62 return rv;
117 } 63 }
118 64
119 int32_t PPB_FileIO_Shared::ReadToArray( 65 int32_t PPB_FileIO_Shared::DoWrite(int64_t offset,
120 int64_t offset, 66 const char* buffer,
121 int32_t max_read_length, 67 int32_t bytes_to_write) {
122 PP_ArrayOutput* output_array_buffer, 68 int32_t rv = CommonPreCondition(true, OPERATION_WRITE);
123 scoped_refptr<TrackedCallback> callback) {
124 int32_t rv = CommonCallValidation(true, OPERATION_READ);
125 if (rv != PP_OK) 69 if (rv != PP_OK)
126 return rv; 70 return rv;
127 if (!output_array_buffer) 71 rv = WriteValidated(offset, buffer, bytes_to_write);
128 return PP_ERROR_BADARGUMENT; 72 CommonPostCondition(OPERATION_WRITE);
129 return ReadValidated(offset, *output_array_buffer, max_read_length, callback); 73 return rv;
130 } 74 }
131 75
132 int32_t PPB_FileIO_Shared::Write(int64_t offset, 76 int32_t PPB_FileIO_Shared::DoSetLength(int64_t length) {
133 const char* buffer, 77 int32_t rv = CommonPreCondition(true, OPERATION_EXCLUSIVE);
134 int32_t bytes_to_write,
135 scoped_refptr<TrackedCallback> callback) {
136 int32_t rv = CommonCallValidation(true, OPERATION_WRITE);
137 if (rv != PP_OK) 78 if (rv != PP_OK)
138 return rv; 79 return rv;
139 return WriteValidated(offset, buffer, bytes_to_write, callback); 80 rv = SetLengthValidated(length);
81 CommonPostCondition(OPERATION_EXCLUSIVE);
82 return rv;
140 } 83 }
141 84
142 int32_t PPB_FileIO_Shared::SetLength(int64_t length, 85 int32_t PPB_FileIO_Shared::DoFlush() {
143 scoped_refptr<TrackedCallback> callback) { 86 int32_t rv = CommonPreCondition(true, OPERATION_EXCLUSIVE);
144 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE);
145 if (rv != PP_OK) 87 if (rv != PP_OK)
146 return rv; 88 return rv;
147 return SetLengthValidated(length, callback); 89 rv = FlushValidated();
90 CommonPostCondition(OPERATION_EXCLUSIVE);
91 return rv;
148 } 92 }
149 93
150 int32_t PPB_FileIO_Shared::Flush(scoped_refptr<TrackedCallback> callback) { 94 void PPB_FileIO_Shared::SetOpenSucceed() {
151 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); 95 file_open_ = true;
152 if (rv != PP_OK)
153 return rv;
154 return FlushValidated(callback);
155 } 96 }
156 97
157 void PPB_FileIO_Shared::ExecuteGeneralCallback(int32_t pp_error) { 98 bool PPB_FileIO_Shared::CheckOpenState(bool should_be_open) const {
158 RunAndRemoveFirstPendingCallback(pp_error); 99 if (should_be_open) {
100 if (!file_open_)
101 return false;
102 } else {
103 if (file_open_)
104 return false;
105 }
106 return true;
159 } 107 }
160 108
161 void PPB_FileIO_Shared::ExecuteOpenFileCallback(int32_t pp_error) { 109 int32_t PPB_FileIO_Shared::CommonPreCondition(bool should_be_open,
162 if (pp_error == PP_OK) 110 OperationType new_op) {
163 file_open_ = true; 111 if (!CheckOpenState(should_be_open))
164 ExecuteGeneralCallback(pp_error); 112 return PP_ERROR_FAILED;
165 }
166
167 void PPB_FileIO_Shared::ExecuteQueryCallback(int32_t pp_error,
168 const PP_FileInfo& info) {
169 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty() ||
170 !callbacks_.front().info) {
171 NOTREACHED();
172 return;
173 }
174 *callbacks_.front().info = info;
175 RunAndRemoveFirstPendingCallback(pp_error);
176 }
177
178 void PPB_FileIO_Shared::ExecuteReadCallback(int32_t pp_error_or_bytes,
179 const char* data) {
180 if (pending_op_ != OPERATION_READ || callbacks_.empty()) {
181 NOTREACHED();
182 return;
183 }
184
185 // The result code contains the number of bytes if it's positive.
186 ArrayWriter output;
187 output.set_pp_array_output(callbacks_.front().read_buffer);
188 if (output.is_valid()) {
189 output.StoreArray(data, std::max(0, pp_error_or_bytes));
190 }
191
192 // Always issue the callback.
193 RunAndRemoveFirstPendingCallback(pp_error_or_bytes);
194 }
195
196 int32_t PPB_FileIO_Shared::CommonCallValidation(bool should_be_open,
197 OperationType new_op) {
198 // Only asynchronous operation is supported.
199 if (should_be_open) {
200 if (!file_open_)
201 return PP_ERROR_FAILED;
202 } else {
203 if (file_open_)
204 return PP_ERROR_FAILED;
205 }
206 113
207 if (pending_op_ != OPERATION_NONE && 114 if (pending_op_ != OPERATION_NONE &&
208 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) { 115 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE))
209 return PP_ERROR_INPROGRESS; 116 return PP_ERROR_INPROGRESS;
210 }
211 117
212 return PP_OK; 118 return PP_OK;
213 } 119 }
214 120
215 void PPB_FileIO_Shared::RegisterCallback( 121 void PPB_FileIO_Shared::CommonPostCondition(OperationType new_op) {
216 OperationType op, 122 SetOpInProgress(new_op);
217 scoped_refptr<TrackedCallback> callback,
218 const PP_ArrayOutput* read_buffer,
219 PP_FileInfo* info) {
220 DCHECK(pending_op_ == OPERATION_NONE ||
221 (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == op));
222
223 CallbackEntry entry;
224 entry.callback = callback;
225 if (read_buffer)
226 entry.read_buffer = *read_buffer;
227 entry.info = info;
228 callbacks_.push_back(entry);
229
230 pending_op_ = op;
231 } 123 }
232 124
233 void PPB_FileIO_Shared::RunAndRemoveFirstPendingCallback(int32_t result) { 125 void PPB_FileIO_Shared::SetOpInProgress(OperationType new_op) {
234 DCHECK(!callbacks_.empty()); 126 DCHECK(pending_op_ == OPERATION_NONE ||
127 (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == new_op));
128 pending_op_ = new_op;
129 num_pending_ops_++;
130 }
235 131
236 CallbackEntry front = callbacks_.front(); 132 void PPB_FileIO_Shared::SetOpFinished() {
237 callbacks_.pop_front(); 133 DCHECK(num_pending_ops_ > 0);
238 if (callbacks_.empty()) 134 if (--num_pending_ops_ == 0)
239 pending_op_ = OPERATION_NONE; 135 pending_op_ = OPERATION_NONE;
240
241 front.callback->Run(result);
242 } 136 }
243 137
244 } // namespace ppapi 138 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698