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 #ifndef CONTENT_BROWSER_LOADER_CERTIFICATE_RESOURCE_HANDLER_H_ 6 #define CONTENT_BROWSER_LOADER_CERTIFICATE_RESOURCE_HANDLER_H_ 7 8 #include <string> 9 #include <utility> 10 #include <vector> 11 12 #include "base/compiler_specific.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "content/browser/loader/resource_handler.h" 16 #include "net/base/mime_util.h" 17 #include "url/gurl.h" 18 19 namespace net { 20 class IOBuffer; 21 class URLRequest; 22 class URLRequestStatus; 23 } // namespace net 24 25 namespace content { 26 27 // This class handles certificate mime types such as: 28 // - "application/x-x509-user-cert" 29 // - "application/x-x509-ca-cert" 30 // - "application/x-pkcs12" 31 // 32 class CertificateResourceHandler : public ResourceHandler { 33 public: 34 CertificateResourceHandler(net::URLRequest* request, 35 int render_process_host_id, 36 int render_view_id); 37 virtual ~CertificateResourceHandler(); 38 39 virtual bool OnUploadProgress(int request_id, 40 uint64 position, 41 uint64 size) OVERRIDE; 42 43 // Not needed, as this event handler ought to be the final resource. 44 virtual bool OnRequestRedirected(int request_id, 45 const GURL& url, 46 ResourceResponse* resp, 47 bool* defer) OVERRIDE; 48 49 // Check if this indeed an X509 cert. 50 virtual bool OnResponseStarted(int request_id, 51 ResourceResponse* resp, 52 bool* defer) OVERRIDE; 53 54 // Pass-through implementation. 55 virtual bool OnWillStart(int request_id, 56 const GURL& url, 57 bool* defer) OVERRIDE; 58 59 // Create a new buffer to store received data. 60 virtual bool OnWillRead(int request_id, 61 net::IOBuffer** buf, 62 int* buf_size, 63 int min_size) OVERRIDE; 64 65 // A read was completed, maybe allocate a new buffer for further data. 66 virtual bool OnReadCompleted(int request_id, 67 int bytes_read, 68 bool* defer) OVERRIDE; 69 70 // Done downloading the certificate. 71 virtual bool OnResponseCompleted(int request_id, 72 const net::URLRequestStatus& urs, 73 const std::string& sec_info) OVERRIDE; 74 75 // N/A to cert downloading. 76 virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE; 77 78 private: 79 typedef std::vector<std::pair<scoped_refptr<net::IOBuffer>, 80 size_t> > ContentVector; 81 82 void AssembleResource(); 83 84 GURL url_; 85 net::URLRequest* request_; 86 size_t content_length_; 87 ContentVector buffer_; 88 scoped_refptr<net::IOBuffer> read_buffer_; 89 scoped_refptr<net::IOBuffer> resource_buffer_; // Downloaded certificate. 90 // The id of the |RenderProcessHost| which started the download. 91 int render_process_host_id_; 92 // The id of the |RenderView| which started the download. 93 int render_view_id_; 94 net::CertificateMimeType cert_type_; 95 DISALLOW_COPY_AND_ASSIGN(CertificateResourceHandler); 96 }; 97 98 } // namespace content 99 100 #endif // CONTENT_BROWSER_LOADER_CERTIFICATE_RESOURCE_HANDLER_H_ 101