Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / devtools / protocol / io_handler.cc
blob1cc1354cda7d611be66862cd67e078adfa410a21
1 // Copyright 2015 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 "content/browser/devtools/protocol/io_handler.h"
7 #include <stdint.h>
9 #include "base/bind.h"
10 #include "base/files/file.h"
11 #include "base/files/file_util.h"
12 #include "base/memory/ref_counted_delete_on_message_loop.h"
13 #include "base/memory/ref_counted_memory.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "content/browser/devtools/devtools_io_context.h"
16 #include "content/public/browser/browser_thread.h"
18 namespace content {
19 namespace devtools {
20 namespace io {
22 using Response = DevToolsProtocolClient::Response;
24 IOHandler::IOHandler(DevToolsIOContext* io_context)
25 : io_context_(io_context)
26 , weak_factory_(this) {}
28 IOHandler::~IOHandler() {}
30 void IOHandler::SetClient(scoped_ptr<Client> client) {
31 client_.swap(client);
34 Response IOHandler::Read(DevToolsCommandId command_id,
35 const std::string& handle, const int* offset, const int* max_size) {
36 static const size_t kDefaultChunkSize = 10 * 1024 * 1024;
38 scoped_refptr<DevToolsIOContext::Stream> stream =
39 io_context_->GetByHandle(handle);
40 if (!stream)
41 return Response::InvalidParams("Invalid stream handle");
42 stream->Read(offset ? *offset : -1,
43 max_size && *max_size ? *max_size : kDefaultChunkSize,
44 base::Bind(&IOHandler::ReadComplete,
45 weak_factory_.GetWeakPtr(), command_id));
46 return Response::OK();
49 void IOHandler::ReadComplete(DevToolsCommandId command_id,
50 const scoped_refptr<base::RefCountedString>& data,
51 int status) {
52 if (status == DevToolsIOContext::Stream::StatusFailure) {
53 client_->SendError(command_id, Response::ServerError("Read failed"));
54 return;
56 bool eof = status == DevToolsIOContext::Stream::StatusEOF;
57 client_->SendReadResponse(command_id,
58 ReadResponse::Create()->set_data(data->data())->set_eof(eof));
61 Response IOHandler::Close(const std::string& handle) {
62 return io_context_->Close(handle) ? Response::OK()
63 : Response::InvalidParams("Invalid stream handle");
66 } // namespace io
67 } // namespace devtools
68 } // namespace content