Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(531)

Unified Diff: chrome/browser/extensions/token_cache/token_cache.cc

Issue 12380006: Build a new TokenCacheService so I can stop using TokenService for something it wasn't designed for. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Token Cache Service - more CR fixes per DCheng Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/token_cache/token_cache.cc
diff --git a/chrome/browser/extensions/token_cache/token_cache.cc b/chrome/browser/extensions/token_cache/token_cache.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cc8ee63aeb18cecba0cfbca8a87311a5ab0ed466
--- /dev/null
+++ b/chrome/browser/extensions/token_cache/token_cache.cc
@@ -0,0 +1,96 @@
+// 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/token_cache/token_cache.h"
+
+#include "base/logging.h"
+#include "chrome/common/chrome_notification_types.h"
+#include "content/public/browser/notification_source.h"
+
+using base::Time;
+using base::TimeDelta;
+
+namespace extensions {
+
+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,
+ 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 zero_delta;
+
+ // Negative time deltas are meaningless to this function.
+ DCHECK(time_to_live >= zero_delta);
+
+ if (zero_delta < time_to_live) {
+ expiration_time = Time::Now();
+ expiration_time += time_to_live;
+ }
+
+ token_data.token = token_value;
+ token_data.expiration_time = expiration_time;
+
+ // Find if the token exists, and if so replace it.
+ std::map<std::string, TokenCacheData>::iterator it = FindMatch(token_name);
+ if (it != token_cache_.end()) {
Andrew T Wilson (Slow) 2013/03/06 16:55:28 Not sure why we are doing this - why not skip the
Pete Williamson 2013/03/06 19:04:31 Done.
+ token_cache_[token_name] = token_data;
+ return;
+ }
+ // Otherwise, add the token to our cache.
+ token_cache_[token_name] = 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::map<std::string, TokenCacheData>::iterator it = FindMatch(token_name);
+
+ if (it != token_cache_.end()) {
+ return it->second.token;
+ }
+
+ return std::string();
+}
+
+// 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();
+}
+
+std::map<std::string, TokenCacheData>::iterator TokenCacheService::FindMatch(
Andrew T Wilson (Slow) 2013/03/06 16:55:28 BTW, you could probably name this more explicitly
Pete Williamson 2013/03/06 19:04:31 I just removed it.
+ const std::string& token_name) {
+ std::map<std::string, TokenCacheData>::iterator it =
+ token_cache_.find(token_name);
+
+ if (it != token_cache_.end()) {
+ Time now = Time::Now();
+ if (it->second.expiration_time.is_null() ||
+ now < it->second.expiration_time) {
+ return it;
+ } else {
+ // Remove this entry if it is expired.
+ token_cache_.erase(it);
+ return token_cache_.end();
+ }
+ }
+
+ return token_cache_.end();
+}
+
Andrew T Wilson (Slow) 2013/03/06 16:55:28 nit: remove superfluous blank line
Pete Williamson 2013/03/06 19:04:31 Done.
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698