1// Copyright (c) 2011 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_POLICY_NOTIFIER_H_
6#define CHROME_BROWSER_POLICY_POLICY_NOTIFIER_H_
7#pragma once
8
9#include "base/observer_list.h"
10#include "chrome/browser/policy/cloud_policy_subsystem.h"
11
12namespace policy {
13
14// Keeps track of the state of the policy subsystem components as far as it's
15// relevant to the outside world. Is informed by components about status
16// changes (failures and successes), determines the overall state and
17// communicates it.
18class PolicyNotifier {
19 public:
20  typedef CloudPolicySubsystem::PolicySubsystemState PolicySubsystemState;
21  typedef CloudPolicySubsystem::ErrorDetails ErrorDetails;
22
23  enum StatusSource {
24    TOKEN_FETCHER,
25    POLICY_CONTROLLER,
26    POLICY_CACHE,
27    NUM_SOURCES  // This must be the last element in the enum.
28  };
29
30  PolicyNotifier();
31  ~PolicyNotifier();
32
33  // Called by components of the policy subsystem. Determines the new overall
34  // state and triggers observer notifications as necessary.
35  void Inform(PolicySubsystemState state,
36              ErrorDetails error_details,
37              StatusSource source);
38
39  CloudPolicySubsystem::PolicySubsystemState state() const {
40    return state_;
41  }
42
43  CloudPolicySubsystem::ErrorDetails error_details() const {
44    return error_details_;
45  }
46
47 private:
48  friend class CloudPolicyController;
49  friend class CloudPolicySubsystem::ObserverRegistrar;
50
51  void AddObserver(CloudPolicySubsystem::Observer* observer);
52  void RemoveObserver(CloudPolicySubsystem::Observer* observer);
53
54  void RecomputeState();
55
56  PolicySubsystemState state_;
57  ErrorDetails error_details_;
58
59  PolicySubsystemState component_states_[NUM_SOURCES];
60  ErrorDetails component_error_details_[NUM_SOURCES];
61
62  ObserverList<CloudPolicySubsystem::Observer, true> observer_list_;
63
64  DISALLOW_COPY_AND_ASSIGN(PolicyNotifier);
65};
66
67}  // namespace policy
68
69#endif  // CHROME_BROWSER_POLICY_POLICY_NOTIFIER_H_
70