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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/file_system_provider/throttled_file_system.h"
10 #include "base/files/file.h"
11 #include "chrome/browser/chromeos/file_system_provider/queue.h"
14 namespace file_system_provider
{
16 ThrottledFileSystem::ThrottledFileSystem(
17 scoped_ptr
<ProvidedFileSystemInterface
> file_system
)
18 : file_system_(file_system
.Pass()), weak_ptr_factory_(this) {
19 const int opened_files_limit
=
20 file_system_
->GetFileSystemInfo().opened_files_limit();
21 open_queue_
.reset(opened_files_limit
22 ? new Queue(static_cast<size_t>(opened_files_limit
))
23 : new Queue(std::numeric_limits
<size_t>::max()));
26 ThrottledFileSystem::~ThrottledFileSystem() {
29 AbortCallback
ThrottledFileSystem::RequestUnmount(
30 const storage::AsyncFileUtil::StatusCallback
& callback
) {
31 return file_system_
->RequestUnmount(callback
);
34 AbortCallback
ThrottledFileSystem::GetMetadata(
35 const base::FilePath
& entry_path
,
36 MetadataFieldMask fields
,
37 const GetMetadataCallback
& callback
) {
38 return file_system_
->GetMetadata(entry_path
, fields
, callback
);
41 AbortCallback
ThrottledFileSystem::ReadDirectory(
42 const base::FilePath
& directory_path
,
43 const storage::AsyncFileUtil::ReadDirectoryCallback
& callback
) {
44 return file_system_
->ReadDirectory(directory_path
, callback
);
47 AbortCallback
ThrottledFileSystem::ReadFile(
49 net::IOBuffer
* buffer
,
52 const ReadChunkReceivedCallback
& callback
) {
53 return file_system_
->ReadFile(file_handle
, buffer
, offset
, length
, callback
);
56 AbortCallback
ThrottledFileSystem::OpenFile(const base::FilePath
& file_path
,
58 const OpenFileCallback
& callback
) {
59 const size_t task_token
= open_queue_
->NewToken();
63 &ProvidedFileSystemInterface::OpenFile
,
64 base::Unretained(file_system_
.get()), // Outlives the queue.
66 base::Bind(&ThrottledFileSystem::OnOpenFileCompleted
,
67 weak_ptr_factory_
.GetWeakPtr(), task_token
, callback
)));
68 return base::Bind(&ThrottledFileSystem::Abort
, weak_ptr_factory_
.GetWeakPtr(),
72 AbortCallback
ThrottledFileSystem::CloseFile(
74 const storage::AsyncFileUtil::StatusCallback
& callback
) {
75 return file_system_
->CloseFile(
77 base::Bind(&ThrottledFileSystem::OnCloseFileCompleted
,
78 weak_ptr_factory_
.GetWeakPtr(), file_handle
, callback
));
81 AbortCallback
ThrottledFileSystem::CreateDirectory(
82 const base::FilePath
& directory_path
,
84 const storage::AsyncFileUtil::StatusCallback
& callback
) {
85 return file_system_
->CreateDirectory(directory_path
, recursive
, callback
);
88 AbortCallback
ThrottledFileSystem::DeleteEntry(
89 const base::FilePath
& entry_path
,
91 const storage::AsyncFileUtil::StatusCallback
& callback
) {
92 return file_system_
->DeleteEntry(entry_path
, recursive
, callback
);
95 AbortCallback
ThrottledFileSystem::CreateFile(
96 const base::FilePath
& file_path
,
97 const storage::AsyncFileUtil::StatusCallback
& callback
) {
98 return file_system_
->CreateFile(file_path
, callback
);
101 AbortCallback
ThrottledFileSystem::CopyEntry(
102 const base::FilePath
& source_path
,
103 const base::FilePath
& target_path
,
104 const storage::AsyncFileUtil::StatusCallback
& callback
) {
105 return file_system_
->CopyEntry(source_path
, target_path
, callback
);
108 AbortCallback
ThrottledFileSystem::WriteFile(
110 net::IOBuffer
* buffer
,
113 const storage::AsyncFileUtil::StatusCallback
& callback
) {
114 return file_system_
->WriteFile(file_handle
, buffer
, offset
, length
, callback
);
117 AbortCallback
ThrottledFileSystem::MoveEntry(
118 const base::FilePath
& source_path
,
119 const base::FilePath
& target_path
,
120 const storage::AsyncFileUtil::StatusCallback
& callback
) {
121 return file_system_
->MoveEntry(source_path
, target_path
, callback
);
124 AbortCallback
ThrottledFileSystem::Truncate(
125 const base::FilePath
& file_path
,
127 const storage::AsyncFileUtil::StatusCallback
& callback
) {
128 return file_system_
->Truncate(file_path
, length
, callback
);
131 AbortCallback
ThrottledFileSystem::AddWatcher(
133 const base::FilePath
& entry_path
,
136 const storage::AsyncFileUtil::StatusCallback
& callback
,
137 const storage::WatcherManager::NotificationCallback
&
138 notification_callback
) {
139 return file_system_
->AddWatcher(origin
, entry_path
, recursive
, persistent
,
140 callback
, notification_callback
);
143 void ThrottledFileSystem::RemoveWatcher(
145 const base::FilePath
& entry_path
,
147 const storage::AsyncFileUtil::StatusCallback
& callback
) {
148 file_system_
->RemoveWatcher(origin
, entry_path
, recursive
, callback
);
151 const ProvidedFileSystemInfo
& ThrottledFileSystem::GetFileSystemInfo() const {
152 return file_system_
->GetFileSystemInfo();
155 RequestManager
* ThrottledFileSystem::GetRequestManager() {
156 return file_system_
->GetRequestManager();
159 Watchers
* ThrottledFileSystem::GetWatchers() {
160 return file_system_
->GetWatchers();
163 const OpenedFiles
& ThrottledFileSystem::GetOpenedFiles() const {
164 return file_system_
->GetOpenedFiles();
167 void ThrottledFileSystem::AddObserver(ProvidedFileSystemObserver
* observer
) {
168 file_system_
->AddObserver(observer
);
171 void ThrottledFileSystem::RemoveObserver(ProvidedFileSystemObserver
* observer
) {
172 file_system_
->RemoveObserver(observer
);
175 void ThrottledFileSystem::Notify(
176 const base::FilePath
& entry_path
,
178 storage::WatcherManager::ChangeType change_type
,
179 scoped_ptr
<ProvidedFileSystemObserver::Changes
> changes
,
180 const std::string
& tag
,
181 const storage::AsyncFileUtil::StatusCallback
& callback
) {
182 return file_system_
->Notify(entry_path
, recursive
, change_type
,
183 changes
.Pass(), tag
, callback
);
186 base::WeakPtr
<ProvidedFileSystemInterface
> ThrottledFileSystem::GetWeakPtr() {
187 return weak_ptr_factory_
.GetWeakPtr();
190 void ThrottledFileSystem::Abort(int queue_token
) {
191 open_queue_
->Abort(queue_token
);
194 void ThrottledFileSystem::OnOpenFileCompleted(int queue_token
,
195 const OpenFileCallback
& callback
,
197 base::File::Error result
) {
198 // If the file is opened successfully then hold the queue token until the file
200 if (result
== base::File::FILE_OK
)
201 opened_files_
[file_handle
] = queue_token
;
203 open_queue_
->Complete(queue_token
);
205 callback
.Run(file_handle
, result
);
208 void ThrottledFileSystem::OnCloseFileCompleted(
210 const storage::AsyncFileUtil::StatusCallback
& callback
,
211 base::File::Error result
) {
212 // Closing is always final. Even if an error happened, the file is considered
213 // closed on the C++ side. Release the task from the queue, so other files
214 // which are enqueued can be opened.
215 const auto it
= opened_files_
.find(file_handle
);
216 DCHECK(it
!= opened_files_
.end());
218 const int queue_token
= it
->second
;
219 open_queue_
->Complete(queue_token
);
220 opened_files_
.erase(file_handle
);
222 callback
.Run(result
);
225 } // namespace file_system_provider
226 } // namespace chromeos