summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_system_proxy.h
blob: 96ac917d222750f759f6a950375e8a5d29502360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_PROXY_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_PROXY_H_

#include "chrome/browser/chromeos/drive/file_errors.h"
#include "webkit/fileapi/remote_file_system_proxy.h"

namespace fileapi {
class FileSystemURL;
}

namespace drive {

class DriveEntryProto;
class FileSystemInterface;

typedef std::vector<DriveEntryProto> DriveEntryProtoVector;

// Implementation of File API's remote file system proxy for Drive file system.
class FileSystemProxy : public fileapi::RemoteFileSystemProxyInterface {
 public:
  using fileapi::RemoteFileSystemProxyInterface::OpenFileCallback;

  // |file_system| is the FileSystem instance owned by DriveSystemService.
  explicit FileSystemProxy(FileSystemInterface* file_system);

  // Detaches this instance from |file_system_|.
  // Method calls may result in no-op after calling this method.
  // This method must be called on UI thread.
  void DetachFromFileSystem();

  // fileapi::RemoteFileSystemProxyInterface overrides.
  virtual void GetFileInfo(
      const fileapi::FileSystemURL& url,
      const fileapi::FileSystemOperation::GetMetadataCallback&
          callback) OVERRIDE;
  virtual void Copy(
      const fileapi::FileSystemURL& src_url,
      const fileapi::FileSystemURL& dest_url,
      const fileapi::FileSystemOperation::StatusCallback& callback)
          OVERRIDE;
  virtual void Move(
      const fileapi::FileSystemURL& src_url,
      const fileapi::FileSystemURL& dest_url,
      const fileapi::FileSystemOperation::StatusCallback& callback)
          OVERRIDE;
  virtual void ReadDirectory(const fileapi::FileSystemURL& url,
     const fileapi::FileSystemOperation::ReadDirectoryCallback&
         callback) OVERRIDE;
  virtual void Remove(
      const fileapi::FileSystemURL& url, bool recursive,
      const fileapi::FileSystemOperation::StatusCallback& callback)
          OVERRIDE;
  virtual void CreateDirectory(
      const fileapi::FileSystemURL& file_url,
      bool exclusive,
      bool recursive,
      const fileapi::FileSystemOperation::StatusCallback& callback)
          OVERRIDE;
  virtual void CreateFile(
      const fileapi::FileSystemURL& file_url,
      bool exclusive,
      const fileapi::FileSystemOperation::StatusCallback& callback)
          OVERRIDE;
  virtual void Truncate(
      const fileapi::FileSystemURL& file_url, int64 length,
      const fileapi::FileSystemOperation::StatusCallback& callback)
          OVERRIDE;
  virtual void CreateSnapshotFile(
      const fileapi::FileSystemURL& url,
      const fileapi::FileSystemOperation::SnapshotFileCallback&
      callback) OVERRIDE;
  virtual void CreateWritableSnapshotFile(
      const fileapi::FileSystemURL& url,
      const fileapi::WritableSnapshotFile& callback) OVERRIDE;
  virtual void OpenFile(
      const fileapi::FileSystemURL& url,
      int file_flags,
      base::ProcessHandle peer_handle,
      const OpenFileCallback& callback) OVERRIDE;
  virtual void NotifyCloseFile(const fileapi::FileSystemURL& url) OVERRIDE;
  virtual void TouchFile(
      const fileapi::FileSystemURL& url,
      const base::Time& last_access_time,
      const base::Time& last_modified_time,
      const fileapi::FileSystemOperation::StatusCallback& callback)
          OVERRIDE;

 protected:
  virtual ~FileSystemProxy();

 private:
  // Checks if a given |url| belongs to this file system. If it does,
  // the call will return true and fill in |file_path| with a file path of
  // a corresponding element within this file system.
  static bool ValidateUrl(const fileapi::FileSystemURL& url,
                          base::FilePath* file_path);

  // Helper method to call methods of DriveFilesSystem. This method aborts
  // method calls in case DetachFromFileSystem() has been called.
  void CallFileSystemMethodOnUIThread(const base::Closure& method_call);

  // Used to implement CallFileSystemMethodOnUIThread.
  void CallFileSystemMethodOnUIThreadInternal(
      const base::Closure& method_call);

  // Helper callback for relaying reply for status callbacks to the
  // calling thread.
  void OnStatusCallback(
      const fileapi::FileSystemOperation::StatusCallback& callback,
      FileError error);

  // Helper callback for relaying reply for metadata retrieval request to the
  // calling thread.
  void OnGetMetadata(
      const base::FilePath& file_path,
      const fileapi::FileSystemOperation::GetMetadataCallback&
          callback,
      FileError error,
      scoped_ptr<DriveEntryProto> entry_proto);

  // Helper callback for relaying reply for GetEntryInfoByPath() to the
  // calling thread.
  void OnGetEntryInfoByPath(
      const base::FilePath& entry_path,
      const fileapi::FileSystemOperation::SnapshotFileCallback&
          callback,
      FileError error,
      scoped_ptr<DriveEntryProto> entry_proto);

  // Helper callback for relaying reply for ReadDirectory() to the calling
  // thread.
  void OnReadDirectory(
      const fileapi::FileSystemOperation::ReadDirectoryCallback&
          callback,
      FileError error,
      bool hide_hosted_documents,
      scoped_ptr<DriveEntryProtoVector> proto_entries);

  // Helper callback for relaying reply for CreateWritableSnapshotFile() to
  // the calling thread.
  void OnCreateWritableSnapshotFile(
      const base::FilePath& virtual_path,
      const fileapi::WritableSnapshotFile& callback,
      FileError result,
      const base::FilePath& local_path);

  // Helper callback for closing the local cache file and committing the dirty
  // flag. This is triggered when the callback for CreateWritableSnapshotFile
  // released the refcounted reference to the file.
  void CloseWritableSnapshotFile(
      const base::FilePath& virtual_path,
      const base::FilePath& local_path);

  // Invoked during Truncate() operation. This is called when a local modifiable
  // cache is ready for truncation.
  void OnFileOpenedForTruncate(
      const base::FilePath& virtual_path,
      int64 length,
      const fileapi::FileSystemOperation::StatusCallback& callback,
      FileError open_result,
      const base::FilePath& local_cache_path);

  // Invoked during Truncate() operation. This is called when the truncation of
  // a local cache file is finished on FILE thread.
  void DidTruncate(
      const base::FilePath& virtual_path,
      const fileapi::FileSystemOperation::StatusCallback& callback,
      base::PlatformFileError truncate_result);

  // Invoked during OpenFile() operation when truncate or write flags are set.
  // This is called when a local modifiable cached file is ready for such
  // operation.
  void OnOpenFileForWriting(
      int file_flags,
      base::ProcessHandle peer_handle,
      const OpenFileCallback& callback,
      FileError file_error,
      const base::FilePath& local_cache_path);

  // Invoked during OpenFile() operation when file create flags are set.
  void OnCreateFileForOpen(
      const base::FilePath& file_path,
      int file_flags,
      base::ProcessHandle peer_handle,
      const OpenFileCallback& callback,
      FileError file_error);

  // Invoked during OpenFile() operation when base::PLATFORM_FILE_OPEN_TRUNCATED
  // flag is set. This is called when the truncation of a local cache file is
  // finished on FILE thread.
  void OnOpenAndTruncate(
      base::ProcessHandle peer_handle,
      const OpenFileCallback& callback,
      base::PlatformFile* platform_file,
      base::PlatformFileError* truncate_result);

  FileSystemInterface* file_system_;
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_PROXY_H_