BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / read_file.cc
blobdb8076a886cb93c7508942af36d61f8fef39bf96
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/read_file.h"
7 #include <limits>
8 #include <string>
10 #include "base/stl_util.h"
11 #include "base/trace_event/trace_event.h"
12 #include "chrome/common/extensions/api/file_system_provider.h"
13 #include "chrome/common/extensions/api/file_system_provider_internal.h"
15 namespace chromeos {
16 namespace file_system_provider {
17 namespace operations {
18 namespace {
20 // Convert |value| into |output|. If parsing fails, then returns a negative
21 // value. Otherwise returns number of bytes written to the buffer.
22 int CopyRequestValueToBuffer(scoped_ptr<RequestValue> value,
23 scoped_refptr<net::IOBuffer> buffer,
24 int buffer_offset,
25 int buffer_length) {
26 using extensions::api::file_system_provider_internal::
27 ReadFileRequestedSuccess::Params;
29 const Params* params = value->read_file_success_params();
30 if (!params)
31 return -1;
33 const size_t chunk_size = params->data.size();
35 // Check for overflows.
36 if (chunk_size > static_cast<size_t>(buffer_length) - buffer_offset)
37 return -1;
39 memcpy(buffer->data() + buffer_offset, vector_as_array(&params->data),
40 chunk_size);
42 return chunk_size;
45 } // namespace
47 ReadFile::ReadFile(
48 extensions::EventRouter* event_router,
49 const ProvidedFileSystemInfo& file_system_info,
50 int file_handle,
51 scoped_refptr<net::IOBuffer> buffer,
52 int64 offset,
53 int length,
54 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback)
55 : Operation(event_router, file_system_info),
56 file_handle_(file_handle),
57 buffer_(buffer),
58 offset_(offset),
59 length_(length),
60 current_offset_(0),
61 callback_(callback) {
64 ReadFile::~ReadFile() {
67 bool ReadFile::Execute(int request_id) {
68 using extensions::api::file_system_provider::ReadFileRequestedOptions;
69 TRACE_EVENT0("file_system_provider", "ReadFile::Execute");
71 ReadFileRequestedOptions options;
72 options.file_system_id = file_system_info_.file_system_id();
73 options.request_id = request_id;
74 options.open_request_id = file_handle_;
75 options.offset = offset_;
76 options.length = length_;
78 return SendEvent(
79 request_id,
80 extensions::events::FILE_SYSTEM_PROVIDER_ON_READ_FILE_REQUESTED,
81 extensions::api::file_system_provider::OnReadFileRequested::kEventName,
82 extensions::api::file_system_provider::OnReadFileRequested::Create(
83 options));
86 void ReadFile::OnSuccess(int /* request_id */,
87 scoped_ptr<RequestValue> result,
88 bool has_more) {
89 TRACE_EVENT0("file_system_provider", "ReadFile::OnSuccess");
90 const int copy_result = CopyRequestValueToBuffer(
91 result.Pass(), buffer_, current_offset_, length_);
93 if (copy_result < 0) {
94 LOG(ERROR) << "Failed to parse a response for the read file operation.";
95 callback_.Run(
96 0 /* chunk_length */, false /* has_more */, base::File::FILE_ERROR_IO);
97 return;
100 if (copy_result > 0)
101 current_offset_ += copy_result;
102 callback_.Run(copy_result, has_more, base::File::FILE_OK);
105 void ReadFile::OnError(int /* request_id */,
106 scoped_ptr<RequestValue> /* result */,
107 base::File::Error error) {
108 TRACE_EVENT0("file_system_provider", "ReadFile::OnError");
109 callback_.Run(0 /* chunk_length */, false /* has_more */, error);
112 } // namespace operations
113 } // namespace file_system_provider
114 } // namespace chromeos