Pepper_47_C++_interfaces
Classes | Public Member Functions | List of all members
pp::FileIO Class Reference

The FileIO class represents a regular file. More...

#include <file_io.h>

Inheritance diagram for pp::FileIO:
Inheritance graph
Collaboration diagram for pp::FileIO:
Collaboration graph

Public Member Functions

 FileIO ()
 
 FileIO (const InstanceHandle &instance)
 
 FileIO (const FileIO &other)
 
int32_t Open (const FileRef &file_ref, int32_t open_flags, const CompletionCallback &cc)
 
int32_t Query (PP_FileInfo *result_buf, const CompletionCallback &cc)
 
int32_t Touch (PP_Time last_access_time, PP_Time last_modified_time, const CompletionCallback &cc)
 
int32_t Read (int64_t offset, char *buffer, int32_t bytes_to_read, const CompletionCallback &cc)
 
int32_t Read (int32_t offset, int32_t max_read_length, const CompletionCallbackWithOutput< std::vector< char > > &cc)
 
int32_t Write (int64_t offset, const char *buffer, int32_t bytes_to_write, const CompletionCallback &cc)
 
int32_t SetLength (int64_t length, const CompletionCallback &cc)
 
int32_t Flush (const CompletionCallback &cc)
 
void Close ()
 
- Public Member Functions inherited from pp::Resource
 Resource ()
 The default constructor. More...
 
 Resource (const Resource &other)
 
virtual ~Resource ()
 Destructor. More...
 
Resourceoperator= (const Resource &other)
 
bool is_null () const
 
PP_Resource pp_resource () const
 
PP_Resource detach ()
 

Additional Inherited Members

- Protected Member Functions inherited from pp::Resource
 Resource (PP_Resource resource)
 
 Resource (PassRef, PP_Resource resource)
 
void PassRefFromConstructor (PP_Resource resource)
 
void Clear ()
 Sets this resource to null. This releases ownership of the resource. More...
 

Detailed Description

The FileIO class represents a regular file.

Definition at line 23 of file file_io.h.

Constructor & Destructor Documentation

pp::FileIO::FileIO ( )

Default constructor for creating an is_null() FileIO object.

Definition at line 31 of file file_io.cc.

pp::FileIO::FileIO ( const InstanceHandle instance)
explicit

A constructor used to create a FileIO and associate it with the provided Instance.

Parameters
[in]instanceThe instance with which this resource will be associated.

Definition at line 34 of file file_io.cc.

References pp::Resource::PassRefFromConstructor(), and pp::InstanceHandle::pp_instance().

pp::FileIO::FileIO ( const FileIO other)

The copy constructor for FileIO.

Parameters
[in]otherA reference to a FileIO.

Definition at line 44 of file file_io.cc.

Member Function Documentation

void pp::FileIO::Close ( )

Close() cancels any IO that may be pending, and closes the FileIO object. Any pending callbacks will still run, reporting PP_ERROR_ABORTED if pending IO was interrupted. It is not valid to call Open() again after a call to this method.

Note: If the FileIO object is destroyed, and it is still open, then it will be implicitly closed, so you are not required to call Close().

Definition at line 170 of file file_io.cc.

References pp::Resource::pp_resource().

int32_t pp::FileIO::Flush ( const CompletionCallback cc)

Flush() flushes changes to disk. This call can be very expensive!

Parameters
[in]ccA CompletionCallback to be called upon completion of Flush().
Returns
An int32_t containing an error code from pp_errors.h.

Definition at line 159 of file file_io.cc.

References pp::CompletionCallback::MayForce(), pp::CompletionCallback::pp_completion_callback(), and pp::Resource::pp_resource().

int32_t pp::FileIO::Open ( const FileRef file_ref,
int32_t  open_flags,
const CompletionCallback cc 
)

Open() opens the specified regular file for I/O according to the given open flags, which is a bit-mask of the PP_FileOpenFlags values. Upon success, the corresponding file is classified as "in use" by this FileIO object until such time as the FileIO object is closed or destroyed.

Parameters
[in]file_refA PP_Resource corresponding to a file reference.
[in]open_flagsA bit-mask of the PP_FileOpenFlags values. Valid values are:
  • PP_FILEOPENFLAG_READ
  • PP_FILEOPENFLAG_WRITE
  • PP_FILEOPENFLAG_CREATE
  • PP_FILEOPENFLAG_TRUNCATE
  • PP_FILEOPENFLAG_EXCLUSIVE See PP_FileOpenFlags in ppb_file_io.h for more details on these flags.
[in]ccA CompletionCallback to be called upon completion of Open().
Returns
An int32_t containing an error code from pp_errors.h.

Definition at line 48 of file file_io.cc.

References pp::CompletionCallback::MayForce(), pp::CompletionCallback::pp_completion_callback(), and pp::Resource::pp_resource().

int32_t pp::FileIO::Query ( PP_FileInfo *  result_buf,
const CompletionCallback cc 
)

Query() queries info about the file opened by this FileIO object. This function will fail if the FileIO object has not been opened.

Parameters
[in]result_bufThe PP_FileInfo structure representing all information about the file.
[in]ccA CompletionCallback to be called upon completion of Query(). result_buf must remain valid until after the callback runs. If you pass a blocking callback, result_buf must remain valid until after Query() returns.
Returns
An int32_t containing an error code from pp_errors.h.

Definition at line 63 of file file_io.cc.

References pp::CompletionCallback::MayForce(), pp::CompletionCallback::pp_completion_callback(), and pp::Resource::pp_resource().

