xref: /aosp_15_r20/external/cronet/net/extras/shared_dictionary/shared_dictionary_isolation_key.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
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_EXTRAS_SHARED_DICTIONARY_SHARED_DICTIONARY_ISOLATION_KEY_H_
6 #define NET_EXTRAS_SHARED_DICTIONARY_SHARED_DICTIONARY_ISOLATION_KEY_H_
7 
8 #include <optional>
9 
10 #include "base/component_export.h"
11 #include "net/base/schemeful_site.h"
12 #include "url/origin.h"
13 
14 namespace net {
15 class IsolationInfo;
16 class NetworkIsolationKey;
17 
18 // Key used to isolate shared dictionary storages.
COMPONENT_EXPORT(NET_SHARED_DICTIONARY)19 class COMPONENT_EXPORT(NET_SHARED_DICTIONARY) SharedDictionaryIsolationKey {
20  public:
21   // Creates a SharedDictionaryIsolationKey. Returns nullopt when
22   // `frame_origin` or `top_frame_origin` of `isolation_info` is not set or
23   // opaque, or `nonce` is set.
24   static std::optional<SharedDictionaryIsolationKey> MaybeCreate(
25       const net::IsolationInfo& isolation_info);
26 
27   // Creates a SharedDictionaryIsolationKey. Returns nullopt when
28   // `frame_origin` or `top_frame_origin` of `network_isolation_key` is not set
29   // or opaque, or `nonce` of `network_isolation_key` is set.
30   static std::optional<SharedDictionaryIsolationKey> MaybeCreate(
31       const NetworkIsolationKey& network_isolation_key,
32       const std::optional<url::Origin>& frame_origin);
33 
34   SharedDictionaryIsolationKey() = default;
35   SharedDictionaryIsolationKey(const url::Origin& frame_origin,
36                                const net::SchemefulSite& top_frame_site);
37 
38   const url::Origin& frame_origin() const { return frame_origin_; }
39   const net::SchemefulSite& top_frame_site() const { return top_frame_site_; }
40 
41   ~SharedDictionaryIsolationKey();
42 
43   SharedDictionaryIsolationKey(const SharedDictionaryIsolationKey& other);
44   SharedDictionaryIsolationKey(SharedDictionaryIsolationKey&& other);
45   SharedDictionaryIsolationKey& operator=(
46       const SharedDictionaryIsolationKey& other);
47   SharedDictionaryIsolationKey& operator=(SharedDictionaryIsolationKey&& other);
48 
49   bool operator==(const SharedDictionaryIsolationKey& other) const {
50     return std::tie(frame_origin_, top_frame_site_) ==
51            std::tie(other.frame_origin_, other.top_frame_site_);
52   }
53   bool operator!=(const SharedDictionaryIsolationKey& other) const {
54     return !(*this == other);
55   }
56   bool operator<(const SharedDictionaryIsolationKey& other) const {
57     return std::tie(frame_origin_, top_frame_site_) <
58            std::tie(other.frame_origin_, other.top_frame_site_);
59   }
60 
61  private:
62   url::Origin frame_origin_;
63   net::SchemefulSite top_frame_site_;
64 };
65 
66 }  // namespace net
67 
68 #endif  // NET_EXTRAS_SHARED_DICTIONARY_SHARED_DICTIONARY_ISOLATION_KEY_H_
69