blob: 1c5865e230f5a3a98a6d1ba6ee240cbd075773c1 [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// Copyright (c) 2012 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_FILE_SYSTEM_INTERFACE_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_
7
8#include <string>
9#include <vector>
10
11#include "base/memory/scoped_ptr.h"
12#include "chrome/browser/chromeos/drive/drive.pb.h"
13#include "chrome/browser/chromeos/drive/file_cache.h"
14#include "chrome/browser/chromeos/drive/file_system_metadata.h"
15#include "chrome/browser/chromeos/drive/resource_metadata.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010016#include "chrome/browser/google_apis/base_requests.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010018namespace drive {
19
20class FileSystemObserver;
21
22typedef std::vector<ResourceEntry> ResourceEntryVector;
23
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010024// Information about search result returned by Search Async callback.
25// This is data needed to create a file system entry that will be used by file
26// browser.
27struct SearchResultInfo {
28 SearchResultInfo(const base::FilePath& path,
29 const ResourceEntry& entry)
30 : path(path),
31 entry(entry) {
32 }
33
34 base::FilePath path;
35 ResourceEntry entry;
36};
37
38// Struct to represent a search result for SearchMetadata().
39struct MetadataSearchResult {
Ben Murdochca12bfa2013-07-23 11:17:05 +010040 MetadataSearchResult(const ResourceEntry& in_entry,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010041 const std::string& in_highlighted_base_name)
Ben Murdochca12bfa2013-07-23 11:17:05 +010042 : entry(in_entry),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010043 highlighted_base_name(in_highlighted_base_name) {
Ben Murdochca12bfa2013-07-23 11:17:05 +010044 // Note: |path| is set separately from |entry| or other fields, because
45 // getting path typically takes longer time hence we want to fill it only
46 // when it is necessary. (I.e., not for temporary candidates, just for
47 // final user visible results.)
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010048 }
49
50 // The two members are used to create FileEntry object.
51 base::FilePath path;
52 ResourceEntry entry;
53
54 // The base name to be displayed in the UI. The parts matched the search
55 // query are highlighted with <b> tag. Meta characters are escaped like &lt;
56 //
57 // Why HTML? we could instead provide matched ranges using pairs of
58 // integers, but this is fragile as we'll eventually converting strings
59 // from UTF-8 (StringValue in base/values.h uses std::string) to UTF-16
60 // when sending strings from C++ to JavaScript.
61 //
62 // Why <b> instead of <strong>? Because <b> is shorter.
63 std::string highlighted_base_name;
64};
65
66typedef std::vector<MetadataSearchResult> MetadataSearchResultVector;
67
68// Used to get files from the file system.
69typedef base::Callback<void(FileError error,
70 const base::FilePath& file_path,
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +010071 scoped_ptr<ResourceEntry> entry)> GetFileCallback;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010072
73// Used to get file content from the file system.
74// If the file content is available in local cache, |local_file| is filled with
75// the path to the cache file and |cancel_download_closure| is null. If the file
76// content starts to be downloaded from the server, |local_file| is empty and
77// |cancel_download_closure| is filled with a closure by calling which the
78// download job can be cancelled.
79// |cancel_download_closure| must be called on the UI thread.
80typedef base::Callback<void(FileError error,
81 scoped_ptr<ResourceEntry> entry,
82 const base::FilePath& local_file,
83 const base::Closure& cancel_download_closure)>
84 GetFileContentInitializedCallback;
85
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010086// Used to get drive content search results.
87// If |error| is not FILE_ERROR_OK, |result_paths| is empty.
88typedef base::Callback<void(
89 FileError error,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010090 const GURL& next_url,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010091 scoped_ptr<std::vector<SearchResultInfo> > result_paths)> SearchCallback;
92
93// Callback for SearchMetadata(). On success, |error| is FILE_ERROR_OK, and
94// |result| contains the search result.
95typedef base::Callback<void(
96 FileError error,
97 scoped_ptr<MetadataSearchResultVector> result)> SearchMetadataCallback;
98
99// Used to open files from the file system. |file_path| is the path on the local
100// file system for the opened file.
Ben Murdocha3f7b4e2013-07-24 10:36:34 +0100101// If |close_callback| is not null, it must be called when the
102// modification to the cache is done. Otherwise, Drive file system does not
103// pick up the file for uploading.
104// |close_callback| must not be called more than once.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100105typedef base::Callback<void(FileError error,
Ben Murdocha3f7b4e2013-07-24 10:36:34 +0100106 const base::FilePath& file_path,
107 const base::Closure& close_callback)>
108 OpenFileCallback;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100109
110// Used to get available space for the account from Drive.
111typedef base::Callback<void(FileError error,
112 int64 bytes_total,
113 int64 bytes_used)> GetAvailableSpaceCallback;
114
Ben Murdochbbcdd452013-07-25 10:06:34 +0100115// Used to get the url to the sharing dialog.
116typedef base::Callback<void(FileError error,
117 const GURL& share_url)> GetShareUrlCallback;
118
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100119// Used to get filesystem metadata.
120typedef base::Callback<void(const FileSystemMetadata&)>
121 GetFilesystemMetadataCallback;
122
Ben Murdocha3f7b4e2013-07-24 10:36:34 +0100123// Used to mark cached files mounted.
124typedef base::Callback<void(FileError error,
125 const base::FilePath& file_path)>
126 MarkMountedCallback;
127
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100128// The mode of opening a file.
129enum OpenMode {
130 // Open the file if exists. If not, failed.
131 OPEN_FILE,
132
133 // Create a new file if not exists, and then open it. If exists, failed.
134 CREATE_FILE,
135
136 // Open the file if exists. If not, create a new file and then open it.
137 OPEN_OR_CREATE_FILE,
138};
139
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100140// Priority of a job. Higher values are lower priority.
141enum ContextType {
142 USER_INITIATED,
143 BACKGROUND,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100144 // Indicates the number of values of this enum.
145 NUM_CONTEXT_TYPES,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100146};
147
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100148struct ClientContext {
149 explicit ClientContext(ContextType in_type) : type(in_type) {}
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100150 ContextType type;
151};
152
Ben Murdochbb1529c2013-08-08 10:24:53 +0100153// Option enum to control eligible entries for SearchMetadata().
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100154// SEARCH_METADATA_ALL is the default to investigate all the entries.
155// SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS excludes the hosted documents.
156// SEARCH_METADATA_EXCLUDE_DIRECTORIES excludes the directories from the result.
157// SEARCH_METADATA_SHARED_WITH_ME targets only "shared-with-me" entries.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100158// SEARCH_METADATA_OFFLINE targets only "offline" entries. This option can not
159// be used with other options.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100160enum SearchMetadataOptions {
161 SEARCH_METADATA_ALL = 0,
162 SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS = 1,
163 SEARCH_METADATA_EXCLUDE_DIRECTORIES = 1 << 1,
164 SEARCH_METADATA_SHARED_WITH_ME = 1 << 2,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100165 SEARCH_METADATA_OFFLINE = 1 << 3,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100166};
167
168// Drive file system abstraction layer.
169// The interface is defined to make FileSystem mockable.
170class FileSystemInterface {
171 public:
172 virtual ~FileSystemInterface() {}
173
174 // Initializes the object. This function should be called before any
175 // other functions.
176 virtual void Initialize() = 0;
177
178 // Adds and removes the observer.
179 virtual void AddObserver(FileSystemObserver* observer) = 0;
180 virtual void RemoveObserver(FileSystemObserver* observer) = 0;
181
182 // Checks for updates on the server.
183 virtual void CheckForUpdates() = 0;
184
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100185 // Initiates transfer of |remote_src_file_path| to |local_dest_file_path|.
186 // |remote_src_file_path| is the virtual source path on the Drive file system.
187 // |local_dest_file_path| is the destination path on the local file system.
188 //
189 // |callback| must not be null.
190 virtual void TransferFileFromRemoteToLocal(
191 const base::FilePath& remote_src_file_path,
192 const base::FilePath& local_dest_file_path,
193 const FileOperationCallback& callback) = 0;
194
195 // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|.
196 // |local_src_file_path| must be a file from the local file system.
197 // |remote_dest_file_path| is the virtual destination path within Drive file
198 // system.
199 //
200 // |callback| must not be null.
201 virtual void TransferFileFromLocalToRemote(
202 const base::FilePath& local_src_file_path,
203 const base::FilePath& remote_dest_file_path,
204 const FileOperationCallback& callback) = 0;
205
206 // Retrieves a file at the virtual path |file_path| on the Drive file system
207 // onto the cache, and mark it dirty. The local path to the cache file is
208 // returned to |callback|. After opening the file, both read and write
209 // on the file can be done with normal local file operations.
210 //
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100211 // |callback| must not be null.
212 virtual void OpenFile(const base::FilePath& file_path,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100213 OpenMode open_mode,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100214 const OpenFileCallback& callback) = 0;
215
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100216 // Copies |src_file_path| to |dest_file_path| on the file system.
217 // |src_file_path| can be a hosted document (see limitations below).
218 // |dest_file_path| is expected to be of the same type of |src_file_path|
219 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
220 // a file).
221 //
222 // This method also has the following assumptions/limitations that may be
223 // relaxed or addressed later:
224 // - |src_file_path| cannot be a regular file (i.e. non-hosted document)
225 // or a directory.
226 // - |dest_file_path| must not exist.
227 // - The parent of |dest_file_path| must already exist.
228 //
229 // The file entries represented by |src_file_path| and the parent directory
230 // of |dest_file_path| need to be present in the in-memory representation
231 // of the file system.
232 //
233 // |callback| must not be null.
234 virtual void Copy(const base::FilePath& src_file_path,
235 const base::FilePath& dest_file_path,
236 const FileOperationCallback& callback) = 0;
237
238 // Moves |src_file_path| to |dest_file_path| on the file system.
239 // |src_file_path| can be a file (regular or hosted document) or a directory.
240 // |dest_file_path| is expected to be of the same type of |src_file_path|
241 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
242 // a file).
243 //
244 // This method also has the following assumptions/limitations that may be
245 // relaxed or addressed later:
246 // - |dest_file_path| must not exist.
247 // - The parent of |dest_file_path| must already exist.
248 //
249 // The file entries represented by |src_file_path| and the parent directory
250 // of |dest_file_path| need to be present in the in-memory representation
251 // of the file system.
252 //
253 // |callback| must not be null.
254 virtual void Move(const base::FilePath& src_file_path,
255 const base::FilePath& dest_file_path,
256 const FileOperationCallback& callback) = 0;
257
258 // Removes |file_path| from the file system. If |is_recursive| is set and
259 // |file_path| represents a directory, we will also delete all of its
260 // contained children elements. The file entry represented by |file_path|
261 // needs to be present in in-memory representation of the file system that
262 // in order to be removed.
263 //
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100264 // |callback| must not be null.
265 virtual void Remove(const base::FilePath& file_path,
266 bool is_recursive,
267 const FileOperationCallback& callback) = 0;
268
269 // Creates new directory under |directory_path|. If |is_exclusive| is true,
270 // an error is raised in case a directory is already present at the
271 // |directory_path|. If |is_recursive| is true, the call creates parent
272 // directories as needed just like mkdir -p does.
273 //
274 // |callback| must not be null.
275 virtual void CreateDirectory(const base::FilePath& directory_path,
276 bool is_exclusive,
277 bool is_recursive,
278 const FileOperationCallback& callback) = 0;
279
280 // Creates a file at |file_path|. If the flag |is_exclusive| is true, an
281 // error is raised when a file already exists at the path. It is
282 // an error if a directory or a hosted document is already present at the
283 // path, or the parent directory of the path is not present yet.
284 //
285 // |callback| must not be null.
286 virtual void CreateFile(const base::FilePath& file_path,
287 bool is_exclusive,
288 const FileOperationCallback& callback) = 0;
289
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100290 // Touches the file at |file_path| by updating the timestamp to
291 // |last_access_time| and |last_modified_time|.
292 // Upon completion, invokes |callback|.
293 // Note that, differently from unix touch command, this doesn't create a file
294 // if the target file doesn't exist.
295 //
296 // |last_access_time|, |last_modified_time| and |callback| must not be null.
297 virtual void TouchFile(const base::FilePath& file_path,
298 const base::Time& last_access_time,
299 const base::Time& last_modified_time,
300 const FileOperationCallback& callback) = 0;
301
Ben Murdocheb525c52013-07-10 11:40:50 +0100302 // Truncates the file content at |file_path| to the |length|.
303 //
304 // |callback| must not be null.
305 virtual void TruncateFile(const base::FilePath& file_path,
306 int64 length,
307 const FileOperationCallback& callback) = 0;
308
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100309 // Pins a file at |file_path|.
310 //
311 // |callback| must not be null.
312 virtual void Pin(const base::FilePath& file_path,
313 const FileOperationCallback& callback) = 0;
314
315 // Unpins a file at |file_path|.
316 //
317 // |callback| must not be null.
318 virtual void Unpin(const base::FilePath& file_path,
319 const FileOperationCallback& callback) = 0;
320
321 // Gets |file_path| from the file system. The file entry represented by
322 // |file_path| needs to be present in in-memory representation of the file
323 // system in order to be retrieved. If the file is not cached, the file
324 // will be downloaded through GData API or Drive V2 API.
325 //
326 // |callback| must not be null.
327 virtual void GetFileByPath(const base::FilePath& file_path,
328 const GetFileCallback& callback) = 0;
329
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100330 // Gets a file by the given |file_path|.
331 // Calls |initialized_callback| when either:
332 // 1) The cached file (or JSON file for hosted file) is found, or
333 // 2) Starting to download the file from drive server.
334 // In case of 2), the given FilePath is empty, and |get_content_callback| is
335 // called repeatedly with downloaded content following the
336 // |initialized_callback| invocation.
337 // |completion_callback| is invoked if an error is found, or the operation
338 // is successfully done.
339 // |initialized_callback|, |get_content_callback| and |completion_callback|
340 // must not be null.
341 virtual void GetFileContentByPath(
342 const base::FilePath& file_path,
343 const GetFileContentInitializedCallback& initialized_callback,
344 const google_apis::GetContentCallback& get_content_callback,
345 const FileOperationCallback& completion_callback) = 0;
346
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100347 // Finds an entry (a file or a directory) by |file_path|. This call will also
348 // retrieve and refresh file system content from server and disk cache.
349 //
350 // |callback| must not be null.
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100351 virtual void GetResourceEntryByPath(
352 const base::FilePath& file_path,
353 const GetResourceEntryCallback& callback) = 0;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100354
355 // Finds and reads a directory by |file_path|. This call will also retrieve
356 // and refresh file system content from server and disk cache.
357 //
358 // |callback| must not be null.
359 virtual void ReadDirectoryByPath(
360 const base::FilePath& file_path,
Ben Murdocheb525c52013-07-10 11:40:50 +0100361 const ReadDirectoryCallback& callback) = 0;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100362
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100363 // Does server side content search for |search_query|.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100364 // If |next_url| is set, this is the search result url that will be fetched.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100365 // Search results will be returned as a list of results' |SearchResultInfo|
366 // structs, which contains file's path and is_directory flag.
367 //
368 // |callback| must not be null.
369 virtual void Search(const std::string& search_query,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100370 const GURL& next_url,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100371 const SearchCallback& callback) = 0;
372
373 // Searches the local resource metadata, and returns the entries
374 // |at_most_num_matches| that contain |query| in their base names. Search is
375 // done in a case-insensitive fashion. The eligible entries are selected based
376 // on the given |options|, which is a bit-wise OR of SearchMetadataOptions.
377 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS will be automatically added based
378 // on the preference. |callback| must not be null. Must be called on UI
379 // thread. Empty |query| matches any base name. i.e. returns everything.
380 virtual void SearchMetadata(const std::string& query,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100381 int options,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100382 int at_most_num_matches,
383 const SearchMetadataCallback& callback) = 0;
384
385 // Fetches the user's Account Metadata to find out current quota information
386 // and returns it to the callback.
387 virtual void GetAvailableSpace(const GetAvailableSpaceCallback& callback) = 0;
388
Ben Murdochbbcdd452013-07-25 10:06:34 +0100389 // Fetches the url to the sharing dialog to be embedded in |embed_origin|,
390 // for the specified file or directory. |callback| must not be null.
391 virtual void GetShareUrl(
392 const base::FilePath& file_path,
393 const GURL& embed_origin,
394 const GetShareUrlCallback& callback) = 0;
395
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100396 // Returns miscellaneous metadata of the file system like the largest
397 // timestamp. Used in chrome:drive-internals. |callback| must not be null.
398 virtual void GetMetadata(
399 const GetFilesystemMetadataCallback& callback) = 0;
400
401 // Marks the cached file as mounted, and runs |callback| upon completion.
402 // If succeeded, the cached file path will be passed to the |callback|.
403 // |callback| must not be null.
404 virtual void MarkCacheFileAsMounted(const base::FilePath& drive_file_path,
Ben Murdocha3f7b4e2013-07-24 10:36:34 +0100405 const MarkMountedCallback& callback) = 0;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100406
407 // Marks the cached file as unmounted, and runs |callback| upon completion.
408 // Note that this method expects that the |cached_file_path| is the path
409 // returned by MarkCacheFileAsMounted().
410 // |callback| must not be null.
411 virtual void MarkCacheFileAsUnmounted(
412 const base::FilePath& cache_file_path,
413 const FileOperationCallback& callback) = 0;
414
Ben Murdoch558790d2013-07-30 15:19:42 +0100415 // Gets the cache entry for file corresponding to |resource_id| and runs
416 // |callback| with true and the found entry if the entry exists in the cache
417 // map. Otherwise, runs |callback| with false.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100418 // |callback| must not be null.
419 virtual void GetCacheEntryByResourceId(
420 const std::string& resource_id,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100421 const GetCacheEntryCallback& callback) = 0;
422
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100423 // Reloads the resource metadata from the server.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100424 virtual void Reload() = 0;
425};
426
427} // namespace drive
428
429#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_