| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H
_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_THROTTLED_FILE_SYSTEM_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H
_ | 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_THROTTLED_FILE_SYSTEM_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | |
| 11 | 10 |
| 12 #include "base/callback.h" | 11 #include "base/memory/ref_counted.h" |
| 13 #include "base/files/file.h" | 12 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/linked_ptr.h" | |
| 15 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 16 #include "base/observer_list.h" | 14 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h" |
| 17 #include "base/task/cancelable_task_tracker.h" | 15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
| 18 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" | 16 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" |
| 19 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | 17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
| 20 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_obse
rver.h" | 18 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_obse
rver.h" |
| 21 #include "chrome/browser/chromeos/file_system_provider/watcher.h" | |
| 22 #include "storage/browser/fileapi/async_file_util.h" | 19 #include "storage/browser/fileapi/async_file_util.h" |
| 23 #include "storage/browser/fileapi/watcher_manager.h" | 20 #include "storage/browser/fileapi/watcher_manager.h" |
| 24 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 25 | 22 |
| 26 class Profile; | 23 class Profile; |
| 27 | 24 |
| 28 namespace base { | |
| 29 class Time; | |
| 30 } // namespace base | |
| 31 | |
| 32 namespace net { | 25 namespace net { |
| 33 class IOBuffer; | 26 class IOBuffer; |
| 34 } // namespace net | 27 } // namespace net |
| 35 | 28 |
| 29 namespace base { |
| 30 class FilePath; |
| 31 } // namespace base |
| 32 |
| 36 namespace chromeos { | 33 namespace chromeos { |
| 37 namespace file_system_provider { | 34 namespace file_system_provider { |
| 38 | 35 |
| 36 class Queue; |
| 39 class RequestManager; | 37 class RequestManager; |
| 40 | 38 |
| 41 // Path of a sample fake file, which is added to the fake file system by | 39 // Decorates ProvidedFileSystemInterface with throttling capabilities. |
| 42 // default. | 40 class ThrottledFileSystem : public ProvidedFileSystemInterface { |
| 43 extern const base::FilePath::CharType kFakeFilePath[]; | |
| 44 | |
| 45 // Represents a file or a directory on a fake file system. | |
| 46 struct FakeEntry { | |
| 47 FakeEntry(); | |
| 48 FakeEntry(scoped_ptr<EntryMetadata> metadata, const std::string& contents); | |
| 49 ~FakeEntry(); | |
| 50 | |
| 51 scoped_ptr<EntryMetadata> metadata; | |
| 52 std::string contents; | |
| 53 | |
| 54 private: | |
| 55 DISALLOW_COPY_AND_ASSIGN(FakeEntry); | |
| 56 }; | |
| 57 | |
| 58 // Fake provided file system implementation. Does not communicate with target | |
| 59 // extensions. Used for unit tests. | |
| 60 class FakeProvidedFileSystem : public ProvidedFileSystemInterface { | |
| 61 public: | 41 public: |
| 62 explicit FakeProvidedFileSystem( | 42 explicit ThrottledFileSystem( |
| 63 const ProvidedFileSystemInfo& file_system_info); | 43 scoped_ptr<ProvidedFileSystemInterface> file_system); |
| 64 virtual ~FakeProvidedFileSystem(); | 44 virtual ~ThrottledFileSystem(); |
| 65 | |
| 66 // Adds a fake entry to the fake file system. | |
| 67 void AddEntry(const base::FilePath& entry_path, | |
| 68 bool is_directory, | |
| 69 const std::string& name, | |
| 70 int64 size, | |
| 71 base::Time modification_time, | |
| 72 std::string mime_type, | |
| 73 std::string contents); | |
| 74 | |
| 75 // Fetches a pointer to a fake entry registered in the fake file system. If | |
| 76 // not found, then returns NULL. The returned pointes is owned by | |
| 77 // FakeProvidedFileSystem. | |
| 78 const FakeEntry* GetEntry(const base::FilePath& entry_path) const; | |
| 79 | 45 |
| 80 // ProvidedFileSystemInterface overrides. | 46 // ProvidedFileSystemInterface overrides. |
| 81 virtual AbortCallback RequestUnmount( | 47 virtual AbortCallback RequestUnmount( |
| 82 const storage::AsyncFileUtil::StatusCallback& callback) override; | 48 const storage::AsyncFileUtil::StatusCallback& callback) override; |
| 83 virtual AbortCallback GetMetadata( | 49 virtual AbortCallback GetMetadata( |
| 84 const base::FilePath& entry_path, | 50 const base::FilePath& entry_path, |
| 85 ProvidedFileSystemInterface::MetadataFieldMask fields, | 51 MetadataFieldMask fields, |
| 86 const ProvidedFileSystemInterface::GetMetadataCallback& callback) | 52 const GetMetadataCallback& callback) override; |
| 87 override; | |
| 88 virtual AbortCallback ReadDirectory( | 53 virtual AbortCallback ReadDirectory( |
| 89 const base::FilePath& directory_path, | 54 const base::FilePath& directory_path, |
| 90 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) override; | 55 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) override; |
| 91 virtual AbortCallback OpenFile(const base::FilePath& file_path, | 56 virtual AbortCallback OpenFile(const base::FilePath& file_path, |
| 92 OpenFileMode mode, | 57 OpenFileMode mode, |
| 93 const OpenFileCallback& callback) override; | 58 const OpenFileCallback& callback) override; |
| 94 virtual AbortCallback CloseFile( | 59 virtual AbortCallback CloseFile( |
| 95 int file_handle, | 60 int file_handle, |
| 96 const storage::AsyncFileUtil::StatusCallback& callback) override; | 61 const storage::AsyncFileUtil::StatusCallback& callback) override; |
| 97 virtual AbortCallback ReadFile( | 62 virtual AbortCallback ReadFile( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 virtual void RemoveObserver(ProvidedFileSystemObserver* observer) override; | 114 virtual void RemoveObserver(ProvidedFileSystemObserver* observer) override; |
| 150 virtual void Notify( | 115 virtual void Notify( |
| 151 const base::FilePath& entry_path, | 116 const base::FilePath& entry_path, |
| 152 bool recursive, | 117 bool recursive, |
| 153 storage::WatcherManager::ChangeType change_type, | 118 storage::WatcherManager::ChangeType change_type, |
| 154 scoped_ptr<ProvidedFileSystemObserver::Changes> changes, | 119 scoped_ptr<ProvidedFileSystemObserver::Changes> changes, |
| 155 const std::string& tag, | 120 const std::string& tag, |
| 156 const storage::AsyncFileUtil::StatusCallback& callback) override; | 121 const storage::AsyncFileUtil::StatusCallback& callback) override; |
| 157 virtual base::WeakPtr<ProvidedFileSystemInterface> GetWeakPtr() override; | 122 virtual base::WeakPtr<ProvidedFileSystemInterface> GetWeakPtr() override; |
| 158 | 123 |
| 159 // Factory callback, to be used in Service::SetFileSystemFactory(). The | 124 private: |
| 160 // |event_router| argument can be NULL. | 125 // Called when opening a file is completed with either a success or an error. |
| 161 static ProvidedFileSystemInterface* Create( | 126 void OnOpenFileCompleted(int queue_token, |
| 162 Profile* profile, | 127 const OpenFileCallback& callback, |
| 163 const ProvidedFileSystemInfo& file_system_info); | 128 int file_handle, |
| 129 base::File::Error result); |
| 164 | 130 |
| 165 private: | 131 // Called when closing a file is completed with either a success or an error. |
| 166 typedef std::map<base::FilePath, linked_ptr<FakeEntry> > Entries; | 132 void OnCloseFileCompleted( |
| 167 typedef std::map<int, base::FilePath> OpenedFilesMap; | 133 int file_handle, |
| 134 const storage::AsyncFileUtil::StatusCallback& callback, |
| 135 base::File::Error result); |
| 168 | 136 |
| 169 // Utility function for posting a task which can be aborted by calling the | 137 scoped_ptr<ProvidedFileSystemInterface> file_system_; |
| 170 // returned callback. | 138 scoped_ptr<Queue> open_queue_; |
| 171 AbortCallback PostAbortableTask(const base::Closure& callback); | |
| 172 | 139 |
| 173 // Aborts a request. |task_id| refers to a posted callback returning a | 140 // Map from file handles to open queue tokens. |
| 174 // response for the operation, which will be cancelled, hence not called. | 141 std::map<int, int> opened_files_; |
| 175 void Abort(int task_id, | |
| 176 const storage::AsyncFileUtil::StatusCallback& callback); | |
| 177 | 142 |
| 178 // Aborts a request. |task_ids| refers to a vector of posted callbacks | 143 base::WeakPtrFactory<ThrottledFileSystem> weak_ptr_factory_; |
| 179 // returning a response for the operation, which will be cancelled, hence not | 144 DISALLOW_COPY_AND_ASSIGN(ThrottledFileSystem); |
| 180 // called. | |
| 181 void AbortMany(const std::vector<int>& task_ids, | |
| 182 const storage::AsyncFileUtil::StatusCallback& callback); | |
| 183 | |
| 184 ProvidedFileSystemInfo file_system_info_; | |
| 185 Entries entries_; | |
| 186 OpenedFilesMap opened_files_; | |
| 187 int last_file_handle_; | |
| 188 base::CancelableTaskTracker tracker_; | |
| 189 ObserverList<ProvidedFileSystemObserver> observers_; | |
| 190 Watchers watchers_; | |
| 191 | |
| 192 base::WeakPtrFactory<FakeProvidedFileSystem> weak_ptr_factory_; | |
| 193 DISALLOW_COPY_AND_ASSIGN(FakeProvidedFileSystem); | |
| 194 }; | 145 }; |
| 195 | 146 |
| 196 } // namespace file_system_provider | 147 } // namespace file_system_provider |
| 197 } // namespace chromeos | 148 } // namespace chromeos |
| 198 | 149 |
| 199 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTE
M_H_ | 150 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_THROTTLED_FILE_SYSTEM_H_ |
| OLD | NEW |