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