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 COMPONENTS_POLICY_CORE_BROWSER_BROWSER_POLICY_CONNECTOR_H_
6 #define COMPONENTS_POLICY_CORE_BROWSER_BROWSER_POLICY_CONNECTOR_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "components/policy/core/browser/configuration_policy_handler_list.h"
15 #include "components/policy/core/common/schema.h"
16 #include "components/policy/core/common/schema_registry.h"
17 #include "components/policy/policy_export.h"
19 class PrefRegistrySimple
;
23 class URLRequestContextGetter
;
28 class ConfigurationPolicyProvider
;
29 class DeviceManagementService
;
31 class PolicyStatisticsCollector
;
33 // The BrowserPolicyConnector keeps the browser-global (non Profile-specific)
34 // policy providers, and some shared components of the policy system.
35 // This is a basic implementation that gets extended by platform-specific
37 class POLICY_EXPORT BrowserPolicyConnector
{
39 // Invoke Shutdown() before deleting, see below.
40 virtual ~BrowserPolicyConnector();
42 // Finalizes the initialization of the connector. This call can be skipped on
43 // tests that don't require the full policy system running.
45 PrefService
* local_state
,
46 scoped_refptr
<net::URLRequestContextGetter
> request_context
) = 0;
48 // Stops the policy providers and cleans up the connector before it can be
49 // safely deleted. This must be invoked before the destructor and while the
50 // threads are still running. The policy providers are still valid but won't
51 // update anymore after this call.
52 virtual void Shutdown();
54 // Returns true if Init() has been called but Shutdown() hasn't been yet.
55 bool is_initialized() const { return is_initialized_
; }
57 // Returns a handle to the Chrome schema.
58 const Schema
& GetChromeSchema() const;
60 // Returns the global CombinedSchemaRegistry. SchemaRegistries from Profiles
61 // should be tracked by the global registry, so that the global policy
62 // providers also load policies for the components of each Profile.
63 CombinedSchemaRegistry
* GetSchemaRegistry();
65 // Returns the browser-global PolicyService, that contains policies for the
67 PolicyService
* GetPolicyService();
69 // Returns the platform-specific policy provider, if there is one.
70 ConfigurationPolicyProvider
* GetPlatformProvider();
72 // Schedules initialization of the cloud policy backend services, if the
73 // services are already constructed.
74 void ScheduleServiceInitialization(int64 delay_milliseconds
);
76 const ConfigurationPolicyHandlerList
* GetHandlerList() const;
78 DeviceManagementService
* device_management_service() {
79 return device_management_service_
.get();
82 // Sets a |provider| that will be included in PolicyServices returned by
83 // GetPolicyService. This is a static method because local state is
84 // created immediately after the connector, and tests don't have a chance to
85 // inject the provider otherwise. |provider| must outlive the connector, and
86 // its ownership is not taken though the connector will initialize and shut it
88 static void SetPolicyProviderForTesting(
89 ConfigurationPolicyProvider
* provider
);
91 // Check whether a user is known to be non-enterprise. Domains such as
92 // gmail.com and googlemail.com are known to not be managed. Also returns
93 // false if the username is empty.
94 static bool IsNonEnterpriseUser(const std::string
& username
);
96 // Returns the URL for the device management service endpoint.
97 static std::string
GetDeviceManagementUrl();
99 // Registers refresh rate prefs.
100 static void RegisterPrefs(PrefRegistrySimple
* registry
);
103 // Builds an uninitialized BrowserPolicyConnector.
104 // Init() should be called to create and start the policy components.
105 explicit BrowserPolicyConnector(
106 const HandlerListFactory
& handler_list_factory
);
108 // Helper for the public Init() that must be called by subclasses.
109 void Init(PrefService
* local_state
,
110 scoped_refptr
<net::URLRequestContextGetter
> request_context
,
111 scoped_ptr
<DeviceManagementService
> device_management_service
);
113 // Adds |provider| to the list of |policy_providers_|. Providers should
114 // be added in decreasing order of priority.
115 void AddPolicyProvider(scoped_ptr
<ConfigurationPolicyProvider
> provider
);
117 // Same as AddPolicyProvider(), but |provider| becomes the platform provider
118 // which can be retrieved by GetPlatformProvider(). This can be called at
119 // most once, and uses the same priority order as AddPolicyProvider().
120 void SetPlatformPolicyProvider(
121 scoped_ptr
<ConfigurationPolicyProvider
> provider
);
124 // Whether Init() but not Shutdown() has been invoked.
125 bool is_initialized_
;
127 // Used to convert policies to preferences. The providers declared below
128 // may trigger policy updates during shutdown, which will result in
129 // |handler_list_| being consulted for policy translation.
130 // Therefore, it's important to destroy |handler_list_| after the providers.
131 scoped_ptr
<ConfigurationPolicyHandlerList
> handler_list_
;
133 // The Chrome schema. This wraps the structure generated by
134 // generate_policy_source.py at compile time.
135 Schema chrome_schema_
;
137 // The global SchemaRegistry, which will track all the other registries.
138 CombinedSchemaRegistry schema_registry_
;
140 // The browser-global policy providers, in decreasing order of priority.
141 ScopedVector
<ConfigurationPolicyProvider
> policy_providers_
;
142 ConfigurationPolicyProvider
* platform_policy_provider_
;
144 // Must be deleted before all the policy providers.
145 scoped_ptr
<PolicyService
> policy_service_
;
147 scoped_ptr
<PolicyStatisticsCollector
> policy_statistics_collector_
;
149 scoped_ptr
<DeviceManagementService
> device_management_service_
;
151 DISALLOW_COPY_AND_ASSIGN(BrowserPolicyConnector
);
154 } // namespace policy
156 #endif // COMPONENTS_POLICY_CORE_BROWSER_BROWSER_POLICY_CONNECTOR_H_