Home | History | Annotate | Download | only in policy
      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.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_
      6 #define CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     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"
     16 
     17 namespace base {
     18 class TaskRunner;
     19 }
     20 
     21 namespace chromeos {
     22 class SessionManagerClient;
     23 }
     24 
     25 namespace policy {
     26 
     27 // Brokers server-backed state keys for the device. Retrieves them from session
     28 // manager via DBus and refreshes them periodically. Consumers can register
     29 // 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;
     35 
     36   ServerBackedStateKeysBroker(
     37       chromeos::SessionManagerClient* session_manager_client,
     38       scoped_refptr<base::TaskRunner> delayed_task_runner);
     39   ~ServerBackedStateKeysBroker();
     40 
     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);
     46 
     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);
     52 
     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_; }
     56 
     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();
     61   }
     62 
     63   // Whether state key retrieval is pending.
     64   bool pending() const { return !initial_retrieval_completed_; }
     65 
     66   // Whether state keys are available.
     67   bool available() const { return !state_keys_.empty(); }
     68 
     69  private:
     70   // Asks |session_manager_client_| to provide current state keys..
     71   void FetchStateKeys();
     72 
     73   // Stores newly-received state keys and notifies consumers.
     74   void StoreStateKeys(const std::vector<std::string>& state_keys);
     75 
     76   chromeos::SessionManagerClient* session_manager_client_;
     77 
     78   scoped_refptr<base::TaskRunner> delayed_task_runner_;
     79 
     80   // The current set of state keys.
     81   std::vector<std::string> state_keys_;
     82 
     83   // Whether a request for state keys is pending.
     84   bool requested_;
     85 
     86   // Whether the initial retrieval operation completed.
     87   bool initial_retrieval_completed_;
     88 
     89   // List of callbacks to receive update notifications.
     90   base::CallbackList<void()> update_callbacks_;
     91 
     92   // List of pending one-shot state key request callbacks.
     93   std::vector<StateKeysCallback> request_callbacks_;
     94 
     95   base::WeakPtrFactory<ServerBackedStateKeysBroker> weak_factory_;
     96 
     97   DISALLOW_COPY_AND_ASSIGN(ServerBackedStateKeysBroker);
     98 };
     99 
    100 }  // namespace policy
    101 
    102 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_SERVER_BACKED_STATE_KEYS_BROKER_H_
    103