1// Copyright 2014 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. 4 5#ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_ 6#define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_ 7 8#include "base/basictypes.h" 9#include "base/files/file_path.h" 10#include "base/memory/weak_ptr.h" 11#include "webkit/browser/blob/file_stream_reader.h" 12#include "webkit/browser/fileapi/file_system_url.h" 13 14namespace fileapi { 15class AsyncFileUtil; 16} // namespace fileapi 17 18namespace chromeos { 19namespace file_system_provider { 20 21class ProvidedFileSystemInterface; 22 23// Implements a streamed file reader. It is lazily initialized by the first call 24// to Read(). 25class FileStreamReader : public webkit_blob::FileStreamReader { 26 public: 27 typedef base::Callback< 28 void(base::WeakPtr<ProvidedFileSystemInterface> file_system, 29 const base::FilePath& file_path, 30 int file_handle, 31 base::File::Error result)> InitializeCompletedCallback; 32 33 FileStreamReader(fileapi::FileSystemContext* context, 34 const fileapi::FileSystemURL& url, 35 int64 initial_offset, 36 const base::Time& expected_modification_time); 37 38 virtual ~FileStreamReader(); 39 40 // webkit_blob::FileStreamReader overrides. 41 virtual int Read(net::IOBuffer* buf, 42 int buf_len, 43 const net::CompletionCallback& callback) OVERRIDE; 44 virtual int64 GetLength( 45 const net::Int64CompletionCallback& callback) OVERRIDE; 46 47 private: 48 // Initializes the reader by opening the file. When completed with success, 49 // runs the |pending_closure|. Otherwise, calls the |error_callback|. 50 void Initialize(const base::Closure& pending_closure, 51 const net::Int64CompletionCallback& error_callback); 52 53 // Called when initializing is completed with either a success or an error. 54 void OnInitializeCompleted( 55 const base::Closure& pending_closure, 56 const net::Int64CompletionCallback& error_callback, 57 base::WeakPtr<ProvidedFileSystemInterface> file_system, 58 const base::FilePath& file_path, 59 int file_handle, 60 base::File::Error result); 61 62 // Called when a file system provider returns chunk of read data. Note, that 63 // this may be called multiple times per single Read() call, as long as 64 // |has_more| is set to true. |result| is set to success only if reading is 65 // successful, and the file has not changed while reading. 66 void OnReadChunkReceived(const net::CompletionCallback& callback, 67 int chunk_length, 68 bool has_more, 69 base::File::Error result); 70 71 // Called when fetching length of the file is completed with either a success 72 // or an error. 73 void OnGetMetadataForGetLengthReceived( 74 const net::Int64CompletionCallback& callback, 75 base::File::Error result, 76 const base::File::Info& file_info); 77 78 // Same as Read(), but called after initializing is completed. 79 void ReadAfterInitialized(scoped_refptr<net::IOBuffer> buffer, 80 int buffer_length, 81 const net::CompletionCallback& callback); 82 83 // Same as GetLength(), but called after initializing is completed. 84 void GetLengthAfterInitialized(const net::Int64CompletionCallback& callback); 85 86 fileapi::FileSystemURL url_; 87 int64 current_offset_; 88 int64 current_length_; 89 base::Time expected_modification_time_; 90 91 // Set during initialization (in case of a success). 92 base::WeakPtr<ProvidedFileSystemInterface> file_system_; 93 base::FilePath file_path_; 94 int file_handle_; 95 96 base::WeakPtrFactory<FileStreamReader> weak_ptr_factory_; 97 DISALLOW_COPY_AND_ASSIGN(FileStreamReader); 98}; 99 100} // namespace file_system_provider 101} // namespace chromeos 102 103#endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_ 104