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/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" | 7 #include "base/logging.h" |
11 #include "base/message_loop.h" | |
12 #include "ppapi/c/pp_errors.h" | 8 #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" | |
17 #include "ppapi/thunk/ppb_file_ref_api.h" | |
18 | 9 |
19 namespace ppapi { | 10 namespace ppapi { |
20 | 11 |
21 using thunk::EnterResourceNoLock; | 12 PPB_FileIO_Shared::PPB_FileIO_Shared() |
22 using thunk::PPB_FileIO_API; | 13 : num_pending_ops_(0), |
23 using thunk::PPB_FileRef_API; | 14 pending_op_(OPERATION_NONE), |
24 | 15 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 } | 16 } |
60 | 17 |
61 PPB_FileIO_Shared::~PPB_FileIO_Shared() { | 18 PPB_FileIO_Shared::~PPB_FileIO_Shared() { |
62 } | 19 } |
63 | 20 |
64 thunk::PPB_FileIO_API* PPB_FileIO_Shared::AsPPB_FileIO_API() { | 21 void PPB_FileIO_Shared::SetOpenSucceed() { |
65 return this; | 22 file_open_ = true; |
66 } | 23 } |
67 | 24 |
68 int32_t PPB_FileIO_Shared::Open(PP_Resource file_ref, | 25 bool PPB_FileIO_Shared::CheckOpenState(bool should_be_open) const { |
raymes
2012/11/28 18:39:35
We can inline this function, it's only used in one
victorhsieh
2012/11/29 06:34:55
Done.
| |
69 int32_t open_flags, | 26 if (should_be_open) { |
70 scoped_refptr<TrackedCallback> callback) { | 27 if (!file_open_) |
71 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); | 28 return false; |
72 if (enter.failed()) | 29 } else { |
73 return PP_ERROR_BADRESOURCE; | 30 if (file_open_) |
74 | 31 return false; |
75 int32_t rv = CommonCallValidation(false, OPERATION_EXCLUSIVE); | 32 } |
76 if (rv != PP_OK) | 33 return true; |
77 return rv; | |
78 | |
79 PP_FileSystemType type = enter.object()->GetFileSystemType(); | |
80 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | |
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::CommonPreCondition(bool should_be_open, |
90 scoped_refptr<TrackedCallback> callback) { | 37 OperationType new_op) { |
91 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); | 38 if (!CheckOpenState(should_be_open)) |
92 if (rv != PP_OK) | 39 return PP_ERROR_FAILED; |
93 return rv; | |
94 if (!info) | |
95 return PP_ERROR_BADARGUMENT; | |
96 return QueryValidated(info, callback); | |
97 } | |
98 | |
99 int32_t PPB_FileIO_Shared::Touch(PP_Time last_access_time, | |
100 PP_Time last_modified_time, | |
101 scoped_refptr<TrackedCallback> callback) { | |
102 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); | |
103 if (rv != PP_OK) | |
104 return rv; | |
105 return TouchValidated(last_access_time, last_modified_time, callback); | |
106 } | |
107 | |
108 int32_t PPB_FileIO_Shared::Read(int64_t offset, | |
109 char* buffer, | |
110 int32_t bytes_to_read, | |
111 scoped_refptr<TrackedCallback> callback) { | |
112 int32_t rv = CommonCallValidation(true, OPERATION_READ); | |
113 if (rv != PP_OK) | |
114 return rv; | |
115 PP_ArrayOutput buffer_adapter = { &DummyGetDataBuffer, buffer }; | |
116 return ReadValidated(offset, buffer_adapter, bytes_to_read, callback); | |
117 } | |
118 | |
119 int32_t PPB_FileIO_Shared::ReadToArray( | |
120 int64_t offset, | |
121 int32_t max_read_length, | |
122 PP_ArrayOutput* output_array_buffer, | |
123 scoped_refptr<TrackedCallback> callback) { | |
124 int32_t rv = CommonCallValidation(true, OPERATION_READ); | |
125 if (rv != PP_OK) | |
126 return rv; | |
127 if (!output_array_buffer) | |
128 return PP_ERROR_BADARGUMENT; | |
129 return ReadValidated(offset, *output_array_buffer, max_read_length, callback); | |
130 } | |
131 | |
132 int32_t PPB_FileIO_Shared::Write(int64_t offset, | |
133 const char* buffer, | |
134 int32_t bytes_to_write, | |
135 scoped_refptr<TrackedCallback> callback) { | |
136 int32_t rv = CommonCallValidation(true, OPERATION_WRITE); | |
137 if (rv != PP_OK) | |
138 return rv; | |
139 return WriteValidated(offset, buffer, bytes_to_write, callback); | |
140 } | |
141 | |
142 int32_t PPB_FileIO_Shared::SetLength(int64_t length, | |
143 scoped_refptr<TrackedCallback> callback) { | |
144 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); | |
145 if (rv != PP_OK) | |
146 return rv; | |
147 return SetLengthValidated(length, callback); | |
148 } | |
149 | |
150 int32_t PPB_FileIO_Shared::Flush(scoped_refptr<TrackedCallback> callback) { | |
151 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); | |
152 if (rv != PP_OK) | |
153 return rv; | |
154 return FlushValidated(callback); | |
155 } | |
156 | |
157 void PPB_FileIO_Shared::ExecuteGeneralCallback(int32_t pp_error) { | |
158 RunAndRemoveFirstPendingCallback(pp_error); | |
159 } | |
160 | |
161 void PPB_FileIO_Shared::ExecuteOpenFileCallback(int32_t pp_error) { | |
162 if (pp_error == PP_OK) | |
163 file_open_ = true; | |
164 ExecuteGeneralCallback(pp_error); | |
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 | 40 |
207 if (pending_op_ != OPERATION_NONE && | 41 if (pending_op_ != OPERATION_NONE && |
208 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) { | 42 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) |
209 return PP_ERROR_INPROGRESS; | 43 return PP_ERROR_INPROGRESS; |
210 } | |
211 | 44 |
212 return PP_OK; | 45 return PP_OK; |
213 } | 46 } |
214 | 47 |
215 void PPB_FileIO_Shared::RegisterCallback( | 48 void PPB_FileIO_Shared::CommonPostCondition(OperationType new_op) { |
216 OperationType op, | 49 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 } | 50 } |
232 | 51 |
233 void PPB_FileIO_Shared::RunAndRemoveFirstPendingCallback(int32_t result) { | 52 void PPB_FileIO_Shared::SetOpInProgress(OperationType new_op) { |
234 DCHECK(!callbacks_.empty()); | 53 DCHECK(pending_op_ == OPERATION_NONE || |
54 (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == new_op)); | |
55 pending_op_ = new_op; | |
56 num_pending_ops_++; | |
57 } | |
235 | 58 |
236 CallbackEntry front = callbacks_.front(); | 59 void PPB_FileIO_Shared::SetOpFinished() { |
237 callbacks_.pop_front(); | 60 DCHECK(num_pending_ops_ > 0); |
238 if (callbacks_.empty()) | 61 if (--num_pending_ops_ == 0) |
239 pending_op_ = OPERATION_NONE; | 62 pending_op_ = OPERATION_NONE; |
240 | |
241 front.callback->Run(result); | |
242 } | 63 } |
243 | 64 |
244 } // namespace ppapi | 65 } // namespace ppapi |
OLD | NEW |