Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / policy / core / browser / browser_policy_connector.h
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 COMPONENTS_POLICY_CORE_BROWSER_BROWSER_POLICY_CONNECTOR_H_
6 #define COMPONENTS_POLICY_CORE_BROWSER_BROWSER_POLICY_CONNECTOR_H_
7
8 #include <string>
9
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"
18
19 class PrefRegistrySimple;
20 class PrefService;
21
22 namespace net {
23 class URLRequestContextGetter;
24 }
25
26 namespace policy {
27
28 class ConfigurationPolicyProvider;
29 class DeviceManagementService;
30 class PolicyService;
31 class PolicyStatisticsCollector;
32
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
36 // subclasses.
37 class POLICY_EXPORT BrowserPolicyConnector {
38  public:
39   // Invoke Shutdown() before deleting, see below.
40   virtual ~BrowserPolicyConnector();
41
42   // Finalizes the initialization of the connector. This call can be skipped on
43   // tests that don't require the full policy system running.
44   virtual void Init(
45       PrefService* local_state,
46       scoped_refptr<net::URLRequestContextGetter> request_context) = 0;
47
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();
53
54   // Returns true if Init() has been called but Shutdown() hasn't been yet.
55   bool is_initialized() const { return is_initialized_; }
56
57   // Returns a handle to the Chrome schema.
58   const Schema& GetChromeSchema() const;
59
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();
64
65   // Returns the browser-global PolicyService, that contains policies for the
66   // whole browser.
67   PolicyService* GetPolicyService();
68
69   // Returns the platform-specific policy provider, if there is one.
70   ConfigurationPolicyProvider* GetPlatformProvider();
71
72   // Schedules initialization of the cloud policy backend services, if the
73   // services are already constructed.
74   void ScheduleServiceInitialization(int64 delay_milliseconds);
75
76   const ConfigurationPolicyHandlerList* GetHandlerList() const;
77
78   DeviceManagementService* device_management_service() {
79     return device_management_service_.get();
80   }
81
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
87   // down.
88   static void SetPolicyProviderForTesting(
89       ConfigurationPolicyProvider* provider);
90
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);
95
96   // Returns the URL for the device management service endpoint.
97   static std::string GetDeviceManagementUrl();
98
99   // Registers refresh rate prefs.
100   static void RegisterPrefs(PrefRegistrySimple* registry);
101
102  protected:
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);
107
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);
112
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);
116
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);
122
123  private:
124   // Whether Init() but not Shutdown() has been invoked.
125   bool is_initialized_;
126
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_;
132
133   // The Chrome schema. This wraps the structure generated by
134   // generate_policy_source.py at compile time.
135   Schema chrome_schema_;
136
137   // The global SchemaRegistry, which will track all the other registries.
138   CombinedSchemaRegistry schema_registry_;
139
140   // The browser-global policy providers, in decreasing order of priority.
141   ScopedVector<ConfigurationPolicyProvider> policy_providers_;
142   ConfigurationPolicyProvider* platform_policy_provider_;
143
144   // Must be deleted before all the policy providers.
145   scoped_ptr<PolicyService> policy_service_;
146
147   scoped_ptr<PolicyStatisticsCollector> policy_statistics_collector_;
148
149   scoped_ptr<DeviceManagementService> device_management_service_;
150
151   DISALLOW_COPY_AND_ASSIGN(BrowserPolicyConnector);
152 };
153
154 }  // namespace policy
155
156 #endif  // COMPONENTS_POLICY_CORE_BROWSER_BROWSER_POLICY_CONNECTOR_H_