summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/extensions/file_system_provider/file_system_provider_api.cc
blob: 430f28b3992ac246e6a78202ff125f1a0f11d244 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2013 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/extensions/file_system_provider/file_system_provider_api.h"

#include <string>
#include <utility>
#include <vector>

#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/trace_event/trace_event.h"
#include "base/values.h"
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
#include "chrome/browser/chromeos/file_system_provider/request_manager.h"
#include "chrome/browser/chromeos/file_system_provider/request_value.h"
#include "chrome/browser/chromeos/file_system_provider/service.h"
#include "chrome/common/extensions/api/file_system_provider.h"
#include "chrome/common/extensions/api/file_system_provider_internal.h"
#include "storage/browser/fileapi/watcher_manager.h"

using chromeos::file_system_provider::MountOptions;
using chromeos::file_system_provider::OpenedFiles;
using chromeos::file_system_provider::ProvidedFileSystemInfo;
using chromeos::file_system_provider::ProvidedFileSystemInterface;
using chromeos::file_system_provider::ProvidedFileSystemObserver;
using chromeos::file_system_provider::RequestValue;
using chromeos::file_system_provider::Service;
using chromeos::file_system_provider::Watchers;

namespace extensions {
namespace {

typedef std::vector<linked_ptr<api::file_system_provider::Change>> IDLChanges;

// Converts the change type from the IDL type to a native type. |changed_type|
// must be specified (not CHANGE_TYPE_NONE).
storage::WatcherManager::ChangeType ParseChangeType(
    const api::file_system_provider::ChangeType& change_type) {
  switch (change_type) {
    case api::file_system_provider::CHANGE_TYPE_CHANGED:
      return storage::WatcherManager::CHANGED;
    case api::file_system_provider::CHANGE_TYPE_DELETED:
      return storage::WatcherManager::DELETED;
    default:
      break;
  }
  NOTREACHED();
  return storage::WatcherManager::CHANGED;
}

// Convert the change from the IDL type to a native type. The reason IDL types
// are not used is since they are imperfect, eg. paths are stored as strings.
ProvidedFileSystemObserver::Change ParseChange(
    const api::file_system_provider::Change& change) {
  ProvidedFileSystemObserver::Change result;
  result.entry_path = base::FilePath::FromUTF8Unsafe(change.entry_path);
  result.change_type = ParseChangeType(change.change_type);
  return result;
}

// Converts a list of child changes from the IDL type to a native type.
scoped_ptr<ProvidedFileSystemObserver::Changes> ParseChanges(
    const IDLChanges& changes) {
  scoped_ptr<ProvidedFileSystemObserver::Changes> results(
      new ProvidedFileSystemObserver::Changes);
  for (const auto& change : changes) {
    results->push_back(ParseChange(*change));
  }
  return results;
}

// Fills the IDL's FileSystemInfo with FSP's ProvidedFileSystemInfo and
// Watchers.
void FillFileSystemInfo(const ProvidedFileSystemInfo& file_system_info,
                        const Watchers& watchers,
                        const OpenedFiles& opened_files,
                        api::file_system_provider::FileSystemInfo* output) {
  using api::file_system_provider::Watcher;
  using api::file_system_provider::OpenedFile;

  output->file_system_id = file_system_info.file_system_id();
  output->display_name = file_system_info.display_name();
  output->writable = file_system_info.writable();
  output->opened_files_limit = file_system_info.opened_files_limit();

  std::vector<linked_ptr<Watcher>> watcher_items;
  for (const auto& watcher : watchers) {
    const linked_ptr<Watcher> watcher_item(new Watcher);
    watcher_item->entry_path = watcher.second.entry_path.value();
    watcher_item->recursive = watcher.second.recursive;
    if (!watcher.second.last_tag.empty())
      watcher_item->last_tag.reset(new std::string(watcher.second.last_tag));
    watcher_items.push_back(watcher_item);
  }
  output->watchers = watcher_items;

  std::vector<linked_ptr<OpenedFile>> opened_file_items;
  for (const auto& opened_file : opened_files) {
    const linked_ptr<OpenedFile> opened_file_item(new OpenedFile);
    opened_file_item->open_request_id = opened_file.first;
    opened_file_item->file_path = opened_file.second.file_path.value();
    switch (opened_file.second.mode) {
      case chromeos::file_system_provider::OPEN_FILE_MODE_READ:
        opened_file_item->mode =
            extensions::api::file_system_provider::OPEN_FILE_MODE_READ;
        break;
      case chromeos::file_system_provider::OPEN_FILE_MODE_WRITE:
        opened_file_item->mode =
            extensions::api::file_system_provider::OPEN_FILE_MODE_WRITE;
        break;
    }
    opened_file_items.push_back(opened_file_item);
  }
  output->opened_files = opened_file_items;
}

}  // namespace

bool FileSystemProviderMountFunction::RunSync() {
  using api::file_system_provider::Mount::Params;
  const scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  // It's an error if the file system Id is empty.
  if (params->options.file_system_id.empty()) {
    SetError(FileErrorToString(base::File::FILE_ERROR_INVALID_OPERATION));
    return false;
  }

  // It's an error if the display name is empty.
  if (params->options.display_name.empty()) {
    SetError(FileErrorToString(base::File::FILE_ERROR_INVALID_OPERATION));
    return false;
  }

  // If the opened files limit is set, then it must be larger or equal than 0.
  if (params->options.opened_files_limit.get() &&
      *params->options.opened_files_limit.get() < 0) {
    SetError(FileErrorToString(base::File::FILE_ERROR_INVALID_OPERATION));
    return false;
  }

  Service* const service = Service::Get(GetProfile());
  DCHECK(service);

  MountOptions options;
  options.file_system_id = params->options.file_system_id;
  options.display_name = params->options.display_name;
  options.writable = params->options.writable != nullptr;
  options.opened_files_limit = params->options.opened_files_limit.get()
                                   ? *params->options.opened_files_limit.get()
                                   : 0;
  options.supports_notify_tag = params->options.supports_notify_tag != nullptr;

  const base::File::Error result =
      service->MountFileSystem(extension_id(), options);
  if (result != base::File::FILE_OK) {
    SetError(FileErrorToString(result));
    return false;
  }

  return true;
}

bool FileSystemProviderUnmountFunction::RunSync() {
  using api::file_system_provider::Unmount::Params;
  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  Service* const service = Service::Get(GetProfile());
  DCHECK(service);

  const base::File::Error result =
      service->UnmountFileSystem(extension_id(), params->options.file_system_id,
                                 Service::UNMOUNT_REASON_USER);
  if (result != base::File::FILE_OK) {
    SetError(FileErrorToString(result));
    return false;
  }

  return true;
}

bool FileSystemProviderGetAllFunction::RunSync() {
  using api::file_system_provider::FileSystemInfo;
  Service* const service = Service::Get(GetProfile());
  DCHECK(service);

  const std::vector<ProvidedFileSystemInfo> file_systems =
      service->GetProvidedFileSystemInfoList();
  std::vector<linked_ptr<FileSystemInfo>> items;

  for (const auto& file_system_info : file_systems) {
    if (file_system_info.extension_id() == extension_id()) {
      const linked_ptr<FileSystemInfo> item(new FileSystemInfo);

      chromeos::file_system_provider::ProvidedFileSystemInterface* const
          file_system =
              service->GetProvidedFileSystem(file_system_info.extension_id(),
                                             file_system_info.file_system_id());
      DCHECK(file_system);

      FillFileSystemInfo(file_system_info, *file_system->GetWatchers(),
                         file_system->GetOpenedFiles(), item.get());
      items.push_back(item);
    }
  }

  SetResultList(api::file_system_provider::GetAll::Results::Create(items));
  return true;
}

bool FileSystemProviderGetFunction::RunSync() {
  using api::file_system_provider::Get::Params;
  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  using api::file_system_provider::FileSystemInfo;
  Service* const service = Service::Get(GetProfile());
  DCHECK(service);

  chromeos::file_system_provider::ProvidedFileSystemInterface* const
      file_system = service->GetProvidedFileSystem(extension_id(),
                                                   params->file_system_id);

  if (!file_system) {
    SetError(FileErrorToString(base::File::FILE_ERROR_NOT_FOUND));
    return false;
  }

  FileSystemInfo file_system_info;
  FillFileSystemInfo(file_system->GetFileSystemInfo(),
                     *file_system->GetWatchers(), file_system->GetOpenedFiles(),
                     &file_system_info);
  SetResultList(
      api::file_system_provider::Get::Results::Create(file_system_info));
  return true;
}

bool FileSystemProviderNotifyFunction::RunAsync() {
  using api::file_system_provider::Notify::Params;
  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  Service* const service = Service::Get(GetProfile());
  DCHECK(service);

  ProvidedFileSystemInterface* const file_system =
      service->GetProvidedFileSystem(extension_id(),
                                     params->options.file_system_id);
  if (!file_system) {
    SetError(FileErrorToString(base::File::FILE_ERROR_NOT_FOUND));
    return false;
  }

  file_system->Notify(
      base::FilePath::FromUTF8Unsafe(params->options.observed_path),
      params->options.recursive, ParseChangeType(params->options.change_type),
      params->options.changes.get()
          ? ParseChanges(*params->options.changes.get())
          : make_scoped_ptr(new ProvidedFileSystemObserver::Changes),
      params->options.tag.get() ? *params->options.tag.get() : "",
      base::Bind(&FileSystemProviderNotifyFunction::OnNotifyCompleted, this));

  return true;
}

void FileSystemProviderNotifyFunction::OnNotifyCompleted(
    base::File::Error result) {
  if (result != base::File::FILE_OK) {
    SetError(FileErrorToString(result));
    SendResponse(false);
    return;
  }

  SendResponse(true);
}

bool FileSystemProviderInternalUnmountRequestedSuccessFunction::RunWhenValid() {
  using api::file_system_provider_internal::UnmountRequestedSuccess::Params;
  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  return FulfillRequest(
      RequestValue::CreateForUnmountSuccess(std::move(params)),
      false /* has_more */);
}

bool
FileSystemProviderInternalGetMetadataRequestedSuccessFunction::RunWhenValid() {
  using api::file_system_provider_internal::GetMetadataRequestedSuccess::Params;
  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  return FulfillRequest(
      RequestValue::CreateForGetMetadataSuccess(std::move(params)),
      false /* has_more */);
}

bool FileSystemProviderInternalGetActionsRequestedSuccessFunction::
    RunWhenValid() {
  using api::file_system_provider_internal::GetActionsRequestedSuccess::Params;
  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  return FulfillRequest(
      RequestValue::CreateForGetActionsSuccess(std::move(params)),
      false /* has_more */);
}

bool FileSystemProviderInternalReadDirectoryRequestedSuccessFunction::
    RunWhenValid() {
  using api::file_system_provider_internal::ReadDirectoryRequestedSuccess::
      Params;
  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  const bool has_more = params->has_more;
  return FulfillRequest(
      RequestValue::CreateForReadDirectorySuccess(std::move(params)), has_more);
}

bool
FileSystemProviderInternalReadFileRequestedSuccessFunction::RunWhenValid() {
  TRACE_EVENT0("file_system_provider", "ReadFileRequestedSuccess");
  using api::file_system_provider_internal::ReadFileRequestedSuccess::Params;

  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  const bool has_more = params->has_more;
  return FulfillRequest(
      RequestValue::CreateForReadFileSuccess(std::move(params)), has_more);
}

bool
FileSystemProviderInternalOperationRequestedSuccessFunction::RunWhenValid() {
  using api::file_system_provider_internal::OperationRequestedSuccess::Params;
  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  return FulfillRequest(
      scoped_ptr<RequestValue>(
          RequestValue::CreateForOperationSuccess(std::move(params))),
      false /* has_more */);
}

bool FileSystemProviderInternalOperationRequestedErrorFunction::RunWhenValid() {
  using api::file_system_provider_internal::OperationRequestedError::Params;
  scoped_ptr<Params> params(Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params);

  const base::File::Error error = ProviderErrorToFileError(params->error);
  return RejectRequest(RequestValue::CreateForOperationError(std::move(params)),
                       error);
}

}  // namespace extensions