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

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: Rebasing... 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" 8 #include "base/values.h"
7 #include "components/policy/core/common/async_policy_loader.h" 9 #include "components/policy/core/common/async_policy_loader.h"
8 #include "components/policy/core/common/async_policy_provider.h" 10 #include "components/policy/core/common/async_policy_provider.h"
9 #include "components/policy/core/common/policy_namespace.h" 11 #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" 12 #include "components/policy/core/common/policy_service_impl.h"
12 #include "components/policy/core/common/schema.h" 13 #include "components/policy/core/common/schema.h"
13 #include "components/policy/core/common/schema_registry.h" 14 #include "components/policy/core/common/schema_registry.h"
14 #include "policy/policy_constants.h" 15 #include "policy/policy_constants.h"
15 #include "remoting/host/policy_hack/policy_watcher.h"
16 16
17 #if defined(OS_CHROMEOS) 17 #if defined(OS_CHROMEOS)
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 #elif defined(OS_WIN) 19 #elif defined(OS_WIN)
20 #include "components/policy/core/common/policy_loader_win.h" 20 #include "components/policy/core/common/policy_loader_win.h"
21 #elif defined(OS_MACOSX) 21 #elif defined(OS_MACOSX)
22 #include "components/policy/core/common/policy_loader_mac.h" 22 #include "components/policy/core/common/policy_loader_mac.h"
23 #include "components/policy/core/common/preferences_mac.h" 23 #include "components/policy/core/common/preferences_mac.h"
24 #elif defined(OS_POSIX) && !defined(OS_ANDROID) 24 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
25 #include "components/policy/core/common/config_dir_policy_loader.h" 25 #include "components/policy/core/common/config_dir_policy_loader.h"
26 #endif 26 #endif
27 27
28 using namespace policy; 28 using namespace policy;
29 29
30 namespace remoting { 30 namespace remoting {
31 namespace policy_hack { 31 namespace policy_hack {
32 32
33 namespace { 33 namespace {
34 34
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() { 35 PolicyNamespace GetPolicyNamespace() {
95 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()); 36 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string());
96 } 37 }
97 38
39 } // namespace
40
98 PolicyServiceWatcher::PolicyServiceWatcher( 41 PolicyServiceWatcher::PolicyServiceWatcher(
99 const scoped_refptr<base::SingleThreadTaskRunner>& 42 const scoped_refptr<base::SingleThreadTaskRunner>&
100 policy_service_task_runner, 43 policy_service_task_runner,
101 PolicyService* policy_service) 44 PolicyService* policy_service)
102 : PolicyWatcher(policy_service_task_runner) { 45 : PolicyWatcher(policy_service_task_runner) {
103 policy_service_ = policy_service; 46 policy_service_ = policy_service;
104 } 47 }
105 48
106 PolicyServiceWatcher::PolicyServiceWatcher( 49 PolicyServiceWatcher::PolicyServiceWatcher(
107 const scoped_refptr<base::SingleThreadTaskRunner>& 50 const scoped_refptr<base::SingleThreadTaskRunner>&
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 // Process current policy state. 90 // Process current policy state.
148 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) { 91 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) {
149 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME); 92 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME);
150 } 93 }
151 } 94 }
152 95
153 void PolicyServiceWatcher::StopWatchingInternal() { 96 void PolicyServiceWatcher::StopWatchingInternal() {
154 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); 97 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
155 } 98 }
156 99
157 #if !defined(OS_CHROMEOS) 100 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>& 101 const scoped_refptr<base::SingleThreadTaskRunner>&
166 policy_service_task_runner, 102 policy_service_task_runner,
167 scoped_ptr<AsyncPolicyLoader> async_policy_loader) { 103 scoped_ptr<AsyncPolicyLoader> async_policy_loader) {
168 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific 104 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific
169 // policies (expecting perf and maintanability improvement, but no functional 105 // policies (expecting perf and maintanability improvement, but no functional
170 // impact). 106 // impact).
171 Schema schema = Schema::Wrap(GetChromeSchemaData()); 107 Schema schema = Schema::Wrap(GetChromeSchemaData());
172 108
173 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry()); 109 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry());
174 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); 110 schema_registry->RegisterComponent(GetPolicyNamespace(), schema);
175 111
176 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider( 112 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider(
177 schema_registry.get(), async_policy_loader.Pass())); 113 schema_registry.get(), async_policy_loader.Pass()));
178 policy_provider->Init(schema_registry.get()); 114 policy_provider->Init(schema_registry.get());
179 115
180 PolicyServiceImpl::Providers providers; 116 PolicyServiceImpl::Providers providers;
181 providers.push_back(policy_provider.get()); 117 providers.push_back(policy_provider.get());
182 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers)); 118 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers));
183 119
184 return make_scoped_ptr(new PolicyServiceWatcher( 120 return make_scoped_ptr(new PolicyServiceWatcher(
185 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(), 121 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(),
186 schema_registry.Pass())); 122 schema_registry.Pass()));
187 } 123 }
188 124
189 #endif
190
191 } // anonymous namespace
192
193 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( 125 scoped_ptr<PolicyWatcher> PolicyWatcher::Create(
194 policy::PolicyService* policy_service, 126 policy::PolicyService* policy_service,
195 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) { 127 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) {
196 #if defined(OS_CHROMEOS) 128 #if defined(OS_CHROMEOS)
197 DCHECK(policy_service); 129 DCHECK(policy_service);
198 return make_scoped_ptr(new PolicyServiceWatcher( 130 return make_scoped_ptr(new PolicyServiceWatcher(
199 content::BrowserThread::GetMessageLoopProxyForThread( 131 content::BrowserThread::GetMessageLoopProxyForThread(
200 content::BrowserThread::UI), 132 content::BrowserThread::UI),
201 policy_service)); 133 policy_service));
202 #elif defined(OS_WIN) 134 #elif defined(OS_WIN)
203 DCHECK(!policy_service); 135 DCHECK(!policy_service);
204 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; 136 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome";
205 return CreateFromPolicyLoader( 137 return PolicyServiceWatcher::CreateFromPolicyLoader(
206 network_task_runner, 138 network_task_runner,
207 PolicyLoaderWin::Create(network_task_runner, kRegistryKey)); 139 PolicyLoaderWin::Create(network_task_runner, kRegistryKey));
208 #elif defined(OS_MACOSX) 140 #elif defined(OS_MACOSX)
209 CFStringRef bundle_id = CFSTR("com.google.Chrome"); 141 CFStringRef bundle_id = CFSTR("com.google.Chrome");
210 DCHECK(!policy_service); 142 DCHECK(!policy_service);
211 return CreateFromPolicyLoader( 143 return PolicyServiceWatcher::CreateFromPolicyLoader(
212 network_task_runner, 144 network_task_runner,
213 make_scoped_ptr(new PolicyLoaderMac( 145 make_scoped_ptr(new PolicyLoaderMac(
214 network_task_runner, 146 network_task_runner,
215 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), 147 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id),
216 new MacPreferences(), bundle_id))); 148 new MacPreferences(), bundle_id)));
217 #elif defined(OS_POSIX) && !defined(OS_ANDROID) 149 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
218 DCHECK(!policy_service); 150 DCHECK(!policy_service);
219 // Always read the Chrome policies (even on Chromium) so that policy 151 // Always read the Chrome policies (even on Chromium) so that policy
220 // enforcement can't be bypassed by running Chromium. 152 // enforcement can't be bypassed by running Chromium.
221 static const base::FilePath::CharType kPolicyDir[] = 153 static const base::FilePath::CharType kPolicyDir[] =
222 FILE_PATH_LITERAL("/etc/opt/chrome/policies"); 154 FILE_PATH_LITERAL("/etc/opt/chrome/policies");
223 return CreateFromPolicyLoader( 155 return PolicyServiceWatcher::CreateFromPolicyLoader(
224 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader( 156 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader(
225 network_task_runner, base::FilePath(kPolicyDir), 157 network_task_runner, base::FilePath(kPolicyDir),
226 POLICY_SCOPE_MACHINE))); 158 POLICY_SCOPE_MACHINE)));
227 #else 159 #else
228 #error OS that is not yet supported by PolicyWatcher code. 160 #error OS that is not yet supported by PolicyWatcher code.
229 #endif 161 #endif
230 } 162 }
231 163
232 } // namespace policy_hack 164 } // namespace policy_hack
233 } // namespace remoting 165 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/policy_hack/policy_service_watcher.h ('k') | remoting/host/policy_hack/policy_watcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698