12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef CHROME_BROWSER_EXTENSIONS_TOKEN_CACHE_TOKEN_CACHE_SERVICE_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define CHROME_BROWSER_EXTENSIONS_TOKEN_CACHE_TOKEN_CACHE_SERVICE_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <map>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/basictypes.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/compiler_specific.h"
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
15effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/signin_manager_base.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Profile;
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace extensions {
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This class caches tokens for the current user.  It will clear tokens out
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// when the user logs out or after the specified timeout interval, or when
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the instance of chrome shuts down.
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class TokenCacheService : public KeyedService,
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                          public SigninManagerBase::Observer {
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit TokenCacheService(Profile* profile);
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~TokenCacheService();
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Store a token for the currently logged in user. We will look it up later by
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the name given here, and we will expire the token after the timeout.  For a
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // timeout of 0, we never expire the token.  After time_to_live expires, the
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // token will be expired.
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void StoreToken(const std::string& token_name, const std::string& token_value,
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  base::TimeDelta time_to_live);
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Retrieve a token for the currently logged in user.  This returns an empty
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // string if the token was not found or timed out.
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string RetrieveToken(const std::string& token_name);
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // KeyedService:
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void Shutdown() OVERRIDE;
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class TokenCacheTest;
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(TokenCacheTest, SignoutTest);
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // SigninManagerBase::Observer:
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void GoogleSignedOut(const std::string& account_id,
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                               const std::string& username) OVERRIDE;
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  struct TokenCacheData {
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::string token;
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::Time expiration_time;
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Map the token name (string) to token data.
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::map<std::string, TokenCacheData> token_cache_;
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Profile* const profile_;
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TokenCacheService);
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace extensions
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // CHROME_BROWSER_EXTENSIONS_TOKEN_CACHE_TOKEN_CACHE_SERVICE_H_
67