OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_ | |
6 #define CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/file_path.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "chrome/browser/policy/cloud_policy_data_store.h" | |
15 | |
16 namespace policy { | |
17 | |
18 // Handles disk access and threading details for loading and storing tokens. | |
19 class UserPolicyTokenLoader | |
20 : public base::RefCountedThreadSafe<UserPolicyTokenLoader> { | |
21 public: | |
22 // Callback interface for reporting a successfull load. | |
23 class Delegate { | |
24 public: | |
25 virtual ~Delegate(); | |
26 virtual void OnTokenLoaded(const std::string& token, | |
27 const std::string& device_id) = 0; | |
28 }; | |
29 | |
30 UserPolicyTokenLoader(const base::WeakPtr<Delegate>& delegate, | |
31 const FilePath& cache_file); | |
32 | |
33 // Starts loading the disk cache. After the load is finished, the result is | |
34 // reported through the delegate. | |
35 void Load(); | |
36 | |
37 // Stores credentials asynchronously to disk. | |
38 void Store(const std::string& token, const std::string& device_id); | |
39 | |
40 private: | |
41 friend class base::RefCountedThreadSafe<UserPolicyTokenLoader>; | |
42 ~UserPolicyTokenLoader(); | |
43 | |
44 void LoadOnFileThread(); | |
45 void NotifyOnUIThread(const std::string& token, | |
46 const std::string& device_id); | |
47 void StoreOnFileThread(const std::string& token, | |
48 const std::string& device_id); | |
49 | |
50 const base::WeakPtr<Delegate> delegate_; | |
51 const FilePath cache_file_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(UserPolicyTokenLoader); | |
54 }; | |
55 | |
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 | |
89 | |
90 #endif // CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_ | |
OLD | NEW |