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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_
11 #include "base/callback.h"
12 #include "base/callback_list.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
22 class SessionManagerClient;
27 // Brokers server-backed FRE state keys for the device. Retrieves them from
28 // session manager via DBus and refreshes them periodically. Consumers can
29 // register callbacks to invoke when the state keys change.
30 class ServerBackedStateKeysBroker {
32 typedef scoped_ptr<base::CallbackList<void()>::Subscription> Subscription;
33 typedef base::Callback<void(const std::vector<std::string>&, bool)>
36 ServerBackedStateKeysBroker(
37 chromeos::SessionManagerClient* session_manager_client,
38 scoped_refptr<base::TaskRunner> delayed_task_runner);
39 ~ServerBackedStateKeysBroker();
41 // Registers a callback to be invoked whenever the state keys get updated.
42 // Note that consuming code needs to hold on to the returned Subscription as
43 // long as it wants to receive the callback. If the state keys haven't been
44 // requested yet, calling this will also trigger their initial fetch.
45 Subscription RegisterUpdateCallback(const base::Closure& callback);
47 // Requests state keys asynchronously. Invokes the passed callback exactly
48 // once (unless |this| gets destroyed before the callback happens), with the
49 // current state keys passed as a parameter to the callback. If there's a
50 // problem determining the state keys, the passed vector will be empty.
51 void RequestStateKeys(const StateKeysCallback& callback);
53 // Get the set of current state keys. Empty if state keys are unavailable
54 // or pending retrieval.
55 const std::vector<std::string>& state_keys() const { return state_keys_; }
57 // Returns the state key for the current point in time. Returns an empty
58 // string if state keys are unavailable or pending retrieval.
59 std::string current_state_key() const {
60 return state_keys_.empty() ? std::string() : state_keys_.front();
63 // Whether state key retrieval is pending.
64 bool pending() const { return !initial_retrieval_completed_; }
66 // Whether state keys are available.
67 bool available() const { return !state_keys_.empty(); }
70 // Asks |session_manager_client_| to provide current state keys..
71 void FetchStateKeys();
73 // Stores newly-received state keys and notifies consumers.
74 void StoreStateKeys(const std::vector<std::string>& state_keys,
77 chromeos::SessionManagerClient* session_manager_client_;
79 scoped_refptr<base::TaskRunner> delayed_task_runner_;
81 // The current set of state keys.
82 std::vector<std::string> state_keys_;
84 // Set to true on first run after factory reset.
87 // Whether a request for state keys is pending.
90 // Whether the initial retrieval operation completed.
91 bool initial_retrieval_completed_;
93 // List of callbacks to receive update notifications.
94 base::CallbackList<void()> update_callbacks_;
96 // List of pending one-shot state key request callbacks.
97 std::vector<StateKeysCallback> request_callbacks_;
99 base::WeakPtrFactory<ServerBackedStateKeysBroker> weak_factory_;
101 DISALLOW_COPY_AND_ASSIGN(ServerBackedStateKeysBroker);
104 } // namespace policy
106 #endif // CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_