remote_status_update.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2014 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/proximity_auth/remote_status_update.h"
  5. #include "ash/components/multidevice/logging/logging.h"
  6. namespace {
  7. // The value of the 'type' status update field.
  8. const char kStatusUpdateType[] = "status_update";
  9. // Keys in the serialized RemoteStatusUpdate JSON object.
  10. const char kType[] = "type";
  11. const char kUserPresence[] = "user_presence";
  12. const char kSecureScreenLock[] = "secure_screen_lock";
  13. const char kTrustAgent[] = "trust_agent";
  14. // Values in the serialized RemoteStatusUpdate JSON object.
  15. const char kUserPresent[] = "present";
  16. const char kUserAbsent[] = "absent";
  17. const char kUserPresenceUnknown[] = "unknown";
  18. const char kUserPresenceSecondary[] = "secondary";
  19. const char kUserPresenceBackground[] = "background";
  20. const char kSecureScreenLockEnabled[] = "enabled";
  21. const char kSecureScreenLockDisabled[] = "disabled";
  22. const char kSecureScreenLockStateUnknown[] = "unknown";
  23. const char kTrustAgentEnabled[] = "enabled";
  24. const char kTrustAgentDisabled[] = "disabled";
  25. const char kTrustAgentUnsupported[] = "unsupported";
  26. } // namespace
  27. namespace proximity_auth {
  28. // static
  29. std::unique_ptr<RemoteStatusUpdate> RemoteStatusUpdate::Deserialize(
  30. const base::Value::Dict& serialized_value) {
  31. const std::string* type = serialized_value.FindString(kType);
  32. if (!type || *type != kStatusUpdateType) {
  33. PA_LOG(ERROR) << "Unable to parse remote status update: unexpected type. "
  34. << "Expected: '" << kStatusUpdateType << "', "
  35. << "Saw: '" << *type << "'.";
  36. return nullptr;
  37. }
  38. const std::string* user_presence = serialized_value.FindString(kUserPresence);
  39. const std::string* secure_screen_lock_state =
  40. serialized_value.FindString(kSecureScreenLock);
  41. const std::string* trust_agent_state =
  42. serialized_value.FindString(kTrustAgent);
  43. if (!user_presence || !secure_screen_lock_state || !trust_agent_state) {
  44. PA_LOG(ERROR) << "Unable to parse remote status update: missing data value."
  45. << " Status update:\n"
  46. << serialized_value;
  47. return nullptr;
  48. }
  49. std::unique_ptr<RemoteStatusUpdate> parsed_update(new RemoteStatusUpdate);
  50. if (*user_presence == kUserPresent) {
  51. parsed_update->user_presence = USER_PRESENT;
  52. } else if (*user_presence == kUserAbsent) {
  53. parsed_update->user_presence = USER_ABSENT;
  54. } else if (*user_presence == kUserPresenceUnknown) {
  55. parsed_update->user_presence = USER_PRESENCE_UNKNOWN;
  56. } else if (*user_presence == kUserPresenceSecondary) {
  57. parsed_update->user_presence = USER_PRESENCE_SECONDARY;
  58. } else if (*user_presence == kUserPresenceBackground) {
  59. parsed_update->user_presence = USER_PRESENCE_BACKGROUND;
  60. } else {
  61. PA_LOG(ERROR)
  62. << "Unable to parse remote status update: invalid user presence: '"
  63. << *user_presence << "'.";
  64. return nullptr;
  65. }
  66. if (*secure_screen_lock_state == kSecureScreenLockEnabled) {
  67. parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED;
  68. } else if (*secure_screen_lock_state == kSecureScreenLockDisabled) {
  69. parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_DISABLED;
  70. } else if (*secure_screen_lock_state == kSecureScreenLockStateUnknown) {
  71. parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_STATE_UNKNOWN;
  72. } else {
  73. PA_LOG(ERROR) << "Unable to parse remote status update: invalid secure "
  74. << "screen lock state: '" << *secure_screen_lock_state
  75. << "'.";
  76. return nullptr;
  77. }
  78. if (*trust_agent_state == kTrustAgentEnabled) {
  79. parsed_update->trust_agent_state = TRUST_AGENT_ENABLED;
  80. } else if (*trust_agent_state == kTrustAgentDisabled) {
  81. parsed_update->trust_agent_state = TRUST_AGENT_DISABLED;
  82. } else if (*trust_agent_state == kTrustAgentUnsupported) {
  83. parsed_update->trust_agent_state = TRUST_AGENT_UNSUPPORTED;
  84. } else {
  85. PA_LOG(ERROR) << "Unable to parse remote status update: invalid trust "
  86. << "agent state: '" << *trust_agent_state << "'.";
  87. return nullptr;
  88. }
  89. return parsed_update;
  90. }
  91. } // namespace proximity_auth