client_tag_hash.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "components/sync/base/client_tag_hash.h"
  5. #include <utility>
  6. #include "base/base64.h"
  7. #include "base/hash/sha1.h"
  8. #include "base/trace_event/memory_usage_estimator.h"
  9. #include "components/sync/protocol/entity_specifics.pb.h"
  10. namespace syncer {
  11. // static
  12. ClientTagHash ClientTagHash::FromUnhashed(ModelType model_type,
  13. const std::string& client_tag) {
  14. // Blank PB with just the field in it has termination symbol,
  15. // handy for delimiter.
  16. sync_pb::EntitySpecifics serialized_type;
  17. AddDefaultFieldValue(model_type, &serialized_type);
  18. std::string hash_input;
  19. serialized_type.AppendToString(&hash_input);
  20. hash_input.append(client_tag);
  21. std::string encode_output;
  22. base::Base64Encode(base::SHA1HashString(hash_input), &encode_output);
  23. return FromHashed(encode_output);
  24. }
  25. // static
  26. ClientTagHash ClientTagHash::FromHashed(std::string hash_value) {
  27. return ClientTagHash(std::move(hash_value));
  28. }
  29. ClientTagHash::ClientTagHash() = default;
  30. ClientTagHash::ClientTagHash(std::string value) : value_(std::move(value)) {}
  31. ClientTagHash::ClientTagHash(const ClientTagHash& other) = default;
  32. ClientTagHash::ClientTagHash(ClientTagHash&& other) = default;
  33. ClientTagHash::~ClientTagHash() = default;
  34. ClientTagHash& ClientTagHash::operator=(const ClientTagHash& other) = default;
  35. ClientTagHash& ClientTagHash::operator=(ClientTagHash&& other) = default;
  36. size_t ClientTagHash::EstimateMemoryUsage() const {
  37. return base::trace_event::EstimateMemoryUsage(value_);
  38. }
  39. bool operator<(const ClientTagHash& lhs, const ClientTagHash& rhs) {
  40. return lhs.value() < rhs.value();
  41. }
  42. bool operator==(const ClientTagHash& lhs, const ClientTagHash& rhs) {
  43. return lhs.value() == rhs.value();
  44. }
  45. bool operator!=(const ClientTagHash& lhs, const ClientTagHash& rhs) {
  46. return lhs.value() != rhs.value();
  47. }
  48. std::ostream& operator<<(std::ostream& os,
  49. const ClientTagHash& client_tag_hash) {
  50. return os << client_tag_hash.value();
  51. }
  52. } // namespace syncer