int32_t pp::FileIO::Read ( int64_t  offset,
char *  buffer,
int32_t  bytes_to_read,
const CompletionCallback cc 
)

Reads from an offset in the file.

The size of the buffer must be large enough to hold the specified number of bytes to read. This function might perform a partial read, meaning that all the requested bytes might not be returned, even if the end of the file has not been reached.

This function reads into a buffer that the caller supplies. This buffer must remain valid as long as the FileIO resource is alive. If you use a completion callback factory and it goes out of scope, it will not issue the callback on your class, BUT the callback factory can NOT cancel the request from the browser's perspective. This means that the browser will still try to write to your buffer even if the callback factory is destroyed!

So you must ensure that your buffer outlives the FileIO resource. If you have one class and use the FileIO resource exclusively from that class and never make any copies, this will be fine: the resource will be destroyed when your class is. But keep in mind that copying a pp::FileIO object just creates a second reference to the original resource. For example, if you have a function like this: pp::FileIO MyClass::GetFileIO(); where a copy of your FileIO resource could outlive your class, the callback will still be pending when your class goes out of scope, creating the possibility of writing into invalid memory. So it's recommended to keep your FileIO resource and any output buffers tightly controlled in the same scope.

Caveat: This Read() is potentially unsafe if you're using a CompletionCallbackFactory to scope callbacks to the lifetime of your class. When your class goes out of scope, the callback factory will not actually cancel the callback, but will rather just skip issuing the callback on your class. This means that if the FileIO object outlives your class (if you made a copy saved somewhere else, for example), then the browser will still try to write into your buffer when the asynchronous read completes, potentially causing a crash.

See the other version of Read() which avoids this problem by writing into CompletionCallbackWithOutput, where the output buffer is automatically managed by the callback.

Parameters
[in]offsetThe offset into the file.
[out]bufferThe buffer to hold the specified number of bytes read.
[in]bytes_to_readThe number of bytes to read from offset.
[in]ccA CompletionCallback to be called upon completion of Read(). buffer must remain valid until after the callback runs. If you pass a blocking callback, buffer must remain valid until after Read() returns.
Returns
An The number of bytes read an error code from pp_errors.h. If the return value is 0, then end-of-file was reached. It is valid to call Read() multiple times with a completion callback to queue up parallel reads from the file at different offsets.

Definition at line 90 of file file_io.cc.

References pp::CompletionCallback::MayForce(), pp::CompletionCallback::pp_completion_callback(), and pp::Resource::pp_resource().

Referenced by Read().

int32_t pp::FileIO::Read ( int32_t  offset,
int32_t  max_read_length,
const CompletionCallbackWithOutput< std::vector< char > > &  cc 
)

Read() reads from an offset in the file. A PP_ArrayOutput must be provided so that output will be stored in its allocated buffer. This function might perform a partial read.

Parameters
[in]file_ioA PP_Resource corresponding to a file FileIO.
[in]offsetThe offset into the file.
[in]max_read_lengthThe maximum number of bytes to read from offset.
[in]outputA PP_ArrayOutput to hold the output data.
[in]callbackA PP_CompletionCallback to be called upon completion of Read().
Returns
The number of bytes read or an error code from pp_errors.h. If the return value is 0, then end-of-file was reached. It is valid to call Read() multiple times with a completion callback to queue up parallel reads from the file, but pending reads cannot be interleaved with other operations.

Definition at line 104 of file file_io.cc.

References pp::Resource::pp_resource(), and Read().

int32_t pp::FileIO::SetLength ( int64_t  length,
const CompletionCallback cc 
)

SetLength() sets the length of the file. If the file size is extended, then the extended area of the file is zero-filled. The FileIO object must have been opened with write access.

Parameters
[in]lengthThe length of the file to be set.
[in]ccA CompletionCallback to be called upon completion of SetLength().
Returns
An int32_t containing an error code from pp_errors.h.

Definition at line 147 of file file_io.cc.

References pp::CompletionCallback::MayForce(), pp::CompletionCallback::pp_completion_callback(), and pp::Resource::pp_resource().

int32_t pp::FileIO::Touch ( PP_Time  last_access_time,
PP_Time  last_modified_time,
const CompletionCallback cc 
)

Touch() Updates time stamps for the file opened by this FileIO object. This function will fail if the FileIO object has not been opened.

Parameters
[in]last_access_timeThe last time the FileIO was accessed.
[in]last_modified_timeThe last time the FileIO was modified.
[in]ccA CompletionCallback to be called upon completion of Touch().
Returns
An int32_t containing an error code from pp_errors.h.

Definition at line 75 of file file_io.cc.

References pp::CompletionCallback::MayForce(), pp::CompletionCallback::pp_completion_callback(), and pp::Resource::pp_resource().

int32_t pp::FileIO::Write ( int64_t  offset,
const char *  buffer,
int32_t  bytes_to_write,
const CompletionCallback cc 
)

Write() writes to an offset in the file. This function might perform a partial write. The FileIO object must have been opened with write access.

Parameters
[in]offsetThe offset into the file.
[in]bufferThe buffer to hold the specified number of bytes to write.
[in]bytes_to_writeThe number of bytes to write to offset.
[in]ccA CompletionCallback to be called upon completion of Write().
Returns
An The number of bytes written or an error code from pp_errors.h. If the return value is 0, then end-of-file was reached. It is valid to call Write() multiple times with a completion callback to queue up parallel writes to the file at different offsets.

Definition at line 131 of file file_io.cc.

References pp::CompletionCallback::MayForce(), pp::CompletionCallback::pp_completion_callback(), and pp::Resource::pp_resource().


The documentation for this class was generated from the following files: