summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_system_interface.h
blob: ebe632321ef26d5551538e85ab513d85bb246373 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// 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_INTERFACE_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_

#include <string>
#include <vector>

#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/file_system_metadata.h"
#include "chrome/browser/chromeos/drive/resource_metadata.h"
#include "google_apis/drive/base_requests.h"
#include "google_apis/drive/drive_api_requests.h"

namespace drive {

class FileSystemObserver;

// Information about search result returned by Search Async callback.
// This is data needed to create a file system entry that will be used by file
// browser.
struct SearchResultInfo {
  SearchResultInfo(const base::FilePath& path, bool is_directory)
      : path(path), is_directory(is_directory) {}

  base::FilePath path;
  bool is_directory;
};

// File path and its MD5 hash obtained from drive API.
struct HashAndFilePath {
  std::string hash;
  base::FilePath path;
};

// Struct to represent a search result for SearchMetadata().
struct MetadataSearchResult {
  MetadataSearchResult(const base::FilePath& path,
                       bool is_directory,
                       const std::string& highlighted_base_name,
                       const std::string& md5);

  // The two members are used to create FileEntry object.
  base::FilePath path;
  bool is_directory;

  // The base name to be displayed in the UI. The parts matched the search
  // query are highlighted with <b> tag. Meta characters are escaped like &lt;
  //
  // Why HTML? we could instead provide matched ranges using pairs of
  // integers, but this is fragile as we'll eventually converting strings
  // from UTF-8 (StringValue in base/values.h uses std::string) to UTF-16
  // when sending strings from C++ to JavaScript.
  //
  // Why <b> instead of <strong>? Because <b> is shorter.
  std::string highlighted_base_name;

  // MD5 hash of the file.
  std::string md5;
};

typedef std::vector<MetadataSearchResult> MetadataSearchResultVector;

// Used to get a resource entry from the file system.
// If |error| is not FILE_ERROR_OK, |entry_info| is set to NULL.
typedef base::Callback<void(FileError error,
                            scoped_ptr<ResourceEntry> entry)>
    GetResourceEntryCallback;

// Used to get files from the file system.
typedef base::Callback<void(FileError error,
                            const base::FilePath& file_path,
                            scoped_ptr<ResourceEntry> entry)> GetFileCallback;

// Used to get file content from the file system.
// If the file content is available in local cache, |local_file| is filled with
// the path to the cache file. If the file content starts to be downloaded from
// the server, |local_file| is empty.
typedef base::Callback<void(FileError error,
                            const base::FilePath& local_file,
                            scoped_ptr<ResourceEntry> entry)>
    GetFileContentInitializedCallback;

// Used to get list of entries under a directory.
typedef base::Callback<void(scoped_ptr<ResourceEntryVector> entries)>
    ReadDirectoryEntriesCallback;

// Used to get drive content search results.
// If |error| is not FILE_ERROR_OK, |result_paths| is empty.
typedef base::Callback<void(
    FileError error,
    const GURL& next_link,
    scoped_ptr<std::vector<SearchResultInfo> > result_paths)> SearchCallback;

// Callback for SearchMetadata(). On success, |error| is FILE_ERROR_OK, and
// |result| contains the search result.
typedef base::Callback<void(
    FileError error,
    scoped_ptr<MetadataSearchResultVector> result)> SearchMetadataCallback;

// Callback for SearchByHashesCallback. On success, vector contains hash and
// corresponding files. The vector can include multiple entries for one hash.
typedef base::Callback<void(FileError, const std::vector<HashAndFilePath>&)>
    SearchByHashesCallback;

// Used to open files from the file system. |file_path| is the path on the local
// file system for the opened file.
// If |close_callback| is not null, it must be called when the
// modification to the cache is done. Otherwise, Drive file system does not
// pick up the file for uploading.
// |close_callback| must not be called more than once.
typedef base::Callback<void(FileError error,
                            const base::FilePath& file_path,
                            const base::Closure& close_callback)>
    OpenFileCallback;

// Used to get available space for the account from Drive.
typedef base::Callback<void(FileError error,
                            int64 bytes_total,
                            int64 bytes_used)> GetAvailableSpaceCallback;

// Used to get the url to the sharing dialog.
typedef base::Callback<void(FileError error,
                            const GURL& share_url)> GetShareUrlCallback;

// Used to get filesystem metadata.
typedef base::Callback<void(const FileSystemMetadata&)>
    GetFilesystemMetadataCallback;

// Used to mark cached files mounted.
typedef base::Callback<void(FileError error,
                            const base::FilePath& file_path)>
    MarkMountedCallback;

// Used to get file path.
typedef base::Callback<void(FileError error, const base::FilePath& file_path)>
    GetFilePathCallback;

// Used to free space.
typedef base::Callback<void(bool)> FreeDiskSpaceCallback;

// The mode of opening a file.
enum OpenMode {
  // Open the file if exists. If not, failed.
  OPEN_FILE,

  // Create a new file if not exists, and then open it. If exists, failed.
  CREATE_FILE,

  // Open the file if exists. If not, create a new file and then open it.
  OPEN_OR_CREATE_FILE,
};

// Option enum to control eligible entries for SearchMetadata().
// SEARCH_METADATA_ALL is the default to investigate all the entries.
// SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS excludes the hosted documents.
// SEARCH_METADATA_EXCLUDE_DIRECTORIES excludes the directories from the result.
// SEARCH_METADATA_SHARED_WITH_ME targets only "shared-with-me" entries.
// SEARCH_METADATA_OFFLINE targets only "offline" entries. This option can not
// be used with other options.
enum SearchMetadataOptions {
  SEARCH_METADATA_ALL = 0,
  SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS = 1,
  SEARCH_METADATA_EXCLUDE_DIRECTORIES = 1 << 1,
  SEARCH_METADATA_SHARED_WITH_ME = 1 << 2,
  SEARCH_METADATA_OFFLINE = 1 << 3,
};

// Drive file system abstraction layer.
// The interface is defined to make FileSystem mockable.
class FileSystemInterface {
 public:
  virtual ~FileSystemInterface() {}

  // Adds and removes the observer.
  virtual void AddObserver(FileSystemObserver* observer) = 0;
  virtual void RemoveObserver(FileSystemObserver* observer) = 0;

  // Checks for updates on the server.
  virtual void CheckForUpdates() = 0;

  // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|.
  // |local_src_file_path| must be a file from the local file system.
  // |remote_dest_file_path| is the virtual destination path within Drive file
  // system.
  //
  // |callback| must not be null.
  virtual void TransferFileFromLocalToRemote(
      const base::FilePath& local_src_file_path,
      const base::FilePath& remote_dest_file_path,
      const FileOperationCallback& callback) = 0;

  // Retrieves a file at the virtual path |file_path| on the Drive file system
  // onto the cache, and mark it dirty. The local path to the cache file is
  // returned to |callback|. After opening the file, both read and write
  // on the file can be done with normal local file operations.
  // If |mime_type| is set and the file is newly created, the mime type is
  // set to the specified value. If |mime_type| is empty, it is guessed from
  // |file_path|.
  //
  // |callback| must not be null.
  virtual void OpenFile(const base::FilePath& file_path,
                        OpenMode open_mode,
                        const std::string& mime_type,
                        const OpenFileCallback& callback) = 0;

  // Copies |src_file_path| to |dest_file_path| on the file system.
  // |src_file_path| can be a hosted document (see limitations below).
  // |dest_file_path| is expected to be of the same type of |src_file_path|
  // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
  // a file).
  // If |preserve_last_modified| is set to true, the last modified time will be
  // preserved. This feature is only supported on Drive API v2 protocol because
  // GData WAPI doesn't support updating modification time.
  //
  // This method also has the following assumptions/limitations that may be
  // relaxed or addressed later:
  // - |src_file_path| cannot be a regular file (i.e. non-hosted document)
  //   or a directory.
  // - |dest_file_path| must not exist.
  // - The parent of |dest_file_path| must already exist.
  //
  // The file entries represented by |src_file_path| and the parent directory
  // of |dest_file_path| need to be present in the in-memory representation
  // of the file system.
  //
  // |callback| must not be null.
  virtual void Copy(const base::FilePath& src_file_path,
                    const base::FilePath& dest_file_path,
                    bool preserve_last_modified,
                    const FileOperationCallback& callback) = 0;

  // Moves |src_file_path| to |dest_file_path| on the file system.
  // |src_file_path| can be a file (regular or hosted document) or a directory.
  // |dest_file_path| is expected to be of the same type of |src_file_path|
  // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
  // a file).
  //
  // This method also has the following assumptions/limitations that may be
  // relaxed or addressed later:
  // - |dest_file_path| must not exist.
  // - The parent of |dest_file_path| must already exist.
  //
  // The file entries represented by |src_file_path| and the parent directory
  // of |dest_file_path| need to be present in the in-memory representation
  // of the file system.
  //
  // |callback| must not be null.
  virtual void Move(const base::FilePath& src_file_path,
                    const base::FilePath& dest_file_path,
                    const FileOperationCallback& callback) = 0;

  // Removes |file_path| from the file system.  If |is_recursive| is set and
  // |file_path| represents a directory, we will also delete all of its
  // contained children elements. The file entry represented by |file_path|
  // needs to be present in in-memory representation of the file system that
  // in order to be removed.
  //
  // |callback| must not be null.
  virtual void Remove(const base::FilePath& file_path,
                      bool is_recursive,
                      const FileOperationCallback& callback) = 0;

  // Creates new directory under |directory_path|. If |is_exclusive| is true,
  // an error is raised in case a directory is already present at the
  // |directory_path|. If |is_recursive| is true, the call creates parent
  // directories as needed just like mkdir -p does.
  //
  // |callback| must not be null.
  virtual void CreateDirectory(const base::FilePath& directory_path,
                               bool is_exclusive,
                               bool is_recursive,
                               const FileOperationCallback& callback) = 0;

  // Creates a file at |file_path|. If the flag |is_exclusive| is true, an
  // error is raised when a file already exists at the path. It is
  // an error if a directory or a hosted document is already present at the
  // path, or the parent directory of the path is not present yet.
  // If |mime_type| is set and the file is newly created, the mime type is
  // set to the specified value. If |mime_type| is empty, it is guessed from
  // |file_path|.
  //
  // |callback| must not be null.
  virtual void CreateFile(const base::FilePath& file_path,
                          bool is_exclusive,
                          const std::string& mime_type,
                          const FileOperationCallback& callback) = 0;

  // Touches the file at |file_path| by updating the timestamp to
  // |last_access_time| and |last_modified_time|.
  // Upon completion, invokes |callback|.
  // Note that, differently from unix touch command, this doesn't create a file
  // if the target file doesn't exist.
  //
  // |last_access_time|, |last_modified_time| and |callback| must not be null.
  virtual void TouchFile(const base::FilePath& file_path,
                         const base::Time& last_access_time,
                         const base::Time& last_modified_time,
                         const FileOperationCallback& callback) = 0;

  // Truncates the file content at |file_path| to the |length|.
  //
  // |callback| must not be null.
  virtual void TruncateFile(const base::FilePath& file_path,
                            int64 length,
                            const FileOperationCallback& callback) = 0;

  // Pins a file at |file_path|.
  //
  // |callback| must not be null.
  virtual void Pin(const base::FilePath& file_path,
                   const FileOperationCallback& callback) = 0;

  // Unpins a file at |file_path|.
  //
  // |callback| must not be null.
  virtual void Unpin(const base::FilePath& file_path,
                     const FileOperationCallback& callback) = 0;

  // Makes sure that |file_path| in the file system is available in the local
  // cache. If the file is not cached, the file will be downloaded. The entry
  // needs to be present in the file system.
  //
  // Returns the cache path and entry info to |callback|. It must not be null.
  virtual void GetFile(const base::FilePath& file_path,
                       const GetFileCallback& callback) = 0;

  // Makes sure that |file_path| in the file system is available in the local
  // cache, and mark it as dirty. The next modification to the cache file is
  // watched and is automatically uploaded to the server. If the entry is not
  // present in the file system, it is created.
  //
  // Returns the cache path and entry info to |callback|. It must not be null.
  virtual void GetFileForSaving(const base::FilePath& file_path,
                                const GetFileCallback& callback) = 0;

  // Gets a file by the given |file_path| and returns a closure to cancel the
  // task.
  // Calls |initialized_callback| when either:
  //   1) The cached file (or JSON file for hosted file) is found, or
  //   2) Starting to download the file from drive server.
  // In case of 2), the given FilePath is empty, and |get_content_callback| is
  // called repeatedly with downloaded content following the
  // |initialized_callback| invocation.
  // |completion_callback| is invoked if an error is found, or the operation
  // is successfully done.
  // |initialized_callback|, |get_content_callback| and |completion_callback|
  // must not be null.
  virtual base::Closure GetFileContent(
      const base::FilePath& file_path,
      const GetFileContentInitializedCallback& initialized_callback,
      const google_apis::GetContentCallback& get_content_callback,
      const FileOperationCallback& completion_callback) = 0;

