Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / token_cache / token_cache_service.cc
blob9d5aabd3b403aa91c6d5ba9060fa497cf85a91ca
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.
5 #include "chrome/browser/extensions/token_cache/token_cache_service.h"
7 #include "base/logging.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "content/public/browser/notification_source.h"
11 using base::Time;
12 using base::TimeDelta;
14 namespace extensions {
16 TokenCacheService::TokenCacheService(Profile* profile) : profile_(profile) {
17 registrar_.Add(this,
18 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT,
19 content::Source<Profile>(profile_));
22 TokenCacheService::~TokenCacheService() {
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;
30 // Get the current time, and make sure that the token has not already expired.
31 Time expiration_time;
32 TimeDelta zero_delta;
34 // Negative time deltas are meaningless to this function.
35 DCHECK(time_to_live >= zero_delta);
37 if (zero_delta < time_to_live) {
38 expiration_time = Time::Now();
39 expiration_time += time_to_live;
42 token_data.token = token_value;
43 token_data.expiration_time = expiration_time;
45 // Add the token to our cache, overwriting any existing token with this name.
46 token_cache_[token_name] = token_data;
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);
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();
67 return std::string();
70 // Inherited from NotificationObserver.
71 void TokenCacheService::Observe(int type,
72 const content::NotificationSource& source,
73 const content::NotificationDetails& details) {
74 DCHECK(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT == type);
75 token_cache_.clear();
78 } // namespace extensions