Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / fileapi / file_stream_reader.h
blob6d348d177add36cb8532c745cf70947439eaa10a
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.
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_
8 #include "base/basictypes.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "storage/browser/fileapi/file_stream_reader.h"
14 #include "storage/browser/fileapi/file_system_url.h"
16 namespace chromeos {
17 namespace file_system_provider {
19 struct EntryMetadata;
20 class ProvidedFileSystemInterface;
22 // Implements a streamed file reader. It is lazily initialized by the first call
23 // to Read().
24 class FileStreamReader : public storage::FileStreamReader {
25 public:
26 typedef base::Callback<
27 void(base::WeakPtr<ProvidedFileSystemInterface> file_system,
28 const base::FilePath& file_path,
29 int file_handle,
30 base::File::Error result)> OpenFileCompletedCallback;
32 FileStreamReader(storage::FileSystemContext* context,
33 const storage::FileSystemURL& url,
34 int64 initial_offset,
35 const base::Time& expected_modification_time);
37 ~FileStreamReader() override;
39 // storage::FileStreamReader overrides.
40 int Read(net::IOBuffer* buf,
41 int buf_len,
42 const net::CompletionCallback& callback) override;
43 int64 GetLength(const net::Int64CompletionCallback& callback) override;
45 private:
46 // Helper class for executing operations on the provided file system. All
47 // of its methods must be called on UI thread. Callbacks are called on IO
48 // thread.
49 class OperationRunner;
51 // State of the file stream reader.
52 enum State { NOT_INITIALIZED, INITIALIZING, INITIALIZED, FAILED };
54 // Called when Read() operation is completed with either a success of an
55 // error.
56 void OnReadCompleted(net::CompletionCallback callback, int result);
58 // Initializes the reader by opening the file. When completed with success,
59 // runs the |pending_closure|. Otherwise, calls the |error_callback|.
60 void Initialize(const base::Closure& pending_closure,
61 const net::Int64CompletionCallback& error_callback);
63 // Called when opening a file is completed with either a success or an error.
64 void OnOpenFileCompleted(
65 const base::Closure& pending_closure,
66 const net::Int64CompletionCallback& error_callback,
67 base::File::Error result);
69 // Called when initialization is completed with either a success or an error.
70 void OnInitializeCompleted(const base::Closure& pending_closure,
71 const net::Int64CompletionCallback& error_callback,
72 scoped_ptr<EntryMetadata> metadata,
73 base::File::Error result);
75 // Called when a file system provider returns chunk of read data. Note, that
76 // this may be called multiple times per single Read() call, as long as
77 // |has_more| is set to true. |result| is set to success only if reading is
78 // successful, and the file has not changed while reading.
79 void OnReadChunkReceived(const net::CompletionCallback& callback,
80 int chunk_length,
81 bool has_more,
82 base::File::Error result);
84 // Called when fetching length of the file is completed with either a success
85 // or an error.
86 void OnGetMetadataForGetLengthReceived(
87 const net::Int64CompletionCallback& callback,
88 scoped_ptr<EntryMetadata> metadata,
89 base::File::Error result);
91 // Same as Read(), but called after initializing is completed.
92 void ReadAfterInitialized(scoped_refptr<net::IOBuffer> buffer,
93 int buffer_length,
94 const net::CompletionCallback& callback);
96 // Same as GetLength(), but called after initializing is completed.
97 void GetLengthAfterInitialized(const net::Int64CompletionCallback& callback);
99 storage::FileSystemURL url_;
100 int64 current_offset_;
101 int64 current_length_;
102 base::Time expected_modification_time_;
103 scoped_refptr<OperationRunner> runner_;
104 State state_;
106 base::WeakPtrFactory<FileStreamReader> weak_ptr_factory_;
107 DISALLOW_COPY_AND_ASSIGN(FileStreamReader);
110 } // namespace file_system_provider
111 } // namespace chromeos
113 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_