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

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

Issue 830193002: Using PolicyServiceWatcher instead of PolicyWatcherLinux/Win/Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed code review feedback from Mattias. 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
(Empty)
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 #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_provider.h"
9 #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/schema.h"
13 #include "components/policy/core/common/schema_registry.h"
14 #include "policy/policy_constants.h"
15 #include "remoting/host/policy_hack/policy_watcher.h"
16
17 #if defined(OS_CHROMEOS)
18 #include "content/public/browser/browser_thread.h"
19 #elif defined(OS_WIN)
20 #include "components/policy/core/common/policy_loader_win.h"
21 #elif defined(OS_MACOSX)
22 #include "components/policy/core/common/policy_loader_mac.h"
23 #include "components/policy/core/common/preferences_mac.h"
24 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
25 #include "components/policy/core/common/config_dir_policy_loader.h"
26 #endif
27
28 using namespace policy;
29
30 namespace remoting {
31 namespace policy_hack {
32
33 bool GetManagedPrefsDir(base::FilePath* result);
34
35 namespace {
36
37 // PolicyServiceWatcher is a concrete implementation of PolicyWatcher that wraps
38 // an instance of PolicyService.
39 class PolicyServiceWatcher : public PolicyWatcher,
40 public PolicyService::Observer {
41 public:
42 // Constructor for the case when |policy_service| is borrowed.
43 //
44 // |policy_service_task_runner| is the task runner where it is safe
45 // to call |policy_service| methods and where we expect to get callbacks
46 // from |policy_service|.
47 PolicyServiceWatcher(const scoped_refptr<base::SingleThreadTaskRunner>&
48 policy_service_task_runner,
49 PolicyService* policy_service);
50
51 // Constructor for the case when |policy_service| is owned (and uses also
52 // owned |owned_policy_provider| and |owned_schema_registry|.
53 //
54 // |policy_service_task_runner| is the task runner where it is safe
55 // to call |policy_service| methods and where we expect to get callbacks
56 // from |policy_service|.
57 PolicyServiceWatcher(
58 const scoped_refptr<base::SingleThreadTaskRunner>&
59 policy_service_task_runner,
60 scoped_ptr<PolicyService> owned_policy_service,
61 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider,
62 scoped_ptr<SchemaRegistry> owned_schema_registry);
63
64 ~PolicyServiceWatcher() override;
65
66 // PolicyService::Observer interface.
67 void OnPolicyUpdated(const PolicyNamespace& ns,
68 const PolicyMap& previous,
69 const PolicyMap& current) override;
70 void OnPolicyServiceInitialized(PolicyDomain domain) override;
71
72 static PolicyNamespace GetPolicyNamespace() {
73 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string());
74 }
75
76 protected:
77 // PolicyWatcher interface.
78 void StartWatchingInternal() override;
79 void StopWatchingInternal() override;
80
81 private:
82 PolicyService* policy_service_;
83
84 // Order of fields below is important to ensure destruction takes object
85 // dependencies into account:
86 // - |owned_policy_service_| uses |owned_policy_provider_|
87 // - |owned_policy_provider_| uses |owned_schema_registry_|
88 scoped_ptr<SchemaRegistry> owned_schema_registry_;
89 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider_;
90 scoped_ptr<PolicyService> owned_policy_service_;
91
92 DISALLOW_COPY_AND_ASSIGN(PolicyServiceWatcher);
93 };
94
95 PolicyServiceWatcher::PolicyServiceWatcher(
96 const scoped_refptr<base::SingleThreadTaskRunner>&
97 policy_service_task_runner,
98 PolicyService* policy_service)
99 : PolicyWatcher(policy_service_task_runner) {
100 policy_service_ = policy_service;
101 }
102
103 PolicyServiceWatcher::PolicyServiceWatcher(
104 const scoped_refptr<base::SingleThreadTaskRunner>&
105 policy_service_task_runner,
106 scoped_ptr<PolicyService> owned_policy_service,
107 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider,
108 scoped_ptr<SchemaRegistry> owned_schema_registry)
109 : PolicyWatcher(policy_service_task_runner),
110 owned_schema_registry_(owned_schema_registry.Pass()),
111 owned_policy_provider_(owned_policy_provider.Pass()),
112 owned_policy_service_(owned_policy_service.Pass()) {
113 policy_service_ = owned_policy_service_.get();
114 }
115
116 PolicyServiceWatcher::~PolicyServiceWatcher() {
117 if (owned_policy_provider_) {
118 owned_policy_provider_->Shutdown();
119 }
120 }
121
122 void PolicyServiceWatcher::OnPolicyUpdated(const PolicyNamespace& ns,
123 const PolicyMap& previous,
124 const PolicyMap& current) {
125 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue());
126 for (PolicyMap::const_iterator it = current.begin(); it != current.end();
127 it++) {
128 // TODO(lukasza): Use policy::Schema::Normalize for schema verification.
129 policy_dict->Set(it->first, it->second.value->DeepCopy());
130 }
131 UpdatePolicies(policy_dict.get());
132 }
133
134 void PolicyServiceWatcher::OnPolicyServiceInitialized(PolicyDomain domain) {
135 PolicyNamespace ns = GetPolicyNamespace();
136 const PolicyMap& current = policy_service_->GetPolicies(ns);
137 OnPolicyUpdated(ns, current, current);
138 }
139
140 void PolicyServiceWatcher::StartWatchingInternal() {
141 // Listen for future policy changes.
142 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this);
143
144 // Process current policy state.
145 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) {
146 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME);
147 }
148 }
149
150 void PolicyServiceWatcher::StopWatchingInternal() {
151 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
152 }
153
154 #if !defined(OS_CHROMEOS)
155
156 scoped_ptr<PolicyServiceWatcher> CreateFromPolicyLoader(
157 const scoped_refptr<base::SingleThreadTaskRunner>&
158 policy_service_task_runner,
Mattias Nissler (ping if slow) 2015/01/08 09:58:37 Can you put a comment saying that policy_service_t
Łukasz Anforowicz 2015/01/08 23:09:27 I think this is more of a restriction for PolicyWa
159 scoped_ptr<AsyncPolicyLoader> async_policy_loader) {
160 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry());
161 schema_registry->RegisterComponent(PolicyServiceWatcher::GetPolicyNamespace(),
162 Schema::Wrap(GetChromeSchemaData()));
163
164 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider(
165 schema_registry.get(), async_policy_loader.Pass()));
166 policy_provider->Init(schema_registry.get());
167
168 PolicyServiceImpl::Providers providers;
169 providers.push_back(policy_provider.get());
170 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers));
171
172 return make_scoped_ptr(new PolicyServiceWatcher(
173 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(),
174 schema_registry.Pass()));
175 }
176
177 #endif
178
179 } // anonymous namespace
180
181 scoped_ptr<PolicyWatcher> PolicyWatcher::Create(
182 policy::PolicyService* policy_service,
183 const scoped_refptr<base::SingleThreadTaskRunner>&
184 policy_service_task_runner) {
Mattias Nissler (ping if slow) 2015/01/08 09:58:37 In this case, the task runner should probably be n
Łukasz Anforowicz 2015/01/08 23:09:27 Yes - makes sense. Thanks.
185 #if defined(OS_CHROMEOS)
186 DCHECK(policy_service != nullptr);
187 return make_scoped_ptr(new PolicyServiceWatcher(
188 content::BrowserThread::GetMessageLoopProxyForThread(
189 content::BrowserThread::UI),
190 policy_service));
191 #elif defined(OS_WIN)
192 DCHECK(policy_service == nullptr);
193 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome";
194 return CreateFromPolicyLoader(
195 policy_service_task_runner,
196 scoped_ptr<AsyncPolicyLoader>(
197 PolicyLoaderWin::Create(policy_service_task_runner, kRegistryKey)));
198 #elif defined(OS_MACOSX)
199 CFStringRef bundle_id = CFSTR("com.google.Chrome");
200 DCHECK(policy_service == nullptr);
201 return CreateFromPolicyLoader(
202 policy_service_task_runner,
203 make_scoped_ptr(new PolicyLoaderMac(
204 policy_service_task_runner,
205 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id),
206 new MacPreferences(), bundle_id)));
207 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
208 DCHECK(policy_service == nullptr);
209 // Always read the Chrome policies (even on Chromium) so that policy
210 // enforcement can't be bypassed by running Chromium.
211 static const base::FilePath::CharType kPolicyDir[] =
212 FILE_PATH_LITERAL("/etc/opt/chrome/policies");
213 return CreateFromPolicyLoader(
214 policy_service_task_runner,
215 make_scoped_ptr(new ConfigDirPolicyLoader(policy_service_task_runner,
216 base::FilePath(kPolicyDir),
217 POLICY_SCOPE_MACHINE)));
218 #else
219 #error OS that is not yet supported by PolicyWatcher code.
220 #endif
221 }
222
223 } // namespace policy_hack
224 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698