Add views implementation for AXTreeSource.
[chromium-blink-merge.git] / ppapi / proxy / file_io_resource.h
blob5d580350384ea09f5db4809856244f9219087142
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.
5 #ifndef PPAPI_PROXY_FILE_IO_RESOURCE_H_
6 #define PPAPI_PROXY_FILE_IO_RESOURCE_H_
8 #include <string>
10 #include "base/files/file.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "ppapi/c/private/pp_file_handle.h"
14 #include "ppapi/proxy/connection.h"
15 #include "ppapi/proxy/plugin_resource.h"
16 #include "ppapi/proxy/ppapi_proxy_export.h"
17 #include "ppapi/shared_impl/file_io_state_manager.h"
18 #include "ppapi/shared_impl/resource.h"
19 #include "ppapi/shared_impl/scoped_pp_resource.h"
20 #include "ppapi/thunk/ppb_file_io_api.h"
22 namespace ppapi {
24 class TrackedCallback;
26 namespace proxy {
28 class PPAPI_PROXY_EXPORT FileIOResource
29 : public PluginResource,
30 public thunk::PPB_FileIO_API {
31 public:
32 FileIOResource(Connection connection, PP_Instance instance);
33 virtual ~FileIOResource();
35 // Resource overrides.
36 virtual thunk::PPB_FileIO_API* AsPPB_FileIO_API() OVERRIDE;
38 // PPB_FileIO_API implementation.
39 virtual int32_t Open(PP_Resource file_ref,
40 int32_t open_flags,
41 scoped_refptr<TrackedCallback> callback) OVERRIDE;
42 virtual int32_t Query(PP_FileInfo* info,
43 scoped_refptr<TrackedCallback> callback) OVERRIDE;
44 virtual int32_t Touch(PP_Time last_access_time,
45 PP_Time last_modified_time,
46 scoped_refptr<TrackedCallback> callback) OVERRIDE;
47 virtual int32_t Read(int64_t offset,
48 char* buffer,
49 int32_t bytes_to_read,
50 scoped_refptr<TrackedCallback> callback) OVERRIDE;
51 virtual int32_t ReadToArray(int64_t offset,
52 int32_t max_read_length,
53 PP_ArrayOutput* array_output,
54 scoped_refptr<TrackedCallback> callback) OVERRIDE;
55 virtual int32_t Write(int64_t offset,
56 const char* buffer,
57 int32_t bytes_to_write,
58 scoped_refptr<TrackedCallback> callback) OVERRIDE;
59 virtual int32_t SetLength(int64_t length,
60 scoped_refptr<TrackedCallback> callback) OVERRIDE;
61 virtual int64_t GetMaxWrittenOffset() const OVERRIDE;
62 virtual int64_t GetAppendModeWriteAmount() const OVERRIDE;
63 virtual void SetMaxWrittenOffset(int64_t max_written_offset) OVERRIDE;
64 virtual void SetAppendModeWriteAmount(
65 int64_t append_mode_write_amount) OVERRIDE;
66 virtual int32_t Flush(scoped_refptr<TrackedCallback> callback) OVERRIDE;
67 virtual void Close() OVERRIDE;
68 virtual int32_t RequestOSFileHandle(
69 PP_FileHandle* handle,
70 scoped_refptr<TrackedCallback> callback) OVERRIDE;
72 // FileHandleHolder is used to guarantee that file operations will have a
73 // valid FD to operate on, even if they're in a different thread.
74 // If instead we just passed the raw FD, the FD could be closed before the
75 // file operation has a chance to run. It could interact with an invalid FD,
76 // or worse, the FD value could be reused if another file is opened quickly
77 // (POSIX is required to provide the lowest available value when opening a
78 // file). This could result in strange problems such as writing data to the
79 // wrong file.
81 // Operations that run on a background thread should hold one of these to
82 // ensure they have a valid file descriptor. The file handle is only closed
83 // when the last reference to the FileHandleHolder is removed, so we are
84 // guaranteed to operate on the correct file descriptor. It *is* still
85 // possible that the FileIOResource will be destroyed and "Abort" callbacks
86 // just before the operation does its task (e.g., Reading). In that case, we
87 // might for example Read from a file even though the FileIO has been
88 // destroyed and the plugin's callback got a PP_ERROR_ABORTED result. In the
89 // case of a write, we could write some data to the file despite the plugin
90 // receiving a PP_ERROR_ABORTED instead of a successful result.
91 class FileHandleHolder : public base::RefCountedThreadSafe<FileHandleHolder> {
92 public:
93 explicit FileHandleHolder(PP_FileHandle file_handle_);
94 PP_FileHandle raw_handle() {
95 return raw_handle_;
97 static bool IsValid(
98 const scoped_refptr<FileIOResource::FileHandleHolder>& handle);
99 private:
100 friend class base::RefCountedThreadSafe<FileHandleHolder>;
101 ~FileHandleHolder();
102 PP_FileHandle raw_handle_;
104 scoped_refptr<FileHandleHolder> file_handle() {
105 return file_handle_;
108 private:
109 // Class to perform file query operations across multiple threads.
110 class QueryOp : public base::RefCountedThreadSafe<QueryOp> {
111 public:
112 explicit QueryOp(scoped_refptr<FileHandleHolder> file_handle);
114 // Queries the file. Called on the file thread (non-blocking) or the plugin
115 // thread (blocking). This should not be called when we hold the proxy lock.
116 int32_t DoWork();
118 const base::File::Info& file_info() const { return file_info_; }
120 private:
121 friend class base::RefCountedThreadSafe<QueryOp>;
122 ~QueryOp();
124 scoped_refptr<FileHandleHolder> file_handle_;
125 base::File::Info file_info_;
128 // Class to perform file read operations across multiple threads.
129 class ReadOp : public base::RefCountedThreadSafe<ReadOp> {
130 public:
131 ReadOp(scoped_refptr<FileHandleHolder> file_handle,
132 int64_t offset,
133 int32_t bytes_to_read);
135 // Reads the file. Called on the file thread (non-blocking) or the plugin
136 // thread (blocking). This should not be called when we hold the proxy lock.
137 int32_t DoWork();
139 char* buffer() const { return buffer_.get(); }
141 private:
142 friend class base::RefCountedThreadSafe<ReadOp>;
143 ~ReadOp();
145 scoped_refptr<FileHandleHolder> file_handle_;
146 int64_t offset_;
147 int32_t bytes_to_read_;
148 scoped_ptr<char[]> buffer_;
151 // Class to perform file write operations across multiple threads.
152 class WriteOp : public base::RefCountedThreadSafe<WriteOp> {
153 public:
154 WriteOp(scoped_refptr<FileHandleHolder> file_handle,
155 int64_t offset,
156 const char* buffer,
157 int32_t bytes_to_write,
158 bool append);
160 // Writes the file. Called on the file thread (non-blocking) or the plugin
161 // thread (blocking). This should not be called when we hold the proxy lock.
162 int32_t DoWork();
164 private:
165 friend class base::RefCountedThreadSafe<WriteOp>;
166 ~WriteOp();
168 scoped_refptr<FileHandleHolder> file_handle_;
169 int64_t offset_;
170 const char* buffer_;
171 int32_t bytes_to_write_;
172 bool append_;
175 void OnRequestWriteQuotaComplete(int64_t offset,
176 const char* buffer,
177 int32_t bytes_to_write,
178 scoped_refptr<TrackedCallback> callback,
179 int64_t granted);
180 void OnRequestSetLengthQuotaComplete(int64_t length,
181 scoped_refptr<TrackedCallback> callback,
182 int64_t granted);
184 int32_t ReadValidated(int64_t offset,
185 int32_t bytes_to_read,
186 const PP_ArrayOutput& array_output,
187 scoped_refptr<TrackedCallback> callback);
188 int32_t WriteValidated(int64_t offset,
189 const char* buffer,
190 int32_t bytes_to_write,
191 scoped_refptr<TrackedCallback> callback);
192 void SetLengthValidated(int64_t length,
193 scoped_refptr<TrackedCallback> callback);
195 // Completion tasks for file operations that are done in the plugin.
196 int32_t OnQueryComplete(scoped_refptr<QueryOp> query_op,
197 PP_FileInfo* info,
198 int32_t result);
199 int32_t OnReadComplete(scoped_refptr<ReadOp> read_op,
200 PP_ArrayOutput array_output,
201 int32_t result);
202 int32_t OnWriteComplete(scoped_refptr<WriteOp> write_op,
203 int32_t result);
205 // Reply message handlers for operations that are done in the host.
206 void OnPluginMsgGeneralComplete(scoped_refptr<TrackedCallback> callback,
207 const ResourceMessageReplyParams& params);
208 void OnPluginMsgOpenFileComplete(scoped_refptr<TrackedCallback> callback,
209 const ResourceMessageReplyParams& params,
210 PP_Resource quota_file_system,
211 int64_t max_written_offset);
212 void OnPluginMsgRequestOSFileHandleComplete(
213 scoped_refptr<TrackedCallback> callback,
214 PP_FileHandle* output_handle,
215 const ResourceMessageReplyParams& params);
217 scoped_refptr<FileHandleHolder> file_handle_;
218 PP_FileSystemType file_system_type_;
219 scoped_refptr<Resource> file_system_resource_;
220 FileIOStateManager state_manager_;
222 scoped_refptr<Resource> file_ref_;
224 int32_t open_flags_;
225 int64_t max_written_offset_;
226 int64_t append_mode_write_amount_;
227 bool check_quota_;
228 bool called_close_;
230 DISALLOW_COPY_AND_ASSIGN(FileIOResource);
233 } // namespace proxy
234 } // namespace ppapi
236 #endif // PPAPI_PROXY_FILE_IO_RESOURCE_H_