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

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

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 #ifndef PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ 5 #ifndef PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_
6 #define PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ 6 #define PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_
7 7
8 #include <deque> 8 #include "base/basictypes.h"
9
10 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
11 #include "ppapi/shared_impl/ppapi_shared_export.h" 10 #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 11
16 namespace ppapi { 12 namespace ppapi {
17 13
18 namespace thunk { 14 namespace thunk {
19 class PPB_FileRef_API; 15 class PPB_FileRef_API;
20 } 16 }
21 17
22 class PPAPI_SHARED_EXPORT PPB_FileIO_Shared : public Resource, 18 class PPAPI_SHARED_EXPORT PPB_FileIO_Shared {
raymes 2012/11/28 18:39:35 Let's change the name to FileIOStateChecker (or ma
victorhsieh 2012/11/29 06:34:55 Done.
23 public thunk::PPB_FileIO_API { 19 protected:
24 public: 20 PPB_FileIO_Shared();
25 PPB_FileIO_Shared(PP_Instance instance);
26 PPB_FileIO_Shared(const HostResource& host_resource);
27 ~PPB_FileIO_Shared(); 21 ~PPB_FileIO_Shared();
28 22
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 { 23 enum OperationType {
80 // There is no pending operation right now. 24 // There is no pending operation right now.
81 OPERATION_NONE, 25 OPERATION_NONE,
82 26
83 // If there are pending reads, any other kind of async operation is not 27 // If there are pending reads, any other kind of async operation is not
84 // allowed. 28 // allowed.
85 OPERATION_READ, 29 OPERATION_READ,
86 30
87 // If there are pending writes, any other kind of async operation is not 31 // If there are pending writes, any other kind of async operation is not
88 // allowed. 32 // allowed.
89 OPERATION_WRITE, 33 OPERATION_WRITE,
90 34
91 // If there is a pending operation that is neither read nor write, no 35 // If there is a pending operation that is neither read nor write, no
92 // further async operation is allowed. 36 // further async operation is allowed.
93 OPERATION_EXCLUSIVE 37 OPERATION_EXCLUSIVE
94 }; 38 };
95 39
96 // Validated versions of the FileIO API. Subclasses in the proxy and impl 40 void SetOpenSucceed();
97 // implement these so the common error checking stays here. 41 bool CheckOpenState(bool should_be_open) const;
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 42
120 // Called for every "Validated" function. 43 // Called before every "Validated" function. It is responsible to make sure
44 // that "state" is correct. For example, some operations are only valid after
45 // the file is opened, or operations might need to run excludively. See
46 // subclasses implementation for detail.
121 // 47 //
122 // This verifies that the callback is valid and that no callback is already 48 // It returns |PP_OK| on success, or |PP_ERROR_...| for various reasons.
123 // pending, or it is a read(write) request and currently the pending 49 int32_t CommonPreCondition(bool should_be_open, OperationType new_op);
raymes 2012/11/28 18:39:35 Let's make this something more descriptive, like "
victorhsieh 2012/11/29 06:34:55 Done.
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 50
130 // Sets up a pending callback. This should only be called once it is certain 51 // Called after every "Validated" function.
131 // that |PP_OK_COMPLETIONPENDING| will be returned. 52 void CommonPostCondition(OperationType new_op);
raymes 2012/11/28 18:39:35 This function can be removed - it just calls SetOp
victorhsieh 2012/11/29 06:34:55 Done.
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 53
140 // Pops the oldest callback from the queue and runs it. 54 // Marks the state of current operations as started or finished.
141 void RunAndRemoveFirstPendingCallback(int32_t result); 55 void SetOpInProgress(OperationType op);
raymes 2012/11/28 18:39:35 I'd prefer SetOperationInProgress (as opposed to "
victorhsieh 2012/11/29 06:34:55 Done.
56 void SetOpFinished();
142 57
143 // The file system type specified in the Open() call. This will be 58 int num_pending_ops_;
144 // PP_FILESYSTEMTYPE_INVALID before open was called. This value does not 59 OperationType pending_op_;
145 // indicate that the open command actually succeeded.
146 PP_FileSystemType file_system_type_;
147 60
148 // Set to true when the file has been successfully opened. 61 // Set to true when the file has been successfully opened.
149 bool file_open_; 62 bool file_open_;
150 63
151 std::deque<CallbackEntry> callbacks_;
152 OperationType pending_op_;
153
154 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Shared); 64 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Shared);
155 }; 65 };
156 66
157 } // namespace ppapi 67 } // namespace ppapi
158 68
159 #endif // PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ 69 #endif // PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698