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" | |
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() : file_open_(false) { |
22 using thunk::PPB_FileIO_API; | |
23 using thunk::PPB_FileRef_API; | |
24 | |
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 } | 14 } |
60 | 15 |
61 PPB_FileIO_Shared::~PPB_FileIO_Shared() { | 16 PPB_FileIO_Shared::~PPB_FileIO_Shared() { |
62 } | 17 } |
63 | 18 |
64 thunk::PPB_FileIO_API* PPB_FileIO_Shared::AsPPB_FileIO_API() { | 19 int32_t PPB_FileIO_Shared::DoOpen(PP_Resource file_ref, |
65 return this; | 20 int32_t open_flags) { |
66 } | 21 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()) | 22 if (enter.failed()) |
73 return PP_ERROR_BADRESOURCE; | 23 return PP_ERROR_BADRESOURCE; |
74 | 24 |
75 int32_t rv = CommonCallValidation(false, OPERATION_EXCLUSIVE); | 25 int32_t rv = CommonPreCondition(false, OPERATION_EXCLUSIVE); |
76 if (rv != PP_OK) | 26 if (rv != PP_OK) |
77 return rv; | 27 return rv; |
78 | 28 rv = OpenValidated(file_ref, enter.object(), open_flags); |
79 PP_FileSystemType type = enter.object()->GetFileSystemType(); | 29 CommonPostCondition(OPERATION_EXCLUSIVE); |
80 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | 30 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 } | 31 } |
88 | 32 |
89 int32_t PPB_FileIO_Shared::Query(PP_FileInfo* info, | 33 int32_t PPB_FileIO_Shared::DoQuery() { |
90 scoped_refptr<TrackedCallback> callback) { | 34 int32_t rv = CommonPreCondition(true, OPERATION_EXCLUSIVE); |
91 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); | |
92 if (rv != PP_OK) | 35 if (rv != PP_OK) |
93 return rv; | 36 return rv; |
94 if (!info) | 37 rv = QueryValidated(); |
95 return PP_ERROR_BADARGUMENT; | 38 CommonPostCondition(OPERATION_EXCLUSIVE); |
96 return QueryValidated(info, callback); | 39 return rv; |
97 } | 40 } |
98 | 41 |
99 int32_t PPB_FileIO_Shared::Touch(PP_Time last_access_time, | 42 int32_t PPB_FileIO_Shared::DoTouch(PP_Time last_access_time, |
100 PP_Time last_modified_time, | 43 PP_Time last_modified_time) { |
101 scoped_refptr<TrackedCallback> callback) { | 44 int32_t rv = CommonPreCondition(true, OPERATION_EXCLUSIVE); |
102 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); | |
103 if (rv != PP_OK) | 45 if (rv != PP_OK) |
104 return rv; | 46 return rv; |
105 return TouchValidated(last_access_time, last_modified_time, callback); | 47 rv = TouchValidated(last_access_time, last_modified_time); |
| 48 CommonPostCondition(OPERATION_EXCLUSIVE); |
| 49 return rv; |
106 } | 50 } |
107 | 51 |
108 int32_t PPB_FileIO_Shared::Read(int64_t offset, | 52 int32_t PPB_FileIO_Shared::DoRead(int64_t offset, |
109 char* buffer, | 53 int32_t bytes_to_read) { |
110 int32_t bytes_to_read, | 54 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) | 55 if (rv != PP_OK) |
114 return rv; | 56 return rv; |
115 PP_ArrayOutput buffer_adapter = { &DummyGetDataBuffer, buffer }; | 57 rv = ReadValidated(offset, bytes_to_read); |
116 return ReadValidated(offset, buffer_adapter, bytes_to_read, callback); | 58 CommonPostCondition(OPERATION_READ); |
| 59 return rv; |
117 } | 60 } |
118 | 61 |
119 int32_t PPB_FileIO_Shared::ReadToArray( | 62 int32_t PPB_FileIO_Shared::DoWrite(int64_t offset, |
120 int64_t offset, | 63 const char* buffer, |
121 int32_t max_read_length, | 64 int32_t bytes_to_write) { |
122 PP_ArrayOutput* output_array_buffer, | 65 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) | 66 if (rv != PP_OK) |
126 return rv; | 67 return rv; |
127 if (!output_array_buffer) | 68 rv = WriteValidated(offset, buffer, bytes_to_write); |
128 return PP_ERROR_BADARGUMENT; | 69 CommonPostCondition(OPERATION_WRITE); |
129 return ReadValidated(offset, *output_array_buffer, max_read_length, callback); | 70 return rv; |
130 } | 71 } |
131 | 72 |
132 int32_t PPB_FileIO_Shared::Write(int64_t offset, | 73 int32_t PPB_FileIO_Shared::DoSetLength(int64_t length) { |
133 const char* buffer, | 74 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) | 75 if (rv != PP_OK) |
138 return rv; | 76 return rv; |
139 return WriteValidated(offset, buffer, bytes_to_write, callback); | 77 rv = SetLengthValidated(length); |
| 78 CommonPostCondition(OPERATION_EXCLUSIVE); |
| 79 return rv; |
140 } | 80 } |
141 | 81 |
142 int32_t PPB_FileIO_Shared::SetLength(int64_t length, | 82 int32_t PPB_FileIO_Shared::DoFlush() { |
143 scoped_refptr<TrackedCallback> callback) { | 83 int32_t rv = CommonPreCondition(true, OPERATION_EXCLUSIVE); |
144 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); | |
145 if (rv != PP_OK) | 84 if (rv != PP_OK) |
146 return rv; | 85 return rv; |
147 return SetLengthValidated(length, callback); | 86 rv = FlushValidated(); |
| 87 CommonPostCondition(OPERATION_EXCLUSIVE); |
| 88 return rv; |
148 } | 89 } |
149 | 90 |
150 int32_t PPB_FileIO_Shared::Flush(scoped_refptr<TrackedCallback> callback) { | 91 void PPB_FileIO_Shared::SetOpenSucceed() { |
151 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); | 92 file_open_ = true; |
152 if (rv != PP_OK) | |
153 return rv; | |
154 return FlushValidated(callback); | |
155 } | 93 } |
156 | 94 |
157 void PPB_FileIO_Shared::ExecuteGeneralCallback(int32_t pp_error) { | 95 bool PPB_FileIO_Shared::CheckOpenState(bool should_be_open) const { |
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) { | 96 if (should_be_open) { |
200 if (!file_open_) | 97 if (!file_open_) |
201 return PP_ERROR_FAILED; | 98 return false; |
202 } else { | 99 } else { |
203 if (file_open_) | 100 if (file_open_) |
204 return PP_ERROR_FAILED; | 101 return false; |
205 } | 102 } |
206 | 103 return true; |
207 if (pending_op_ != OPERATION_NONE && | |
208 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) { | |
209 return PP_ERROR_INPROGRESS; | |
210 } | |
211 | |
212 return PP_OK; | |
213 } | |
214 | |
215 void PPB_FileIO_Shared::RegisterCallback( | |
216 OperationType 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 } | |
232 | |
233 void PPB_FileIO_Shared::RunAndRemoveFirstPendingCallback(int32_t result) { | |
234 DCHECK(!callbacks_.empty()); | |
235 | |
236 CallbackEntry front = callbacks_.front(); | |
237 callbacks_.pop_front(); | |
238 if (callbacks_.empty()) | |
239 pending_op_ = OPERATION_NONE; | |
240 | |
241 front.callback->Run(result); | |
242 } | 104 } |
243 | 105 |
244 } // namespace ppapi | 106 } // namespace ppapi |
OLD | NEW |