Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / tools / quic / quic_in_memory_cache.h
blobe10e02d1b2e6e07629df089d5a81429b5fdea80b
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.
5 #ifndef NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_
6 #define NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_
8 #include <string>
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/singleton.h"
12 #include "base/strings/string_piece.h"
13 #include "net/spdy/spdy_framer.h"
15 template <typename Type> struct DefaultSingletonTraits;
17 namespace net {
18 namespace tools {
20 namespace test {
21 class QuicInMemoryCachePeer;
22 } // namespace test
24 class QuicServer;
26 // In-memory cache for HTTP responses.
27 // Reads from disk cache generated by:
28 // `wget -p --save_headers <url>`
29 class QuicInMemoryCache {
30 public:
31 enum SpecialResponseType {
32 REGULAR_RESPONSE, // Send the headers and body like a server should.
33 CLOSE_CONNECTION, // Close the connection (sending the close packet).
34 IGNORE_REQUEST, // Do nothing, expect the client to time out.
37 // Container for response header/body pairs.
38 class Response {
39 public:
40 Response();
41 ~Response();
43 SpecialResponseType response_type() const { return response_type_; }
44 const SpdyHeaderBlock& headers() const { return headers_; }
45 const StringPiece body() const { return StringPiece(body_); }
47 void set_response_type(SpecialResponseType response_type) {
48 response_type_ = response_type;
50 void set_headers(const SpdyHeaderBlock& headers) {
51 headers_ = headers;
53 void set_body(base::StringPiece body) {
54 body.CopyToString(&body_);
57 private:
58 SpecialResponseType response_type_;
59 SpdyHeaderBlock headers_;
60 std::string body_;
62 DISALLOW_COPY_AND_ASSIGN(Response);
65 // Returns the singleton instance of the cache.
66 static QuicInMemoryCache* GetInstance();
68 // Retrieve a response from this cache for a given host and path..
69 // If no appropriate response exists, nullptr is returned.
70 const Response* GetResponse(base::StringPiece host,
71 base::StringPiece path) const;
73 // Adds a simple response to the cache. The response headers will
74 // only contain the "content-length" header with the length of |body|.
75 void AddSimpleResponse(base::StringPiece host,
76 base::StringPiece path,
77 int response_code,
78 base::StringPiece response_detail,
79 base::StringPiece body);
81 // Add a response to the cache.
82 void AddResponse(base::StringPiece host,
83 base::StringPiece path,
84 const SpdyHeaderBlock& response_headers,
85 base::StringPiece response_body);
87 // Simulate a special behavior at a particular path.
88 void AddSpecialResponse(base::StringPiece host,
89 base::StringPiece path,
90 SpecialResponseType response_type);
92 // Sets a default response in case of cache misses. Takes ownership of
93 // 'response'.
94 void AddDefaultResponse(Response* response);
96 // |cache_cirectory| can be generated using `wget -p --save-headers <url>`.
97 void InitializeFromDirectory(const std::string& cache_directory);
99 private:
100 typedef base::hash_map<std::string, Response*> ResponseMap;
102 friend struct DefaultSingletonTraits<QuicInMemoryCache>;
103 friend class test::QuicInMemoryCachePeer;
105 QuicInMemoryCache();
106 ~QuicInMemoryCache();
108 void ResetForTests();
110 void AddResponseImpl(base::StringPiece host,
111 base::StringPiece path,
112 SpecialResponseType response_type,
113 const SpdyHeaderBlock& response_headers,
114 base::StringPiece response_body);
116 std::string GetKey(base::StringPiece host, base::StringPiece path) const;
118 // Cached responses.
119 ResponseMap responses_;
121 // The default response for cache misses, if set.
122 scoped_ptr<Response> default_response_;
124 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache);
127 } // namespace tools
128 } // namespace net
130 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_