Index: chrome/browser/extensions/api/token_cache/token_cache.cc |
diff --git a/chrome/browser/extensions/api/token_cache/token_cache.cc b/chrome/browser/extensions/api/token_cache/token_cache.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..483e02b60359023276f328b8fad4e3caa12e52dd |
--- /dev/null |
+++ b/chrome/browser/extensions/api/token_cache/token_cache.cc |
@@ -0,0 +1,99 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/api/token_cache/token_cache.h" |
+ |
+#include "base/logging.h" |
+#include "chrome/common/chrome_notification_types.h" |
+ |
+using base::Time; |
+using base::TimeDelta; |
+ |
+// TODO: Should I put this into a namespace? which one? |
+ |
+TokenCacheService::TokenCacheService(Profile* profile) : profile_(profile) { |
+ registrar_.Add(this, |
+ chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
+ content::Source<Profile>(profile_)); |
+} |
+ |
+TokenCacheService::~TokenCacheService() { |
+} |
+ |
+void TokenCacheService::StoreToken(const std::string& token_name, |
+ const std::string& token_value, |
+ const base::TimeDelta time_to_live) { |
+ TokenCacheData token_data; |
+ |
+ // Get the current time, and make sure that the token has not already expired. |
+ Time expiration_time; |
+ TimeDelta zeroDelta; |
dcheng
2013/03/04 22:40:59
Naming.
Pete Williamson
2013/03/05 19:42:25
Done.
|
+ |
+ if (zeroDelta != time_to_live) { |
+ expiration_time = Time::Now(); |
+ expiration_time += time_to_live; |
+ } |
+ |
+ token_data.token_name = token_name; |
+ token_data.token = token_value; |
+ token_data.expiration_time = expiration_time; |
+ |
+ // Find if the token exists, and if so replace it. |
+ std::vector<TokenCacheData>::iterator it = FindMatch(token_name); |
+ if (it != token_cache_.end()) { |
+ *it = token_data; |
+ return; |
+ } |
+ // Otherwise, add the token to our cache. |
+ token_cache_.push_back(token_data); |
+} |
+ |
+// Retrieve a token for the currently logged in user. This returns an empty |
+// string if the token was not found or timed out. |
+std::string TokenCacheService::RetrieveToken(const std::string& token_name) { |
+ std::vector<TokenCacheData>::iterator it = FindMatch(token_name); |
+ |
+ if (it != token_cache_.end()) { |
+ return it->token; |
+ } |
+ |
+ return std::string(); |
+} |
+ |
+// Inherited from ProfileKeyedService. |
+void TokenCacheService::Shutdown() { |
+ 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.
|
+} |
+ |
+// Inherited from NotificationObserver. |
+void TokenCacheService::Observe(int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) { |
+ if (type == chrome::NOTIFICATION_GOOGLE_SIGNED_OUT) |
+ token_cache_.clear(); |
+} |
+ |
+// TODO(petewil): Replace this with find_if, functor, and bind first argument, |
+// that should be more elegant and possibly more performant. |
+std::vector<TokenCacheData>::iterator TokenCacheService::FindMatch( |
+ const std::string& token_name) { |
+ |
+ std::vector<TokenCacheData>::iterator it; |
+ |
+ for (it = token_cache_.begin(); token_cache_.end() != it; ++it) { |
+ if (0 == token_name.compare(it->token_name)) { |
+ Time now = Time::Now(); |
+ Time zero; |
+ if (it->expiration_time.is_null() || now < it->expiration_time) { |
+ return it; |
+ } else { |
+ // Remove this entry if it is expired. |
+ token_cache_.erase(it); |
+ return token_cache_.end(); |
+ } |
+ } |
+ } |
+ |
+ return token_cache_.end(); |
+} |