Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 "chrome/browser/extensions/token_cache/token_cache.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "chrome/common/chrome_notification_types.h" | |
| 9 | |
| 10 using base::Time; | |
| 11 using base::TimeDelta; | |
| 12 | |
| 13 namespace extensions { | |
| 14 | |
| 15 TokenCacheService::TokenCacheService(Profile* profile) : profile_(profile) { | |
| 16 registrar_.Add(this, | |
| 17 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | |
| 18 content::Source<Profile>(profile_)); | |
| 19 } | |
| 20 | |
| 21 TokenCacheService::~TokenCacheService() { | |
| 22 } | |
| 23 | |
| 24 void TokenCacheService::StoreToken(const std::string& token_name, | |
| 25 const std::string& token_value, | |
| 26 base::TimeDelta time_to_live) { | |
| 27 TokenCacheData token_data; | |
| 28 | |
| 29 // Get the current time, and make sure that the token has not already expired. | |
| 30 Time expiration_time; | |
| 31 TimeDelta zero_delta; | |
| 32 | |
| 33 // Negative time deltas are meaningless to this function. | |
| 34 DCHECK(time_to_live >= zero_delta); | |
| 35 | |
| 36 if (zero_delta < time_to_live) { | |
| 37 expiration_time = Time::Now(); | |
| 38 expiration_time += time_to_live; | |
| 39 } | |
| 40 | |
| 41 token_data.token = token_value; | |
| 42 token_data.expiration_time = expiration_time; | |
| 43 | |
| 44 // Find if the token exists, and if so replace it. | |
| 45 std::map<std::string, TokenCacheData>::iterator it = FindMatch(token_name); | |
|
dcheng
2013/03/05 22:38:44
Why do we need to use FindMatch() for this? Isn't
Pete Williamson
2013/03/06 00:09:40
FindMatch has the desirable side effect of lazily
dcheng
2013/03/06 00:26:35
Even if you remove it, aren't you going to immedia
Pete Williamson
2013/03/06 00:50:05
No, we don't always populate it again. In the ret
dcheng
2013/03/06 02:30:25
I still don't understand. I'm talking specifically
| |
| 46 if (it != token_cache_.end()) { | |
| 47 token_cache_[token_name] = token_data; | |
| 48 return; | |
| 49 } | |
| 50 // Otherwise, add the token to our cache. | |
| 51 token_cache_[token_name] = token_data; | |
| 52 } | |
| 53 | |
| 54 // Retrieve a token for the currently logged in user. This returns an empty | |
| 55 // string if the token was not found or timed out. | |
| 56 std::string TokenCacheService::RetrieveToken(const std::string& token_name) { | |
| 57 std::map<std::string, TokenCacheData>::iterator it = FindMatch(token_name); | |
| 58 | |
| 59 if (it != token_cache_.end()) { | |
| 60 return it->second.token; | |
| 61 } | |
| 62 | |
| 63 return std::string(); | |
| 64 } | |
| 65 | |
| 66 // Inherited from ProfileKeyedService. | |
| 67 void TokenCacheService::Shutdown() { | |
|
dcheng
2013/03/05 22:38:44
You can remove this, since it simply matches the d
Pete Williamson
2013/03/06 00:09:40
Done.
| |
| 68 // Nothing to do. | |
| 69 } | |
| 70 | |
| 71 // Inherited from NotificationObserver. | |
| 72 void TokenCacheService::Observe(int type, | |
| 73 const content::NotificationSource& source, | |
| 74 const content::NotificationDetails& details) { | |
| 75 if (type == chrome::NOTIFICATION_GOOGLE_SIGNED_OUT) | |
| 76 token_cache_.clear(); | |
| 77 } | |
| 78 | |
| 79 std::map<std::string, TokenCacheData>::iterator TokenCacheService::FindMatch( | |
|
dcheng
2013/03/05 22:38:44
Just move this logic into RetrieveToken, since thi
Pete Williamson
2013/03/06 00:09:40
I use this from the unit test also, so I'd like to
dcheng
2013/03/06 00:26:35
Looking at the unit test, it seems like it'd be be
Pete Williamson
2013/03/06 00:50:05
The advantage of FindMatch in the unit test is tha
| |
| 80 const std::string& token_name) { | |
| 81 | |
|
dcheng
2013/03/05 22:38:44
Extra newline.
Pete Williamson
2013/03/06 00:09:40
Done.
| |
| 82 std::map<std::string, TokenCacheData>::iterator it; | |
| 83 | |
| 84 it = token_cache_.find(token_name); | |
|
dcheng
2013/03/05 22:38:44
Combine declaration and initialization.
Pete Williamson
2013/03/06 00:09:40
Done.
| |
| 85 | |
| 86 if (it != token_cache_.end()) { | |
| 87 Time now = Time::Now(); | |
| 88 Time zero; | |
|
dcheng
2013/03/05 22:38:44
Unused.
Pete Williamson
2013/03/06 00:09:40
Done.
| |
| 89 if (it->second.expiration_time.is_null() || | |
| 90 now < it->second.expiration_time) { | |
| 91 return it; | |
| 92 } else { | |
| 93 // Remove this entry if it is expired. | |
| 94 token_cache_.erase(it); | |
| 95 return token_cache_.end(); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 return token_cache_.end(); | |
| 100 } | |
| 101 | |
| 102 | |
| 103 } // namespace extensions | |
| OLD | NEW |