  // Finds an entry (a file or a directory) by |file_path|. This call will also
  // retrieve and refresh file system content from server and disk cache.
  //
  // |callback| must not be null.
  virtual void GetResourceEntry(const base::FilePath& file_path,
                                const GetResourceEntryCallback& callback) = 0;

  // Finds and reads a directory by |file_path|. This call will also retrieve
  // and refresh file system content from server and disk cache.
  // |entries_callback| can be a null callback when not interested in entries.
  //
  // |completion_callback| must not be null.
  virtual void ReadDirectory(
      const base::FilePath& file_path,
      const ReadDirectoryEntriesCallback& entries_callback,
      const FileOperationCallback& completion_callback) = 0;

  // Does server side content search for |search_query|.
  // If |next_link| is set, this is the search result url that will be
  // fetched. Search results will be returned as a list of results'
  // |SearchResultInfo| structs, which contains file's path and is_directory
  // flag.
  //
  // |callback| must not be null.
  virtual void Search(const std::string& search_query,
                      const GURL& next_link,
                      const SearchCallback& callback) = 0;

  // Searches the local resource metadata, and returns the entries
  // |at_most_num_matches| that contain |query| in their base names. Search is
  // done in a case-insensitive fashion. The eligible entries are selected based
  // on the given |options|, which is a bit-wise OR of SearchMetadataOptions.
  // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS will be automatically added based
  // on the preference. |callback| must not be null. Must be called on UI
  // thread. Empty |query| matches any base name. i.e. returns everything.
  virtual void SearchMetadata(const std::string& query,
                              int options,
                              int at_most_num_matches,
                              const SearchMetadataCallback& callback) = 0;

  // Searches the local resource metadata, and returns the entries that have the
  // given |hashes|. The list of resource entries are passed to |callback|. The
  // item of the list can be null if the corresponding file is not found.
  // |callback| must not be null.
  virtual void SearchByHashes(const std::set<std::string>& hashes,
                              const SearchByHashesCallback& callback) = 0;

  // Fetches the user's Account Metadata to find out current quota information
  // and returns it to the callback.
  virtual void GetAvailableSpace(const GetAvailableSpaceCallback& callback) = 0;

  // Fetches the url to the sharing dialog to be embedded in |embed_origin|,
  // for the specified file or directory. |callback| must not be null.
  virtual void GetShareUrl(
      const base::FilePath& file_path,
      const GURL& embed_origin,
      const GetShareUrlCallback& callback) = 0;

  // Returns miscellaneous metadata of the file system like the largest
  // timestamp. Used in chrome:drive-internals. |callback| must not be null.
  virtual void GetMetadata(
      const GetFilesystemMetadataCallback& callback) = 0;

  // Marks the cached file as mounted, and runs |callback| upon completion.
  // If succeeded, the cached file path will be passed to the |callback|.
  // |callback| must not be null.
  virtual void MarkCacheFileAsMounted(const base::FilePath& drive_file_path,
                                      const MarkMountedCallback& callback) = 0;

  // Marks the cached file as unmounted, and runs |callback| upon completion.
  // Note that this method expects that the |cached_file_path| is the path
  // returned by MarkCacheFileAsMounted().
  // |callback| must not be null.
  virtual void MarkCacheFileAsUnmounted(
      const base::FilePath& cache_file_path,
      const FileOperationCallback& callback) = 0;

  // Adds permission as |role| to |email| for the entry at |drive_file_path|.
  // |callback| must not be null.
  virtual void AddPermission(const base::FilePath& drive_file_path,
                             const std::string& email,
                             google_apis::drive::PermissionRole role,
                             const FileOperationCallback& callback) = 0;

  // Sets the |key| property on the file or directory at |drive_file_path| with
  // the specified |visibility|. If already exists, then it will be overwritten.
  virtual void SetProperty(const base::FilePath& drive_file_path,
                           google_apis::drive::Property::Visibility visibility,
                           const std::string& key,
                           const std::string& value,
                           const FileOperationCallback& callback) = 0;

  // Resets local data.
  virtual void Reset(const FileOperationCallback& callback) = 0;

  // Finds a path of an entry (a file or a directory) by |resource_id|.
  virtual void GetPathFromResourceId(const std::string& resource_id,
                                     const GetFilePathCallback& callback) = 0;

  // Free drive caches if needed to secure given available spaces. |callback|
  // takes whether given bytes are available or not.
  virtual void FreeDiskSpaceIfNeededFor(
      int64 num_bytes,
      const FreeDiskSpaceCallback& callback) = 0;
};

}  // namespace drive

#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_