1// Copyright (c) 2011 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 NET_HTTP_DISK_CACHE_BASED_SSL_HOST_INFO_H_
6#define NET_HTTP_DISK_CACHE_BASED_SSL_HOST_INFO_H_
7
8#include <string>
9
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "base/threading/non_thread_safe.h"
13#include "net/base/completion_callback.h"
14#include "net/disk_cache/disk_cache.h"
15#include "net/socket/ssl_host_info.h"
16
17namespace net {
18
19class HttpCache;
20class IOBuffer;
21struct SSLConfig;
22
23// DiskCacheBasedSSLHostInfo fetches information about an SSL host from our
24// standard disk cache. Since the information is defined to be non-sensitive,
25// it's ok for us to keep it on disk.
26class DiskCacheBasedSSLHostInfo : public SSLHostInfo,
27                                  public base::NonThreadSafe {
28 public:
29  DiskCacheBasedSSLHostInfo(const std::string& hostname,
30                            const SSLConfig& ssl_config,
31                            CertVerifier* cert_verifier,
32                            HttpCache* http_cache);
33
34  // Implementation of SSLHostInfo
35  virtual void Start();
36  virtual int WaitForDataReady(CompletionCallback* callback);
37  virtual void Persist();
38
39 private:
40  enum State {
41    GET_BACKEND,
42    GET_BACKEND_COMPLETE,
43    OPEN,
44    OPEN_COMPLETE,
45    READ,
46    READ_COMPLETE,
47    WAIT_FOR_DATA_READY_DONE,
48    CREATE,
49    CREATE_COMPLETE,
50    WRITE,
51    WRITE_COMPLETE,
52    SET_DONE,
53    NONE,
54  };
55
56  class CallbackImpl : public CallbackRunner<Tuple1<int> > {
57   public:
58    CallbackImpl(const base::WeakPtr<DiskCacheBasedSSLHostInfo>& obj,
59                 void (DiskCacheBasedSSLHostInfo::*meth)(int));
60    virtual ~CallbackImpl();
61
62    disk_cache::Backend** backend_pointer() { return &backend_; }
63    disk_cache::Entry** entry_pointer() { return &entry_; }
64    disk_cache::Backend* backend() const { return backend_; }
65    disk_cache::Entry* entry() const { return entry_; }
66
67    // CallbackRunner<Tuple1<int> >:
68    virtual void RunWithParams(const Tuple1<int>& params);
69
70   private:
71    base::WeakPtr<DiskCacheBasedSSLHostInfo> obj_;
72    void (DiskCacheBasedSSLHostInfo::*meth_)(int);
73
74    disk_cache::Backend* backend_;
75    disk_cache::Entry* entry_;
76  };
77
78  virtual ~DiskCacheBasedSSLHostInfo();
79
80  std::string key() const;
81
82  void DoLoop(int rv);
83
84  int DoGetBackendComplete(int rv);
85  int DoOpenComplete(int rv);
86  int DoReadComplete(int rv);
87  int DoWriteComplete(int rv);
88  int DoCreateComplete(int rv);
89
90  int DoGetBackend();
91  int DoOpen();
92  int DoRead();
93  int DoWrite();
94  int DoCreate();
95
96  // WaitForDataReadyDone is the terminal state of the read operation.
97  int WaitForDataReadyDone();
98
99  // SetDone is the terminal state of the write operation.
100  int SetDone();
101
102  // IsCallbackPending returns true if we have a pending callback.
103  bool IsCallbackPending() const;
104
105  base::WeakPtrFactory<DiskCacheBasedSSLHostInfo> weak_ptr_factory_;
106  CallbackImpl* callback_;
107  State state_;
108  bool ready_;
109  std::string new_data_;
110  const std::string hostname_;
111  HttpCache* const http_cache_;
112  disk_cache::Backend* backend_;
113  disk_cache::Entry* entry_;
114  CompletionCallback* user_callback_;
115  scoped_refptr<net::IOBuffer> read_buffer_;
116  scoped_refptr<net::IOBuffer> write_buffer_;
117  std::string data_;
118};
119
120}  // namespace net
121
122#endif  // NET_HTTP_DISK_CACHE_BASED_SSL_HOST_INFO_H_
123