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::OnUploadProgress(uint64 position
,
33 bool CertificateResourceHandler::OnRequestRedirected(
34 const net::RedirectInfo
& redirect_info
,
35 ResourceResponse
* resp
,
40 bool CertificateResourceHandler::OnResponseStarted(ResourceResponse
* resp
,
43 mime_util::GetCertificateMimeTypeForMimeType(resp
->head
.mime_type
);
44 return cert_type_
!= net::CERTIFICATE_MIME_TYPE_UNKNOWN
;
47 bool CertificateResourceHandler::OnWillStart(const GURL
& url
, bool* defer
) {
51 bool CertificateResourceHandler::OnBeforeNetworkStart(const GURL
& url
,
56 bool CertificateResourceHandler::OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
59 static const int kInitialBufferSizeInBytes
= 32768;
60 static const int kMaxCertificateSizeInBytes
= 1024 * 1024;
62 // TODO(gauravsh): Should we use 'min_size' here?
66 if (buffer_
->capacity() == 0) {
67 buffer_
->SetCapacity(kInitialBufferSizeInBytes
);
68 } else if (buffer_
->RemainingCapacity() == 0) {
69 int capacity
= buffer_
->capacity();
70 if (capacity
>= kMaxCertificateSizeInBytes
)
72 static_assert(kMaxCertificateSizeInBytes
< INT_MAX
/ 2,
73 "The size limit ensures the capacity remains in bounds.");
75 if (capacity
> kMaxCertificateSizeInBytes
)
76 capacity
= kMaxCertificateSizeInBytes
;
77 buffer_
->SetCapacity(capacity
);
81 *buf_size
= buffer_
->RemainingCapacity();
86 bool CertificateResourceHandler::OnReadCompleted(int bytes_read
, bool* defer
) {
87 DCHECK_LE(0, bytes_read
);
88 DCHECK_LE(bytes_read
, buffer_
->RemainingCapacity());
92 buffer_
->set_offset(buffer_
->offset() + bytes_read
);
96 void CertificateResourceHandler::OnResponseCompleted(
97 const net::URLRequestStatus
& urs
,
98 const std::string
& sec_info
,
100 if (urs
.status() != net::URLRequestStatus::SUCCESS
)
103 // Note that it's up to the browser to verify that the certificate
104 // data is well-formed.
105 const ResourceRequestInfo
* info
= GetRequestInfo();
106 GetContentClient()->browser()->AddCertificate(
107 cert_type_
, buffer_
->StartOfBuffer(),
108 static_cast<size_t>(buffer_
->offset()), info
->GetChildID(),
109 info
->GetRenderFrameID());
112 void CertificateResourceHandler::OnDataDownloaded(int bytes_downloaded
) {
116 } // namespace content