• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   explicit CertificateResourceHandler(net::URLRequest* request);
35   virtual ~CertificateResourceHandler();
36 
37   virtual bool OnUploadProgress(int request_id,
38                                 uint64 position,
39                                 uint64 size) OVERRIDE;
40 
41   // Not needed, as this event handler ought to be the final resource.
42   virtual bool OnRequestRedirected(int request_id,
43                                    const GURL& url,
44                                    ResourceResponse* resp,
45                                    bool* defer) OVERRIDE;
46 
47   // Check if this indeed an X509 cert.
48   virtual bool OnResponseStarted(int request_id,
49                                  ResourceResponse* resp,
50                                  bool* defer) OVERRIDE;
51 
52   // Pass-through implementation.
53   virtual bool OnWillStart(int request_id,
54                            const GURL& url,
55                            bool* defer) OVERRIDE;
56 
57   // Create a new buffer to store received data.
58   virtual bool OnWillRead(int request_id,
59                           scoped_refptr<net::IOBuffer>* buf,
60                           int* buf_size,
61                           int min_size) OVERRIDE;
62 
63   // A read was completed, maybe allocate a new buffer for further data.
64   virtual bool OnReadCompleted(int request_id,
65                                int bytes_read,
66                                bool* defer) OVERRIDE;
67 
68   // Done downloading the certificate.
69   virtual void OnResponseCompleted(int request_id,
70                                    const net::URLRequestStatus& urs,
71                                    const std::string& sec_info,
72                                    bool* defer) OVERRIDE;
73 
74   // N/A to cert downloading.
75   virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE;
76 
77  private:
78   typedef std::vector<std::pair<scoped_refptr<net::IOBuffer>,
79                                 size_t> > ContentVector;
80 
81   void AssembleResource();
82 
83   GURL url_;
84   size_t content_length_;
85   ContentVector buffer_;
86   scoped_refptr<net::IOBuffer> read_buffer_;
87   scoped_refptr<net::IOBuffer> resource_buffer_;  // Downloaded certificate.
88   net::CertificateMimeType cert_type_;
89   DISALLOW_COPY_AND_ASSIGN(CertificateResourceHandler);
90 };
91 
92 }  // namespace content
93 
94 #endif  // CONTENT_BROWSER_LOADER_CERTIFICATE_RESOURCE_HANDLER_H_
95