| Index: chrome/browser/devtools/protocol_http_request.cc
|
| diff --git a/chrome/browser/devtools/protocol_http_request.cc b/chrome/browser/devtools/protocol_http_request.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5dcf1d8afc5a59a9a081d86d7c0e036b76a401ba
|
| --- /dev/null
|
| +++ b/chrome/browser/devtools/protocol_http_request.cc
|
| @@ -0,0 +1,81 @@
|
| +// Copyright (c) 2013 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/devtools/protocol_http_request.h"
|
| +
|
| +#include "base/threading/thread.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "net/base/io_buffer.h"
|
| +#include "net/url_request/url_request.h"
|
| +#include "net/url_request/url_request_context_getter.h"
|
| +
|
| +using content::BrowserThread;
|
| +
|
| +namespace {
|
| +
|
| +const int kBufferSize = 16 * 1024;
|
| +
|
| +} // namespace
|
| +
|
| +ProtocolHttpRequest::ProtocolHttpRequest(
|
| + Profile* profile,
|
| + const std::string& url,
|
| + const Callback& callback)
|
| + : request_context_(profile->GetRequestContext()),
|
| + url_(url),
|
| + callback_(callback) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| +
|
| + if (!url_.is_valid()) {
|
| + error_ = "Invalid URL: " + url_.possibly_invalid_spec();
|
| + RespondOnUIThread();
|
| + return;
|
| + }
|
| +
|
| + BrowserThread::PostTask(
|
| + BrowserThread::IO, FROM_HERE,
|
| + base::Bind(&ProtocolHttpRequest::Start, base::Unretained(this)));
|
| +}
|
| +
|
| +ProtocolHttpRequest::~ProtocolHttpRequest() {
|
| +}
|
| +
|
| +void ProtocolHttpRequest::Start() {
|
| + request_ = new net::URLRequest(url_, this,
|
| + request_context_->GetURLRequestContext());
|
| + io_buffer_ = new net::IOBuffer(kBufferSize);
|
| + request_->Start();
|
| +}
|
| +
|
| +void ProtocolHttpRequest::OnResponseStarted(net::URLRequest* request) {
|
| + if (!request->status().is_success())
|
| + error_ = "HTTP 404";
|
| + int bytes_read = 0;
|
| + if (request->status().is_success())
|
| + request->Read(io_buffer_.get(), kBufferSize, &bytes_read);
|
| + OnReadCompleted(request, bytes_read);
|
| +}
|
| +
|
| +void ProtocolHttpRequest::OnReadCompleted(net::URLRequest* request,
|
| + int bytes_read) {
|
| + do {
|
| + if (!request->status().is_success() || bytes_read <= 0)
|
| + break;
|
| + data_ += std::string(io_buffer_->data(), bytes_read);
|
| + } while (request->Read(io_buffer_, kBufferSize, &bytes_read));
|
| +
|
| + if (!request->status().is_io_pending()) {
|
| + BrowserThread::PostTask(
|
| + BrowserThread::UI, FROM_HERE,
|
| + base::Bind(&ProtocolHttpRequest::RespondOnUIThread,
|
| + base::Unretained(this)));
|
| + delete request;
|
| + }
|
| +}
|
| +
|
| +void ProtocolHttpRequest::RespondOnUIThread() {
|
| + callback_.Run(error_, data_);
|
| + delete this;
|
| +}
|
|
|