Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / quic / crypto / quic_server_info.h
1 // Copyright 2014 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_QUIC_CRYPTO_QUIC_SERVER_INFO_H_
6 #define NET_QUIC_CRYPTO_QUIC_SERVER_INFO_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/net_export.h"
16 #include "net/quic/quic_server_id.h"
17
18 namespace net {
19
20 class X509Certificate;
21
22 // QuicServerInfo is an interface for fetching information about a QUIC server.
23 // This information may be stored on disk so does not include keys or other
24 // sensitive information. Primarily it's intended for caching the QUIC server's
25 // crypto config.
26 class NET_EXPORT_PRIVATE QuicServerInfo {
27  public:
28   QuicServerInfo(const QuicServerId& server_id);
29   virtual ~QuicServerInfo();
30
31   // Start will commence the lookup. This must be called before any other
32   // methods. By opportunistically calling this early, it may be possible to
33   // overlap this object's lookup and reduce latency.
34   virtual void Start() = 0;
35
36   // WaitForDataReady returns OK if the fetch of the requested data has
37   // completed. Otherwise it returns ERR_IO_PENDING and will call |callback| on
38   // the current thread when ready.
39   //
40   // Only a single callback can be outstanding at a given time and, in the
41   // event that WaitForDataReady returns OK, it's the caller's responsibility
42   // to delete |callback|.
43   //
44   // |callback| may be NULL, in which case ERR_IO_PENDING may still be returned
45   // but, obviously, a callback will never be made.
46   virtual int WaitForDataReady(const CompletionCallback& callback) = 0;
47
48   // Cancel's WaitForDataReady callback. |callback| passed in WaitForDataReady
49   // will not be called.
50   virtual void CancelWaitForDataReadyCallback() = 0;
51
52   // Returns true if data is loaded from disk cache and ready (WaitForDataReady
53   // doesn't have a pending callback).
54   virtual bool IsDataReady() = 0;
55
56   // Returns true if the object is ready to persist data, in other words, if
57   // data is loaded from disk cache and ready and there are no pending writes.
58   virtual bool IsReadyToPersist() = 0;
59
60   // Persist allows for the server information to be updated for future users.
61   // This is a fire and forget operation: the caller may drop its reference
62   // from this object and the store operation will still complete. This can
63   // only be called once WaitForDataReady has returned OK or called its
64   // callback.
65   virtual void Persist() = 0;
66
67   // Called whenever an external cache reuses quic server config.
68   virtual void OnExternalCacheHit() = 0;
69
70   struct State {
71     State();
72     ~State();
73
74     void Clear();
75
76     // This class matches QuicClientCryptoConfig::CachedState.
77     std::string server_config;         // A serialized handshake message.
78     std::string source_address_token;  // An opaque proof of IP ownership.
79     std::vector<std::string> certs;    // A list of certificates in leaf-first
80                                        // order.
81     std::string server_config_sig;     // A signature of |server_config_|.
82
83    private:
84     DISALLOW_COPY_AND_ASSIGN(State);
85   };
86
87   // Once the data is ready, it can be read using the following members. These
88   // members can then be updated before calling |Persist|.
89   const State& state() const;
90   State* mutable_state();
91
92  protected:
93   // Parse parses pickled data and fills out the public member fields of this
94   // object. It returns true iff the parse was successful. The public member
95   // fields will be set to something sane in any case.
96   bool Parse(const std::string& data);
97   std::string Serialize();
98   State state_;
99
100  private:
101   // ParseInner is a helper function for Parse.
102   bool ParseInner(const std::string& data);
103
104   // SerializeInner is a helper function for Serialize.
105   std::string SerializeInner() const;
106
107   // This is the QUIC server (hostname, port, is_https, privacy_mode) tuple for
108   // which we restore the crypto_config.
109   const QuicServerId server_id_;
110
111   DISALLOW_COPY_AND_ASSIGN(QuicServerInfo);
112 };
113
114 class NET_EXPORT_PRIVATE QuicServerInfoFactory {
115  public:
116   QuicServerInfoFactory() {}
117   virtual ~QuicServerInfoFactory();
118
119   // GetForServer returns a fresh, allocated QuicServerInfo for the given
120   // |server_id| or NULL on failure.
121   virtual QuicServerInfo* GetForServer(const QuicServerId& server_id) = 0;
122
123   DISALLOW_COPY_AND_ASSIGN(QuicServerInfoFactory);
124 };
125
126 }  // namespace net
127
128 #endif  // NET_QUIC_CRYPTO_QUIC_SERVER_INFO_H_