[email protected] | 8b96de12 | 2010-02-15 15:15:22 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
[email protected] | e1d4edb | 2010-02-17 17:33:56 | [diff] [blame^] | 5 | // Defines the Geolocation access token store, and associated factory. |
| 6 | // An access token store is responsible for providing the API to persist a |
| 7 | // single network provider's access token. The factory is responsible for |
| 8 | // first loading tokens when required, and creating the associated token stores. |
| 9 | // The API is a little more complex than one might wish, due to the need for |
| 10 | // prefs access to happen asynchronously on the UI thread. |
| 11 | // These APIs are provided as abstract base classes to allow mocking and testing |
| 12 | // of clients, without dependency on browser process singleton objects etc. |
| 13 | |
[email protected] | 8b96de12 | 2010-02-15 15:15:22 | [diff] [blame] | 14 | #ifndef CHROME_BROWSER_GEOLOCATION_ACCESS_TOKEN_STORE_H_ |
| 15 | #define CHROME_BROWSER_GEOLOCATION_ACCESS_TOKEN_STORE_H_ |
| 16 | |
[email protected] | e1d4edb | 2010-02-17 17:33:56 | [diff] [blame^] | 17 | #include <map> |
| 18 | |
[email protected] | 8b96de12 | 2010-02-15 15:15:22 | [diff] [blame] | 19 | #include "base/ref_counted.h" |
| 20 | #include "base/string16.h" |
[email protected] | e1d4edb | 2010-02-17 17:33:56 | [diff] [blame^] | 21 | #include "base/weak_ptr.h" |
| 22 | #include "googleurl/src/gurl.h" |
[email protected] | 8b96de12 | 2010-02-15 15:15:22 | [diff] [blame] | 23 | |
| 24 | class GURL; |
| 25 | class PrefService; |
| 26 | |
| 27 | // Provides storage for the access token used in the network request. |
[email protected] | e1d4edb | 2010-02-17 17:33:56 | [diff] [blame^] | 28 | class AccessTokenStore : public base::RefCountedThreadSafe<AccessTokenStore> { |
[email protected] | 8b96de12 | 2010-02-15 15:15:22 | [diff] [blame] | 29 | public: |
| 30 | static void RegisterPrefs(PrefService* prefs); |
| 31 | |
[email protected] | e1d4edb | 2010-02-17 17:33:56 | [diff] [blame^] | 32 | GURL server_url() const; |
| 33 | void SetAccessToken(const string16& access_token); |
| 34 | bool GetAccessToken(string16* access_token) const; |
[email protected] | 8b96de12 | 2010-02-15 15:15:22 | [diff] [blame] | 35 | |
| 36 | protected: |
[email protected] | e1d4edb | 2010-02-17 17:33:56 | [diff] [blame^] | 37 | friend class base::RefCountedThreadSafe<AccessTokenStore>; |
| 38 | AccessTokenStore( |
| 39 | const GURL& url, bool token_valid, const string16& initial_access_token); |
| 40 | virtual ~AccessTokenStore(); |
| 41 | |
| 42 | virtual void DoSetAccessToken(const string16& access_token) = 0; |
| 43 | |
| 44 | private: |
| 45 | const GURL url_; |
| 46 | bool access_token_valid_; |
| 47 | string16 access_token_; |
| 48 | }; |
| 49 | |
| 50 | // Factory for access token stores. Asynchronously loads all access tokens, and |
| 51 | // calls back with a set of token stores one per server URL. |
| 52 | class AccessTokenStoreFactory |
| 53 | : public base::RefCountedThreadSafe<AccessTokenStoreFactory> { |
| 54 | public: |
| 55 | typedef std::map<GURL, scoped_refptr<AccessTokenStore> > TokenStoreSet; |
| 56 | class Delegate { |
| 57 | public: |
| 58 | virtual void OnAccessTokenStoresCreated( |
| 59 | const TokenStoreSet& access_token_store) = 0; |
| 60 | |
| 61 | protected: |
| 62 | virtual ~Delegate() {} |
| 63 | }; |
| 64 | // delegate will recieve a single callback once existing access tokens have |
| 65 | // been loaded from persistent store. |
| 66 | // If default_url is valid, this additional URL will be added to the |
| 67 | // set of access token stores returned. |
| 68 | virtual void CreateAccessTokenStores( |
| 69 | const base::WeakPtr<AccessTokenStoreFactory::Delegate>& delegate, |
| 70 | const GURL& default_url) = 0; |
| 71 | |
| 72 | protected: |
| 73 | friend class base::RefCountedThreadSafe<AccessTokenStoreFactory>; |
| 74 | ~AccessTokenStoreFactory(); |
[email protected] | 8b96de12 | 2010-02-15 15:15:22 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | // Creates a new access token store backed by the global chome prefs. |
[email protected] | e1d4edb | 2010-02-17 17:33:56 | [diff] [blame^] | 78 | AccessTokenStoreFactory* NewChromePrefsAccessTokenStoreFactory(); |
[email protected] | 8b96de12 | 2010-02-15 15:15:22 | [diff] [blame] | 79 | |
| 80 | #endif // CHROME_BROWSER_GEOLOCATION_ACCESS_TOKEN_STORE_H_ |