1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/basictypes.h"
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/files/file_path.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/ref_counted.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/memory/scoped_ptr.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/weak_ptr.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/blob/file_stream_reader.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_url.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace chromeos {
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace file_system_provider {
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstruct EntryMetadata;
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ProvidedFileSystemInterface;
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Implements a streamed file reader. It is lazily initialized by the first call
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// to Read().
2403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class FileStreamReader : public storage::FileStreamReader {
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  typedef base::Callback<
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      void(base::WeakPtr<ProvidedFileSystemInterface> file_system,
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)           const base::FilePath& file_path,
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)           int file_handle,
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch           base::File::Error result)> OpenFileCompletedCallback;
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  FileStreamReader(storage::FileSystemContext* context,
3303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                   const storage::FileSystemURL& url,
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   int64 initial_offset,
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   const base::Time& expected_modification_time);
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~FileStreamReader();
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // storage::FileStreamReader overrides.
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual int Read(net::IOBuffer* buf,
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   int buf_len,
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   const net::CompletionCallback& callback) OVERRIDE;
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual int64 GetLength(
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const net::Int64CompletionCallback& callback) OVERRIDE;
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) private:
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Helper class for executing operations on the provided file system. All
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // of its methods must be called on UI thread. Callbacks are called on IO
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // thread.
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class OperationRunner;
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // State of the file stream reader.
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  enum State { NOT_INITIALIZED, INITIALIZING, INITIALIZED, FAILED };
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Called when Read() operation is completed with either a success of an
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // error.
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void OnReadCompleted(net::CompletionCallback callback, int result);
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Initializes the reader by opening the file. When completed with success,
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // runs the |pending_closure|. Otherwise, calls the |error_callback|.
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void Initialize(const base::Closure& pending_closure,
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                  const net::Int64CompletionCallback& error_callback);
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Called when opening a file is completed with either a success or an error.
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void OnOpenFileCompleted(
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const base::Closure& pending_closure,
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const net::Int64CompletionCallback& error_callback,
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::File::Error result);
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Called when initialization is completed with either a success or an error.
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void OnInitializeCompleted(const base::Closure& pending_closure,
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             const net::Int64CompletionCallback& error_callback,
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             scoped_ptr<EntryMetadata> metadata,
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             base::File::Error result);
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Called when a file system provider returns chunk of read data. Note, that
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // this may be called multiple times per single Read() call, as long as
7846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // |has_more| is set to true. |result| is set to success only if reading is
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // successful, and the file has not changed while reading.
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void OnReadChunkReceived(const net::CompletionCallback& callback,
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                           int chunk_length,
8246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                           bool has_more,
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                           base::File::Error result);
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Called when fetching length of the file is completed with either a success
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // or an error.
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void OnGetMetadataForGetLengthReceived(
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const net::Int64CompletionCallback& callback,
891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      scoped_ptr<EntryMetadata> metadata,
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      base::File::Error result);
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Same as Read(), but called after initializing is completed.
9346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void ReadAfterInitialized(scoped_refptr<net::IOBuffer> buffer,
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            int buffer_length,
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            const net::CompletionCallback& callback);
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Same as GetLength(), but called after initializing is completed.
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void GetLengthAfterInitialized(const net::Int64CompletionCallback& callback);
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
10003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  storage::FileSystemURL url_;
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int64 current_offset_;
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int64 current_length_;
103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::Time expected_modification_time_;
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_refptr<OperationRunner> runner_;
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  State state_;
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::WeakPtrFactory<FileStreamReader> weak_ptr_factory_;
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FileStreamReader);
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace file_system_provider
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace chromeos
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_
115