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_CLOUD_POLICY_SUBSYSTEM_H_
6#define CHROME_BROWSER_POLICY_CLOUD_POLICY_SUBSYSTEM_H_
7#pragma once
8
9#include "base/memory/scoped_ptr.h"
10#include "chrome/browser/prefs/pref_member.h"
11#include "content/common/notification_observer.h"
12#include "net/base/network_change_notifier.h"
13
14class PrefService;
15
16namespace net {
17class URLRequestContextGetter;
18}
19
20namespace policy {
21
22class CloudPolicyCacheBase;
23class CloudPolicyController;
24class CloudPolicyIdentityStrategy;
25class ConfigurationPolicyProvider;
26class DeviceManagementService;
27class DeviceTokenFetcher;
28class PolicyNotifier;
29
30// This class is a container for the infrastructure required to support cloud
31// policy. It glues together the backend, the policy controller and manages the
32// life cycle of the policy providers.
33class CloudPolicySubsystem
34    : public NotificationObserver,
35      public net::NetworkChangeNotifier::IPAddressObserver {
36 public:
37  enum PolicySubsystemState {
38    UNENROLLED,     // No enrollment attempt has been performed yet.
39    BAD_GAIA_TOKEN, // The server rejected the GAIA auth token.
40    UNMANAGED,      // This device is unmanaged.
41    NETWORK_ERROR,  // A network error occurred, retrying makes sense.
42    LOCAL_ERROR,    // Retrying is futile.
43    TOKEN_FETCHED,  // Device has been successfully registered.
44    SUCCESS         // Policy has been fetched successfully and is in effect.
45  };
46
47  enum ErrorDetails {
48    NO_DETAILS,            // No error, so no error details either.
49    DMTOKEN_NETWORK_ERROR, // DeviceTokenFetcher encountered a network error.
50    POLICY_NETWORK_ERROR,  // CloudPolicyController encountered a network error.
51    BAD_DMTOKEN,           // The server rejected the DMToken.
52    POLICY_LOCAL_ERROR,    // The policy cache encountered a local error.
53    SIGNATURE_MISMATCH,    // The policy cache detected a signature mismatch.
54  };
55
56  class Observer {
57   public:
58    virtual ~Observer() {}
59    virtual void OnPolicyStateChanged(PolicySubsystemState state,
60                                      ErrorDetails error_details) = 0;
61  };
62
63  class ObserverRegistrar {
64   public:
65    ObserverRegistrar(CloudPolicySubsystem* cloud_policy_subsystem,
66                      CloudPolicySubsystem::Observer* observer);
67    ~ObserverRegistrar();
68
69   private:
70    PolicyNotifier* policy_notifier_;
71    CloudPolicySubsystem::Observer* observer_;
72    DISALLOW_COPY_AND_ASSIGN(ObserverRegistrar);
73  };
74
75  CloudPolicySubsystem(CloudPolicyIdentityStrategy* identity_strategy,
76                       CloudPolicyCacheBase* policy_cache);
77  virtual ~CloudPolicySubsystem();
78
79  // net::NetworkChangeNotifier::IPAddressObserver:
80  virtual void OnIPAddressChanged() OVERRIDE;
81
82  // Initializes the subsystem.
83  void Initialize(PrefService* prefs,
84                  net::URLRequestContextGetter* request_context);
85
86  // Shuts the subsystem down. This must be called before threading and network
87  // infrastructure goes away.
88  void Shutdown();
89
90  // Returns the externally visible state and corresponding error details.
91  PolicySubsystemState state();
92  ErrorDetails error_details();
93
94  // Stops all auto-retrying error handling behavior inside the policy
95  // subsystem.
96  void StopAutoRetry();
97
98  ConfigurationPolicyProvider* GetManagedPolicyProvider();
99  ConfigurationPolicyProvider* GetRecommendedPolicyProvider();
100
101  // Registers cloud policy related prefs.
102  static void RegisterPrefs(PrefService* pref_service);
103
104 private:
105  // Updates the policy controller with a new refresh rate value.
106  void UpdatePolicyRefreshRate();
107
108  // Returns a weak pointer to this subsystem's PolicyNotifier.
109  PolicyNotifier* notifier() {
110    return notifier_.get();
111  }
112
113  // NotificationObserver overrides.
114  virtual void Observe(NotificationType type,
115                       const NotificationSource& source,
116                       const NotificationDetails& details);
117
118  // The pref service that controls the refresh rate.
119  PrefService* prefs_;
120
121  // Tracks the pref value for the policy refresh rate.
122  IntegerPrefMember policy_refresh_rate_;
123
124  // Cloud policy infrastructure stuff.
125  scoped_ptr<PolicyNotifier> notifier_;
126  scoped_ptr<DeviceManagementService> device_management_service_;
127  scoped_ptr<DeviceTokenFetcher> device_token_fetcher_;
128  scoped_ptr<CloudPolicyCacheBase> cloud_policy_cache_;
129  scoped_ptr<CloudPolicyController> cloud_policy_controller_;
130
131  DISALLOW_COPY_AND_ASSIGN(CloudPolicySubsystem);
132};
133
134}  // namespace policy
135
136#endif  // CHROME_BROWSER_POLICY_CLOUD_POLICY_SUBSYSTEM_H_
137