1 // Copyright 2013 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_DRIVE_LOCAL_FILE_READER_H_ 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_LOCAL_FILE_READER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/weak_ptr.h" 11 #include "net/base/completion_callback.h" 12 #include "net/base/file_stream.h" 13 14 namespace base { 15 class FilePath; 16 class SequencedTaskRunner; 17 } // namespace base 18 19 namespace net { 20 class IOBuffer; 21 } // namespace net 22 23 namespace drive { 24 namespace util { 25 26 // This is simple local file reader implementation focusing on Drive's use 27 // case. All the operations run on |sequenced_task_runner| asynchronously and 28 // the result will be notified to the caller via |callback|s on the caller's 29 // thread. 30 class LocalFileReader { 31 public: 32 explicit LocalFileReader(base::SequencedTaskRunner* sequenced_task_runner); 33 ~LocalFileReader(); 34 35 // Opens the file at |file_path|. The initial position of the read will be 36 // at |offset| from the beginning of the file. 37 // Upon completion, |callback| will be called. 38 // |callback| must not be null. 39 void Open(const base::FilePath& file_path, 40 int64 offset, 41 const net::CompletionCallback& callback); 42 43 // Reads the file and copies the data into |buffer|. |buffer_length| 44 // is the length of |buffer|. 45 // Upon completion, |callback| will be called with the result. 46 // |callback| must not be null. 47 void Read(net::IOBuffer* buffer, 48 int buffer_length, 49 const net::CompletionCallback& callback); 50 51 private: 52 void DidOpen(const net::CompletionCallback& callback, 53 int64 offset, 54 int error); 55 void DidSeek(const net::CompletionCallback& callback, 56 int64 offset, 57 int64 error); 58 59 net::FileStream file_stream_; 60 61 // Note: This should remain the last member so it'll be destroyed and 62 // invalidate the weak pointers before any other members are destroyed. 63 base::WeakPtrFactory<LocalFileReader> weak_ptr_factory_; 64 DISALLOW_COPY_AND_ASSIGN(LocalFileReader); 65 }; 66 67 } // namespace util 68 } // namespace drive 69 70 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_LOCAL_FILE_READER_H_ 71