Roll ANGLE e754fb8..6ffeb74
[chromium-blink-merge.git] / content / browser / loader / certificate_resource_handler.cc
blobb6226ca55107a8b864901f0d2af4ddfc76ba9810
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::OnUploadProgress(uint64 position,
29 uint64 size) {
30 return true;
33 bool CertificateResourceHandler::OnRequestRedirected(
34 const net::RedirectInfo& redirect_info,
35 ResourceResponse* resp,
36 bool* defer) {
37 return true;
40 bool CertificateResourceHandler::OnResponseStarted(ResourceResponse* resp,
41 bool* defer) {
42 cert_type_ =
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) {
48 return true;
51 bool CertificateResourceHandler::OnBeforeNetworkStart(const GURL& url,
52 bool* defer) {
53 return true;
56 bool CertificateResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
57 int* buf_size,
58 int min_size) {
59 static const int kInitialBufferSizeInBytes = 32768;
60 static const int kMaxCertificateSizeInBytes = 1024 * 1024;
62 // TODO(gauravsh): Should we use 'min_size' here?
63 DCHECK(buf);
64 DCHECK(buf_size);
66 if (buffer_->capacity() == 0) {
67 buffer_->SetCapacity(kInitialBufferSizeInBytes);
68 } else if (buffer_->RemainingCapacity() == 0) {
69 int capacity = buffer_->capacity();
70 if (capacity >= kMaxCertificateSizeInBytes)
71 return false;
72 static_assert(kMaxCertificateSizeInBytes < INT_MAX / 2,
73 "The size limit ensures the capacity remains in bounds.");
74 capacity *= 2;
75 if (capacity > kMaxCertificateSizeInBytes)
76 capacity = kMaxCertificateSizeInBytes;
77 buffer_->SetCapacity(capacity);
80 *buf = buffer_.get();
81 *buf_size = buffer_->RemainingCapacity();
83 return true;
86 bool CertificateResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
87 DCHECK_LE(0, bytes_read);
88 DCHECK_LE(bytes_read, buffer_->RemainingCapacity());
89 if (!bytes_read)
90 return true;
92 buffer_->set_offset(buffer_->offset() + bytes_read);
93 return true;
96 void CertificateResourceHandler::OnResponseCompleted(
97 const net::URLRequestStatus& urs,
98 const std::string& sec_info,
99 bool* defer) {
100 if (urs.status() != net::URLRequestStatus::SUCCESS)
101 return;
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) {
113 NOTREACHED();
116 } // namespace content