1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_
12 #include "base/callback.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/task/cancelable_task_tracker.h"
16 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
30 namespace file_system_provider {
34 // Path of a sample fake file, which is added to the fake file system by
36 extern const char kFakeFilePath[];
38 // Represents a file or a directory on a fake file system.
41 FakeEntry(scoped_ptr<EntryMetadata> metadata, const std::string& contents);
44 scoped_ptr<EntryMetadata> metadata;
48 DISALLOW_COPY_AND_ASSIGN(FakeEntry);
51 // Fake provided file system implementation. Does not communicate with target
52 // extensions. Used for unit tests.
53 class FakeProvidedFileSystem : public ProvidedFileSystemInterface {
55 explicit FakeProvidedFileSystem(
56 const ProvidedFileSystemInfo& file_system_info);
57 virtual ~FakeProvidedFileSystem();
59 // Adds a fake entry to the fake file system.
60 void AddEntry(const base::FilePath& entry_path,
62 const std::string& name,
64 base::Time modification_time,
65 std::string mime_type,
66 std::string contents);
68 // Fetches a pointer to a fake entry registered in the fake file system. If
69 // not found, then returns NULL. The returned pointes is owned by
70 // FakeProvidedFileSystem.
71 const FakeEntry* GetEntry(const base::FilePath& entry_path) const;
73 // ProvidedFileSystemInterface overrides.
74 virtual AbortCallback RequestUnmount(
75 const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
76 virtual AbortCallback GetMetadata(
77 const base::FilePath& entry_path,
78 ProvidedFileSystemInterface::MetadataFieldMask fields,
79 const ProvidedFileSystemInterface::GetMetadataCallback& callback)
81 virtual AbortCallback ReadDirectory(
82 const base::FilePath& directory_path,
83 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) OVERRIDE;
84 virtual AbortCallback OpenFile(const base::FilePath& file_path,
86 const OpenFileCallback& callback) OVERRIDE;
87 virtual AbortCallback CloseFile(
89 const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
90 virtual AbortCallback ReadFile(
92 net::IOBuffer* buffer,
95 const ReadChunkReceivedCallback& callback) OVERRIDE;
96 virtual AbortCallback CreateDirectory(
97 const base::FilePath& directory_path,
99 const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
100 virtual AbortCallback DeleteEntry(
101 const base::FilePath& entry_path,
103 const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
104 virtual AbortCallback CreateFile(
105 const base::FilePath& file_path,
106 const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
107 virtual AbortCallback CopyEntry(
108 const base::FilePath& source_path,
109 const base::FilePath& target_path,
110 const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
111 virtual AbortCallback MoveEntry(
112 const base::FilePath& source_path,
113 const base::FilePath& target_path,
114 const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
115 virtual AbortCallback Truncate(
116 const base::FilePath& file_path,
118 const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
119 virtual AbortCallback WriteFile(
121 net::IOBuffer* buffer,
124 const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
125 virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const OVERRIDE;
126 virtual RequestManager* GetRequestManager() OVERRIDE;
127 virtual base::WeakPtr<ProvidedFileSystemInterface> GetWeakPtr() OVERRIDE;
129 // Factory callback, to be used in Service::SetFileSystemFactory(). The
130 // |event_router| argument can be NULL.
131 static ProvidedFileSystemInterface* Create(
133 const ProvidedFileSystemInfo& file_system_info);
136 typedef std::map<base::FilePath, linked_ptr<FakeEntry> > Entries;
137 typedef std::map<int, base::FilePath> OpenedFilesMap;
139 // Utility function for posting a task which can be aborted by calling the
140 // returned callback.
141 AbortCallback PostAbortableTask(const base::Closure& callback);
143 // Aborts a request. |task_id| refers to a posted callback returning a
144 // response for the operation, which will be cancelled, hence not called.
145 void Abort(int task_id,
146 const storage::AsyncFileUtil::StatusCallback& callback);
148 // Aborts a request. |task_ids| refers to a vector of posted callbacks
149 // returning a response for the operation, which will be cancelled, hence not
151 void AbortMany(const std::vector<int>& task_ids,
152 const storage::AsyncFileUtil::StatusCallback& callback);
154 ProvidedFileSystemInfo file_system_info_;
156 OpenedFilesMap opened_files_;
157 int last_file_handle_;
158 base::CancelableTaskTracker tracker_;
160 base::WeakPtrFactory<FakeProvidedFileSystem> weak_ptr_factory_;
161 DISALLOW_COPY_AND_ASSIGN(FakeProvidedFileSystem);
164 } // namespace file_system_provider
165 } // namespace chromeos
167 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_