Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / http / disk_cache_based_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_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_
6 #define NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_
7
8 #include <string>
9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/time/time.h"
14 #include "net/base/completion_callback.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "net/quic/crypto/quic_server_info.h"
17
18 namespace net {
19
20 class HttpCache;
21 class IOBuffer;
22 class QuicServerId;
23
24 // DiskCacheBasedQuicServerInfo fetches information about a QUIC server from
25 // our standard disk cache. Since the information is defined to be
26 // non-sensitive, it's ok for us to keep it on disk.
27 class NET_EXPORT_PRIVATE DiskCacheBasedQuicServerInfo
28     : public QuicServerInfo,
29       public NON_EXPORTED_BASE(base::NonThreadSafe) {
30  public:
31   DiskCacheBasedQuicServerInfo(const QuicServerId& server_id,
32                                HttpCache* http_cache);
33
34   // QuicServerInfo implementation.
35   void Start() override;
36   int WaitForDataReady(const CompletionCallback& callback) override;
37   void CancelWaitForDataReadyCallback() override;
38   bool IsDataReady() override;
39   bool IsReadyToPersist() override;
40   void Persist() override;
41   void OnExternalCacheHit() override;
42
43  private:
44   struct CacheOperationDataShim;
45
46   enum State {
47     GET_BACKEND,
48     GET_BACKEND_COMPLETE,
49     OPEN,
50     OPEN_COMPLETE,
51     READ,
52     READ_COMPLETE,
53     WAIT_FOR_DATA_READY_DONE,
54     CREATE_OR_OPEN,
55     CREATE_OR_OPEN_COMPLETE,
56     WRITE,
57     WRITE_COMPLETE,
58     SET_DONE,
59     NONE,
60   };
61
62   // Enum to track number of times data read/parse/write API calls of
63   // QuicServerInfo to and from disk cache is called.
64   enum QuicServerInfoAPICall {
65     QUIC_SERVER_INFO_START = 0,
66     QUIC_SERVER_INFO_WAIT_FOR_DATA_READY = 1,
67     QUIC_SERVER_INFO_PARSE = 2,
68     QUIC_SERVER_INFO_WAIT_FOR_DATA_READY_CANCEL = 3,
69     QUIC_SERVER_INFO_READY_TO_PERSIST = 4,
70     QUIC_SERVER_INFO_PERSIST = 5,
71     QUIC_SERVER_INFO_EXTERNAL_CACHE_HIT = 6,
72     QUIC_SERVER_INFO_NUM_OF_API_CALLS = 7,
73   };
74
75   // Enum to track failure reasons to read/load/write of QuicServerInfo to
76   // and from disk cache.
77   enum FailureReason {
78     WAIT_FOR_DATA_READY_INVALID_ARGUMENT_FAILURE = 0,
79     GET_BACKEND_FAILURE = 1,
80     OPEN_FAILURE = 2,
81     CREATE_OR_OPEN_FAILURE = 3,
82     PARSE_NO_DATA_FAILURE = 4,
83     PARSE_FAILURE = 5,
84     READ_FAILURE = 6,
85     READY_TO_PERSIST_FAILURE = 7,
86     PERSIST_NO_BACKEND_FAILURE = 8,
87     WRITE_FAILURE = 9,
88     NO_FAILURE = 10,
89     NUM_OF_FAILURES = 11,
90   };
91
92   ~DiskCacheBasedQuicServerInfo() override;
93
94   // Persists |pending_write_data_| if it is not empty, otherwise serializes the
95   // data and pesists it.
96   void PersistInternal();
97
98   std::string key() const;
99
100   // The |unused| parameter is a small hack so that we can have the
101   // CacheOperationDataShim object owned by the Callback that is created for
102   // this method.  See comment above CacheOperationDataShim for details.
103   void OnIOComplete(CacheOperationDataShim* unused, int rv);
104
105   int DoLoop(int rv);
106
107   int DoGetBackendComplete(int rv);
108   int DoOpenComplete(int rv);
109   int DoReadComplete(int rv);
110   int DoWriteComplete(int rv);
111   int DoCreateOrOpenComplete(int rv);
112
113   int DoGetBackend();
114   int DoOpen();
115   int DoRead();
116   int DoWrite();
117   int DoCreateOrOpen();
118
119   // DoWaitForDataReadyDone is the terminal state of the read operation.
120   int DoWaitForDataReadyDone();
121
122   // DoSetDone is the terminal state of the write operation.
123   int DoSetDone();
124
125   // Tracks in a histogram the number of times data read/parse/write API calls
126   // of QuicServerInfo to and from disk cache is called.
127   void RecordQuicServerInfoStatus(QuicServerInfoAPICall call);
128
129   // Tracks in a histogram the failure reasons to read/load/write of
130   // QuicServerInfo to and from disk cache. It also saves the |failure| in
131   // |last_failure_|.
132   void RecordQuicServerInfoFailure(FailureReason failure);
133
134   // Tracks in a histogram if |last_failure_| is not NO_FAILURE.
135   void RecordLastFailure();
136
137   CacheOperationDataShim* data_shim_;  // Owned by |io_callback_|.
138   CompletionCallback io_callback_;
139   State state_;
140   bool ready_;
141   bool found_entry_;  // Controls the behavior of DoCreateOrOpen.
142   std::string new_data_;
143   std::string pending_write_data_;
144   const QuicServerId server_id_;
145   HttpCache* const http_cache_;
146   disk_cache::Backend* backend_;
147   disk_cache::Entry* entry_;
148   CompletionCallback wait_for_ready_callback_;
149   scoped_refptr<IOBuffer> read_buffer_;
150   scoped_refptr<IOBuffer> write_buffer_;
151   std::string data_;
152   base::TimeTicks load_start_time_;
153   FailureReason last_failure_;
154
155   base::WeakPtrFactory<DiskCacheBasedQuicServerInfo> weak_factory_;
156
157   DISALLOW_COPY_AND_ASSIGN(DiskCacheBasedQuicServerInfo);
158 };
159
160 }  // namespace net
161
162 #endif  // NET_HTTP_DISK_CACHE_BASED_QUIC_SERVER_INFO_H_