OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_ |
6 #define CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/callback_list.h" | 13 #include "base/callback_list.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
17 | 16 |
18 namespace base { | |
19 class TaskRunner; | |
20 } | |
21 | |
22 namespace chromeos { | 17 namespace chromeos { |
23 class SessionManagerClient; | 18 class SessionManagerClient; |
24 } | 19 } |
25 | 20 |
26 namespace policy { | 21 namespace policy { |
27 | 22 |
28 // Brokers server-backed FRE state keys for the device. Retrieves them from | 23 // Brokers server-backed FRE state keys for the device. Retrieves them from |
29 // session manager via DBus and refreshes them periodically. Consumers can | 24 // session manager via DBus and refreshes them periodically. Consumers can |
30 // register callbacks to invoke when the state keys change. | 25 // register callbacks to invoke when the state keys change. |
31 class ServerBackedStateKeysBroker { | 26 class ServerBackedStateKeysBroker { |
32 public: | 27 public: |
33 typedef std::unique_ptr<base::CallbackList<void()>::Subscription> | 28 typedef std::unique_ptr<base::CallbackList<void()>::Subscription> |
34 Subscription; | 29 Subscription; |
35 typedef base::Callback<void(const std::vector<std::string>&)> | 30 typedef base::Callback<void(const std::vector<std::string>&)> |
36 StateKeysCallback; | 31 StateKeysCallback; |
37 | 32 |
38 ServerBackedStateKeysBroker( | 33 ServerBackedStateKeysBroker( |
39 chromeos::SessionManagerClient* session_manager_client, | 34 chromeos::SessionManagerClient* session_manager_client); |
40 scoped_refptr<base::TaskRunner> delayed_task_runner); | |
41 ~ServerBackedStateKeysBroker(); | 35 ~ServerBackedStateKeysBroker(); |
42 | 36 |
43 // Registers a callback to be invoked whenever the state keys get updated. | 37 // Registers a callback to be invoked whenever the state keys get updated. |
44 // Note that consuming code needs to hold on to the returned Subscription as | 38 // Note that consuming code needs to hold on to the returned Subscription as |
45 // long as it wants to receive the callback. If the state keys haven't been | 39 // long as it wants to receive the callback. If the state keys haven't been |
46 // requested yet, calling this will also trigger their initial fetch. | 40 // requested yet, calling this will also trigger their initial fetch. |
47 Subscription RegisterUpdateCallback(const base::Closure& callback); | 41 Subscription RegisterUpdateCallback(const base::Closure& callback); |
48 | 42 |
49 // Requests state keys asynchronously. Invokes the passed callback at most | 43 // Requests state keys asynchronously. Invokes the passed callback at most |
50 // once, with the current state keys passed as a parameter to the callback. If | 44 // once, with the current state keys passed as a parameter to the callback. If |
51 // there's a problem determining the state keys, the passed vector will be | 45 // there's a problem determining the state keys, the passed vector will be |
52 // empty. If |this| gets destroyed before the callback happens or if the time | 46 // empty. If |this| gets destroyed before the callback happens or if the time |
53 // sync fails / the network is not established, then the |callback| is never | 47 // sync fails / the network is not established, then the |callback| is never |
54 // invoked. See http://crbug.com/649422 for more context. | 48 // invoked. See http://crbug.com/649422 for more context. |
55 void RequestStateKeys(const StateKeysCallback& callback); | 49 void RequestStateKeys(const StateKeysCallback& callback); |
56 | 50 |
| 51 static base::TimeDelta GetPollIntervalForTesting(); |
| 52 |
57 // Get the set of current state keys. Empty if state keys are unavailable | 53 // Get the set of current state keys. Empty if state keys are unavailable |
58 // or pending retrieval. | 54 // or pending retrieval. |
59 const std::vector<std::string>& state_keys() const { return state_keys_; } | 55 const std::vector<std::string>& state_keys() const { return state_keys_; } |
60 | 56 |
61 // Returns the state key for the current point in time. Returns an empty | 57 // Returns the state key for the current point in time. Returns an empty |
62 // string if state keys are unavailable or pending retrieval. | 58 // string if state keys are unavailable or pending retrieval. |
63 std::string current_state_key() const { | 59 std::string current_state_key() const { |
64 return state_keys_.empty() ? std::string() : state_keys_.front(); | 60 return state_keys_.empty() ? std::string() : state_keys_.front(); |
65 } | 61 } |
66 | 62 |
67 // Whether state key retrieval is pending. | 63 // Whether state key retrieval is pending. |
68 bool pending() const { return !initial_retrieval_completed_; } | 64 bool pending() const { return !initial_retrieval_completed_; } |
69 | 65 |
70 // Whether state keys are available. | 66 // Whether state keys are available. |
71 bool available() const { return !state_keys_.empty(); } | 67 bool available() const { return !state_keys_.empty(); } |
72 | 68 |
73 private: | 69 private: |
74 // Asks |session_manager_client_| to provide current state keys.. | 70 // Asks |session_manager_client_| to provide current state keys.. |
75 void FetchStateKeys(); | 71 void FetchStateKeys(); |
76 | 72 |
77 // Stores newly-received state keys and notifies consumers. | 73 // Stores newly-received state keys and notifies consumers. |
78 void StoreStateKeys(const std::vector<std::string>& state_keys); | 74 void StoreStateKeys(const std::vector<std::string>& state_keys); |
79 | 75 |
80 chromeos::SessionManagerClient* session_manager_client_; | 76 chromeos::SessionManagerClient* session_manager_client_; |
81 | 77 |
82 scoped_refptr<base::TaskRunner> delayed_task_runner_; | |
83 | |
84 // The current set of state keys. | 78 // The current set of state keys. |
85 std::vector<std::string> state_keys_; | 79 std::vector<std::string> state_keys_; |
86 | 80 |
87 // Whether a request for state keys is pending. | 81 // Whether a request for state keys is pending. |
88 bool requested_; | 82 bool requested_; |
89 | 83 |
90 // Whether the initial retrieval operation completed. | 84 // Whether the initial retrieval operation completed. |
91 bool initial_retrieval_completed_; | 85 bool initial_retrieval_completed_; |
92 | 86 |
93 // List of callbacks to receive update notifications. | 87 // List of callbacks to receive update notifications. |
94 base::CallbackList<void()> update_callbacks_; | 88 base::CallbackList<void()> update_callbacks_; |
95 | 89 |
96 // List of pending one-shot state key request callbacks. | 90 // List of pending one-shot state key request callbacks. |
97 std::vector<StateKeysCallback> request_callbacks_; | 91 std::vector<StateKeysCallback> request_callbacks_; |
98 | 92 |
99 base::WeakPtrFactory<ServerBackedStateKeysBroker> weak_factory_; | 93 base::WeakPtrFactory<ServerBackedStateKeysBroker> weak_factory_; |
100 | 94 |
101 DISALLOW_COPY_AND_ASSIGN(ServerBackedStateKeysBroker); | 95 DISALLOW_COPY_AND_ASSIGN(ServerBackedStateKeysBroker); |
102 }; | 96 }; |
103 | 97 |
104 } // namespace policy | 98 } // namespace policy |
105 | 99 |
106 #endif // CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_ | 100 #endif // CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_ |
OLD | NEW |