1 // Copyright (c) 2012 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/loader/certificate_resource_handler.h"
7 #include "base/strings/string_util.h"
8 #include "content/browser/loader/resource_request_info_impl.h"
9 #include "content/public/browser/content_browser_client.h"
10 #include "content/public/common/resource_response.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/mime_sniffer.h"
13 #include "net/base/mime_util.h"
14 #include "net/http/http_response_headers.h"
15 #include "net/url_request/url_request.h"
16 #include "net/url_request/url_request_status.h"
20 CertificateResourceHandler::CertificateResourceHandler(
21 net::URLRequest
* request
)
22 : ResourceHandler(request
),
25 resource_buffer_(NULL
),
26 cert_type_(net::CERTIFICATE_MIME_TYPE_UNKNOWN
) {
29 CertificateResourceHandler::~CertificateResourceHandler() {
32 bool CertificateResourceHandler::OnUploadProgress(int request_id
,
38 bool CertificateResourceHandler::OnRequestRedirected(int request_id
,
40 ResourceResponse
* resp
,
46 bool CertificateResourceHandler::OnResponseStarted(int request_id
,
47 ResourceResponse
* resp
,
49 cert_type_
= net::GetCertificateMimeTypeForMimeType(resp
->head
.mime_type
);
50 return cert_type_
!= net::CERTIFICATE_MIME_TYPE_UNKNOWN
;
53 bool CertificateResourceHandler::OnWillStart(int request_id
,
59 bool CertificateResourceHandler::OnBeforeNetworkStart(int request_id
,
65 bool CertificateResourceHandler::OnWillRead(int request_id
,
66 scoped_refptr
<net::IOBuffer
>* buf
,
69 static const int kReadBufSize
= 32768;
71 // TODO(gauravsh): Should we use 'min_size' here?
72 DCHECK(buf
&& buf_size
);
73 if (!read_buffer_
.get()) {
74 read_buffer_
= new net::IOBuffer(kReadBufSize
);
76 *buf
= read_buffer_
.get();
77 *buf_size
= kReadBufSize
;
82 bool CertificateResourceHandler::OnReadCompleted(int request_id
,
88 // We have more data to read.
89 DCHECK(read_buffer_
.get());
90 content_length_
+= bytes_read
;
92 // Release the ownership of the buffer, and store a reference
93 // to it. A new one will be allocated in OnWillRead().
94 net::IOBuffer
* buffer
= NULL
;
95 read_buffer_
.swap(&buffer
);
96 // TODO(gauravsh): Should this be handled by a separate thread?
97 buffer_
.push_back(std::make_pair(buffer
, bytes_read
));
102 void CertificateResourceHandler::OnResponseCompleted(
104 const net::URLRequestStatus
& urs
,
105 const std::string
& sec_info
,
107 if (urs
.status() != net::URLRequestStatus::SUCCESS
)
112 const void* content_bytes
= NULL
;
113 if (resource_buffer_
.get())
114 content_bytes
= resource_buffer_
->data();
116 // Note that it's up to the browser to verify that the certificate
117 // data is well-formed.
118 const ResourceRequestInfo
* info
= GetRequestInfo();
119 GetContentClient()->browser()->AddCertificate(
120 request(), cert_type_
, content_bytes
, content_length_
,
121 info
->GetChildID(), info
->GetRouteID());
124 void CertificateResourceHandler::AssembleResource() {
125 // 0-length IOBuffers are not allowed.
126 if (content_length_
== 0) {
127 resource_buffer_
= NULL
;
131 // Create the new buffer.
132 resource_buffer_
= new net::IOBuffer(content_length_
);
134 // Copy the data into it.
135 size_t bytes_copied
= 0;
136 for (size_t i
= 0; i
< buffer_
.size(); ++i
) {
137 net::IOBuffer
* data
= buffer_
[i
].first
.get();
138 size_t data_len
= buffer_
[i
].second
;
139 DCHECK(data
!= NULL
);
140 DCHECK_LE(bytes_copied
+ data_len
, content_length_
);
141 memcpy(resource_buffer_
->data() + bytes_copied
, data
->data(), data_len
);
142 bytes_copied
+= data_len
;
144 DCHECK_EQ(content_length_
, bytes_copied
);
147 void CertificateResourceHandler::OnDataDownloaded(
149 int bytes_downloaded
) {
153 } // namespace content