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/token_cache/token_cache_service.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 // Add the token to our cache, overwriting any existing token with this name. | |
46 token_cache_[token_name] = token_data; | |
47 } | |
48 | |
49 // Retrieve a token for the currently logged in user. This returns an empty | |
50 // string if the token was not found or timed out. | |
51 std::string TokenCacheService::RetrieveToken(const std::string& token_name) { | |
52 std::map<std::string, TokenCacheData>::iterator it = | |
53 token_cache_.find(token_name); | |
54 | |
55 if (it != token_cache_.end()) { | |
56 Time now = Time::Now(); | |
57 if (it->second.expiration_time.is_null() || | |
58 now < it->second.expiration_time) { | |
59 return it->second.token; | |
60 } else { | |
61 // Remove this entry if it is expired. | |
62 token_cache_.erase(it); | |
63 return std::string(); | |
64 } | |
65 } | |
66 | |
67 return std::string(); | |
68 } | |
69 | |
70 // Inherited from NotificationObserver. | |
71 void TokenCacheService::Observe(int type, | |
72 const content::NotificationSource& source, | |
73 const content::NotificationDetails& details) { | |
74 if (type == chrome::NOTIFICATION_GOOGLE_SIGNED_OUT) | |
Andrew T Wilson (Slow)
2013/03/07 11:48:09
I'd make this:
DCHECK_EQ(type, chrome::NOTIFICATI
Pete Williamson
2013/03/07 20:02:46
Done.
| |
75 token_cache_.clear(); | |
76 } | |
77 | |
78 } // namespace extensions | |
OLD | NEW |