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_
10 #include "ppapi/c/private/pp_file_handle.h"
11 #include "ppapi/proxy/connection.h"
12 #include "ppapi/proxy/plugin_resource.h"
13 #include "ppapi/proxy/ppapi_proxy_export.h"
14 #include "ppapi/shared_impl/file_io_state_manager.h"
15 #include "ppapi/thunk/ppb_file_io_api.h"
19 class TrackedCallback
;
23 class PPAPI_PROXY_EXPORT FileIOResource
24 : public PluginResource
,
25 public thunk::PPB_FileIO_API
{
27 FileIOResource(Connection connection
, PP_Instance instance
);
28 virtual ~FileIOResource();
30 // Resource overrides.
31 virtual thunk::PPB_FileIO_API
* AsPPB_FileIO_API() OVERRIDE
;
33 // PPB_FileIO_API implementation.
34 virtual int32_t Open(PP_Resource file_ref
,
36 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
37 virtual int32_t Query(PP_FileInfo
* info
,
38 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
39 virtual int32_t Touch(PP_Time last_access_time
,
40 PP_Time last_modified_time
,
41 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
42 virtual int32_t Read(int64_t offset
,
44 int32_t bytes_to_read
,
45 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
46 virtual int32_t ReadToArray(int64_t offset
,
47 int32_t max_read_length
,
48 PP_ArrayOutput
* array_output
,
49 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
50 virtual int32_t Write(int64_t offset
,
52 int32_t bytes_to_write
,
53 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
54 virtual int32_t SetLength(int64_t length
,
55 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
56 virtual int32_t Flush(scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
57 virtual void Close() OVERRIDE
;
58 virtual int32_t GetOSFileDescriptor() OVERRIDE
;
59 virtual int32_t RequestOSFileHandle(
60 PP_FileHandle
* handle
,
61 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
62 virtual int32_t WillWrite(int64_t offset
,
63 int32_t bytes_to_write
,
64 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
65 virtual int32_t WillSetLength(
67 scoped_refptr
<TrackedCallback
> callback
) OVERRIDE
;
70 // Class to perform file query operations across multiple threads.
71 class QueryOp
: public base::RefCountedThreadSafe
<QueryOp
> {
73 explicit QueryOp(PP_FileHandle file_handle
);
75 // Queries the file. Called on the file thread (non-blocking) or the plugin
76 // thread (blocking). This should not be called when we hold the proxy lock.
79 const base::PlatformFileInfo
& file_info() const { return file_info_
; }
82 friend class base::RefCountedThreadSafe
<QueryOp
>;
85 PP_FileHandle file_handle_
;
86 base::PlatformFileInfo file_info_
;
89 // Class to perform file read operations across multiple threads.
90 class ReadOp
: public base::RefCountedThreadSafe
<ReadOp
> {
92 ReadOp(PP_FileHandle file_handle
, int64_t offset
, int32_t bytes_to_read
);
94 // Reads the file. Called on the file thread (non-blocking) or the plugin
95 // thread (blocking). This should not be called when we hold the proxy lock.
98 char* buffer() const { return buffer_
.get(); }
101 friend class base::RefCountedThreadSafe
<ReadOp
>;
104 PP_FileHandle file_handle_
;
106 int32_t bytes_to_read_
;
107 scoped_ptr
<char[]> buffer_
;
110 int32_t ReadValidated(int64_t offset
,
111 int32_t bytes_to_read
,
112 const PP_ArrayOutput
& array_output
,
113 scoped_refptr
<TrackedCallback
> callback
);
115 void CloseFileHandle();
118 // Completion tasks for file operations that are done in the plugin.
119 int32_t OnQueryComplete(scoped_refptr
<QueryOp
> query_op
,
122 int32_t OnReadComplete(scoped_refptr
<ReadOp
> read_op
,
123 PP_ArrayOutput array_output
,
126 // Reply message handlers for operations that are done in the host.
127 void OnPluginMsgGeneralComplete(scoped_refptr
<TrackedCallback
> callback
,
128 const ResourceMessageReplyParams
& params
);
129 void OnPluginMsgOpenFileComplete(scoped_refptr
<TrackedCallback
> callback
,
130 const ResourceMessageReplyParams
& params
);
131 void OnPluginMsgRequestOSFileHandleComplete(
132 scoped_refptr
<TrackedCallback
> callback
,
133 PP_FileHandle
* output_handle
,
134 const ResourceMessageReplyParams
& params
);
136 PP_FileHandle file_handle_
;
137 PP_FileSystemType file_system_type_
;
138 FileIOStateManager state_manager_
;
140 DISALLOW_COPY_AND_ASSIGN(FileIOResource
);
146 #endif // PPAPI_PROXY_FILE_IO_RESOURCE_H_