Implement finding BLE connections in chrome://proximity-auth.
[chromium-blink-merge.git] / components / proximity_auth / remote_status_update.cc
blobd4fbc508f5f41c96c27ac76d86f2e2744129e748
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.
5 #include "components/proximity_auth/remote_status_update.h"
7 #include "base/logging.h"
8 #include "base/values.h"
10 namespace {
12 // The value of the 'type' status update field.
13 const char kStatusUpdateType[] = "status_update";
15 // Keys in the serialized RemoteStatusUpdate JSON object.
16 const char kType[] = "type";
17 const char kUserPresence[] = "user_presence";
18 const char kSecureScreenLock[] = "secure_screen_lock";
19 const char kTrustAgent[] = "trust_agent";
21 // Values in the serialized RemoteStatusUpdate JSON object.
22 const char kUserPresent[] = "present";
23 const char kUserAbsent[] = "absent";
24 const char kUserPresenceUnknown[] = "unknown";
26 const char kSecureScreenLockEnabled[] = "enabled";
27 const char kSecureScreenLockDisabled[] = "disabled";
28 const char kSecureScreenLockStateUnknown[] = "unknown";
30 const char kTrustAgentEnabled[] = "enabled";
31 const char kTrustAgentDisabled[] = "disabled";
32 const char kTrustAgentUnsupported[] = "unsupported";
34 } // namespace
36 namespace proximity_auth {
38 // static
39 scoped_ptr<RemoteStatusUpdate> RemoteStatusUpdate::Deserialize(
40 const base::DictionaryValue& serialized_value) {
41 std::string type;
42 if (!serialized_value.GetString(kType, &type) || type != kStatusUpdateType) {
43 VLOG(1) << "Unable to parse remote status update: unexpected type. "
44 << "Expected: '" << kStatusUpdateType << "', "
45 << "Saw: '" << type << "'.";
46 return scoped_ptr<RemoteStatusUpdate>();
49 std::string user_presence, secure_screen_lock_state, trust_agent_state;
50 if (!serialized_value.GetString(kUserPresence, &user_presence) ||
51 !serialized_value.GetString(kSecureScreenLock,
52 &secure_screen_lock_state) ||
53 !serialized_value.GetString(kTrustAgent, &trust_agent_state)) {
54 VLOG(1) << "Unable to parse remote status update: missing data value. "
55 << "Status update:\n" << serialized_value;
56 return scoped_ptr<RemoteStatusUpdate>();
59 scoped_ptr<RemoteStatusUpdate> parsed_update(new RemoteStatusUpdate);
60 if (user_presence == kUserPresent) {
61 parsed_update->user_presence = USER_PRESENT;
62 } else if (user_presence == kUserAbsent) {
63 parsed_update->user_presence = USER_ABSENT;
64 } else if (user_presence == kUserPresenceUnknown) {
65 parsed_update->user_presence = USER_PRESENCE_UNKNOWN;
66 } else {
67 VLOG(1) << "Unable to parse remote status update: invalid user presence: '"
68 << user_presence << "'.";
69 return scoped_ptr<RemoteStatusUpdate>();
72 if (secure_screen_lock_state == kSecureScreenLockEnabled) {
73 parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED;
74 } else if (secure_screen_lock_state == kSecureScreenLockDisabled) {
75 parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_DISABLED;
76 } else if (secure_screen_lock_state == kSecureScreenLockStateUnknown) {
77 parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_STATE_UNKNOWN;
78 } else {
79 VLOG(1) << "Unable to parse remote status update: invalid secure screen "
80 << "lock state: '" << secure_screen_lock_state << "'.";
81 return scoped_ptr<RemoteStatusUpdate>();
84 if (trust_agent_state == kTrustAgentEnabled) {
85 parsed_update->trust_agent_state = TRUST_AGENT_ENABLED;
86 } else if (trust_agent_state == kTrustAgentDisabled) {
87 parsed_update->trust_agent_state = TRUST_AGENT_DISABLED;
88 } else if (trust_agent_state == kTrustAgentUnsupported) {
89 parsed_update->trust_agent_state = TRUST_AGENT_UNSUPPORTED;
90 } else {
91 VLOG(1) << "Unable to parse remote status update: invalid trust agent "
92 << "state: '" << trust_agent_state << "'.";
93 return scoped_ptr<RemoteStatusUpdate>();
96 return parsed_update.Pass();
99 } // namespace proximity_auth