Chromium Code Reviews| Index: chrome/browser/chromeos/file_system_provider/operations/read_file.cc |
| diff --git a/chrome/browser/chromeos/file_system_provider/operations/read_file.cc b/chrome/browser/chromeos/file_system_provider/operations/read_file.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8119d581eee8186c4d959d697dec59647c89cc3a |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/file_system_provider/operations/read_file.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2014 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/operations/read_file.h" |
| + |
| +#include <limits> |
| +#include <string> |
| + |
| +#include "chrome/common/extensions/api/file_system_provider.h" |
| +#include "chrome/common/extensions/api/file_system_provider_internal.h" |
| + |
| +namespace chromeos { |
| +namespace file_system_provider { |
| +namespace operations { |
| +namespace { |
| + |
| +// Convert |value| into |output|. If parsing fails, then returns a negative |
| +// value. Otherwise returns number of bytes written to the buffer. |
| +int CopyRequestValueToBuffer(scoped_ptr<RequestValue> value, |
| + net::IOBuffer* buffer, |
| + int buffer_offset, |
| + int buffer_length) { |
| + using extensions::api::file_system_provider_internal:: |
| + ReadFileRequestedSuccess::Params; |
| + |
| + const Params* params = value->read_file_success_params(); |
| + if (!params) |
| + return -1; |
| + |
| + // TODO(mtomasz): Consider using a Blob URL, instead of DOMString. |
| + std::string input_data; |
| + |
| + const size_t chunk_size = params->data.length(); |
| + |
| + // Check for overflows. |
| + if (chunk_size > static_cast<size_t>(buffer_length) - buffer_offset) |
| + return -1; |
| + |
| + memcpy(buffer->data() + buffer_offset, params->data.c_str(), chunk_size); |
| + |
| + return chunk_size; |
| +} |
| + |
| +} // namespace |
| + |
| +ReadFile::ReadFile( |
| + extensions::EventRouter* event_router, |
| + const ProvidedFileSystemInfo& file_system_info, |
| + int file_handle, |
| + net::IOBuffer* buffer, |
| + int64 offset, |
| + int length, |
| + const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) |
| + : Operation(event_router, file_system_info), |
| + file_handle_(file_handle), |
| + buffer_(buffer), |
| + offset_(offset), |
| + length_(length), |
| + callback_(callback) { |
| +} |
| + |
| +ReadFile::~ReadFile() { |
| +} |
| + |
| +bool ReadFile::Execute(int request_id) { |
| + scoped_ptr<base::ListValue> values(new base::ListValue); |
| + values->AppendInteger(file_handle_); |
| + values->AppendDouble(offset_); |
| + values->AppendInteger(length_); |
| + return SendEvent( |
| + request_id, |
| + extensions::api::file_system_provider::OnReadFileRequested::kEventName, |
| + values.Pass()); |
| +} |
| + |
| +void ReadFile::OnSuccess(int /* request_id */, |
| + scoped_ptr<RequestValue> result, |
| + bool has_next) { |
| + const int copy_result = |
| + CopyRequestValueToBuffer(result.Pass(), buffer_, offset_, length_); |
|
kinaba
2014/05/15 08:23:40
|offset_| seems to be used in two different ways:
mtomasz
2014/05/16 01:39:39
You're right. Good catch. Fixed.
|
| + DCHECK_LE(0, copy_result); |
| + DCHECK(!has_next || copy_result > 0); |
| + callback_.Run(copy_result, has_next, base::File::FILE_OK); |
| +} |
| + |
| +void ReadFile::OnError(int /* request_id */, base::File::Error error) { |
| + callback_.Run(0 /* chunk_length */, false /* has_next */, error); |
| +} |
| + |
| +} // namespace operations |
| +} // namespace file_system_provider |
| +} // namespace chromeos |