Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/signin/about_signin_internals.h" | 5 #include "chrome/browser/signin/about_signin_internals.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/hash.h" | 8 #include "base/hash.h" |
| 9 #include "base/i18n/time_formatting.h" | 9 #include "base/i18n/time_formatting.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/signin/signin_internals_util.h" | 14 #include "chrome/browser/signin/signin_internals_util.h" |
| 15 #include "chrome/browser/signin/signin_manager.h" | 15 #include "chrome/browser/signin/signin_manager.h" |
| 16 #include "chrome/browser/signin/token_service.h" | 16 #include "chrome/browser/signin/token_service.h" |
| 17 #include "chrome/browser/signin/token_service_factory.h" | 17 #include "chrome/browser/signin/token_service_factory.h" |
| 18 #include "chrome/browser/ui/webui/signin_internals_ui.h" | 18 #include "chrome/browser/ui/webui/signin_internals_ui.h" |
| 19 #include "google_apis/gaia/gaia_constants.h" | 19 #include "google_apis/gaia/gaia_constants.h" |
| 20 | 20 |
| 21 using namespace signin_internals_util; | 21 using namespace signin_internals_util; |
| 22 | 22 |
| 23 namespace { | |
| 24 | |
| 25 std::string TimeToFriendlyStr(base::Time time) { | |
|
tim (not reviewing)
2013/05/03 00:46:43
const& ?
Nicolas Zea
2013/05/03 01:31:41
Done.
| |
| 26 if (time.is_null()) | |
| 27 return ""; | |
|
tim (not reviewing)
2013/05/03 00:46:43
prefer std::string()
Nicolas Zea
2013/05/03 01:31:41
Done.
| |
| 28 return UTF16ToUTF8(base::TimeFormatFriendlyDate(time)); | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 23 AboutSigninInternals::AboutSigninInternals() : profile_(NULL) { | 33 AboutSigninInternals::AboutSigninInternals() : profile_(NULL) { |
| 24 // Initialize default values for tokens. | 34 // Initialize default values for tokens. |
| 25 for (size_t i = 0; i < kNumTokenPrefs; ++i) { | 35 for (size_t i = 0; i < kNumTokenPrefs; ++i) { |
| 26 signin_status_.token_info_map.insert(std::pair<std::string, TokenInfo>( | 36 signin_status_.token_info_map.insert(std::pair<std::string, TokenInfo>( |
| 27 kTokenPrefsArray[i], | 37 kTokenPrefsArray[i], |
| 28 TokenInfo( | 38 TokenInfo("", "Not Loaded", "", 0, kTokenPrefsArray[i]))); |
|
tim (not reviewing)
2013/05/03 00:46:43
std::string() was a better choice than "".
Nicolas Zea
2013/05/03 01:31:41
Done.
| |
| 29 std::string(), "Not Loaded", std::string(), kTokenPrefsArray[i]))); | |
| 30 } | 39 } |
| 31 } | 40 } |
| 32 | 41 |
| 33 AboutSigninInternals::~AboutSigninInternals() { | 42 AboutSigninInternals::~AboutSigninInternals() { |
| 34 } | 43 } |
| 35 | 44 |
| 36 void AboutSigninInternals::AddSigninObserver( | 45 void AboutSigninInternals::AddSigninObserver( |
| 37 AboutSigninInternals::Observer* observer) { | 46 AboutSigninInternals::Observer* observer) { |
| 38 signin_observers_.AddObserver(observer); | 47 signin_observers_.AddObserver(observer); |
| 39 } | 48 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 59 NotifyObservers(); | 68 NotifyObservers(); |
| 60 } | 69 } |
| 61 | 70 |
| 62 void AboutSigninInternals::NotifySigninValueChanged( | 71 void AboutSigninInternals::NotifySigninValueChanged( |
| 63 const TimedSigninStatusField& field, | 72 const TimedSigninStatusField& field, |
| 64 const std::string& value) { | 73 const std::string& value) { |
| 65 unsigned int field_index = field - TIMED_FIELDS_BEGIN; | 74 unsigned int field_index = field - TIMED_FIELDS_BEGIN; |
| 66 DCHECK(field_index >= 0 && | 75 DCHECK(field_index >= 0 && |
| 67 field_index < signin_status_.timed_signin_fields.size()); | 76 field_index < signin_status_.timed_signin_fields.size()); |
| 68 | 77 |
| 69 std::string time_as_str = UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime( | 78 base::Time now = base::Time::NowFromSystemTime(); |
|
tim (not reviewing)
2013/05/03 00:46:43
nit - there's a whole pile of base::Times in here.
Nicolas Zea
2013/05/03 01:31:41
Done.
| |
| 70 base::Time::NowFromSystemTime())); | 79 std::string time_as_str = TimeToFriendlyStr(now); |
| 71 TimedSigninStatusValue timed_value(value, time_as_str); | 80 TimedSigninStatusValue timed_value(value, time_as_str); |
| 72 | 81 |
| 73 signin_status_.timed_signin_fields[field_index] = timed_value; | 82 signin_status_.timed_signin_fields[field_index] = timed_value; |
| 74 | 83 |
| 75 // Also persist these values in the prefs. | 84 // Also persist these values in the prefs. |
| 76 const std::string value_pref = SigninStatusFieldToString(field) + ".value"; | 85 const std::string value_pref = SigninStatusFieldToString(field) + ".value"; |
| 77 const std::string time_pref = SigninStatusFieldToString(field) + ".time"; | 86 const std::string time_pref = SigninStatusFieldToString(field) + ".time"; |
| 78 profile_->GetPrefs()->SetString(value_pref.c_str(), value); | 87 profile_->GetPrefs()->SetString(value_pref.c_str(), value); |
| 79 profile_->GetPrefs()->SetString(time_pref.c_str(), time_as_str); | 88 profile_->GetPrefs()->SetString(time_pref.c_str(), time_as_str); |
| 80 | 89 |
| 81 NotifyObservers(); | 90 NotifyObservers(); |
| 82 } | 91 } |
| 83 | 92 |
| 84 void AboutSigninInternals::RefreshSigninPrefs() { | 93 void AboutSigninInternals::RefreshSigninPrefs() { |
| 85 // Return if no profile exists. Can occur in unit tests. | 94 // Return if no profile exists. Can occur in unit tests. |
| 86 if (!profile_) | 95 if (!profile_) |
| 87 return; | 96 return; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 104 pref_service->GetString(time_pref.c_str())); | 113 pref_service->GetString(time_pref.c_str())); |
| 105 signin_status_.timed_signin_fields[i - TIMED_FIELDS_BEGIN] = value; | 114 signin_status_.timed_signin_fields[i - TIMED_FIELDS_BEGIN] = value; |
| 106 } | 115 } |
| 107 | 116 |
| 108 // Get status and timestamps for all token services. | 117 // Get status and timestamps for all token services. |
| 109 for (size_t i = 0; i < kNumTokenPrefs; i++) { | 118 for (size_t i = 0; i < kNumTokenPrefs; i++) { |
| 110 const std::string pref = TokenPrefPath(kTokenPrefsArray[i]); | 119 const std::string pref = TokenPrefPath(kTokenPrefsArray[i]); |
| 111 const std::string value = pref + ".value"; | 120 const std::string value = pref + ".value"; |
| 112 const std::string status = pref + ".status"; | 121 const std::string status = pref + ".status"; |
| 113 const std::string time = pref + ".time"; | 122 const std::string time = pref + ".time"; |
| 123 const std::string time_internal = pref + ".time_internal"; | |
|
tim (not reviewing)
2013/05/03 00:46:43
What's with the local consts :P
Nicolas Zea
2013/05/03 01:31:41
I was tempted to get rid of them!
| |
| 114 | 124 |
| 115 TokenInfo token_info(pref_service->GetString(value.c_str()), | 125 TokenInfo token_info(pref_service->GetString(value.c_str()), |
| 116 pref_service->GetString(status.c_str()), | 126 pref_service->GetString(status.c_str()), |
| 117 pref_service->GetString(time.c_str()), | 127 pref_service->GetString(time.c_str()), |
| 128 pref_service->GetInt64(time_internal.c_str()), | |
| 118 kTokenPrefsArray[i]); | 129 kTokenPrefsArray[i]); |
| 119 | 130 |
| 120 signin_status_.token_info_map[kTokenPrefsArray[i]] = token_info; | 131 signin_status_.token_info_map[kTokenPrefsArray[i]] = token_info; |
| 121 } | 132 } |
| 122 | 133 |
| 123 NotifyObservers(); | 134 NotifyObservers(); |
| 124 } | 135 } |
| 125 | 136 |
| 126 void AboutSigninInternals::NotifyTokenReceivedSuccess( | 137 void AboutSigninInternals::NotifyTokenReceivedSuccess( |
| 127 const std::string& token_name, | 138 const std::string& token_name, |
| 128 const std::string& token, | 139 const std::string& token, |
| 129 bool update_time) { | 140 bool update_time) { |
| 130 // This should have been initialized already. | 141 // This should have been initialized already. |
| 131 DCHECK(signin_status_.token_info_map.count(token_name)); | 142 DCHECK(signin_status_.token_info_map.count(token_name)); |
| 132 | 143 |
| 133 const std::string status_success = "Successful"; | 144 const std::string status_success = "Successful"; |
| 134 signin_status_.token_info_map[token_name].token = token; | 145 signin_status_.token_info_map[token_name].token = token; |
| 135 signin_status_.token_info_map[token_name].status = status_success; | 146 signin_status_.token_info_map[token_name].status = status_success; |
| 136 | 147 |
| 137 // Also update preferences. | 148 // Also update preferences. |
| 138 const std::string value_pref = TokenPrefPath(token_name) + ".value"; | 149 const std::string value_pref = TokenPrefPath(token_name) + ".value"; |
| 139 const std::string time_pref = TokenPrefPath(token_name) + ".time"; | 150 const std::string time_pref = TokenPrefPath(token_name) + ".time"; |
| 151 const std::string time_internal_pref = | |
| 152 TokenPrefPath(token_name) + ".time_internal"; | |
| 140 const std::string status_pref = TokenPrefPath(token_name) + ".status"; | 153 const std::string status_pref = TokenPrefPath(token_name) + ".status"; |
| 141 profile_->GetPrefs()->SetString(value_pref.c_str(), token); | 154 profile_->GetPrefs()->SetString(value_pref.c_str(), token); |
| 142 profile_->GetPrefs()->SetString(status_pref.c_str(), "Successful"); | 155 profile_->GetPrefs()->SetString(status_pref.c_str(), "Successful"); |
| 143 | 156 |
| 144 // Update timestamp if needed. | 157 // Update timestamp if needed. |
| 145 if (update_time) { | 158 if (update_time) { |
| 146 const std::string time_as_str = UTF16ToUTF8( | 159 base::Time now = base::Time::NowFromSystemTime(); |
| 147 base::TimeFormatFriendlyDateAndTime(base::Time::NowFromSystemTime())); | 160 int64 time_as_int = now.ToInternalValue(); |
| 161 const std::string time_as_str = TimeToFriendlyStr(now); | |
| 148 signin_status_.token_info_map[token_name].time = time_as_str; | 162 signin_status_.token_info_map[token_name].time = time_as_str; |
| 163 signin_status_.token_info_map[token_name].time_internal = time_as_int; | |
| 149 profile_->GetPrefs()->SetString(time_pref.c_str(), time_as_str); | 164 profile_->GetPrefs()->SetString(time_pref.c_str(), time_as_str); |
| 165 profile_->GetPrefs()->SetInt64(time_internal_pref.c_str(), time_as_int); | |
| 150 } | 166 } |
| 151 | 167 |
| 152 NotifyObservers(); | 168 NotifyObservers(); |
| 153 } | 169 } |
| 154 | 170 |
| 155 | 171 |
| 156 void AboutSigninInternals::NotifyTokenReceivedFailure( | 172 void AboutSigninInternals::NotifyTokenReceivedFailure( |
| 157 const std::string& token_name, | 173 const std::string& token_name, |
| 158 const std::string& error) { | 174 const std::string& error) { |
| 159 const std::string time_as_str = | 175 base::Time now = base::Time::NowFromSystemTime(); |
| 160 UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime( | 176 int64 time_as_int = now.ToInternalValue(); |
| 161 base::Time::NowFromSystemTime())); | 177 const std::string time_as_str = TimeToFriendlyStr(now); |
| 162 | 178 |
| 163 // This should have been initialized already. | 179 // This should have been initialized already. |
| 164 DCHECK(signin_status_.token_info_map.count(token_name)); | 180 DCHECK(signin_status_.token_info_map.count(token_name)); |
| 165 | 181 |
| 166 signin_status_.token_info_map[token_name].token.clear(); | 182 signin_status_.token_info_map[token_name].token.clear(); |
| 167 signin_status_.token_info_map[token_name].status = error; | 183 signin_status_.token_info_map[token_name].status = error; |
| 168 signin_status_.token_info_map[token_name].time = time_as_str; | 184 signin_status_.token_info_map[token_name].time = time_as_str; |
| 185 signin_status_.token_info_map[token_name].time_internal = time_as_int; | |
| 169 | 186 |
| 170 // Also update preferences. | 187 // Also update preferences. |
| 171 const std::string value_pref = TokenPrefPath(token_name) + ".value"; | 188 const std::string value_pref = TokenPrefPath(token_name) + ".value"; |
| 172 const std::string time_pref = TokenPrefPath(token_name) + ".time"; | 189 const std::string time_pref = TokenPrefPath(token_name) + ".time"; |
| 190 const std::string time_internal_pref = | |
| 191 TokenPrefPath(token_name) + ".time_internal"; | |
| 173 const std::string status_pref = TokenPrefPath(token_name) + ".status"; | 192 const std::string status_pref = TokenPrefPath(token_name) + ".status"; |
| 174 profile_->GetPrefs()->SetString(value_pref.c_str(), std::string()); | 193 profile_->GetPrefs()->SetString(value_pref.c_str(), std::string()); |
| 175 profile_->GetPrefs()->SetString(time_pref.c_str(), time_as_str); | 194 profile_->GetPrefs()->SetString(time_pref.c_str(), time_as_str); |
| 195 profile_->GetPrefs()->SetInt64(time_internal_pref.c_str(), time_as_int); | |
| 176 profile_->GetPrefs()->SetString(status_pref.c_str(), error); | 196 profile_->GetPrefs()->SetString(status_pref.c_str(), error); |
| 177 | 197 |
| 178 NotifyObservers(); | 198 NotifyObservers(); |
| 179 } | 199 } |
| 180 | 200 |
| 181 // While clearing tokens, we don't update the time or status. | 201 // While clearing tokens, we don't update the time or status. |
| 182 void AboutSigninInternals::NotifyClearStoredToken( | 202 void AboutSigninInternals::NotifyClearStoredToken( |
| 183 const std::string& token_name) { | 203 const std::string& token_name) { |
| 184 // This should have been initialized already. | 204 // This should have been initialized already. |
| 185 DCHECK(signin_status_.token_info_map.count(token_name)); | 205 DCHECK(signin_status_.token_info_map.count(token_name)); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 213 | 233 |
| 214 void AboutSigninInternals::NotifyObservers() { | 234 void AboutSigninInternals::NotifyObservers() { |
| 215 FOR_EACH_OBSERVER(AboutSigninInternals::Observer, | 235 FOR_EACH_OBSERVER(AboutSigninInternals::Observer, |
| 216 signin_observers_, | 236 signin_observers_, |
| 217 OnSigninStateChanged(signin_status_.ToValue())); | 237 OnSigninStateChanged(signin_status_.ToValue())); |
| 218 } | 238 } |
| 219 | 239 |
| 220 scoped_ptr<DictionaryValue> AboutSigninInternals::GetSigninStatus() { | 240 scoped_ptr<DictionaryValue> AboutSigninInternals::GetSigninStatus() { |
| 221 return signin_status_.ToValue().Pass(); | 241 return signin_status_.ToValue().Pass(); |
| 222 } | 242 } |
| 243 | |
| 244 base::Time AboutSigninInternals::GetTokenTime( | |
| 245 const std::string& token_name) const { | |
| 246 TokenInfoMap::const_iterator iter = | |
| 247 signin_status_.token_info_map.find(token_name); | |
| 248 if (iter == signin_status_.token_info_map.end()) | |
| 249 return base::Time(); | |
| 250 return base::Time::FromInternalValue(iter->second.time_internal); | |
| 251 } | |
| OLD | NEW |