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/api/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 // TODO: Should I put this into a namespace? which one? | |
| 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 const 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 zeroDelta; | |
|
dcheng
2013/03/04 22:40:59
Naming.
Pete Williamson
2013/03/05 19:42:25
Done.
| |
| 32 | |
| 33 if (zeroDelta != time_to_live) { | |
| 34 expiration_time = Time::Now(); | |
| 35 expiration_time += time_to_live; | |
| 36 } | |
| 37 | |
| 38 token_data.token_name = token_name; | |
| 39 token_data.token = token_value; | |
| 40 token_data.expiration_time = expiration_time; | |
| 41 | |
| 42 // Find if the token exists, and if so replace it. | |
| 43 std::vector<TokenCacheData>::iterator it = FindMatch(token_name); | |
| 44 if (it != token_cache_.end()) { | |
| 45 *it = token_data; | |
| 46 return; | |
| 47 } | |
| 48 // Otherwise, add the token to our cache. | |
| 49 token_cache_.push_back(token_data); | |
| 50 } | |
| 51 | |
| 52 // Retrieve a token for the currently logged in user. This returns an empty | |
| 53 // string if the token was not found or timed out. | |
| 54 std::string TokenCacheService::RetrieveToken(const std::string& token_name) { | |
| 55 std::vector<TokenCacheData>::iterator it = FindMatch(token_name); | |
| 56 | |
| 57 if (it != token_cache_.end()) { | |
| 58 return it->token; | |
| 59 } | |
| 60 | |
| 61 return std::string(); | |
| 62 } | |
| 63 | |
| 64 // Inherited from ProfileKeyedService. | |
| 65 void TokenCacheService::Shutdown() { | |
| 66 token_cache_.clear(); | |
|
dcheng
2013/03/04 22:40:59
Do we need this method? Can't we just let it happe
Pete Williamson
2013/03/05 19:42:25
Done.
| |
| 67 } | |
| 68 | |
| 69 // Inherited from NotificationObserver. | |
| 70 void TokenCacheService::Observe(int type, | |
| 71 const content::NotificationSource& source, | |
| 72 const content::NotificationDetails& details) { | |
| 73 if (type == chrome::NOTIFICATION_GOOGLE_SIGNED_OUT) | |
| 74 token_cache_.clear(); | |
| 75 } | |
| 76 | |
| 77 // TODO(petewil): Replace this with find_if, functor, and bind first argument, | |
| 78 // that should be more elegant and possibly more performant. | |
| 79 std::vector<TokenCacheData>::iterator TokenCacheService::FindMatch( | |
| 80 const std::string& token_name) { | |
| 81 | |
| 82 std::vector<TokenCacheData>::iterator it; | |
| 83 | |
| 84 for (it = token_cache_.begin(); token_cache_.end() != it; ++it) { | |
| 85 if (0 == token_name.compare(it->token_name)) { | |
| 86 Time now = Time::Now(); | |
| 87 Time zero; | |
| 88 if (it->expiration_time.is_null() || now < it->expiration_time) { | |
| 89 return it; | |
| 90 } else { | |
| 91 // Remove this entry if it is expired. | |
| 92 token_cache_.erase(it); | |
| 93 return token_cache_.end(); | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 return token_cache_.end(); | |
| 99 } | |
| OLD | NEW |