OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ | |
6 #define PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ | |
7 | |
8 #include <deque> | |
9 | |
10 #include "base/compiler_specific.h" | |
11 #include "ppapi/shared_impl/ppapi_shared_export.h" | |
12 #include "ppapi/shared_impl/resource.h" | |
13 #include "ppapi/shared_impl/tracked_callback.h" | |
14 #include "ppapi/thunk/ppb_file_io_api.h" | |
15 | |
16 namespace ppapi { | |
17 | |
18 namespace thunk { | |
19 class PPB_FileRef_API; | |
20 } | |
21 | |
22 class PPAPI_SHARED_EXPORT PPB_FileIO_Shared : public Resource, | |
23 public thunk::PPB_FileIO_API { | |
24 public: | |
25 PPB_FileIO_Shared(PP_Instance instance); | |
26 PPB_FileIO_Shared(const HostResource& host_resource); | |
27 ~PPB_FileIO_Shared(); | |
28 | |
29 // Resource overrides. | |
30 virtual thunk::PPB_FileIO_API* AsPPB_FileIO_API() OVERRIDE; | |
31 | |
32 // PPB_FileIO_API implementation. | |
33 virtual int32_t Open(PP_Resource file_ref, | |
34 int32_t open_flags, | |
35 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
36 virtual int32_t Query(PP_FileInfo* info, | |
37 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
38 virtual int32_t Touch(PP_Time last_access_time, | |
39 PP_Time last_modified_time, | |
40 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
41 virtual int32_t Read(int64_t offset, | |
42 char* buffer, | |
43 int32_t bytes_to_read, | |
44 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
45 virtual int32_t ReadToArray(int64_t offset, | |
46 int32_t max_read_length, | |
47 PP_ArrayOutput* output_array_buffer, | |
48 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
49 virtual int32_t Write(int64_t offset, | |
50 const char* buffer, | |
51 int32_t bytes_to_write, | |
52 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
53 virtual int32_t SetLength(int64_t length, | |
54 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
55 virtual int32_t Flush(scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
56 | |
57 // Callback handler for different types of operations. | |
58 void ExecuteGeneralCallback(int32_t pp_error); | |
59 void ExecuteOpenFileCallback(int32_t pp_error); | |
60 void ExecuteQueryCallback(int32_t pp_error, const PP_FileInfo& info); | |
61 void ExecuteReadCallback(int32_t pp_error_or_bytes, const char* data); | |
62 | |
63 protected: | |
64 struct CallbackEntry { | |
65 CallbackEntry(); | |
66 CallbackEntry(const CallbackEntry& entry); | |
67 ~CallbackEntry(); | |
68 | |
69 scoped_refptr<TrackedCallback> callback; | |
70 | |
71 // Output buffer used only by |Read()|. | |
72 PP_ArrayOutput read_buffer; | |
73 | |
74 // Pointer back to the caller's PP_FileInfo structure for Query operations. | |
75 // NULL for non-query operations. Not owned. | |
76 PP_FileInfo* info; | |
77 }; | |
78 | |
79 enum OperationType { | |
80 // There is no pending operation right now. | |
81 OPERATION_NONE, | |
82 | |
83 // If there are pending reads, any other kind of async operation is not | |
84 // allowed. | |
85 OPERATION_READ, | |
86 | |
87 // If there are pending writes, any other kind of async operation is not | |
88 // allowed. | |
89 OPERATION_WRITE, | |
90 | |
91 // If there is a pending operation that is neither read nor write, no | |
92 // further async operation is allowed. | |
93 OPERATION_EXCLUSIVE | |
94 }; | |
95 | |
96 // Validated versions of the FileIO API. Subclasses in the proxy and impl | |
97 // implement these so the common error checking stays here. | |
98 virtual int32_t OpenValidated(PP_Resource file_ref_resource, | |
99 thunk::PPB_FileRef_API* file_ref_api, | |
100 int32_t open_flags, | |
101 scoped_refptr<TrackedCallback> callback) = 0; | |
102 virtual int32_t QueryValidated(PP_FileInfo* info, | |
103 scoped_refptr<TrackedCallback> callback) = 0; | |
104 virtual int32_t TouchValidated(PP_Time last_access_time, | |
105 PP_Time last_modified_time, | |
106 scoped_refptr<TrackedCallback> callback) = 0; | |
107 virtual int32_t ReadValidated(int64_t offset, | |
108 const PP_ArrayOutput& buffer, | |
109 int32_t max_read_length, | |
110 scoped_refptr<TrackedCallback> callback) = 0; | |
111 virtual int32_t WriteValidated(int64_t offset, | |
112 const char* buffer, | |
113 int32_t bytes_to_write, | |
114 scoped_refptr<TrackedCallback> callback) = 0; | |
115 virtual int32_t SetLengthValidated( | |
116 int64_t length, | |
117 scoped_refptr<TrackedCallback> callback) = 0; | |
118 virtual int32_t FlushValidated(scoped_refptr<TrackedCallback> callback) = 0; | |
119 | |
120 // Called for every "Validated" function. | |
121 // | |
122 // This verifies that the callback is valid and that no callback is already | |
123 // pending, or it is a read(write) request and currently the pending | |
124 // operations are reads(writes). | |
125 // | |
126 // Returns |PP_OK| to indicate that everything is valid or |PP_ERROR_...| if | |
127 // the call should be aborted and that code returned to the plugin. | |
128 int32_t CommonCallValidation(bool should_be_open, OperationType new_op); | |
129 | |
130 // Sets up a pending callback. This should only be called once it is certain | |
131 // that |PP_OK_COMPLETIONPENDING| will be returned. | |
132 // | |
133 // |read_buffer| is only used by read operations, |info| is used only by | |
134 // query operations. | |
135 void RegisterCallback(OperationType op, | |
136 scoped_refptr<TrackedCallback> callback, | |
137 const PP_ArrayOutput* read_buffer, | |
138 PP_FileInfo* info); | |
139 | |
140 // Pops the oldest callback from the queue and runs it. | |
141 void RunAndRemoveFirstPendingCallback(int32_t result); | |
142 | |
143 // The file system type specified in the Open() call. This will be | |
144 // PP_FILESYSTEMTYPE_INVALID before open was called. This value does not | |
145 // indicate that the open command actually succeeded. | |
146 PP_FileSystemType file_system_type_; | |
147 | |
148 // Set to true when the file has been successfully opened. | |
149 bool file_open_; | |
150 | |
151 std::deque<CallbackEntry> callbacks_; | |
152 OperationType pending_op_; | |
153 | |
154 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Shared); | |
155 }; | |
156 | |
157 } // namespace ppapi | |
158 | |
159 #endif // PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ | |
OLD | NEW |