Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_POLICY_USER_POLICY_TOKEN_CACHE_H_ | 5 #ifndef CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_ | 
| 6 #define CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_ | 6 #define CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_ | 
| 7 | 7 | 
| 8 #include <string> | 8 #include <string> | 
| 9 | 9 | 
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" | 
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" | 
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" | 
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" | 
| 14 #include "chrome/browser/policy/cloud_policy_data_store.h" | |
| 15 | 14 | 
| 16 namespace policy { | 15 namespace policy { | 
| 17 | 16 | 
| 18 // Handles disk access and threading details for loading and storing tokens. | 17 // Handles disk access and threading details for loading and storing tokens. | 
| 19 class UserPolicyTokenLoader | 18 class UserPolicyTokenLoader | 
| 
 
Joao da Silva
2013/01/17 09:38:05
Rename this file to user_policy_token_loader.h?
 
Mattias Nissler (ping if slow)
2013/01/22 10:31:39
Done.
 
 | |
| 20 : public base::RefCountedThreadSafe<UserPolicyTokenLoader> { | 19 : public base::RefCountedThreadSafe<UserPolicyTokenLoader> { | 
| 21 public: | 20 public: | 
| 22 // Callback interface for reporting a successfull load. | 21 // Callback interface for reporting a successfull load. | 
| 23 class Delegate { | 22 class Delegate { | 
| 24 public: | 23 public: | 
| 25 virtual ~Delegate(); | 24 virtual ~Delegate(); | 
| 26 virtual void OnTokenLoaded(const std::string& token, | 25 virtual void OnTokenLoaded(const std::string& token, | 
| 27 const std::string& device_id) = 0; | 26 const std::string& device_id) = 0; | 
| 28 }; | 27 }; | 
| 29 | 28 | 
| 30 UserPolicyTokenLoader(const base::WeakPtr<Delegate>& delegate, | 29 UserPolicyTokenLoader(const base::WeakPtr<Delegate>& delegate, | 
| 31 const FilePath& cache_file); | 30 const FilePath& cache_file); | 
| 32 | 31 | 
| 33 // Starts loading the disk cache. After the load is finished, the result is | 32 // Starts loading the disk cache. After the load is finished, the result is | 
| 34 // reported through the delegate. | 33 // reported through the delegate. | 
| 35 void Load(); | 34 void Load(); | 
| 36 | 35 | 
| 37 // Stores credentials asynchronously to disk. | |
| 38 void Store(const std::string& token, const std::string& device_id); | |
| 39 | |
| 40 private: | 36 private: | 
| 41 friend class base::RefCountedThreadSafe<UserPolicyTokenLoader>; | 37 friend class base::RefCountedThreadSafe<UserPolicyTokenLoader>; | 
| 42 ~UserPolicyTokenLoader(); | 38 ~UserPolicyTokenLoader(); | 
| 43 | 39 | 
| 44 void LoadOnFileThread(); | 40 void LoadOnFileThread(); | 
| 45 void NotifyOnUIThread(const std::string& token, | 41 void NotifyOnUIThread(const std::string& token, | 
| 46 const std::string& device_id); | 42 const std::string& device_id); | 
| 47 void StoreOnFileThread(const std::string& token, | |
| 48 const std::string& device_id); | |
| 49 | 43 | 
| 50 const base::WeakPtr<Delegate> delegate_; | 44 const base::WeakPtr<Delegate> delegate_; | 
| 51 const FilePath cache_file_; | 45 const FilePath cache_file_; | 
| 52 | 46 | 
| 53 DISALLOW_COPY_AND_ASSIGN(UserPolicyTokenLoader); | 47 DISALLOW_COPY_AND_ASSIGN(UserPolicyTokenLoader); | 
| 54 }; | 48 }; | 
| 55 | 49 | 
| 56 // Responsible for managing the on-disk token cache. | |
| 57 class UserPolicyTokenCache : public CloudPolicyDataStore::Observer, | |
| 58 public UserPolicyTokenLoader::Delegate { | |
| 59 public: | |
| 60 UserPolicyTokenCache(CloudPolicyDataStore* data_store, | |
| 61 const FilePath& cache_file); | |
| 62 virtual ~UserPolicyTokenCache(); | |
| 63 | |
| 64 // Starts loading the disk cache and installs the token in the data store. | |
| 65 void Load(); | |
| 66 | |
| 67 // UserPolicyTokenLoader::Delegate implementation: | |
| 68 virtual void OnTokenLoaded(const std::string& token, | |
| 69 const std::string& device_id) OVERRIDE; | |
| 70 | |
| 71 // CloudPolicyDataStore::Observer implementation: | |
| 72 virtual void OnDeviceTokenChanged() OVERRIDE; | |
| 73 virtual void OnCredentialsChanged() OVERRIDE; | |
| 74 | |
| 75 private: | |
| 76 base::WeakPtrFactory<UserPolicyTokenLoader::Delegate> weak_ptr_factory_; | |
| 77 | |
| 78 CloudPolicyDataStore* data_store_; | |
| 79 scoped_refptr<UserPolicyTokenLoader> loader_; | |
| 80 | |
| 81 // The current token in the cache. Upon new token notifications, we compare | |
| 82 // against this in order to avoid writing the file when there's no change. | |
| 83 std::string token_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(UserPolicyTokenCache); | |
| 86 }; | |
| 87 | |
| 88 } // namespace policy | 50 } // namespace policy | 
| 89 | 51 | 
| 90 #endif // CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_ | 52 #endif // CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_ | 
| OLD | NEW |