summaryrefslogtreecommitdiff
path: root/chromium/net/base/network_isolation_key.h
blob: 82bfba04cfc30c4bc723c1654a6c6ad38a7eeb42 (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
// Copyright 2019 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_BASE_NETWORK_ISOLATION_KEY_H_
#define NET_BASE_NETWORK_ISOLATION_KEY_H_

#include <string>

#include "base/macros.h"
#include "base/optional.h"
#include "net/base/net_export.h"
#include "url/origin.h"

namespace net {

// Key used to isolate shared network stack resources used by requests based on
// the context on which they were made.
class NET_EXPORT NetworkIsolationKey {
 public:
  // Full constructor.  When a request is initiated by the top frame, it must
  // also populate the |frame_origin| parameter when calling this constructor.
  explicit NetworkIsolationKey(const url::Origin& top_frame_origin,
                               const url::Origin& frame_origin);

  // Construct an empty key.
  NetworkIsolationKey();

  NetworkIsolationKey(const NetworkIsolationKey& network_isolation_key);

  ~NetworkIsolationKey();

  NetworkIsolationKey& operator=(
      const NetworkIsolationKey& network_isolation_key);
  NetworkIsolationKey& operator=(NetworkIsolationKey&& network_isolation_key);

  // Compare keys for equality, true if all enabled fields are equal.
  bool operator==(const NetworkIsolationKey& other) const {
    return top_frame_origin_ == other.top_frame_origin_ &&
           frame_origin_ == other.frame_origin_;
  }

  // Compare keys for inequality, true if any enabled field varies.
  bool operator!=(const NetworkIsolationKey& other) const {
    return (top_frame_origin_ != other.top_frame_origin_) ||
           (frame_origin_ != other.frame_origin_);
  }

  // Provide an ordering for keys based on all enabled fields.
  bool operator<(const NetworkIsolationKey& other) const {
    return top_frame_origin_ < other.top_frame_origin_ ||
           (top_frame_origin_ == other.top_frame_origin_ &&
            frame_origin_ < other.frame_origin_);
  }

  // Returns the string representation of the key, which is the string
  // representation of each piece of the key separated by spaces.
  std::string ToString() const;

  // Returns string for debugging. Difference from ToString() is that transient
  // entries may be distinguishable from each other.
  std::string ToDebugString() const;

  // Returns true if all parts of the key are non-empty.
  bool IsFullyPopulated() const;

  // Returns true if this key's lifetime is short-lived. It may not make sense
  // to persist state to disk related to it (e.g., disk cache).
  bool IsTransient() const;

  // APIs for serialization to and from the mojo structure.
  const base::Optional<url::Origin>& GetTopFrameOrigin() const {
    return top_frame_origin_;
  }

  const base::Optional<url::Origin>& GetFrameOrigin() const {
    return frame_origin_;
  }

  // Returns true if all parts of the key are empty.
  bool IsEmpty() const;

 private:
  // Whether or not to use the |frame_origin_| as part of the key.
  bool use_frame_origin_;

  // The origin of the top frame of the page making the request.
  base::Optional<url::Origin> top_frame_origin_;

  // The origin of the frame that initiates the request.
  base::Optional<url::Origin> frame_origin_;
};

}  // namespace net

#endif  // NET_BASE_NETWORK_ISOLATION_KEY_H_