remote_device.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2015 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 "ash/components/multidevice/remote_device.h"
  5. #include "base/base64.h"
  6. namespace ash::multidevice {
  7. // static
  8. std::string RemoteDevice::GenerateDeviceId(const std::string& public_key) {
  9. std::string device_id;
  10. base::Base64Encode(public_key, &device_id);
  11. return device_id;
  12. }
  13. // static
  14. std::string RemoteDevice::DerivePublicKey(const std::string& device_id) {
  15. std::string public_key;
  16. if (base::Base64Decode(device_id, &public_key))
  17. return public_key;
  18. return std::string();
  19. }
  20. RemoteDevice::RemoteDevice() : last_update_time_millis(0L) {}
  21. RemoteDevice::RemoteDevice(
  22. const std::string& user_email,
  23. const std::string& instance_id,
  24. const std::string& name,
  25. const std::string& pii_free_name,
  26. const std::string& public_key,
  27. const std::string& persistent_symmetric_key,
  28. int64_t last_update_time_millis,
  29. const std::map<SoftwareFeature, SoftwareFeatureState>& software_features,
  30. const std::vector<BeaconSeed>& beacon_seeds,
  31. const std::string& bluetooth_public_address)
  32. : user_email(user_email),
  33. instance_id(instance_id),
  34. name(name),
  35. pii_free_name(pii_free_name),
  36. public_key(public_key),
  37. persistent_symmetric_key(persistent_symmetric_key),
  38. last_update_time_millis(last_update_time_millis),
  39. software_features(software_features),
  40. beacon_seeds(beacon_seeds),
  41. bluetooth_public_address(bluetooth_public_address) {}
  42. RemoteDevice::RemoteDevice(const RemoteDevice& other) = default;
  43. RemoteDevice::~RemoteDevice() {}
  44. std::string RemoteDevice::GetDeviceId() const {
  45. return RemoteDevice::GenerateDeviceId(public_key);
  46. }
  47. bool RemoteDevice::operator==(const RemoteDevice& other) const {
  48. return user_email == other.user_email && instance_id == other.instance_id &&
  49. name == other.name && pii_free_name == other.pii_free_name &&
  50. public_key == other.public_key &&
  51. persistent_symmetric_key == other.persistent_symmetric_key &&
  52. last_update_time_millis == other.last_update_time_millis &&
  53. software_features == other.software_features &&
  54. beacon_seeds == other.beacon_seeds &&
  55. bluetooth_public_address == other.bluetooth_public_address;
  56. }
  57. bool RemoteDevice::operator<(const RemoteDevice& other) const {
  58. if (!instance_id.empty() || !other.instance_id.empty())
  59. return instance_id.compare(other.instance_id) < 0;
  60. // |public_key| can contain null bytes, so use GetDeviceId(), which cannot
  61. // contain null bytes, to compare devices.
  62. // Note: Devices that do not have an Instance ID are v1 DeviceSync devices,
  63. // which should have a public key.
  64. return GetDeviceId().compare(other.GetDeviceId()) < 0;
  65. }
  66. } // namespace ash::multidevice