OLD | NEW |
(Empty) | |
| 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/read_file.h" |
| 6 |
| 7 #include <limits> |
| 8 #include <string> |
| 9 |
| 10 #include "chrome/common/extensions/api/file_system_provider.h" |
| 11 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
| 12 |
| 13 namespace chromeos { |
| 14 namespace file_system_provider { |
| 15 namespace operations { |
| 16 namespace { |
| 17 |
| 18 // Convert |value| into |output|. If parsing fails, then returns a negative |
| 19 // value. Otherwise returns number of bytes written to the buffer. |
| 20 int CopyRequestValueToBuffer(scoped_ptr<RequestValue> value, |
| 21 net::IOBuffer* buffer, |
| 22 int buffer_offset, |
| 23 int buffer_length) { |
| 24 using extensions::api::file_system_provider_internal:: |
| 25 ReadFileRequestedSuccess::Params; |
| 26 |
| 27 const Params* params = value->read_file_success_params(); |
| 28 if (!params) |
| 29 return -1; |
| 30 |
| 31 const size_t chunk_size = params->data.length(); |
| 32 |
| 33 // Check for overflows. |
| 34 if (chunk_size > static_cast<size_t>(buffer_length) - buffer_offset) |
| 35 return -1; |
| 36 |
| 37 memcpy(buffer->data() + buffer_offset, params->data.c_str(), chunk_size); |
| 38 |
| 39 return chunk_size; |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 44 ReadFile::ReadFile( |
| 45 extensions::EventRouter* event_router, |
| 46 const ProvidedFileSystemInfo& file_system_info, |
| 47 int file_handle, |
| 48 net::IOBuffer* buffer, |
| 49 int64 offset, |
| 50 int length, |
| 51 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) |
| 52 : Operation(event_router, file_system_info), |
| 53 file_handle_(file_handle), |
| 54 buffer_(buffer), |
| 55 offset_(offset), |
| 56 length_(length), |
| 57 current_offset_(offset), |
| 58 callback_(callback) { |
| 59 } |
| 60 |
| 61 ReadFile::~ReadFile() { |
| 62 } |
| 63 |
| 64 bool ReadFile::Execute(int request_id) { |
| 65 scoped_ptr<base::ListValue> values(new base::ListValue); |
| 66 values->AppendInteger(file_handle_); |
| 67 values->AppendDouble(offset_); |
| 68 values->AppendInteger(length_); |
| 69 return SendEvent( |
| 70 request_id, |
| 71 extensions::api::file_system_provider::OnReadFileRequested::kEventName, |
| 72 values.Pass()); |
| 73 } |
| 74 |
| 75 void ReadFile::OnSuccess(int /* request_id */, |
| 76 scoped_ptr<RequestValue> result, |
| 77 bool has_next) { |
| 78 const int copy_result = CopyRequestValueToBuffer( |
| 79 result.Pass(), buffer_, current_offset_, length_); |
| 80 DCHECK_LE(0, copy_result); |
| 81 DCHECK(!has_next || copy_result > 0); |
| 82 if (copy_result > 0) |
| 83 current_offset_ += copy_result; |
| 84 callback_.Run(copy_result, has_next, base::File::FILE_OK); |
| 85 } |
| 86 |
| 87 void ReadFile::OnError(int /* request_id */, base::File::Error error) { |
| 88 callback_.Run(0 /* chunk_length */, false /* has_next */, error); |
| 89 } |
| 90 |
| 91 } // namespace operations |
| 92 } // namespace file_system_provider |
| 93 } // namespace chromeos |
OLD | NEW |