Home | History | Annotate | Download | only in drive
      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 "base/platform_file.h"
     12 #include "net/base/completion_callback.h"
     13 
     14 namespace base {
     15 class FilePath;
     16 class SequencedTaskRunner;
     17 }  // namespace base
     18 
     19 namespace net {
     20 class IOBuffer;
     21 }  // namespace
     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   // The thin wrapper for the platform file to handle closing correctly.
     53   class ScopedPlatformFile;
     54 
     55   // Part of Open(). Called after the open() operation task running
     56   // on blocking pool.
     57   void OpenAfterBlockingPoolTask(
     58       const net::CompletionCallback& callback,
     59       ScopedPlatformFile* result_platform_file,
     60       int open_result);
     61 
     62   scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
     63   base::PlatformFile platform_file_;
     64 
     65   // Note: This should remain the last member so it'll be destroyed and
     66   // invalidate the weak pointers before any other members are destroyed.
     67   base::WeakPtrFactory<LocalFileReader> weak_ptr_factory_;
     68   DISALLOW_COPY_AND_ASSIGN(LocalFileReader);
     69 };
     70 
     71 }  // namespace util
     72 }  // namespace drive
     73 
     74 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_LOCAL_FILE_READER_H_
     75