OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/loader/certificate_resource_handler.h" | 5 #include "content/browser/loader/certificate_resource_handler.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "content/browser/loader/resource_request_info_impl.h" | 8 #include "content/browser/loader/resource_request_info_impl.h" |
9 #include "content/public/browser/content_browser_client.h" | 9 #include "content/public/browser/content_browser_client.h" |
10 #include "content/public/common/resource_response.h" | 10 #include "content/public/common/resource_response.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 } | 61 } |
62 | 62 |
63 bool CertificateResourceHandler::OnWillRead(int request_id, | 63 bool CertificateResourceHandler::OnWillRead(int request_id, |
64 net::IOBuffer** buf, | 64 net::IOBuffer** buf, |
65 int* buf_size, | 65 int* buf_size, |
66 int min_size) { | 66 int min_size) { |
67 static const int kReadBufSize = 32768; | 67 static const int kReadBufSize = 32768; |
68 | 68 |
69 // TODO(gauravsh): Should we use 'min_size' here? | 69 // TODO(gauravsh): Should we use 'min_size' here? |
70 DCHECK(buf && buf_size); | 70 DCHECK(buf && buf_size); |
71 if (!read_buffer_) { | 71 if (!read_buffer_.get()) { |
72 read_buffer_ = new net::IOBuffer(kReadBufSize); | 72 read_buffer_ = new net::IOBuffer(kReadBufSize); |
73 } | 73 } |
74 *buf = read_buffer_.get(); | 74 *buf = read_buffer_.get(); |
75 *buf_size = kReadBufSize; | 75 *buf_size = kReadBufSize; |
76 | 76 |
77 return true; | 77 return true; |
78 } | 78 } |
79 | 79 |
80 bool CertificateResourceHandler::OnReadCompleted(int request_id, | 80 bool CertificateResourceHandler::OnReadCompleted(int request_id, |
81 int bytes_read, | 81 int bytes_read, |
82 bool* defer) { | 82 bool* defer) { |
83 if (!bytes_read) | 83 if (!bytes_read) |
84 return true; | 84 return true; |
85 | 85 |
86 // We have more data to read. | 86 // We have more data to read. |
87 DCHECK(read_buffer_); | 87 DCHECK(read_buffer_.get()); |
88 content_length_ += bytes_read; | 88 content_length_ += bytes_read; |
89 | 89 |
90 // Release the ownership of the buffer, and store a reference | 90 // Release the ownership of the buffer, and store a reference |
91 // to it. A new one will be allocated in OnWillRead(). | 91 // to it. A new one will be allocated in OnWillRead(). |
92 net::IOBuffer* buffer = NULL; | 92 net::IOBuffer* buffer = NULL; |
93 read_buffer_.swap(&buffer); | 93 read_buffer_.swap(&buffer); |
94 // TODO(gauravsh): Should this be handled by a separate thread? | 94 // TODO(gauravsh): Should this be handled by a separate thread? |
95 buffer_.push_back(std::make_pair(buffer, bytes_read)); | 95 buffer_.push_back(std::make_pair(buffer, bytes_read)); |
96 | 96 |
97 return true; | 97 return true; |
98 } | 98 } |
99 | 99 |
100 bool CertificateResourceHandler::OnResponseCompleted( | 100 bool CertificateResourceHandler::OnResponseCompleted( |
101 int request_id, | 101 int request_id, |
102 const net::URLRequestStatus& urs, | 102 const net::URLRequestStatus& urs, |
103 const std::string& sec_info) { | 103 const std::string& sec_info) { |
104 if (urs.status() != net::URLRequestStatus::SUCCESS) | 104 if (urs.status() != net::URLRequestStatus::SUCCESS) |
105 return false; | 105 return false; |
106 | 106 |
107 AssembleResource(); | 107 AssembleResource(); |
108 | 108 |
109 const void* content_bytes = NULL; | 109 const void* content_bytes = NULL; |
110 if (resource_buffer_) | 110 if (resource_buffer_.get()) |
111 content_bytes = resource_buffer_->data(); | 111 content_bytes = resource_buffer_->data(); |
112 | 112 |
113 // Note that it's up to the browser to verify that the certificate | 113 // Note that it's up to the browser to verify that the certificate |
114 // data is well-formed. | 114 // data is well-formed. |
115 GetContentClient()->browser()->AddCertificate( | 115 GetContentClient()->browser()->AddCertificate( |
116 request_, cert_type_, content_bytes, content_length_, | 116 request_, cert_type_, content_bytes, content_length_, |
117 render_process_host_id_, render_view_id_); | 117 render_process_host_id_, render_view_id_); |
118 | 118 |
119 return true; | 119 return true; |
120 } | 120 } |
121 | 121 |
122 void CertificateResourceHandler::AssembleResource() { | 122 void CertificateResourceHandler::AssembleResource() { |
123 // 0-length IOBuffers are not allowed. | 123 // 0-length IOBuffers are not allowed. |
124 if (content_length_ == 0) { | 124 if (content_length_ == 0) { |
125 resource_buffer_ = NULL; | 125 resource_buffer_ = NULL; |
126 return; | 126 return; |
127 } | 127 } |
128 | 128 |
129 // Create the new buffer. | 129 // Create the new buffer. |
130 resource_buffer_ = new net::IOBuffer(content_length_); | 130 resource_buffer_ = new net::IOBuffer(content_length_); |
131 | 131 |
132 // Copy the data into it. | 132 // Copy the data into it. |
133 size_t bytes_copied = 0; | 133 size_t bytes_copied = 0; |
134 for (size_t i = 0; i < buffer_.size(); ++i) { | 134 for (size_t i = 0; i < buffer_.size(); ++i) { |
135 net::IOBuffer* data = buffer_[i].first; | 135 net::IOBuffer* data = buffer_[i].first.get(); |
136 size_t data_len = buffer_[i].second; | 136 size_t data_len = buffer_[i].second; |
137 DCHECK(data != NULL); | 137 DCHECK(data != NULL); |
138 DCHECK_LE(bytes_copied + data_len, content_length_); | 138 DCHECK_LE(bytes_copied + data_len, content_length_); |
139 memcpy(resource_buffer_->data() + bytes_copied, data->data(), data_len); | 139 memcpy(resource_buffer_->data() + bytes_copied, data->data(), data_len); |
140 bytes_copied += data_len; | 140 bytes_copied += data_len; |
141 } | 141 } |
142 DCHECK_EQ(content_length_, bytes_copied); | 142 DCHECK_EQ(content_length_, bytes_copied); |
143 } | 143 } |
144 | 144 |
145 void CertificateResourceHandler::OnDataDownloaded( | 145 void CertificateResourceHandler::OnDataDownloaded( |
146 int request_id, | 146 int request_id, |
147 int bytes_downloaded) { | 147 int bytes_downloaded) { |
148 NOTREACHED(); | 148 NOTREACHED(); |
149 } | 149 } |
150 | 150 |
151 } // namespace content | 151 } // namespace content |
OLD | NEW |