summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/file_system_provider/throttled_file_system.cc
blob: 7d0a3146a2b4c48c95ef387d3c7eda25054b3fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/file_system_provider/throttled_file_system.h"

#include <limits>
#include <vector>

#include "base/files/file.h"
#include "chrome/browser/chromeos/file_system_provider/queue.h"

namespace chromeos {
namespace file_system_provider {

ThrottledFileSystem::ThrottledFileSystem(
    scoped_ptr<ProvidedFileSystemInterface> file_system)
    : file_system_(file_system.Pass()), weak_ptr_factory_(this) {
  const int opened_files_limit =
      file_system_->GetFileSystemInfo().opened_files_limit();
  open_queue_.reset(opened_files_limit
                        ? new Queue(static_cast<size_t>(opened_files_limit))
                        : new Queue(std::numeric_limits<size_t>::max()));
}

ThrottledFileSystem::~ThrottledFileSystem() {
}

AbortCallback ThrottledFileSystem::RequestUnmount(
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->RequestUnmount(callback);
}

AbortCallback ThrottledFileSystem::GetMetadata(
    const base::FilePath& entry_path,
    MetadataFieldMask fields,
    const GetMetadataCallback& callback) {
  return file_system_->GetMetadata(entry_path, fields, callback);
}

AbortCallback ThrottledFileSystem::GetActions(
    const base::FilePath& entry_path,
    const GetActionsCallback& callback) {
  return file_system_->GetActions(entry_path, callback);
}

AbortCallback ThrottledFileSystem::ExecuteAction(
    const base::FilePath& entry_path,
    const std::string& action_id,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->ExecuteAction(entry_path, action_id, callback);
}

AbortCallback ThrottledFileSystem::ReadDirectory(
    const base::FilePath& directory_path,
    const storage::AsyncFileUtil::ReadDirectoryCallback& callback) {
  return file_system_->ReadDirectory(directory_path, callback);
}

AbortCallback ThrottledFileSystem::ReadFile(
    int file_handle,
    net::IOBuffer* buffer,
    int64 offset,
    int length,
    const ReadChunkReceivedCallback& callback) {
  return file_system_->ReadFile(file_handle, buffer, offset, length, callback);
}

AbortCallback ThrottledFileSystem::OpenFile(const base::FilePath& file_path,
                                            OpenFileMode mode,
                                            const OpenFileCallback& callback) {
  const size_t task_token = open_queue_->NewToken();
  open_queue_->Enqueue(
      task_token,
      base::Bind(
          &ProvidedFileSystemInterface::OpenFile,
          base::Unretained(file_system_.get()),  // Outlives the queue.
          file_path, mode,
          base::Bind(&ThrottledFileSystem::OnOpenFileCompleted,
                     weak_ptr_factory_.GetWeakPtr(), task_token, callback)));
  return base::Bind(&ThrottledFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(),
                    task_token);
}

AbortCallback ThrottledFileSystem::CloseFile(
    int file_handle,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->CloseFile(
      file_handle,
      base::Bind(&ThrottledFileSystem::OnCloseFileCompleted,
                 weak_ptr_factory_.GetWeakPtr(), file_handle, callback));
}

AbortCallback ThrottledFileSystem::CreateDirectory(
    const base::FilePath& directory_path,
    bool recursive,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->CreateDirectory(directory_path, recursive, callback);
}

AbortCallback ThrottledFileSystem::DeleteEntry(
    const base::FilePath& entry_path,
    bool recursive,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->DeleteEntry(entry_path, recursive, callback);
}

AbortCallback ThrottledFileSystem::CreateFile(
    const base::FilePath& file_path,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->CreateFile(file_path, callback);
}

AbortCallback ThrottledFileSystem::CopyEntry(
    const base::FilePath& source_path,
    const base::FilePath& target_path,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->CopyEntry(source_path, target_path, callback);
}

AbortCallback ThrottledFileSystem::WriteFile(
    int file_handle,
    net::IOBuffer* buffer,
    int64 offset,
    int length,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->WriteFile(file_handle, buffer, offset, length, callback);
}

AbortCallback ThrottledFileSystem::MoveEntry(
    const base::FilePath& source_path,
    const base::FilePath& target_path,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->MoveEntry(source_path, target_path, callback);
}

AbortCallback ThrottledFileSystem::Truncate(
    const base::FilePath& file_path,
    int64 length,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->Truncate(file_path, length, callback);
}

AbortCallback ThrottledFileSystem::AddWatcher(
    const GURL& origin,
    const base::FilePath& entry_path,
    bool recursive,
    bool persistent,
    const storage::AsyncFileUtil::StatusCallback& callback,
    const storage::WatcherManager::NotificationCallback&
        notification_callback) {
  return file_system_->AddWatcher(origin, entry_path, recursive, persistent,
                                  callback, notification_callback);
}

void ThrottledFileSystem::RemoveWatcher(
    const GURL& origin,
    const base::FilePath& entry_path,
    bool recursive,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  file_system_->RemoveWatcher(origin, entry_path, recursive, callback);
}

const ProvidedFileSystemInfo& ThrottledFileSystem::GetFileSystemInfo() const {
  return file_system_->GetFileSystemInfo();
}

RequestManager* ThrottledFileSystem::GetRequestManager() {
  return file_system_->GetRequestManager();
}

Watchers* ThrottledFileSystem::GetWatchers() {
  return file_system_->GetWatchers();
}

const OpenedFiles& ThrottledFileSystem::GetOpenedFiles() const {
  return file_system_->GetOpenedFiles();
}

void ThrottledFileSystem::AddObserver(ProvidedFileSystemObserver* observer) {
  file_system_->AddObserver(observer);
}

void ThrottledFileSystem::RemoveObserver(ProvidedFileSystemObserver* observer) {
  file_system_->RemoveObserver(observer);
}

void ThrottledFileSystem::Notify(
    const base::FilePath& entry_path,
    bool recursive,
    storage::WatcherManager::ChangeType change_type,
    scoped_ptr<ProvidedFileSystemObserver::Changes> changes,
    const std::string& tag,
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->Notify(entry_path, recursive, change_type,
                              changes.Pass(), tag, callback);
}

void ThrottledFileSystem::Configure(
    const storage::AsyncFileUtil::StatusCallback& callback) {
  return file_system_->Configure(callback);
}

base::WeakPtr<ProvidedFileSystemInterface> ThrottledFileSystem::GetWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

void ThrottledFileSystem::Abort(int queue_token) {
  open_queue_->Abort(queue_token);
}

void ThrottledFileSystem::OnOpenFileCompleted(int queue_token,
                                              const OpenFileCallback& callback,
                                              int file_handle,
                                              base::File::Error result) {
  // If the file is opened successfully then hold the queue token until the file
  // is closed.
  if (result == base::File::FILE_OK)
    opened_files_[file_handle] = queue_token;
  else
    open_queue_->Complete(queue_token);

  callback.Run(file_handle, result);
}

void ThrottledFileSystem::OnCloseFileCompleted(
    int file_handle,
    const storage::AsyncFileUtil::StatusCallback& callback,
    base::File::Error result) {
  // Closing is always final. Even if an error happened, the file is considered
  // closed on the C++ side. Release the task from the queue, so other files
  // which are enqueued can be opened.
  const auto it = opened_files_.find(file_handle);
  DCHECK(it != opened_files_.end());

  const int queue_token = it->second;
  open_queue_->Complete(queue_token);
  opened_files_.erase(file_handle);

  callback.Run(result);
}

}  // namespace file_system_provider
}  // namespace chromeos