Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: remoting/host/policy_hack/policy_service_watcher.cc

Issue 858303003: Removing FakePolicyWatcher and introducing FakeAsyncPolicyLoader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tweaked the set of #include-s. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/host/policy_hack/policy_service_watcher.h"
6
5 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
6 #include "base/single_thread_task_runner.h"
7 #include "components/policy/core/common/async_policy_loader.h" 8 #include "components/policy/core/common/async_policy_loader.h"
8 #include "components/policy/core/common/async_policy_provider.h" 9 #include "components/policy/core/common/async_policy_provider.h"
9 #include "components/policy/core/common/policy_namespace.h" 10 #include "components/policy/core/common/policy_namespace.h"
10 #include "components/policy/core/common/policy_service.h"
11 #include "components/policy/core/common/policy_service_impl.h" 11 #include "components/policy/core/common/policy_service_impl.h"
12 #include "components/policy/core/common/schema.h" 12 #include "components/policy/core/common/schema.h"
13 #include "components/policy/core/common/schema_registry.h" 13 #include "components/policy/core/common/schema_registry.h"
14 #include "policy/policy_constants.h" 14 #include "policy/policy_constants.h"
15 #include "remoting/host/policy_hack/policy_watcher.h"
16 15
17 #if defined(OS_CHROMEOS) 16 #if defined(OS_CHROMEOS)
18 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
19 #elif defined(OS_WIN) 18 #elif defined(OS_WIN)
20 #include "components/policy/core/common/policy_loader_win.h" 19 #include "components/policy/core/common/policy_loader_win.h"
21 #elif defined(OS_MACOSX) 20 #elif defined(OS_MACOSX)
22 #include "components/policy/core/common/policy_loader_mac.h" 21 #include "components/policy/core/common/policy_loader_mac.h"
23 #include "components/policy/core/common/preferences_mac.h" 22 #include "components/policy/core/common/preferences_mac.h"
24 #elif defined(OS_POSIX) && !defined(OS_ANDROID) 23 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
25 #include "components/policy/core/common/config_dir_policy_loader.h" 24 #include "components/policy/core/common/config_dir_policy_loader.h"
26 #endif 25 #endif
27 26
28 using namespace policy; 27 using namespace policy;
29 28
30 namespace remoting { 29 namespace remoting {
31 namespace policy_hack { 30 namespace policy_hack {
32 31
33 namespace { 32 namespace {
34 33
35 // TODO(lukasza): Merge PolicyServiceWatcher with PolicyWatcher class
36 // (after removing other classes derived from PolicyWatcher - i.e. after
37 // removing FakePolicyWatcher class and replacing it with mocks of classes
38 // from components/policy instead).
39
40 // PolicyServiceWatcher is a concrete implementation of PolicyWatcher that wraps
41 // an instance of PolicyService.
42 class PolicyServiceWatcher : public PolicyWatcher,
43 public PolicyService::Observer {
44 public:
45 // Constructor for the case when |policy_service| is borrowed.
46 //
47 // |policy_service_task_runner| is the task runner where it is safe
48 // to call |policy_service| methods and where we expect to get callbacks
49 // from |policy_service|.
50 PolicyServiceWatcher(const scoped_refptr<base::SingleThreadTaskRunner>&
51 policy_service_task_runner,
52 PolicyService* policy_service);
53
54 // Constructor for the case when |policy_service| is owned (and uses also
55 // owned |owned_policy_provider| and |owned_schema_registry|.
56 //
57 // |policy_service_task_runner| is the task runner where it is safe
58 // to call |policy_service| methods and where we expect to get callbacks
59 // from |policy_service|.
60 PolicyServiceWatcher(
61 const scoped_refptr<base::SingleThreadTaskRunner>&
62 policy_service_task_runner,
63 scoped_ptr<PolicyService> owned_policy_service,
64 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider,
65 scoped_ptr<SchemaRegistry> owned_schema_registry);
66
67 ~PolicyServiceWatcher() override;
68
69 // PolicyService::Observer interface.
70 void OnPolicyUpdated(const PolicyNamespace& ns,
71 const PolicyMap& previous,
72 const PolicyMap& current) override;
73 void OnPolicyServiceInitialized(PolicyDomain domain) override;
74
75 protected:
76 // PolicyWatcher overrides.
77 void StartWatchingInternal() override;
78 void StopWatchingInternal() override;
79
80 private:
81 PolicyService* policy_service_;
82
83 // Order of fields below is important to ensure destruction takes object
84 // dependencies into account:
85 // - |owned_policy_service_| uses |owned_policy_provider_|
86 // - |owned_policy_provider_| uses |owned_schema_registry_|
87 scoped_ptr<SchemaRegistry> owned_schema_registry_;
88 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider_;
89 scoped_ptr<PolicyService> owned_policy_service_;
90
91 DISALLOW_COPY_AND_ASSIGN(PolicyServiceWatcher);
92 };
93
94 PolicyNamespace GetPolicyNamespace() { 34 PolicyNamespace GetPolicyNamespace() {
95 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()); 35 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string());
96 } 36 }
97 37
38 } // namespace
39
98 PolicyServiceWatcher::PolicyServiceWatcher( 40 PolicyServiceWatcher::PolicyServiceWatcher(
99 const scoped_refptr<base::SingleThreadTaskRunner>& 41 const scoped_refptr<base::SingleThreadTaskRunner>&
100 policy_service_task_runner, 42 policy_service_task_runner,
101 PolicyService* policy_service) 43 PolicyService* policy_service)
102 : PolicyWatcher(policy_service_task_runner) { 44 : PolicyWatcher(policy_service_task_runner) {
103 policy_service_ = policy_service; 45 policy_service_ = policy_service;
104 } 46 }
105 47
106 PolicyServiceWatcher::PolicyServiceWatcher( 48 PolicyServiceWatcher::PolicyServiceWatcher(
107 const scoped_refptr<base::SingleThreadTaskRunner>& 49 const scoped_refptr<base::SingleThreadTaskRunner>&
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 // Process current policy state. 89 // Process current policy state.
148 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) { 90 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) {
149 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME); 91 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME);
150 } 92 }
151 } 93 }
152 94
153 void PolicyServiceWatcher::StopWatchingInternal() { 95 void PolicyServiceWatcher::StopWatchingInternal() {
154 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); 96 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
155 } 97 }
156 98
157 #if !defined(OS_CHROMEOS) 99 scoped_ptr<PolicyServiceWatcher> PolicyServiceWatcher::CreateFromPolicyLoader(
158
159 // Creates PolicyServiceWatcher that wraps the owned |async_policy_loader|
160 // with an appropriate PolicySchema.
161 //
162 // |policy_service_task_runner| is passed through to the constructor
163 // of PolicyServiceWatcher.
164 scoped_ptr<PolicyServiceWatcher> CreateFromPolicyLoader(
165 const scoped_refptr<base::SingleThreadTaskRunner>& 100 const scoped_refptr<base::SingleThreadTaskRunner>&
166 policy_service_task_runner, 101 policy_service_task_runner,
167 scoped_ptr<AsyncPolicyLoader> async_policy_loader) { 102 scoped_ptr<AsyncPolicyLoader> async_policy_loader) {
168 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific 103 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific
169 // policies (expecting perf and maintanability improvement, but no functional 104 // policies (expecting perf and maintanability improvement, but no functional
170 // impact). 105 // impact).
171 Schema schema = Schema::Wrap(GetChromeSchemaData()); 106 Schema schema = Schema::Wrap(GetChromeSchemaData());
172 107
173 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry()); 108 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry());
174 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); 109 schema_registry->RegisterComponent(GetPolicyNamespace(), schema);
175 110
176 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider( 111 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider(
177 schema_registry.get(), async_policy_loader.Pass())); 112 schema_registry.get(), async_policy_loader.Pass()));
178 policy_provider->Init(schema_registry.get()); 113 policy_provider->Init(schema_registry.get());
179 114
180 PolicyServiceImpl::Providers providers; 115 PolicyServiceImpl::Providers providers;
181 providers.push_back(policy_provider.get()); 116 providers.push_back(policy_provider.get());
182 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers)); 117 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers));
183 118
184 return make_scoped_ptr(new PolicyServiceWatcher( 119 return make_scoped_ptr(new PolicyServiceWatcher(
185 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(), 120 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(),
186 schema_registry.Pass())); 121 schema_registry.Pass()));
187 } 122 }
188 123
189 #endif
190
191 } // anonymous namespace
192
193 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( 124 scoped_ptr<PolicyWatcher> PolicyWatcher::Create(
194 policy::PolicyService* policy_service, 125 policy::PolicyService* policy_service,
195 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) { 126 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) {
196 #if defined(OS_CHROMEOS) 127 #if defined(OS_CHROMEOS)
197 DCHECK(policy_service); 128 DCHECK(policy_service);
198 return make_scoped_ptr(new PolicyServiceWatcher( 129 return make_scoped_ptr(new PolicyServiceWatcher(
199 content::BrowserThread::GetMessageLoopProxyForThread( 130 content::BrowserThread::GetMessageLoopProxyForThread(
200 content::BrowserThread::UI), 131 content::BrowserThread::UI),
201 policy_service)); 132 policy_service));
202 #elif defined(OS_WIN) 133 #elif defined(OS_WIN)
203 DCHECK(!policy_service); 134 DCHECK(!policy_service);
204 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; 135 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome";
205 return CreateFromPolicyLoader( 136 return PolicyServiceWatcher::CreateFromPolicyLoader(
206 network_task_runner, 137 network_task_runner,
207 PolicyLoaderWin::Create(network_task_runner, kRegistryKey)); 138 PolicyLoaderWin::Create(network_task_runner, kRegistryKey));
208 #elif defined(OS_MACOSX) 139 #elif defined(OS_MACOSX)
209 CFStringRef bundle_id = CFSTR("com.google.Chrome"); 140 CFStringRef bundle_id = CFSTR("com.google.Chrome");
210 DCHECK(!policy_service); 141 DCHECK(!policy_service);
211 return CreateFromPolicyLoader( 142 return PolicyServiceWatcher::CreateFromPolicyLoader(
212 network_task_runner, 143 network_task_runner,
213 make_scoped_ptr(new PolicyLoaderMac( 144 make_scoped_ptr(new PolicyLoaderMac(
214 network_task_runner, 145 network_task_runner,
215 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), 146 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id),
216 new MacPreferences(), bundle_id))); 147 new MacPreferences(), bundle_id)));
217 #elif defined(OS_POSIX) && !defined(OS_ANDROID) 148 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
218 DCHECK(!policy_service); 149 DCHECK(!policy_service);
219 // Always read the Chrome policies (even on Chromium) so that policy 150 // Always read the Chrome policies (even on Chromium) so that policy
220 // enforcement can't be bypassed by running Chromium. 151 // enforcement can't be bypassed by running Chromium.
221 static const base::FilePath::CharType kPolicyDir[] = 152 static const base::FilePath::CharType kPolicyDir[] =
222 FILE_PATH_LITERAL("/etc/opt/chrome/policies"); 153 FILE_PATH_LITERAL("/etc/opt/chrome/policies");
223 return CreateFromPolicyLoader( 154 return PolicyServiceWatcher::CreateFromPolicyLoader(
224 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader( 155 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader(
225 network_task_runner, base::FilePath(kPolicyDir), 156 network_task_runner, base::FilePath(kPolicyDir),
226 POLICY_SCOPE_MACHINE))); 157 POLICY_SCOPE_MACHINE)));
227 #else 158 #else
228 #error OS that is not yet supported by PolicyWatcher code. 159 #error OS that is not yet supported by PolicyWatcher code.
229 #endif 160 #endif
230 } 161 }
231 162
232 } // namespace policy_hack 163 } // namespace policy_hack
233 } // namespace remoting 164 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698