Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / write_file.cc
blobc1bf23b15b3a96db10a1cf7d8655474897682f99
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.
5 #include "chrome/browser/chromeos/file_system_provider/operations/write_file.h"
7 #include "base/trace_event/trace_event.h"
8 #include "base/values.h"
9 #include "chrome/common/extensions/api/file_system_provider.h"
10 #include "chrome/common/extensions/api/file_system_provider_internal.h"
12 namespace chromeos {
13 namespace file_system_provider {
14 namespace operations {
16 WriteFile::WriteFile(extensions::EventRouter* event_router,
17 const ProvidedFileSystemInfo& file_system_info,
18 int file_handle,
19 scoped_refptr<net::IOBuffer> buffer,
20 int64 offset,
21 int length,
22 const storage::AsyncFileUtil::StatusCallback& callback)
23 : Operation(event_router, file_system_info),
24 file_handle_(file_handle),
25 buffer_(buffer),
26 offset_(offset),
27 length_(length),
28 callback_(callback) {
31 WriteFile::~WriteFile() {
34 bool WriteFile::Execute(int request_id) {
35 TRACE_EVENT0("file_system_provider", "WriteFile::Execute");
36 using extensions::api::file_system_provider::WriteFileRequestedOptions;
38 if (!file_system_info_.writable())
39 return false;
41 WriteFileRequestedOptions options;
42 options.file_system_id = file_system_info_.file_system_id();
43 options.request_id = request_id;
44 options.open_request_id = file_handle_;
45 options.offset = offset_;
46 // Length is not passed directly since it can be accessed via data.byteLength.
48 // Set the data directly on base::Value() to avoid an extra string copy.
49 DCHECK(buffer_.get());
50 scoped_ptr<base::DictionaryValue> options_as_value = options.ToValue();
51 options_as_value->Set(
52 "data",
53 base::BinaryValue::CreateWithCopiedBuffer(buffer_->data(), length_));
55 scoped_ptr<base::ListValue> event_args(new base::ListValue);
56 event_args->Append(options_as_value.release());
58 return SendEvent(
59 request_id,
60 extensions::events::FILE_SYSTEM_PROVIDER_ON_WRITE_FILE_REQUESTED,
61 extensions::api::file_system_provider::OnWriteFileRequested::kEventName,
62 event_args.Pass());
65 void WriteFile::OnSuccess(int /* request_id */,
66 scoped_ptr<RequestValue> /* result */,
67 bool /* has_more */) {
68 TRACE_EVENT0("file_system_provider", "WriteFile::OnSuccess");
69 callback_.Run(base::File::FILE_OK);
72 void WriteFile::OnError(int /* request_id */,
73 scoped_ptr<RequestValue> /* result */,
74 base::File::Error error) {
75 TRACE_EVENT0("file_system_provider", "WriteFile::OnError");
76 callback_.Run(error);
79 } // namespace operations
80 } // namespace file_system_provider
81 } // namespace chromeos