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