1 // Copyright 2019 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.
4 
5 #ifndef COMPONENTS_SYNC_BASE_CLIENT_TAG_HASH_H_
6 #define COMPONENTS_SYNC_BASE_CLIENT_TAG_HASH_H_
7 
8 #include <iosfwd>
9 #include <string>
10 
11 #include "base/hash/hash.h"
12 #include "components/sync/base/model_type.h"
13 
14 namespace syncer {
15 
16 // Represents a client defined unique hash for sync entities. Hash is derived
17 // from client tag, and should be used as |client_defined_unique_tag| for
18 // SyncEntity at least for CommitMessages. For convenience it supports storing
19 // in ordered stl containers, logging and equality comparisons. It also supports
20 // unordered stl containers using ClientTagHash::Hash.
21 class ClientTagHash {
22  public:
23   // For use in std::unordered_map.
24   struct Hash {
operatorHash25     size_t operator()(const ClientTagHash& client_tag_hash) const {
26       return base::FastHash(client_tag_hash.value());
27     }
28   };
29 
30   // Creates ClientTagHash based on |client_tag|.
31   static ClientTagHash FromUnhashed(ModelType type,
32                                     const std::string& client_tag);
33 
34   // Creates ClientTagHash from already hashed client tag.
35   static ClientTagHash FromHashed(std::string hash_value);
36 
37   ClientTagHash();
38   ClientTagHash(const ClientTagHash& other);
39   ClientTagHash(ClientTagHash&& other);
40   ~ClientTagHash();
41 
42   ClientTagHash& operator=(const ClientTagHash& other);
43   ClientTagHash& operator=(ClientTagHash&& other);
44 
value()45   const std::string& value() const { return value_; }
46 
47   size_t EstimateMemoryUsage() const;
48 
49  private:
50   explicit ClientTagHash(std::string value);
51   std::string value_;
52 };
53 
54 bool operator<(const ClientTagHash& lhs, const ClientTagHash& rhs);
55 bool operator==(const ClientTagHash& lhs, const ClientTagHash& rhs);
56 bool operator!=(const ClientTagHash& lhs, const ClientTagHash& rhs);
57 std::ostream& operator<<(std::ostream& os,
58                          const ClientTagHash& client_tag_hash);
59 
60 }  // namespace syncer
61 
62 #endif  // COMPONENTS_SYNC_BASE_CLIENT_TAG_HASH_H_
63