Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / loader / certificate_resource_handler.cc
blob4545f346fad594c10ae9e672e9b61fa706fe8c34
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 "base/strings/string_util.h"
8 #include "content/browser/loader/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/http/http_response_headers.h"
15 #include "net/url_request/url_request.h"
16 #include "net/url_request/url_request_status.h"
18 namespace content {
20 CertificateResourceHandler::CertificateResourceHandler(
21 net::URLRequest* request)
22 : ResourceHandler(request),
23 content_length_(0),
24 read_buffer_(NULL),
25 resource_buffer_(NULL),
26 cert_type_(net::CERTIFICATE_MIME_TYPE_UNKNOWN) {
29 CertificateResourceHandler::~CertificateResourceHandler() {
32 bool CertificateResourceHandler::OnUploadProgress(int request_id,
33 uint64 position,
34 uint64 size) {
35 return true;
38 bool CertificateResourceHandler::OnRequestRedirected(int request_id,
39 const GURL& url,
40 ResourceResponse* resp,
41 bool* defer) {
42 url_ = url;
43 return true;
46 bool CertificateResourceHandler::OnResponseStarted(int request_id,
47 ResourceResponse* resp,
48 bool* defer) {
49 cert_type_ = net::GetCertificateMimeTypeForMimeType(resp->head.mime_type);
50 return cert_type_ != net::CERTIFICATE_MIME_TYPE_UNKNOWN;
53 bool CertificateResourceHandler::OnWillStart(int request_id,
54 const GURL& url,
55 bool* defer) {
56 return true;
59 bool CertificateResourceHandler::OnBeforeNetworkStart(int request_id,
60 const GURL& url,
61 bool* defer) {
62 return true;
65 bool CertificateResourceHandler::OnWillRead(int request_id,
66 scoped_refptr<net::IOBuffer>* buf,
67 int* buf_size,
68 int min_size) {
69 static const int kReadBufSize = 32768;
71 // TODO(gauravsh): Should we use 'min_size' here?
72 DCHECK(buf && buf_size);
73 if (!read_buffer_.get()) {
74 read_buffer_ = new net::IOBuffer(kReadBufSize);
76 *buf = read_buffer_.get();
77 *buf_size = kReadBufSize;
79 return true;
82 bool CertificateResourceHandler::OnReadCompleted(int request_id,
83 int bytes_read,
84 bool* defer) {
85 if (!bytes_read)
86 return true;
88 // We have more data to read.
89 DCHECK(read_buffer_.get());
90 content_length_ += bytes_read;
92 // Release the ownership of the buffer, and store a reference
93 // to it. A new one will be allocated in OnWillRead().
94 net::IOBuffer* buffer = NULL;
95 read_buffer_.swap(&buffer);
96 // TODO(gauravsh): Should this be handled by a separate thread?
97 buffer_.push_back(std::make_pair(buffer, bytes_read));
99 return true;
102 void CertificateResourceHandler::OnResponseCompleted(
103 int request_id,
104 const net::URLRequestStatus& urs,
105 const std::string& sec_info,
106 bool* defer) {
107 if (urs.status() != net::URLRequestStatus::SUCCESS)
108 return;
110 AssembleResource();
112 const void* content_bytes = NULL;
113 if (resource_buffer_.get())
114 content_bytes = resource_buffer_->data();
116 // Note that it's up to the browser to verify that the certificate
117 // data is well-formed.
118 const ResourceRequestInfo* info = GetRequestInfo();
119 GetContentClient()->browser()->AddCertificate(
120 request(), cert_type_, content_bytes, content_length_,
121 info->GetChildID(), info->GetRouteID());
124 void CertificateResourceHandler::AssembleResource() {
125 // 0-length IOBuffers are not allowed.
126 if (content_length_ == 0) {
127 resource_buffer_ = NULL;
128 return;
131 // Create the new buffer.
132 resource_buffer_ = new net::IOBuffer(content_length_);
134 // Copy the data into it.
135 size_t bytes_copied = 0;
136 for (size_t i = 0; i < buffer_.size(); ++i) {
137 net::IOBuffer* data = buffer_[i].first.get();
138 size_t data_len = buffer_[i].second;
139 DCHECK(data != NULL);
140 DCHECK_LE(bytes_copied + data_len, content_length_);
141 memcpy(resource_buffer_->data() + bytes_copied, data->data(), data_len);
142 bytes_copied += data_len;
144 DCHECK_EQ(content_length_, bytes_copied);
147 void CertificateResourceHandler::OnDataDownloaded(
148 int request_id,
149 int bytes_downloaded) {
150 NOTREACHED();
153 } // namespace content