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"
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"
16 namespace file_system_provider
{
17 namespace operations
{
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
,
26 using extensions::api::file_system_provider_internal::
27 ReadFileRequestedSuccess::Params
;
29 const Params
* params
= value
->read_file_success_params();
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
)
39 memcpy(buffer
->data() + buffer_offset
, vector_as_array(¶ms
->data
),
48 extensions::EventRouter
* event_router
,
49 const ProvidedFileSystemInfo
& file_system_info
,
51 scoped_refptr
<net::IOBuffer
> buffer
,
54 const ProvidedFileSystemInterface::ReadChunkReceivedCallback
& callback
)
55 : Operation(event_router
, file_system_info
),
56 file_handle_(file_handle
),
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_
;
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(
86 void ReadFile::OnSuccess(int /* request_id */,
87 scoped_ptr
<RequestValue
> result
,
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.";
96 0 /* chunk_length */, false /* has_more */, base::File::FILE_ERROR_IO
);
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