[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / loader / certificate_resource_handler.cc
blobfd49181f8daabc464260813a3787fafdd3178240
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 <limits.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"
17 namespace content {
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,
31 bool* defer) {
32 return true;
35 bool CertificateResourceHandler::OnResponseStarted(ResourceResponse* resp,
36 bool* defer) {
37 cert_type_ =
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) {
43 return true;
46 bool CertificateResourceHandler::OnBeforeNetworkStart(const GURL& url,
47 bool* defer) {
48 return true;
51 bool CertificateResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
52 int* buf_size,
53 int min_size) {
54 static const int kInitialBufferSizeInBytes = 32768;
55 static const int kMaxCertificateSizeInBytes = 1024 * 1024;
57 // TODO(gauravsh): Should we use 'min_size' here?
58 DCHECK(buf);
59 DCHECK(buf_size);
61 if (buffer_->capacity() == 0) {
62 buffer_->SetCapacity(kInitialBufferSizeInBytes);
63 } else if (buffer_->RemainingCapacity() == 0) {
64 int capacity = buffer_->capacity();
65 if (capacity >= kMaxCertificateSizeInBytes)
66 return false;
67 static_assert(kMaxCertificateSizeInBytes < INT_MAX / 2,
68 "The size limit ensures the capacity remains in bounds.");
69 capacity *= 2;
70 if (capacity > kMaxCertificateSizeInBytes)
71 capacity = kMaxCertificateSizeInBytes;
72 buffer_->SetCapacity(capacity);
75 *buf = buffer_.get();
76 *buf_size = buffer_->RemainingCapacity();
78 return true;
81 bool CertificateResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
82 DCHECK_LE(0, bytes_read);
83 DCHECK_LE(bytes_read, buffer_->RemainingCapacity());
84 if (!bytes_read)
85 return true;
87 buffer_->set_offset(buffer_->offset() + bytes_read);
88 return true;
91 void CertificateResourceHandler::OnResponseCompleted(
92 const net::URLRequestStatus& urs,
93 const std::string& sec_info,
94 bool* defer) {
95 if (urs.status() != net::URLRequestStatus::SUCCESS)
96 return;
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) {
108 NOTREACHED();
111 } // namespace content