| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/browser/loader/certificate_resource_handler.h" | |
| 6 | |
| 7 #include <limits.h> | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include "components/mime_util/mime_util.h" | |
| 11 #include "content/browser/loader/resource_request_info_impl.h" | |
| 12 #include "content/public/browser/content_browser_client.h" | |
| 13 #include "content/public/common/resource_response.h" | |
| 14 #include "net/base/io_buffer.h" | |
| 15 #include "net/url_request/url_request.h" | |
| 16 #include "net/url_request/url_request_status.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 CertificateResourceHandler::CertificateResourceHandler(net::URLRequest* request) | |
| 21 : ResourceHandler(request), | |
| 22 buffer_(new net::GrowableIOBuffer), | |
| 23 cert_type_(net::CERTIFICATE_MIME_TYPE_UNKNOWN) { | |
| 24 } | |
| 25 | |
| 26 CertificateResourceHandler::~CertificateResourceHandler() { | |
| 27 } | |
| 28 | |
| 29 bool CertificateResourceHandler::OnRequestRedirected( | |
| 30 const net::RedirectInfo& redirect_info, | |
| 31 ResourceResponse* resp, | |
| 32 bool* defer) { | |
| 33 return true; | |
| 34 } | |
| 35 | |
| 36 bool CertificateResourceHandler::OnResponseStarted(ResourceResponse* resp, | |
| 37 bool* defer) { | |
| 38 cert_type_ = | |
| 39 mime_util::GetCertificateMimeTypeForMimeType(resp->head.mime_type); | |
| 40 return cert_type_ != net::CERTIFICATE_MIME_TYPE_UNKNOWN; | |
| 41 } | |
| 42 | |
| 43 bool CertificateResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
| 44 return true; | |
| 45 } | |
| 46 | |
| 47 bool CertificateResourceHandler::OnBeforeNetworkStart(const GURL& url, | |
| 48 bool* defer) { | |
| 49 return true; | |
| 50 } | |
| 51 | |
| 52 bool CertificateResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
| 53 int* buf_size, | |
| 54 int min_size) { | |
| 55 static const int kInitialBufferSizeInBytes = 32768; | |
| 56 static const int kMaxCertificateSizeInBytes = 1024 * 1024; | |
| 57 | |
| 58 // TODO(gauravsh): Should we use 'min_size' here? | |
| 59 DCHECK(buf); | |
| 60 DCHECK(buf_size); | |
| 61 | |
| 62 if (buffer_->capacity() == 0) { | |
| 63 buffer_->SetCapacity(kInitialBufferSizeInBytes); | |
| 64 } else if (buffer_->RemainingCapacity() == 0) { | |
| 65 int capacity = buffer_->capacity(); | |
| 66 if (capacity >= kMaxCertificateSizeInBytes) | |
| 67 return false; | |
| 68 static_assert(kMaxCertificateSizeInBytes < INT_MAX / 2, | |
| 69 "The size limit ensures the capacity remains in bounds."); | |
| 70 capacity *= 2; | |
| 71 if (capacity > kMaxCertificateSizeInBytes) | |
| 72 capacity = kMaxCertificateSizeInBytes; | |
| 73 buffer_->SetCapacity(capacity); | |
| 74 } | |
| 75 | |
| 76 *buf = buffer_.get(); | |
| 77 *buf_size = buffer_->RemainingCapacity(); | |
| 78 | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 bool CertificateResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
| 83 DCHECK_LE(0, bytes_read); | |
| 84 DCHECK_LE(bytes_read, buffer_->RemainingCapacity()); | |
| 85 if (!bytes_read) | |
| 86 return true; | |
| 87 | |
| 88 buffer_->set_offset(buffer_->offset() + bytes_read); | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 void CertificateResourceHandler::OnResponseCompleted( | |
| 93 const net::URLRequestStatus& urs, | |
| 94 const std::string& sec_info, | |
| 95 bool* defer) { | |
| 96 if (urs.status() != net::URLRequestStatus::SUCCESS) | |
| 97 return; | |
| 98 | |
| 99 // Note that it's up to the browser to verify that the certificate | |
| 100 // data is well-formed. | |
| 101 const ResourceRequestInfo* info = GetRequestInfo(); | |
| 102 GetContentClient()->browser()->AddCertificate( | |
| 103 cert_type_, buffer_->StartOfBuffer(), | |
| 104 static_cast<size_t>(buffer_->offset()), info->GetChildID(), | |
| 105 info->GetRenderFrameID()); | |
| 106 } | |
| 107 | |
| 108 void CertificateResourceHandler::OnDataDownloaded(int bytes_downloaded) { | |
| 109 NOTREACHED(); | |
| 110 } | |
| 111 | |
| 112 } // namespace content | |
| OLD | NEW |