| 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/renderer_host/x509_user_cert_resource_handler.h" | |
| 6 | |
| 7 #include "base/string_util.h" | |
| 8 #include "content/browser/renderer_host/resource_request_info_impl.h" | |
| 9 #include "content/public/browser/content_browser_client.h" | |
| 10 #include "content/public/common/resource_response.h" | |
| 11 #include "net/base/io_buffer.h" | |
| 12 #include "net/base/mime_sniffer.h" | |
| 13 #include "net/base/mime_util.h" | |
| 14 #include "net/base/x509_certificate.h" | |
| 15 #include "net/http/http_response_headers.h" | |
| 16 #include "net/url_request/url_request.h" | |
| 17 #include "net/url_request/url_request_status.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 X509UserCertResourceHandler::X509UserCertResourceHandler( | |
| 22 net::URLRequest* request, | |
| 23 int render_process_host_id, | |
| 24 int render_view_id) | |
| 25 : request_(request), | |
| 26 content_length_(0), | |
| 27 read_buffer_(NULL), | |
| 28 resource_buffer_(NULL), | |
| 29 render_process_host_id_(render_process_host_id), | |
| 30 render_view_id_(render_view_id) { | |
| 31 } | |
| 32 | |
| 33 X509UserCertResourceHandler::~X509UserCertResourceHandler() { | |
| 34 } | |
| 35 | |
| 36 bool X509UserCertResourceHandler::OnUploadProgress(int request_id, | |
| 37 uint64 position, | |
| 38 uint64 size) { | |
| 39 return true; | |
| 40 } | |
| 41 | |
| 42 bool X509UserCertResourceHandler::OnRequestRedirected(int request_id, | |
| 43 const GURL& url, | |
| 44 ResourceResponse* resp, | |
| 45 bool* defer) { | |
| 46 url_ = url; | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 bool X509UserCertResourceHandler::OnResponseStarted(int request_id, | |
| 51 ResourceResponse* resp, | |
| 52 bool* defer) { | |
| 53 return (resp->head.mime_type == "application/x-x509-user-cert"); | |
| 54 } | |
| 55 | |
| 56 bool X509UserCertResourceHandler::OnWillStart(int request_id, | |
| 57 const GURL& url, | |
| 58 bool* defer) { | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 bool X509UserCertResourceHandler::OnWillRead(int request_id, | |
| 63 net::IOBuffer** buf, | |
| 64 int* buf_size, | |
| 65 int min_size) { | |
| 66 static const int kReadBufSize = 32768; | |
| 67 | |
| 68 // TODO(gauravsh): Should we use 'min_size' here? | |
| 69 DCHECK(buf && buf_size); | |
| 70 if (!read_buffer_) { | |
| 71 read_buffer_ = new net::IOBuffer(kReadBufSize); | |
| 72 } | |
| 73 *buf = read_buffer_.get(); | |
| 74 *buf_size = kReadBufSize; | |
| 75 | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 bool X509UserCertResourceHandler::OnReadCompleted(int request_id, | |
| 80 int bytes_read, | |
| 81 bool* defer) { | |
| 82 if (!bytes_read) | |
| 83 return true; | |
| 84 | |
| 85 // We have more data to read. | |
| 86 DCHECK(read_buffer_); | |
| 87 content_length_ += bytes_read; | |
| 88 | |
| 89 // Release the ownership of the buffer, and store a reference | |
| 90 // to it. A new one will be allocated in OnWillRead(). | |
| 91 net::IOBuffer* buffer = NULL; | |
| 92 read_buffer_.swap(&buffer); | |
| 93 // TODO(gauravsh): Should this be handled by a separate thread? | |
| 94 buffer_.push_back(std::make_pair(buffer, bytes_read)); | |
| 95 | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 bool X509UserCertResourceHandler::OnResponseCompleted( | |
| 100 int request_id, | |
| 101 const net::URLRequestStatus& urs, | |
| 102 const std::string& sec_info) { | |
| 103 if (urs.status() != net::URLRequestStatus::SUCCESS) | |
| 104 return false; | |
| 105 | |
| 106 AssembleResource(); | |
| 107 scoped_refptr<net::X509Certificate> cert; | |
| 108 if (resource_buffer_) { | |
| 109 cert = net::X509Certificate::CreateFromBytes(resource_buffer_->data(), | |
| 110 content_length_); | |
| 111 } | |
| 112 GetContentClient()->browser()->AddNewCertificate( | |
| 113 request_, cert, render_process_host_id_, render_view_id_); | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 void X509UserCertResourceHandler::AssembleResource() { | |
| 118 // 0-length IOBuffers are not allowed. | |
| 119 if (content_length_ == 0) { | |
| 120 resource_buffer_ = NULL; | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 // Create the new buffer. | |
| 125 resource_buffer_ = new net::IOBuffer(content_length_); | |
| 126 | |
| 127 // Copy the data into it. | |
| 128 size_t bytes_copied = 0; | |
| 129 for (size_t i = 0; i < buffer_.size(); ++i) { | |
| 130 net::IOBuffer* data = buffer_[i].first; | |
| 131 size_t data_len = buffer_[i].second; | |
| 132 DCHECK(data != NULL); | |
| 133 DCHECK_LE(bytes_copied + data_len, content_length_); | |
| 134 memcpy(resource_buffer_->data() + bytes_copied, data->data(), data_len); | |
| 135 bytes_copied += data_len; | |
| 136 } | |
| 137 DCHECK_EQ(content_length_, bytes_copied); | |
| 138 } | |
| 139 | |
| 140 } // namespace content | |
| OLD | NEW |