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)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef STORAGE_BLOB_FILE_STREAM_READER_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define STORAGE_BLOB_FILE_STREAM_READER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/files/file.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/completion_callback.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/storage_browser_export.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace base {
15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class FilePath;
16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class TaskRunner;
17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class Time;
18d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IOBuffer;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
25d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class FileSystemContext;
26d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class FileSystemURL;
27d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A generic interface for reading a file-like object.
32d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class FileStreamReader {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Creates a new FileReader for a local file |file_path|.
35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // |initial_offset| specifies the offset in the file where the first read
36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // should start.  If the given offset is out of the file range any
37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // read operation may error out with net::ERR_REQUEST_RANGE_NOT_SATISFIABLE.
38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // |expected_modification_time| specifies the expected last modification
39d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // If the value is non-null, the reader will check the underlying file's
40d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // actual modification time to see if the file has been modified, and if
41d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // it does any succeeding read operations should fail with
42d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // ERR_UPLOAD_FILE_CHANGED error.
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  STORAGE_EXPORT static FileStreamReader*
44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      CreateForLocalFile(base::TaskRunner* task_runner,
45d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                         const base::FilePath& file_path,
46d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                         int64 initial_offset,
47d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                         const base::Time& expected_modification_time);
48d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
49d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Creates a new reader for a filesystem URL |url| form |initial_offset|.
50d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // |expected_modification_time| specifies the expected last modification if
51d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // the value is non-null, the reader will check the underlying file's actual
52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // modification time to see if the file has been modified, and if it does any
53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // succeeding read operations should fail with ERR_UPLOAD_FILE_CHANGED error.
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  STORAGE_EXPORT static FileStreamReader*
5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      CreateForFileSystemFile(storage::FileSystemContext* context,
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                              const storage::FileSystemURL& url,
57d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                              int64 initial_offset,
58d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                              const base::Time& expected_modification_time);
59d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Verify if the underlying file has not been modified.
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  STORAGE_EXPORT static bool VerifySnapshotTime(
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const base::Time& expected_modification_time,
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const base::File::Info& file_info);
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is valid to delete the reader at any time.  If the stream is deleted
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // while it has a pending read, its callback will not be called.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FileStreamReader() {}
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reads from the current cursor position asynchronously.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Up to buf_len bytes will be copied into buf.  (In other words, partial
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // reads are allowed.)  Returns the number of bytes copied, 0 if at
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // end-of-file, or an error code if the operation could not be performed.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the read could not complete synchronously, then ERR_IO_PENDING is
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returned, and the callback will be run on the thread where Read()
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // was called, when the read has completed.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is invalid to call Read while there is an in-flight Read operation.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the stream is deleted while it has an in-flight Read operation
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |callback| will not be called.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int Read(net::IOBuffer* buf, int buf_len,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const net::CompletionCallback& callback) = 0;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the length of the file if it could successfully retrieve the
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // file info *and* its last modification time equals to
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // expected modification time (rv >= 0 cases).
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Otherwise, a negative error code is returned (rv < 0 cases).
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the stream is deleted while it has an in-flight GetLength operation
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |callback| will not be called.
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Note that the return type is int64 to return a larger file's size (a file
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // larger than 2G) but an error code should fit in the int range (may be
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // smaller than int64 range).
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual int64 GetLength(const net::Int64CompletionCallback& callback) = 0;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}  // namespace storage
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // STORAGE_BLOB_FILE_STREAM_READER_H_
100