Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: components/proximity_auth/remote_status_update.cc

Issue 629183003: [Easy Unlock] Port RemoteStatusUpdate class to native code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "components/proximity_auth/remote_status_update.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/logging.h"
9 #include "base/values.h"
10
11 namespace {
12
13 // The value of the 'type' status update field.
14 const char kStatusUpdateType[] = "status_update";
15
16 // Keys in the serialized RemoteStatusUpdate JSON object.
17 const char kType[] = "type";
18 const char kUserPresence[] = "user_presence";
19 const char kSecureScreenLock[] = "secure_screen_lock";
20 const char kTrustAgent[] = "trust_agent";
21
22 // Values in the serialized RemoteStatusUpdate JSON object.
23 const char kUserPresent[] = "present";
24 const char kUserAbsent[] = "absent";
25 const char kUserPresenceUnknown[] = "unknown";
26
27 const char kSecureScreenLockEnabled[] = "enabled";
28 const char kSecureScreenLockDisabled[] = "disabled";
29 const char kSecureScreenLockStateUnknown[] = "unknown";
30
31 const char kTrustAgentEnabled[] = "enabled";
32 const char kTrustAgentDisabled[] = "disabled";
33 const char kTrustAgentUnsupported[] = "unsupported";
34
35 } // namespace
36
37 namespace proximity_auth {
38
39 // static
40 scoped_ptr<RemoteStatusUpdate> RemoteStatusUpdate::FromJson(
41 const std::string& json) {
42 scoped_ptr<base::Value> update_value(base::JSONReader::Read(json));
43 if (!update_value || !update_value->IsType(base::Value::TYPE_DICTIONARY)) {
44 VLOG(1) << "Unable to parse remote status update: invalid JSON object.";
45 return scoped_ptr<RemoteStatusUpdate>();
46 }
47
48 base::DictionaryValue* update;
49 bool success = update_value->GetAsDictionary(&update);
50 DCHECK(success);
51
52 std::string type;
53 if (!update->GetString(kType, &type) || type != kStatusUpdateType) {
54 VLOG(1) << "Unable to parse remote status update: unexpected type. "
55 << "Expected: '" << kStatusUpdateType << "', "
56 << "Saw: '" << type << "'.";
57 return scoped_ptr<RemoteStatusUpdate>();
58 }
59
60 std::string user_presence, secure_screen_lock_state, trust_agent_state;
61 if (!update->GetString(kUserPresence, &user_presence) ||
62 !update->GetString(kSecureScreenLock, &secure_screen_lock_state) ||
63 !update->GetString(kTrustAgent, &trust_agent_state)) {
64 VLOG(1) << "Unable to parse remote status update: missing data value.";
Tim Song 2014/10/09 19:07:30 Also log the json string.
Ilya Sherman 2014/10/09 22:52:33 Done.
65 return scoped_ptr<RemoteStatusUpdate>();
66 }
67
68 scoped_ptr<RemoteStatusUpdate> parsed_update(new RemoteStatusUpdate);
69 if (user_presence == kUserPresent) {
70 parsed_update->user_presence = USER_PRESENT;
71 } else if (user_presence == kUserAbsent) {
72 parsed_update->user_presence = USER_ABSENT;
73 } else if (user_presence == kUserPresenceUnknown) {
74 parsed_update->user_presence = USER_PRESENCE_UNKNOWN;
75 } else {
76 VLOG(1) << "Unable to parse remote status update: invalid user presence: '"
77 << user_presence << "'.";
78 return scoped_ptr<RemoteStatusUpdate>();
79 }
80
81 if (secure_screen_lock_state == kSecureScreenLockEnabled) {
82 parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED;
83 } else if (secure_screen_lock_state == kSecureScreenLockDisabled) {
84 parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_DISABLED;
85 } else if (secure_screen_lock_state == kSecureScreenLockStateUnknown) {
86 parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_STATE_UNKNOWN;
87 } else {
88 VLOG(1) << "Unable to parse remote status update: invalid secure screen "
89 << "lock state: '" << secure_screen_lock_state << "'.";
90 return scoped_ptr<RemoteStatusUpdate>();
91 }
92
93 if (trust_agent_state == kTrustAgentEnabled) {
94 parsed_update->trust_agent_state = TRUST_AGENT_ENABLED;
95 } else if (trust_agent_state == kTrustAgentDisabled) {
96 parsed_update->trust_agent_state = TRUST_AGENT_DISABLED;
97 } else if (trust_agent_state == kTrustAgentUnsupported) {
98 parsed_update->trust_agent_state = TRUST_AGENT_UNSUPPORTED;
99 } else {
100 VLOG(1) << "Unable to parse remote status update: invalid trust agent "
101 << "state: '" << trust_agent_state << "'.";
102 return scoped_ptr<RemoteStatusUpdate>();
103 }
104
105 return parsed_update.Pass();
106 }
107
108 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698