1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_SIGNIN_SIGNIN_INTERNALS_UTIL_H_
6 #define CHROME_BROWSER_SIGNIN_SIGNIN_INTERNALS_UTIL_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
14 namespace signin_internals_util {
16 // Preference prefixes for signin and token values.
17 extern const char kSigninPrefPrefix[];
18 extern const char kTokenPrefPrefix[];
20 // The length of strings returned by GetTruncatedHash() below.
21 const size_t kTruncateTokenStringLength = 6;
23 // Helper enums to access fields from SigninStatus (declared below).
25 SIGNIN_FIELDS_BEGIN = 0,
26 UNTIMED_FIELDS_BEGIN = SIGNIN_FIELDS_BEGIN
29 enum UntimedSigninStatusField {
30 USERNAME = UNTIMED_FIELDS_BEGIN,
37 UNTIMED_FIELDS_COUNT = UNTIMED_FIELDS_END - UNTIMED_FIELDS_BEGIN,
38 TIMED_FIELDS_BEGIN = UNTIMED_FIELDS_END
41 enum TimedSigninStatusField {
42 SIGNIN_TYPE = TIMED_FIELDS_BEGIN,
52 TIMED_FIELDS_COUNT = TIMED_FIELDS_END - TIMED_FIELDS_BEGIN,
53 SIGNIN_FIELDS_END = TIMED_FIELDS_END,
54 SIGNIN_FIELDS_COUNT = SIGNIN_FIELDS_END - SIGNIN_FIELDS_BEGIN
57 // Encapsulates diagnostic information about tokens for different services.
58 // Note that although SigninStatus contains a map of service names to token
59 // values, we replicate the service name within this struct for a cleaner
60 // serialization (with ToValue()).
62 std::string truncated_token; // The hashed and truncated token.
63 std::string status; // Status of the last token fetch.
64 std::string time; // Timestamp of the last token fetch
65 int64 time_internal; // Same as |time|, but in base::Time internal format.
66 std::string service; // The service that this token is for.
68 TokenInfo(const std::string& truncated_token,
69 const std::string& status,
70 const std::string& time,
71 const int64& time_internal,
72 const std::string& service);
76 DictionaryValue* ToValue();
79 // Associates a service name with its token information.
80 typedef std::map<std::string, TokenInfo> TokenInfoMap;
82 // Returns the root preference path for the service. The path should be
83 // qualified with one of .value, .status or .time to get the respective
84 // full preference path names.
85 std::string TokenPrefPath(const std::string& service_name);
87 // Many values in SigninStatus are also associated with a timestamp.
88 // This makes it easier to keep values and their associated times together.
89 typedef std::pair<std::string, std::string> TimedSigninStatusValue;
91 // Returns the name of a SigninStatus field.
92 std::string SigninStatusFieldToString(UntimedSigninStatusField field);
93 std::string SigninStatusFieldToString(TimedSigninStatusField field);
95 // Encapsulates both authentication and token related information. Used
96 // by SigninInternals to maintain information that needs to be shown in
97 // the about:signin-internals page.
99 std::vector<std::string> untimed_signin_fields;
100 std::vector<TimedSigninStatusValue> timed_signin_fields;
101 TokenInfoMap token_info_map;
106 // Returns a dictionary with the following form:
108 // [ {"title": "Basic Information",
109 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
111 // { "title": "Detailed Information",
112 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
115 // [ List of {"name": "foo-name", "token" : "foo-token",
116 // "status": "foo_stat", "time" : "foo_time"} elems]
118 scoped_ptr<DictionaryValue> ToValue();
121 // An Observer class for authentication and token diagnostic information.
122 class SigninDiagnosticsObserver {
124 // Credentials and signin related changes.
125 virtual void NotifySigninValueChanged(const UntimedSigninStatusField& field,
126 const std::string& value) {}
127 virtual void NotifySigninValueChanged(const TimedSigninStatusField& field,
128 const std::string& value) {}
129 // OAuth tokens related changes.
130 virtual void NotifyTokenReceivedSuccess(const std::string& token_name,
131 const std::string& token,
133 virtual void NotifyTokenReceivedFailure(const std::string& token_name,
134 const std::string& error) {}
135 virtual void NotifyClearStoredToken(const std::string& token_name) {}};
137 // Gets the first 6 hex characters of the SHA256 hash of the passed in string.
138 // These are enough to perform equality checks across a single users tokens,
139 // while preventing outsiders from reverse-engineering the actual token from
140 // the displayed value.
141 // Note that for readability (in about:signin-internals), an empty string
142 // is not hashed, but simply returned as an empty string.
143 std::string GetTruncatedHash(const std::string& str);
145 } // namespace signin_internals_util
147 #endif // CHROME_BROWSER_SIGNIN_SIGNIN_INTERNALS_UTIL_H_