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/token_cache/token_cache.h" | |
| 6 | |
| 7 #include <algorithm> | |
|
dcheng
2013/02/28 19:18:04
Are you actually using this header?
Pete Williamson
2013/03/04 18:32:53
Done.
| |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/time.h" | |
|
dcheng
2013/02/28 19:18:04
Typically, people only #include a file in the head
Pete Williamson
2013/03/04 18:32:53
Done.
| |
| 11 #include "chrome/common/chrome_notification_types.h" | |
| 12 | |
| 13 using base::Time; | |
| 14 using base::TimeDelta; | |
| 15 | |
| 16 // TODO: Should I put this into a namespace? which one? | |
| 17 | |
| 18 TokenCacheService::TokenCacheService(Profile* profile) : profile_(profile) { | |
| 19 registrar_.Add(this, | |
| 20 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | |
| 21 content::Source<Profile>(profile_)); | |
| 22 } | |
| 23 | |
| 24 TokenCacheService::~TokenCacheService() { | |
| 25 } | |
| 26 | |
| 27 void TokenCacheService::StoreToken(const std::string& token_name, | |
| 28 const std::string& token_value, | |
| 29 const int64 expiration_timeout) { | |
| 30 TokenCacheData token_data; | |
| 31 | |
| 32 // Get the current time, and make sure that the token has not already expired. | |
| 33 Time expiration_time; | |
| 34 Time now = Time::Now(); | |
| 35 | |
| 36 if (expiration_timeout > 0) { | |
| 37 TimeDelta delta = TimeDelta::FromMilliseconds(expiration_timeout); | |
|
dcheng
2013/02/28 19:18:04
I think second granularity is sufficient.
Pete Williamson
2013/03/04 18:32:53
I'm using time delta now, so it doesn't matter.
| |
| 38 expiration_time = now + delta; | |
| 39 } else if (expiration_timeout < 0) { | |
|
dcheng
2013/02/28 19:18:04
I think it is nonsensical to pass in a negative ti
Pete Williamson
2013/03/04 18:32:53
using time delta now, no need to check
dcheng
2013/03/04 22:40:59
Well, TimeDelta can still be negative. It's probab
Pete Williamson
2013/03/05 19:42:25
Done.
| |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 token_data.token_name = token_name; | |
| 44 token_data.token = token_value; | |
| 45 token_data.expiration_time = expiration_time; | |
| 46 | |
| 47 // Find if the token exists, and if so replace it. | |
| 48 std::vector<TokenCacheData>::iterator it = FindMatch(token_name); | |
| 49 if (it != token_cache_.end()) { | |
| 50 *it = token_data; | |
| 51 return; | |
| 52 } | |
| 53 // Otherwise, add the token to our cache. | |
| 54 token_cache_.push_back(token_data); | |
| 55 } | |
| 56 | |
| 57 // Retrieve a token for the currently logged in user. This returns an empty | |
| 58 // string if the token was not found or timed out. | |
| 59 std::string TokenCacheService::RetrieveToken(const std::string& token_name) { | |
| 60 std::vector<TokenCacheData>::iterator it = FindMatch(token_name); | |
| 61 | |
| 62 if (it != token_cache_.end()) { | |
| 63 return it->token; | |
| 64 } | |
| 65 | |
| 66 return std::string(); | |
| 67 } | |
| 68 | |
| 69 // Inherited from ProfileKeyedService. | |
| 70 void TokenCacheService::Shutdown() { | |
| 71 token_cache_.clear(); | |
|
dcheng
2013/02/28 19:18:04
So we don't persist this to disk at all anymore? I
Pete Williamson
2013/03/04 18:32:53
Right, this is not persisted to disk. You could
| |
| 72 } | |
| 73 | |
| 74 // Inherited from NotificationObserver. | |
| 75 void TokenCacheService::Observe(int type, | |
| 76 const content::NotificationSource& source, | |
| 77 const content::NotificationDetails& details) { | |
| 78 if (type == chrome::NOTIFICATION_GOOGLE_SIGNED_OUT) | |
| 79 token_cache_.clear(); | |
| 80 } | |
| 81 | |
| 82 // TODO(petewil): Replace this with find_if, functor, and bind first argument, | |
| 83 // that should be more elegant and possibly more performant. | |
| 84 std::vector<TokenCacheData>::iterator TokenCacheService::FindMatch( | |
| 85 const std::string& token_name) { | |
| 86 | |
| 87 std::vector<TokenCacheData>::iterator it; | |
| 88 | |
| 89 for (it = token_cache_.begin(); token_cache_.end() != it; ++it) { | |
| 90 if (0 == token_name.compare(it->token_name)) { | |
| 91 Time now = Time::Now(); | |
| 92 Time zero; | |
|
dcheng
2013/02/28 19:18:04
A simpler way would be to use time.is_null().
Pete Williamson
2013/03/04 18:32:53
Done.
| |
| 93 if (zero == it->expiration_time || now < it->expiration_time) { | |
| 94 return it; | |
| 95 } else { | |
| 96 // Remove this entry if it is expired. | |
| 97 token_cache_.erase(it); | |
| 98 return token_cache_.end(); | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 return token_cache_.end(); | |
| 104 } | |
| OLD | NEW |