Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / server_backed_state_keys_broker.h
blobe3d8e6844f6b5b0b68e7f0897618cfc438cddd78
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_
8 #include <string>
9 #include <vector>
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"
17 namespace base {
18 class TaskRunner;
21 namespace chromeos {
22 class SessionManagerClient;
25 namespace policy {
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 {
31 public:
32 typedef scoped_ptr<base::CallbackList<void()>::Subscription> Subscription;
33 typedef base::Callback<void(const std::vector<std::string>&)>
34 StateKeysCallback;
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(); }
69 private:
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);
76 chromeos::SessionManagerClient* session_manager_client_;
78 scoped_refptr<base::TaskRunner> delayed_task_runner_;
80 // The current set of state keys.
81 std::vector<std::string> state_keys_;
83 // Whether a request for state keys is pending.
84 bool requested_;
86 // Whether the initial retrieval operation completed.
87 bool initial_retrieval_completed_;
89 // List of callbacks to receive update notifications.
90 base::CallbackList<void()> update_callbacks_;
92 // List of pending one-shot state key request callbacks.
93 std::vector<StateKeysCallback> request_callbacks_;
95 base::WeakPtrFactory<ServerBackedStateKeysBroker> weak_factory_;
97 DISALLOW_COPY_AND_ASSIGN(ServerBackedStateKeysBroker);
100 } // namespace policy
102 #endif // CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_