open_file.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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.
4
5#include "chrome/browser/chromeos/file_system_provider/operations/open_file.h"
6
7#include <string>
8
9#include "chrome/common/extensions/api/file_system_provider.h"
10#include "chrome/common/extensions/api/file_system_provider_internal.h"
11
12namespace chromeos {
13namespace file_system_provider {
14namespace operations {
15
16OpenFile::OpenFile(
17    extensions::EventRouter* event_router,
18    const ProvidedFileSystemInfo& file_system_info,
19    const base::FilePath& file_path,
20    ProvidedFileSystemInterface::OpenFileMode mode,
21    bool create,
22    const ProvidedFileSystemInterface::OpenFileCallback& callback)
23    : Operation(event_router, file_system_info),
24      file_path_(file_path),
25      mode_(mode),
26      create_(create),
27      callback_(callback) {
28}
29
30OpenFile::~OpenFile() {
31}
32
33bool OpenFile::Execute(int request_id) {
34  scoped_ptr<base::ListValue> values(new base::ListValue);
35  values->AppendString(file_path_.AsUTF8Unsafe());
36
37  switch (mode_) {
38    case ProvidedFileSystemInterface::OPEN_FILE_MODE_READ:
39      values->AppendString(extensions::api::file_system_provider::ToString(
40          extensions::api::file_system_provider::OPEN_FILE_MODE_READ));
41      break;
42    case ProvidedFileSystemInterface::OPEN_FILE_MODE_WRITE:
43      values->AppendString(extensions::api::file_system_provider::ToString(
44          extensions::api::file_system_provider::OPEN_FILE_MODE_WRITE));
45      break;
46  }
47
48  values->AppendBoolean(create_);
49
50  return SendEvent(
51      request_id,
52      extensions::api::file_system_provider::OnOpenFileRequested::kEventName,
53      values.Pass());
54}
55
56void OpenFile::OnSuccess(int request_id,
57                         scoped_ptr<RequestValue> result,
58                         bool has_more) {
59  // File handle is the same as request id of the OpenFile operation.
60  callback_.Run(request_id, base::File::FILE_OK);
61}
62
63void OpenFile::OnError(int /* request_id */, base::File::Error error) {
64  callback_.Run(0 /* file_handle */, error);
65}
66
67}  // namespace operations
68}  // namespace file_system_provider
69}  // namespace chromeos
70