OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/devtools/protocol_http_request.h" | |
6 | |
7 #include "base/threading/thread.h" | |
8 #include "chrome/browser/profiles/profile.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "net/base/io_buffer.h" | |
11 #include "net/url_request/url_request.h" | |
12 #include "net/url_request/url_request_context_getter.h" | |
13 | |
14 using content::BrowserThread; | |
15 | |
16 namespace { | |
17 | |
18 const int kBufferSize = 16 * 1024; | |
19 | |
20 } // namespace | |
21 | |
22 ProtocolHttpRequest::ProtocolHttpRequest( | |
23 Profile* profile, | |
24 const std::string& url, | |
25 const Callback& callback) | |
26 : request_context_(profile->GetRequestContext()), | |
27 url_(url), | |
28 callback_(callback) { | |
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
30 | |
31 if (!url_.is_valid()) { | |
32 callback.Run("Invalid URL: " + url_.possibly_invalid_spec(), | |
33 std::string()); | |
34 return; | |
vsevik
2013/03/11 15:25:00
You are leaking ProtocolHttpRequest here.
| |
35 } | |
36 | |
37 BrowserThread::PostTask( | |
38 BrowserThread::IO, FROM_HERE, | |
39 base::Bind(&ProtocolHttpRequest::Start, base::Unretained(this))); | |
40 } | |
41 | |
42 void ProtocolHttpRequest::Start() { | |
43 request_ = new net::URLRequest(url_, this, | |
44 request_context_->GetURLRequestContext()); | |
45 io_buffer_ = new net::IOBuffer(kBufferSize); | |
46 request_->Start(); | |
47 } | |
48 | |
49 void ProtocolHttpRequest::OnResponseStarted(net::URLRequest* request) OVERRIDE { | |
50 if (!request->status().is_success()) | |
51 error_ = "HTTP 404"; | |
52 int bytes_read = 0; | |
53 if (request->status().is_success()) | |
54 request->Read(io_buffer_.get(), kBufferSize, &bytes_read); | |
55 OnReadCompleted(request, bytes_read); | |
56 } | |
57 | |
58 void ProtocolHttpRequest::OnReadCompleted(net::URLRequest* request, | |
59 int bytes_read) OVERRIDE { | |
60 do { | |
61 if (!request->status().is_success() || bytes_read <= 0) | |
62 break; | |
63 data_ += std::string(io_buffer_->data(), bytes_read); | |
64 } while (request->Read(io_buffer_, kBufferSize, &bytes_read)); | |
65 | |
66 if (!request->status().is_io_pending()) { | |
67 BrowserThread::PostTask( | |
68 BrowserThread::UI, FROM_HERE, | |
69 base::Bind(&ProtocolHttpRequest::RespondOnUIThread, | |
70 base::Unretained(this))); | |
71 delete request; | |
72 } | |
73 } | |
74 | |
75 void ProtocolHttpRequest::RespondOnUIThread() { | |
76 callback_.Run(error_, data_); | |
77 delete this; | |
78 } | |
OLD | NEW |