1 // Copyright 2015 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_BASE_H_
6 #define COMPONENTS_POLICY_CORE_BROWSER_BROWSER_POLICY_CONNECTOR_BASE_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/callback_forward.h"
12 #include "base/macros.h"
13 #include "components/policy/core/browser/configuration_policy_handler_list.h"
14 #include "components/policy/core/common/schema.h"
15 #include "components/policy/core/common/schema_registry.h"
16 #include "components/policy/policy_export.h"
17 
18 namespace policy {
19 
20 class ConfigurationPolicyProvider;
21 class PolicyService;
22 class PolicyServiceImpl;
23 
24 // The BrowserPolicyConnectorBase keeps and initializes some core elements of
25 // the policy component, mainly the PolicyProviders and the PolicyService.
26 class POLICY_EXPORT BrowserPolicyConnectorBase {
27  public:
28   // Invoke Shutdown() before deleting, see below.
29   virtual ~BrowserPolicyConnectorBase();
30 
31   // Stops the policy providers and cleans up the connector before it can be
32   // safely deleted. This must be invoked before the destructor and while the
33   // threads are still running. The policy providers are still valid but won't
34   // update anymore after this call. Subclasses can override this for cleanup
35   // and should call the parent method.
36   virtual void Shutdown();
37 
38   // Returns true if SetPolicyProviders() has been called but Shutdown() hasn't
39   // been yet.
is_initialized()40   bool is_initialized() const { return is_initialized_; }
41 
42   // Returns a handle to the Chrome schema.
43   const Schema& GetChromeSchema() const;
44 
45   // Returns the global CombinedSchemaRegistry. SchemaRegistries from Profiles
46   // should be tracked by the global registry, so that the global policy
47   // providers also load policies for the components of each Profile.
48   CombinedSchemaRegistry* GetSchemaRegistry();
49 
50   // Returns the browser-global PolicyService, that contains policies for the
51   // whole browser.
52   PolicyService* GetPolicyService();
53 
54   const ConfigurationPolicyHandlerList* GetHandlerList() const;
55 
56   std::vector<ConfigurationPolicyProvider*> GetPolicyProviders() const;
57 
58   // Sets a |provider| that will be included in PolicyServices returned by
59   // GetPolicyService. This is a static method because local state is
60   // created immediately after the connector, and tests don't have a chance to
61   // inject the provider otherwise. |provider| must outlive the connector, and
62   // its ownership is not taken though the connector will initialize and shut it
63   // down.
64   static void SetPolicyProviderForTesting(
65       ConfigurationPolicyProvider* provider);
66   ConfigurationPolicyProvider* GetPolicyProviderForTesting();
67 
68   // Adds a callback that is notified the the ResourceBundle is loaded.
69   void NotifyWhenResourceBundleReady(base::OnceClosure closure);
70 
71  protected:
72   // Builds an uninitialized BrowserPolicyConnectorBase. SetPolicyProviders()
73   // should be called to create and start the policy components.
74   explicit BrowserPolicyConnectorBase(
75       const HandlerListFactory& handler_list_factory);
76 
77   // Called from GetPolicyService() to create the set of
78   // ConfigurationPolicyProviders that are used, in decreasing order of
79   // priority.
80   virtual std::vector<std::unique_ptr<ConfigurationPolicyProvider>>
81   CreatePolicyProviders();
82 
83   // Must be called when ui::ResourceBundle has been loaded, results in running
84   // any callbacks scheduled in NotifyWhenResourceBundleReady().
85   void OnResourceBundleCreated();
86 
87  private:
88   // Returns the providers to pass to the PolicyService. Generally this is the
89   // same as |policy_providers_|, unless SetPolicyProviderForTesting() has been
90   // called.
91   std::vector<ConfigurationPolicyProvider*> GetProvidersForPolicyService();
92 
93   // Set to true when the PolicyService has been created, and false in
94   // Shutdown(). Once created the PolicyService is destroyed in the destructor,
95   // not Shutdown().
96   bool is_initialized_ = false;
97 
98   // Used to convert policies to preferences. The providers declared below
99   // may trigger policy updates during shutdown, which will result in
100   // |handler_list_| being consulted for policy translation.
101   // Therefore, it's important to destroy |handler_list_| after the providers.
102   std::unique_ptr<ConfigurationPolicyHandlerList> handler_list_;
103 
104   // The global SchemaRegistry, which will track all the other registries.
105   CombinedSchemaRegistry schema_registry_;
106 
107   // The browser-global policy providers, in decreasing order of priority.
108   std::vector<std::unique_ptr<ConfigurationPolicyProvider>> policy_providers_;
109 
110   // Must be deleted before all the policy providers.
111   std::unique_ptr<PolicyServiceImpl> policy_service_;
112 
113   // Callbacks scheduled via NotifyWhenResourceBundleReady().
114   std::vector<base::OnceClosure> resource_bundle_callbacks_;
115 
116   DISALLOW_COPY_AND_ASSIGN(BrowserPolicyConnectorBase);
117 };
118 
119 }  // namespace policy
120 
121 #endif  // COMPONENTS_POLICY_CORE_BROWSER_BROWSER_POLICY_CONNECTOR_BASE_H_
122