Index: chrome/browser/token_cache/token_cache.cc |
diff --git a/chrome/browser/token_cache/token_cache.cc b/chrome/browser/token_cache/token_cache.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..95dae187ff0ab91cf3beae88a4009b34ebcde630 |
--- /dev/null |
+++ b/chrome/browser/token_cache/token_cache.cc |
@@ -0,0 +1,104 @@ |
+// 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/token_cache/token_cache.h" |
+ |
+#include <algorithm> |
dcheng
2013/02/28 19:18:04
Are you actually using this header?
Pete Williamson
2013/03/04 18:32:53
Done.
|
+ |
+#include "base/logging.h" |
+#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.
|
+#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 int64 expiration_timeout) { |
+ TokenCacheData token_data; |
+ |
+ // Get the current time, and make sure that the token has not already expired. |
+ Time expiration_time; |
+ Time now = Time::Now(); |
+ |
+ if (expiration_timeout > 0) { |
+ 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.
|
+ expiration_time = now + delta; |
+ } 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.
|
+ return; |
+ } |
+ |
+ 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/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
|
+} |
+ |
+// 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; |
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.
|
+ if (zero == it->expiration_time || 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(); |
+} |