xref: /aosp_15_r20/external/cronet/net/extras/shared_dictionary/shared_dictionary_info.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_INFO_H_
6 #define NET_EXTRAS_SHARED_DICTIONARY_SHARED_DICTIONARY_INFO_H_
7 
8 #include <string>
9 
10 #include "base/component_export.h"
11 #include "base/time/time.h"
12 #include "base/unguessable_token.h"
13 #include "net/base/hash_value.h"
14 #include "url/gurl.h"
15 #include "url/scheme_host_port.h"
16 
17 namespace net {
18 
19 // This class represents a shared dictionary information stored in a SQLite
20 // database for compression dictionary transport feature.
COMPONENT_EXPORT(NET_SHARED_DICTIONARY)21 class COMPONENT_EXPORT(NET_SHARED_DICTIONARY) SharedDictionaryInfo {
22  public:
23   SharedDictionaryInfo(const GURL& url,
24                        base::Time response_time,
25                        base::TimeDelta expiration,
26                        const std::string& match,
27                        const std::string& match_dest_string,
28                        const std::string& id,
29                        base::Time last_used_time,
30                        size_t size,
31                        const net::SHA256HashValue& hash,
32                        const base::UnguessableToken& disk_cache_key_token,
33                        const std::optional<int64_t>& primary_key_in_database);
34 
35   SharedDictionaryInfo(const SharedDictionaryInfo&);
36   SharedDictionaryInfo& operator=(const SharedDictionaryInfo&);
37 
38   SharedDictionaryInfo(SharedDictionaryInfo&& other);
39   SharedDictionaryInfo& operator=(SharedDictionaryInfo&& other);
40 
41   ~SharedDictionaryInfo();
42 
43   bool operator==(const SharedDictionaryInfo& other) const;
44 
45   const GURL& url() const { return url_; }
46   base::Time response_time() const { return response_time_; }
47   base::TimeDelta expiration() const { return expiration_; }
48   const std::string& match() const { return match_; }
49   const std::string& match_dest_string() const { return match_dest_string_; }
50   const std::string& id() const { return id_; }
51   base::Time last_used_time() const { return last_used_time_; }
52   size_t size() const { return size_; }
53   const net::SHA256HashValue& hash() const { return hash_; }
54   const base::UnguessableToken& disk_cache_key_token() const {
55     return disk_cache_key_token_;
56   }
57 
58   const std::optional<int64_t>& primary_key_in_database() const {
59     return primary_key_in_database_;
60   }
61   void set_primary_key_in_database(int64_t primary_key_in_database) {
62     primary_key_in_database_ = primary_key_in_database;
63   }
64 
65   void set_last_used_time(base::Time last_used_time) {
66     last_used_time_ = last_used_time;
67   }
68 
69   // An utility method that returns `response_time_` + `expiration_`.
70   base::Time GetExpirationTime() const;
71 
72  private:
73   // URL of the dictionary.
74   GURL url_;
75   // The time of when the dictionary was received from the server.
76   base::Time response_time_;
77   // The expiration time for the dictionary which was declared in
78   // 'use-as-dictionary' response header's `expires` option in seconds.
79   base::TimeDelta expiration_;
80   // The matching path pattern for the dictionary which was declared in
81   // 'use-as-dictionary' response header's `match` option.
82   std::string match_;
83   // The comma separated matching destinations for the dictionary which was
84   // declared in 'use-as-dictionary' response header's `match-dest` option.
85   std::string match_dest_string_;
86   // The Id for the dictionary which was declared in 'use-as-dictionary'
87   // response header's `id` option.
88   std::string id_;
89   // The time when the dictionary was last used.
90   base::Time last_used_time_;
91   // The size of the dictionary binary.
92   size_t size_;
93   // The sha256 hash of the dictionary binary.
94   net::SHA256HashValue hash_;
95   // The UnguessableToken used as a key in the DiskCache to store the dictionary
96   // binary.
97   base::UnguessableToken disk_cache_key_token_;
98   // The primary key in SQLite database. This is nullopt until it is stored to
99   // the database.
100   std::optional<int64_t> primary_key_in_database_;
101 };
102 
103 }  // namespace net
104 
105 #endif  // NET_EXTRAS_SHARED_DICTIONARY_SHARED_DICTIONARY_INFO_H_
106