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
18namespace net {
19class IOBuffer;
20class URLRequest;
21class URLRequestStatus;
22}  // namespace net
23
24namespace content {
25
26// This class handles certificate mime types such as:
27// - "application/x-x509-user-cert"
28// - "application/x-x509-ca-cert"
29// - "application/x-pkcs12"
30//
31class CertificateResourceHandler : public ResourceHandler {
32 public:
33  explicit CertificateResourceHandler(net::URLRequest* request);
34  virtual ~CertificateResourceHandler();
35
36  virtual bool OnUploadProgress(uint64 position, uint64 size) OVERRIDE;
37
38  // Not needed, as this event handler ought to be the final resource.
39  virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
40                                   ResourceResponse* resp,
41                                   bool* defer) OVERRIDE;
42
43  // Check if this indeed an X509 cert.
44  virtual bool OnResponseStarted(ResourceResponse* resp,
45                                 bool* defer) OVERRIDE;
46
47  // Pass-through implementation.
48  virtual bool OnWillStart(const GURL& url, bool* defer) OVERRIDE;
49
50  // Pass-through implementation.
51  virtual bool OnBeforeNetworkStart(const GURL& url, bool* defer) OVERRIDE;
52
53  // Create a new buffer to store received data.
54  virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
55                          int* buf_size,
56                          int min_size) OVERRIDE;
57
58  // A read was completed, maybe allocate a new buffer for further data.
59  virtual bool OnReadCompleted(int bytes_read, bool* defer) OVERRIDE;
60
61  // Done downloading the certificate.
62  virtual void OnResponseCompleted(const net::URLRequestStatus& urs,
63                                   const std::string& sec_info,
64                                   bool* defer) OVERRIDE;
65
66  // N/A to cert downloading.
67  virtual void OnDataDownloaded(int bytes_downloaded) OVERRIDE;
68
69 private:
70  typedef std::vector<std::pair<scoped_refptr<net::IOBuffer>,
71                                size_t> > ContentVector;
72
73  void AssembleResource();
74
75  size_t content_length_;
76  ContentVector buffer_;
77  scoped_refptr<net::IOBuffer> read_buffer_;
78  scoped_refptr<net::IOBuffer> resource_buffer_;  // Downloaded certificate.
79  net::CertificateMimeType cert_type_;
80  DISALLOW_COPY_AND_ASSIGN(CertificateResourceHandler);
81};
82
83}  // namespace content
84
85#endif  // CONTENT_BROWSER_LOADER_CERTIFICATE_RESOURCE_HANDLER_H_
86