file_stream.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file defines FileStream, a basic interface for reading and writing files
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// synchronously or asynchronously with support for seeking to an offset.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that even when used asynchronously, only one operation is supported at
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a time.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_BASE_FILE_STREAM_H_
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_BASE_FILE_STREAM_H_
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/platform_file.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/completion_callback.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/file_stream_whence.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FilePath;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IOBuffer;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT FileStream {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a |FileStream| with a new |BoundNetLog| (based on |net_log|)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // attached.  |net_log| may be NULL if no logging is needed.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FileStream(net::NetLog* net_log);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Construct a FileStream with an existing file handle and opening flags.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |file| is valid file handle.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |flags| is a bitfield of base::PlatformFileFlags when the file handle was
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // opened.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |net_log| is the net log pointer to use to create a |BoundNetLog|.  May be
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NULL if logging is not needed.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note: the new FileStream object takes ownership of the PlatformFile and
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will close it on destruction.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileStream(base::PlatformFile file, int flags, net::NetLog* net_log);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The underlying file is closed automatically.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FileStream();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call this method to open the FileStream asynchronously.  The remaining
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // methods cannot be used unless the file is opened successfully. Returns
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ERR_IO_PENDING if the operation is started. If the operation cannot be
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // started then an error code is returned.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Once the operation is done, |callback| will be run on the thread where
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open() was called, with the result code. open_flags is a bitfield of
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // base::PlatformFileFlags.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the file stream is not closed manually, the underlying file will be
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // automatically closed when FileStream is destructed in an asynchronous
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // manner (i.e. the file stream is closed in the background but you don't
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // know when).
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int Open(const FilePath& path, int open_flags,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const CompletionCallback& callback);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call this method to open the FileStream synchronously.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The remaining methods cannot be used unless this method returns OK.  If
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the file cannot be opened then an error code is returned.  open_flags is
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a bitfield of base::PlatformFileFlags
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the file stream is not closed manually, the underlying file will be
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // automatically closed when FileStream is destructed.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int OpenSync(const FilePath& path, int open_flags);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if Open succeeded and Close has not been called.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsOpen() const;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adjust the position from where data is read asynchronously.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Upon success, ERR_IO_PENDING is returned and |callback| will be run
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on the thread where Seek() was called with the the stream position
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // relative to the start of the file.  Otherwise, an error code is returned.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is invalid to request any asynchronous operations while there is an
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in-flight asynchronous operation.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int Seek(Whence whence, int64 offset,
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const Int64CompletionCallback& callback);
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adjust the position from where data is read synchronously.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Upon success, the stream position relative to the start of the file is
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returned.  Otherwise, an error code is returned.  It is not valid to
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // call SeekSync while a Read call has a pending completion.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int64 SeekSync(Whence whence, int64 offset);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of bytes available to read from the current stream
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // position until the end of the file.  Otherwise, an error code is returned.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int64 Available();
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call this method to read data from the current stream position
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // asynchronously. Up to buf_len bytes will be copied into buf.  (In
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // other words, partial reads are allowed.)  Returns the number of bytes
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // copied, 0 if at end-of-file, or an error code if the operation could
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not be performed.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback must be passed to this method. If the read could not
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // complete synchronously, then ERR_IO_PENDING is returned, and the
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback will be run on the thread where Read() was called, when the
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // read has completed.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is valid to destroy or close the file stream while there is an
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // asynchronous read in progress.  That will cancel the read and allow
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the buffer to be freed.
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is invalid to request any asynchronous operations while there is an
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in-flight asynchronous operation.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method must not be called if the stream was opened WRITE_ONLY.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int Read(IOBuffer* buf, int buf_len,
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const CompletionCallback& callback);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call this method to read data from the current stream position
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // synchronously. Up to buf_len bytes will be copied into buf.  (In
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // other words, partial reads are allowed.)  Returns the number of bytes
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // copied, 0 if at end-of-file, or an error code if the operation could
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not be performed.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The file must not be opened with PLATFORM_FILE_ASYNC.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method must not be called if the stream was opened WRITE_ONLY.
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadSync(char* buf, int buf_len);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Performs the same as ReadSync, but ensures that exactly buf_len bytes
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // are copied into buf.  A partial read may occur, but only as a result of
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // end-of-file or fatal error.  Returns the number of bytes copied into buf,
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 0 if at end-of-file and no bytes have been read into buf yet,
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or an error code if the operation could not be performed.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int ReadUntilComplete(char *buf, int buf_len);
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call this method to write data at the current stream position
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // asynchronously.  Up to buf_len bytes will be written from buf. (In
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // other words, partial writes are allowed.)  Returns the number of
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // bytes written, or an error code if the operation could not be
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // performed.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback must be passed to this method. If the write could not
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // complete synchronously, then ERR_IO_PENDING is returned, and the
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback will be run on the thread where Write() was called when
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the write has completed.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is valid to destroy or close the file stream while there is an
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // asynchronous write in progress.  That will cancel the write and allow
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the buffer to be freed.
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is invalid to request any asynchronous operations while there is an
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in-flight asynchronous operation.
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method must not be called if the stream was opened READ_ONLY.
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int Write(IOBuffer* buf, int buf_len,
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const CompletionCallback& callback);
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call this method to write data at the current stream position
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // synchronously.  Up to buf_len bytes will be written from buf. (In
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // other words, partial writes are allowed.)  Returns the number of
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // bytes written, or an error code if the operation could not be
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // performed.
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The file must not be opened with PLATFORM_FILE_ASYNC.
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method must not be called if the stream was opened READ_ONLY.
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int WriteSync(const char* buf, int buf_len);
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Truncates the file to be |bytes| length. This is only valid for writable
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // files. After truncation the file stream is positioned at |bytes|. The new
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // position is returned, or a value < 0 on error.
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // WARNING: one may not truncate a file beyond its current length on any
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   platform with this call.
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int64 Truncate(int64 bytes);
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Forces out a filesystem sync on this file to make sure that the file was
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // written out to disk and is not currently sitting in the buffer. This does
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not have to be called, it just forces one to happen at the time of
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // calling.
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback must be passed to this method. If the write could not
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // complete synchronously, then ERR_IO_PENDING is returned, and the
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback will be run on the thread where Flush() was called when
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the write has completed.
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is valid to destroy or close the file stream while there is an
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // asynchronous flush in progress.  That will cancel the flush and allow
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the buffer to be freed.
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is invalid to request any asynchronous operations while there is an
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in-flight asynchronous operation.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method should not be called if the stream was opened READ_ONLY.
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int Flush(const CompletionCallback& callback);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Forces out a filesystem sync on this file to make sure that the file was
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // written out to disk and is not currently sitting in the buffer. This does
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not have to be called, it just forces one to happen at the time of
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // calling.
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns an error code if the operation could not be performed.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method should not be called if the stream was opened READ_ONLY.
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int FlushSync();
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Turns on UMA error statistics gathering.
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void EnableErrorStatistics();
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the source reference for net-internals logging.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates source dependency events between |owner_bound_net_log| and
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |bound_net_log_|.  Each gets an event showing the dependency on the other.
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If only one of those is valid, it gets an event showing that a change
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of ownership happened, but without details.
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log);
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the underlying platform file for testing.
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::PlatformFile GetPlatformFileForTesting();
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Context;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_async() const { return !!(open_flags_ & base::PLATFORM_FILE_ASYNC); }
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int open_flags_;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::BoundNetLog bound_net_log_;
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Context performing I/O operations. It was extracted into separate class
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to perform asynchronous operations because FileStream can be destroyed
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // before completion of async operation. Also if async FileStream is destroyed
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // without explicit closing file should be closed asynchronously without
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // delaying FileStream's destructor. To perform all that separate object is
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // necessary.
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<Context> context_;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FileStream);
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_BASE_FILE_STREAM_H_
237