1// Copyright (c) 2013 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_FAKE_FILE_SYSTEM_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/callback_forward.h"
12#include "base/files/scoped_temp_dir.h"
13#include "base/memory/scoped_ptr.h"
14#include "chrome/browser/chromeos/drive/file_errors.h"
15#include "chrome/browser/chromeos/drive/file_system_interface.h"
16#include "google_apis/drive/gdata_errorcode.h"
17
18namespace google_apis {
19
20class AboutResource;
21class FileResource;
22
23}  // namespace google_apis
24
25namespace drive {
26
27class DriveServiceInterface;
28class FileSystemObserver;
29class ResourceEntry;
30
31namespace test_util {
32
33// This class implements a fake FileSystem which acts like a real Drive
34// file system with FakeDriveService, for testing purpose.
35// Note that this class doesn't support "caching" at the moment, so the number
36// of interactions to the FakeDriveService may be bigger than the real
37// implementation.
38// Currently most methods are empty (not implemented).
39class FakeFileSystem : public FileSystemInterface {
40 public:
41  explicit FakeFileSystem(DriveServiceInterface* drive_service);
42  virtual ~FakeFileSystem();
43
44  // FileSystemInterface Overrides.
45  virtual void AddObserver(FileSystemObserver* observer) OVERRIDE;
46  virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE;
47  virtual void CheckForUpdates() OVERRIDE;
48  virtual void TransferFileFromLocalToRemote(
49      const base::FilePath& local_src_file_path,
50      const base::FilePath& remote_dest_file_path,
51      const FileOperationCallback& callback) OVERRIDE;
52  virtual void OpenFile(const base::FilePath& file_path,
53                        OpenMode open_mode,
54                        const std::string& mime_type,
55                        const OpenFileCallback& callback) OVERRIDE;
56  virtual void Copy(const base::FilePath& src_file_path,
57                    const base::FilePath& dest_file_path,
58                    bool preserve_last_modified,
59                    const FileOperationCallback& callback) OVERRIDE;
60  virtual void Move(const base::FilePath& src_file_path,
61                    const base::FilePath& dest_file_path,
62                    const FileOperationCallback& callback) OVERRIDE;
63  virtual void Remove(const base::FilePath& file_path,
64                      bool is_recursive,
65                      const FileOperationCallback& callback) OVERRIDE;
66  virtual void CreateDirectory(const base::FilePath& directory_path,
67                               bool is_exclusive,
68                               bool is_recursive,
69                               const FileOperationCallback& callback) OVERRIDE;
70  virtual void CreateFile(const base::FilePath& file_path,
71                          bool is_exclusive,
72                          const std::string& mime_type,
73                          const FileOperationCallback& callback) OVERRIDE;
74  virtual void TouchFile(const base::FilePath& file_path,
75                         const base::Time& last_access_time,
76                         const base::Time& last_modified_time,
77                         const FileOperationCallback& callback) OVERRIDE;
78  virtual void TruncateFile(const base::FilePath& file_path,
79                            int64 length,
80                            const FileOperationCallback& callback) OVERRIDE;
81  virtual void Pin(const base::FilePath& file_path,
82                   const FileOperationCallback& callback) OVERRIDE;
83  virtual void Unpin(const base::FilePath& file_path,
84                     const FileOperationCallback& callback) OVERRIDE;
85  virtual void GetFile(const base::FilePath& file_path,
86                       const GetFileCallback& callback) OVERRIDE;
87  virtual void GetFileForSaving(const base::FilePath& file_path,
88                                const GetFileCallback& callback) OVERRIDE;
89  virtual base::Closure GetFileContent(
90      const base::FilePath& file_path,
91      const GetFileContentInitializedCallback& initialized_callback,
92      const google_apis::GetContentCallback& get_content_callback,
93      const FileOperationCallback& completion_callback) OVERRIDE;
94  virtual void GetResourceEntry(
95      const base::FilePath& file_path,
96      const GetResourceEntryCallback& callback) OVERRIDE;
97  virtual void ReadDirectory(
98      const base::FilePath& file_path,
99      const ReadDirectoryEntriesCallback& entries_callback,
100      const FileOperationCallback& completion_callback) OVERRIDE;
101  virtual void Search(const std::string& search_query,
102                      const GURL& next_link,
103                      const SearchCallback& callback) OVERRIDE;
104  virtual void SearchMetadata(const std::string& query,
105                              int options,
106                              int at_most_num_matches,
107                              const SearchMetadataCallback& callback) OVERRIDE;
108  virtual void GetAvailableSpace(
109      const GetAvailableSpaceCallback& callback) OVERRIDE;
110  virtual void GetShareUrl(
111      const base::FilePath& file_path,
112      const GURL& embed_origin,
113      const GetShareUrlCallback& callback) OVERRIDE;
114  virtual void GetMetadata(
115      const GetFilesystemMetadataCallback& callback) OVERRIDE;
116  virtual void MarkCacheFileAsMounted(
117      const base::FilePath& drive_file_path,
118      const MarkMountedCallback& callback) OVERRIDE;
119  virtual void MarkCacheFileAsUnmounted(
120      const base::FilePath& cache_file_path,
121      const FileOperationCallback& callback) OVERRIDE;
122  virtual void AddPermission(const base::FilePath& drive_file_path,
123                             const std::string& email,
124                             google_apis::drive::PermissionRole role,
125                             const FileOperationCallback& callback) OVERRIDE;
126  virtual void Reset(const FileOperationCallback& callback) OVERRIDE;
127  virtual void GetPathFromResourceId(const std::string& resource_id,
128                                     const GetFilePathCallback& callback)
129      OVERRIDE;
130
131 private:
132  // Helpers of GetFileContent.
133  // How the method works:
134  // 1) Gets ResourceEntry of the path.
135  // 2) Look at if there is a cache file or not. If found return it.
136  // 3) Otherwise start DownloadFile.
137  // 4) Runs the |completion_callback| upon the download completion.
138  void GetFileContentAfterGetResourceEntry(
139      const GetFileContentInitializedCallback& initialized_callback,
140      const google_apis::GetContentCallback& get_content_callback,
141      const FileOperationCallback& completion_callback,
142      FileError error,
143      scoped_ptr<ResourceEntry> entry);
144  void GetFileContentAfterGetFileResource(
145      const GetFileContentInitializedCallback& initialized_callback,
146      const google_apis::GetContentCallback& get_content_callback,
147      const FileOperationCallback& completion_callback,
148      google_apis::GDataErrorCode gdata_error,
149      scoped_ptr<google_apis::FileResource> gdata_entry);
150  void GetFileContentAfterDownloadFile(
151      const FileOperationCallback& completion_callback,
152      google_apis::GDataErrorCode gdata_error,
153      const base::FilePath& temp_file);
154
155  // Helpers of GetResourceEntry.
156  // How the method works:
157  // 1) If the path is root, gets AboutResrouce from the drive service
158  //    and create ResourceEntry.
159  // 2-1) Otherwise, gets the parent's ResourceEntry by recursive call.
160  // 2-2) Then, gets the resource list by restricting the parent with its id.
161  // 2-3) Search the results based on title, and return the ResourceEntry.
162  // Note that adding suffix (e.g. " (2)") for files sharing a same name is
163  // not supported in FakeFileSystem. Thus, even if the server has
164  // files sharing the same name under a directory, the second (or later)
165  // file cannot be taken with the suffixed name.
166  void GetResourceEntryAfterGetAboutResource(
167      const GetResourceEntryCallback& callback,
168      google_apis::GDataErrorCode gdata_error,
169      scoped_ptr<google_apis::AboutResource> about_resource);
170  void GetResourceEntryAfterGetParentEntryInfo(
171      const base::FilePath& base_name,
172      const GetResourceEntryCallback& callback,
173      FileError error,
174      scoped_ptr<ResourceEntry> parent_entry);
175  void GetResourceEntryAfterGetFileList(
176      const base::FilePath& base_name,
177      const GetResourceEntryCallback& callback,
178      google_apis::GDataErrorCode gdata_error,
179      scoped_ptr<google_apis::FileList> file_list);
180
181  DriveServiceInterface* drive_service_;  // Not owned.
182  base::ScopedTempDir cache_dir_;
183
184  // Note: This should remain the last member so it'll be destroyed and
185  // invalidate the weak pointers before any other members are destroyed.
186  base::WeakPtrFactory<FakeFileSystem> weak_ptr_factory_;
187
188  DISALLOW_COPY_AND_ASSIGN(FakeFileSystem);
189};
190
191}  // namespace test_util
192}  // namespace drive
193
194#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_
195