summaryrefslogtreecommitdiff
path: root/chromium/net/spdy/http2_push_promise_index.h
blob: 1b92274622f8fe3295788d76f10926ff1a812b03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_SPDY_HTTP2_PUSH_PROMISE_INDEX_H_
#define NET_SPDY_HTTP2_PUSH_PROMISE_INDEX_H_

#include <set>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "net/base/net_export.h"
#include "net/http/http_request_info.h"
#include "net/spdy/spdy_session_key.h"
#include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h"
#include "url/gurl.h"

namespace net {

class SpdySession;

namespace test {

class Http2PushPromiseIndexPeer;

}  // namespace test

// Value returned by ClaimPushedStream() and FindStream() if no stream is found.
const spdy::SpdyStreamId kNoPushedStreamFound = 0;

// This class manages unclaimed pushed streams (push promises) from the receipt
// of PUSH_PROMISE frame until they are matched to a request.
// Each SpdySessionPool owns one instance of this class.
// SpdySession uses this class to register, unregister and query pushed streams.
// HttpStreamFactory::Job uses this class to find a SpdySession with a pushed
// stream matching the request, if such exists.
class NET_EXPORT Http2PushPromiseIndex {
 public:
  // Interface for validating pushed streams, signaling when a pushed stream is
  // claimed, and generating SpdySession weak pointer.
  class NET_EXPORT Delegate {
   public:
    Delegate() = default;

    Delegate(const Delegate&) = delete;
    Delegate& operator=(const Delegate&) = delete;

    virtual ~Delegate() = default;

    // Return true if a pushed stream with |url| can be used for a request with
    // |key|.
    virtual bool ValidatePushedStream(spdy::SpdyStreamId stream_id,
                                      const GURL& url,
                                      const HttpRequestInfo& request_info,
                                      const SpdySessionKey& key) const = 0;

    // Generate weak pointer.  Guaranateed to be called synchronously after
    // ValidatePushedStream() is called and returns true.
    virtual base::WeakPtr<SpdySession> GetWeakPtrToSession() = 0;
  };

  Http2PushPromiseIndex();

  Http2PushPromiseIndex(const Http2PushPromiseIndex&) = delete;
  Http2PushPromiseIndex& operator=(const Http2PushPromiseIndex&) = delete;

  ~Http2PushPromiseIndex();

  // Tries to register a Delegate with an unclaimed pushed stream for |url|.
  // Caller must make sure |delegate| stays valid by unregistering the exact
  // same entry before |delegate| is destroyed.
  // Returns true if there is no unclaimed pushed stream with the same URL for
  // the same Delegate, in which case the stream is registered.
  [[nodiscard]] bool RegisterUnclaimedPushedStream(const GURL& url,
                                                   spdy::SpdyStreamId stream_id,
                                                   Delegate* delegate);

  // Tries to unregister a Delegate with an unclaimed pushed stream for |url|
  // with given |stream_id|.
  // Returns true if this exact entry is found, in which case it is removed.
  [[nodiscard]] bool UnregisterUnclaimedPushedStream(
      const GURL& url,
      spdy::SpdyStreamId stream_id,
      Delegate* delegate);

  // Returns the number of pushed streams registered for |delegate|.
  size_t CountStreamsForSession(const Delegate* delegate) const;

  // Returns the stream ID of the entry registered for |delegate| with |url|,
  // or kNoPushedStreamFound if no such entry exists.
  spdy::SpdyStreamId FindStream(const GURL& url,
                                const Delegate* delegate) const;

  // If there exists a session compatible with |key| that has an unclaimed push
  // stream for |url|, then sets |*session| and |*stream| to one such session
  // and stream, and removes entry from index.  Makes no guarantee on which
  // (session, stream_id) pair is claimed if there are multiple matches.  Sets
  // |*session| to nullptr and |*stream| to kNoPushedStreamFound if no such
  // session exists.
  void ClaimPushedStream(const SpdySessionKey& key,
                         const GURL& url,
                         const HttpRequestInfo& request_info,
                         base::WeakPtr<SpdySession>* session,
                         spdy::SpdyStreamId* stream_id);

 private:
  friend test::Http2PushPromiseIndexPeer;

  // An unclaimed pushed stream entry.
  struct NET_EXPORT UnclaimedPushedStream {
    GURL url;
    raw_ptr<Delegate> delegate;
    spdy::SpdyStreamId stream_id;
  };

  // Function object satisfying the requirements of "Compare", see
  // http://en.cppreference.com/w/cpp/concept/Compare.
  // A set ordered by this function object supports O(log n) lookup
  // of the first entry with a given URL, by calling lower_bound() with an entry
  // with that URL and with delegate = nullptr.
  struct NET_EXPORT CompareByUrl {
    bool operator()(const UnclaimedPushedStream& a,
                    const UnclaimedPushedStream& b) const;
  };

  // Collection of all unclaimed pushed streams.  Delegate must unregister
  // its streams before destruction, so that all pointers remain valid.
  // Each Delegate can have at most one pushed stream for each URL (but each
  // Delegate can have pushed streams for different URLs, and different
  // Delegates can have pushed streams for the same GURL).
  std::set<UnclaimedPushedStream, CompareByUrl> unclaimed_pushed_streams_;
};

}  // namespace net

#endif  // NET_SPDY_HTTP2_PUSH_PROMISE_INDEX_H_