OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_TOKEN_CACHE_TOKEN_CACHE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_TOKEN_CACHE_TOKEN_CACHE_SERVICE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_TOKEN_CACHE_TOKEN_CACHE_SERVICE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_TOKEN_CACHE_TOKEN_CACHE_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "components/keyed_service/core/keyed_service.h" | 14 #include "components/keyed_service/core/keyed_service.h" |
15 #include "components/signin/core/browser/signin_manager_base.h" | 15 #include "components/signin/core/browser/signin_manager_base.h" |
16 | 16 |
17 class Profile; | 17 class Profile; |
18 | 18 |
19 namespace extensions { | 19 namespace extensions { |
20 | 20 |
21 // This class caches tokens for the current user. It will clear tokens out | 21 // This class caches tokens for the current user. It will clear tokens out |
22 // when the user logs out or after the specified timeout interval, or when | 22 // when the user logs out or after the specified timeout interval, or when |
23 // the instance of chrome shuts down. | 23 // the instance of chrome shuts down. |
24 class TokenCacheService : public KeyedService, | 24 class TokenCacheService : public KeyedService, |
25 public SigninManagerBase::Observer { | 25 public SigninManagerBase::Observer { |
26 public: | 26 public: |
27 explicit TokenCacheService(Profile* profile); | 27 explicit TokenCacheService(Profile* profile); |
28 virtual ~TokenCacheService(); | 28 ~TokenCacheService() override; |
29 | 29 |
30 // Store a token for the currently logged in user. We will look it up later by | 30 // Store a token for the currently logged in user. We will look it up later by |
31 // the name given here, and we will expire the token after the timeout. For a | 31 // the name given here, and we will expire the token after the timeout. For a |
32 // timeout of 0, we never expire the token. After time_to_live expires, the | 32 // timeout of 0, we never expire the token. After time_to_live expires, the |
33 // token will be expired. | 33 // token will be expired. |
34 void StoreToken(const std::string& token_name, const std::string& token_value, | 34 void StoreToken(const std::string& token_name, const std::string& token_value, |
35 base::TimeDelta time_to_live); | 35 base::TimeDelta time_to_live); |
36 | 36 |
37 // Retrieve a token for the currently logged in user. This returns an empty | 37 // Retrieve a token for the currently logged in user. This returns an empty |
38 // string if the token was not found or timed out. | 38 // string if the token was not found or timed out. |
39 std::string RetrieveToken(const std::string& token_name); | 39 std::string RetrieveToken(const std::string& token_name); |
40 | 40 |
41 // KeyedService: | 41 // KeyedService: |
42 virtual void Shutdown() override; | 42 void Shutdown() override; |
43 | 43 |
44 private: | 44 private: |
45 friend class TokenCacheTest; | 45 friend class TokenCacheTest; |
46 FRIEND_TEST_ALL_PREFIXES(TokenCacheTest, SignoutTest); | 46 FRIEND_TEST_ALL_PREFIXES(TokenCacheTest, SignoutTest); |
47 | 47 |
48 // SigninManagerBase::Observer: | 48 // SigninManagerBase::Observer: |
49 virtual void GoogleSignedOut(const std::string& account_id, | 49 void GoogleSignedOut(const std::string& account_id, |
50 const std::string& username) override; | 50 const std::string& username) override; |
51 | 51 |
52 struct TokenCacheData { | 52 struct TokenCacheData { |
53 std::string token; | 53 std::string token; |
54 base::Time expiration_time; | 54 base::Time expiration_time; |
55 }; | 55 }; |
56 | 56 |
57 // Map the token name (string) to token data. | 57 // Map the token name (string) to token data. |
58 std::map<std::string, TokenCacheData> token_cache_; | 58 std::map<std::string, TokenCacheData> token_cache_; |
59 const Profile* const profile_; | 59 const Profile* const profile_; |
60 | 60 |
61 DISALLOW_COPY_AND_ASSIGN(TokenCacheService); | 61 DISALLOW_COPY_AND_ASSIGN(TokenCacheService); |
62 }; | 62 }; |
63 | 63 |
64 } // namespace extensions | 64 } // namespace extensions |
65 | 65 |
66 #endif // CHROME_BROWSER_EXTENSIONS_TOKEN_CACHE_TOKEN_CACHE_SERVICE_H_ | 66 #endif // CHROME_BROWSER_EXTENSIONS_TOKEN_CACHE_TOKEN_CACHE_SERVICE_H_ |
OLD | NEW |