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

Side by Side 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, 9 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 unified diff | Download patch
OLDNEW
(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/token_cache/token_cache.h"
6
7 #include "base/logging.h"
8 #include "chrome/common/chrome_notification_types.h"
9 #include "content/public/browser/notification_source.h"
10
11 using base::Time;
12 using base::TimeDelta;
13
14 namespace extensions {
15
16 TokenCacheService::TokenCacheService(Profile* profile) : profile_(profile) {
17 registrar_.Add(this,
18 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT,
19 content::Source<Profile>(profile_));
20 }
21
22 TokenCacheService::~TokenCacheService() {
23 }
24
25 void TokenCacheService::StoreToken(const std::string& token_name,
26 const std::string& token_value,
27 base::TimeDelta time_to_live) {
28 TokenCacheData token_data;
29
30 // Get the current time, and make sure that the token has not already expired.
31 Time expiration_time;
32 TimeDelta zero_delta;
33
34 // Negative time deltas are meaningless to this function.
35 DCHECK(time_to_live >= zero_delta);
36
37 if (zero_delta < time_to_live) {
38 expiration_time = Time::Now();
39 expiration_time += time_to_live;
40 }
41
42 token_data.token = token_value;
43 token_data.expiration_time = expiration_time;
44
45 // Find if the token exists, and if so replace it.
46 std::map<std::string, TokenCacheData>::iterator it = FindMatch(token_name);
47 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.
48 token_cache_[token_name] = token_data;
49 return;
50 }
51 // Otherwise, add the token to our cache.
52 token_cache_[token_name] = token_data;
53 }
54
55 // Retrieve a token for the currently logged in user. This returns an empty
56 // string if the token was not found or timed out.
57 std::string TokenCacheService::RetrieveToken(const std::string& token_name) {
58 std::map<std::string, TokenCacheData>::iterator it = FindMatch(token_name);
59
60 if (it != token_cache_.end()) {
61 return it->second.token;
62 }
63
64 return std::string();
65 }
66
67 // Inherited from NotificationObserver.
68 void TokenCacheService::Observe(int type,
69 const content::NotificationSource& source,
70 const content::NotificationDetails& details) {
71 if (type == chrome::NOTIFICATION_GOOGLE_SIGNED_OUT)
72 token_cache_.clear();
73 }
74
75 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.
76 const std::string& token_name) {
77 std::map<std::string, TokenCacheData>::iterator it =
78 token_cache_.find(token_name);
79
80 if (it != token_cache_.end()) {
81 Time now = Time::Now();
82 if (it->second.expiration_time.is_null() ||
83 now < it->second.expiration_time) {
84 return it;
85 } else {
86 // Remove this entry if it is expired.
87 token_cache_.erase(it);
88 return token_cache_.end();
89 }
90 }
91
92 return token_cache_.end();
93 }
94
Andrew T Wilson (Slow) 2013/03/06 16:55:28 nit: remove superfluous blank line
Pete Williamson 2013/03/06 19:04:31 Done.
95
96 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698