Chromium Code Reviews| 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 // TODO(mtomasz): Consider using a Blob URL, instead of DOMString. | |
| 32 std::string input_data; | |
| 33 | |
| 34 const size_t chunk_size = params->data.length(); | |
| 35 | |
| 36 // Check for overflows. | |
| 37 if (chunk_size > static_cast<size_t>(buffer_length) - buffer_offset) | |
| 38 return -1; | |
| 39 | |
| 40 memcpy(buffer->data() + buffer_offset, params->data.c_str(), chunk_size); | |
| 41 | |
| 42 return chunk_size; | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 ReadFile::ReadFile( | |
| 48 extensions::EventRouter* event_router, | |
| 49 const ProvidedFileSystemInfo& file_system_info, | |
| 50 int file_handle, | |
| 51 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 callback_(callback) { | |
| 61 } | |
| 62 | |
| 63 ReadFile::~ReadFile() { | |
| 64 } | |
| 65 | |
| 66 bool ReadFile::Execute(int request_id) { | |
| 67 scoped_ptr<base::ListValue> values(new base::ListValue); | |
| 68 values->AppendInteger(file_handle_); | |
| 69 values->AppendDouble(offset_); | |
| 70 values->AppendInteger(length_); | |
| 71 return SendEvent( | |
| 72 request_id, | |
| 73 extensions::api::file_system_provider::OnReadFileRequested::kEventName, | |
| 74 values.Pass()); | |
| 75 } | |
| 76 | |
| 77 void ReadFile::OnSuccess(int /* request_id */, | |
| 78 scoped_ptr<RequestValue> result, | |
| 79 bool has_next) { | |
| 80 const int copy_result = | |
| 81 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.
| |
| 82 DCHECK_LE(0, copy_result); | |
| 83 DCHECK(!has_next || copy_result > 0); | |
| 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 |