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"
9 #include "components/mime_util/mime_util.h"
10 #include "content/browser/loader/resource_request_info_impl.h"
11 #include "content/public/browser/content_browser_client.h"
12 #include "content/public/common/resource_response.h"
13 #include "net/base/io_buffer.h"
14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_status.h"
19 CertificateResourceHandler::CertificateResourceHandler(net::URLRequest
* request
)
20 : ResourceHandler(request
),
21 buffer_(new net::GrowableIOBuffer
),
22 cert_type_(net::CERTIFICATE_MIME_TYPE_UNKNOWN
) {
25 CertificateResourceHandler::~CertificateResourceHandler() {
28 bool CertificateResourceHandler::OnRequestRedirected(
29 const net::RedirectInfo
& redirect_info
,
30 ResourceResponse
* resp
,
35 bool CertificateResourceHandler::OnResponseStarted(ResourceResponse
* resp
,
38 mime_util::GetCertificateMimeTypeForMimeType(resp
->head
.mime_type
);
39 return cert_type_
!= net::CERTIFICATE_MIME_TYPE_UNKNOWN
;
42 bool CertificateResourceHandler::OnWillStart(const GURL
& url
, bool* defer
) {
46 bool CertificateResourceHandler::OnBeforeNetworkStart(const GURL
& url
,
51 bool CertificateResourceHandler::OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
54 static const int kInitialBufferSizeInBytes
= 32768;
55 static const int kMaxCertificateSizeInBytes
= 1024 * 1024;
57 // TODO(gauravsh): Should we use 'min_size' here?
61 if (buffer_
->capacity() == 0) {
62 buffer_
->SetCapacity(kInitialBufferSizeInBytes
);
63 } else if (buffer_
->RemainingCapacity() == 0) {
64 int capacity
= buffer_
->capacity();
65 if (capacity
>= kMaxCertificateSizeInBytes
)
67 static_assert(kMaxCertificateSizeInBytes
< INT_MAX
/ 2,
68 "The size limit ensures the capacity remains in bounds.");
70 if (capacity
> kMaxCertificateSizeInBytes
)
71 capacity
= kMaxCertificateSizeInBytes
;
72 buffer_
->SetCapacity(capacity
);
76 *buf_size
= buffer_
->RemainingCapacity();
81 bool CertificateResourceHandler::OnReadCompleted(int bytes_read
, bool* defer
) {
82 DCHECK_LE(0, bytes_read
);
83 DCHECK_LE(bytes_read
, buffer_
->RemainingCapacity());
87 buffer_
->set_offset(buffer_
->offset() + bytes_read
);
91 void CertificateResourceHandler::OnResponseCompleted(
92 const net::URLRequestStatus
& urs
,
93 const std::string
& sec_info
,
95 if (urs
.status() != net::URLRequestStatus::SUCCESS
)
98 // Note that it's up to the browser to verify that the certificate
99 // data is well-formed.
100 const ResourceRequestInfo
* info
= GetRequestInfo();
101 GetContentClient()->browser()->AddCertificate(
102 cert_type_
, buffer_
->StartOfBuffer(),
103 static_cast<size_t>(buffer_
->offset()), info
->GetChildID(),
104 info
->GetRenderFrameID());
107 void CertificateResourceHandler::OnDataDownloaded(int bytes_downloaded
) {
111 } // namespace content