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

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: Reuploading with lower "similarity". 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 "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"
5 #include "remoting/host/policy_hack/policy_watcher.h" 15 #include "remoting/host/policy_hack/policy_watcher.h"
6 16
7 #include "components/policy/core/common/policy_service.h" 17 #if defined(OS_CHROMEOS)
8 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
9 #include "remoting/base/auto_thread_task_runner.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
10 27
11 using namespace policy; 28 using namespace policy;
12 29
13 namespace remoting { 30 namespace remoting {
14 namespace policy_hack { 31 namespace policy_hack {
15 32
33 bool GetManagedPrefsDir(base::FilePath* result);
34
16 namespace { 35 namespace {
17 36
18 class PolicyWatcherChromeOS : public PolicyWatcher, 37 // PolicyServiceWatcher is a concrete implementation of PolicyWatcher that wraps
19 public PolicyService::Observer { 38 // an instance of PolicyService.
39 class PolicyServiceWatcher : public PolicyWatcher,
Sergey Ulanov 2015/01/13 01:17:04 Why do we need this class? Can we just move all co
Łukasz Anforowicz 2015/01/13 18:28:29 Hmmm... Mattias pointed this out as well. I lef
40 public PolicyService::Observer {
20 public: 41 public:
21 PolicyWatcherChromeOS(scoped_refptr<base::SingleThreadTaskRunner> task_runner, 42 // Constructor for the case when |policy_service| is borrowed.
22 PolicyService* policy_service); 43 //
23 44 // |policy_service_task_runner| is the task runner where it is safe
24 ~PolicyWatcherChromeOS() override; 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;
25 65
26 // PolicyService::Observer interface. 66 // PolicyService::Observer interface.
27 void OnPolicyUpdated(const PolicyNamespace& ns, 67 void OnPolicyUpdated(const PolicyNamespace& ns,
28 const PolicyMap& previous, 68 const PolicyMap& previous,
29 const PolicyMap& current) override; 69 const PolicyMap& current) override;
70 void OnPolicyServiceInitialized(PolicyDomain domain) override;
71
72 static PolicyNamespace GetPolicyNamespace() {
73 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string());
Sergey Ulanov 2015/01/13 01:17:03 Move definition of this method out of the class de
Łukasz Anforowicz 2015/01/13 18:28:29 Done.
74 }
30 75
31 protected: 76 protected:
32 // PolicyWatcher interface. 77 // PolicyWatcher interface.
Sergey Ulanov 2015/01/13 01:17:04 s/interface/overrides/. PolicyWatcher isn't an int
Łukasz Anforowicz 2015/01/13 18:28:29 Fair, done. (but note that I just retained this co
33 void Reload() override;
34 void StartWatchingInternal() override; 78 void StartWatchingInternal() override;
35 void StopWatchingInternal() override; 79 void StopWatchingInternal() override;
36 80
37 private: 81 private:
38 PolicyService* policy_service_; 82 PolicyService* policy_service_;
39 83
40 DISALLOW_COPY_AND_ASSIGN(PolicyWatcherChromeOS); 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);
41 }; 93 };
42 94
43 PolicyWatcherChromeOS::PolicyWatcherChromeOS( 95 PolicyServiceWatcher::PolicyServiceWatcher(
44 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 96 const scoped_refptr<base::SingleThreadTaskRunner>&
97 policy_service_task_runner,
45 PolicyService* policy_service) 98 PolicyService* policy_service)
46 : PolicyWatcher(task_runner), policy_service_(policy_service) { 99 : PolicyWatcher(policy_service_task_runner) {
47 } 100 policy_service_ = policy_service;
48 101 }
49 PolicyWatcherChromeOS::~PolicyWatcherChromeOS() { 102
50 } 103 PolicyServiceWatcher::PolicyServiceWatcher(
51 104 const scoped_refptr<base::SingleThreadTaskRunner>&
52 void PolicyWatcherChromeOS::OnPolicyUpdated(const PolicyNamespace& ns, 105 policy_service_task_runner,
53 const PolicyMap& previous, 106 scoped_ptr<PolicyService> owned_policy_service,
54 const PolicyMap& current) { 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) {
55 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue()); 125 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue());
56 for (PolicyMap::const_iterator it = current.begin(); it != current.end(); 126 for (PolicyMap::const_iterator it = current.begin(); it != current.end();
57 it++) { 127 it++) {
128 // TODO(lukasza): Use policy::Schema::Normalize for schema verification.
Sergey Ulanov 2015/01/13 01:17:03 nit: Use () after function names in comments, e.g.
Łukasz Anforowicz 2015/01/13 18:28:29 Done.
58 policy_dict->Set(it->first, it->second.value->DeepCopy()); 129 policy_dict->Set(it->first, it->second.value->DeepCopy());
59 } 130 }
60 UpdatePolicies(policy_dict.get()); 131 UpdatePolicies(policy_dict.get());
61 } 132 }
62 133
63 void PolicyWatcherChromeOS::Reload() { 134 void PolicyServiceWatcher::OnPolicyServiceInitialized(PolicyDomain domain) {
64 PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string()); 135 PolicyNamespace ns = GetPolicyNamespace();
65 const PolicyMap& current = policy_service_->GetPolicies(ns); 136 const PolicyMap& current = policy_service_->GetPolicies(ns);
66 OnPolicyUpdated(ns, current, current); 137 OnPolicyUpdated(ns, current, current);
67 }; 138 }
68 139
69 void PolicyWatcherChromeOS::StartWatchingInternal() { 140 void PolicyServiceWatcher::StartWatchingInternal() {
141 // Listen for future policy changes.
70 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this); 142 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this);
71 Reload(); 143
72 }; 144 // Process current policy state.
73 145 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) {
74 void PolicyWatcherChromeOS::StopWatchingInternal() { 146 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME);
147 }
148 }
149
150 void PolicyServiceWatcher::StopWatchingInternal() {
75 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); 151 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
76 }; 152 }
77 153
78 } // namespace 154 #if !defined(OS_CHROMEOS)
155
156 // Creates PolicyServiceWatcher that wraps the owned |async_policy_loader|
157 // with an appropriate PolicySchema.
158 //
159 // |policy_service_task_runner| is passed through to the constructor
160 // of PolicyServiceWatcher.
161 scoped_ptr<PolicyServiceWatcher> CreateFromPolicyLoader(
162 const scoped_refptr<base::SingleThreadTaskRunner>&
163 policy_service_task_runner,
164 scoped_ptr<AsyncPolicyLoader> async_policy_loader) {
165 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific
166 // policies (expecting perf and maintanability improvement, but no functional
167 // impact).
168 Schema schema = Schema::Wrap(GetChromeSchemaData());
169
170 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry());
171 schema_registry->RegisterComponent(PolicyServiceWatcher::GetPolicyNamespace(),
172 schema);
173
174 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider(
175 schema_registry.get(), async_policy_loader.Pass()));
176 policy_provider->Init(schema_registry.get());
177
178 PolicyServiceImpl::Providers providers;
179 providers.push_back(policy_provider.get());
180 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers));
181
182 return make_scoped_ptr(new PolicyServiceWatcher(
183 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(),
184 schema_registry.Pass()));
185 }
186
187 #endif
188
189 } // anonymous namespace
79 190
80 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( 191 scoped_ptr<PolicyWatcher> PolicyWatcher::Create(
81 policy::PolicyService* policy_service, 192 policy::PolicyService* policy_service,
82 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 193 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) {
83 return make_scoped_ptr(new PolicyWatcherChromeOS( 194 #if defined(OS_CHROMEOS)
195 DCHECK(policy_service != nullptr);
Sergey Ulanov 2015/01/13 01:17:04 nit: It's more common omit the nullptr comparison
Łukasz Anforowicz 2015/01/13 18:28:29 Done.
196 return make_scoped_ptr(new PolicyServiceWatcher(
84 content::BrowserThread::GetMessageLoopProxyForThread( 197 content::BrowserThread::GetMessageLoopProxyForThread(
85 content::BrowserThread::UI), 198 content::BrowserThread::UI),
86 policy_service)); 199 policy_service));
200 #elif defined(OS_WIN)
201 DCHECK(policy_service == nullptr);
202 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome";
203 return CreateFromPolicyLoader(
204 network_task_runner,
205 scoped_ptr<AsyncPolicyLoader>(
Sergey Ulanov 2015/01/13 01:17:04 nit: make_scoped_ptr()
Łukasz Anforowicz 2015/01/13 18:28:28 Thanks for pointing this out. I couldn't make it
206 PolicyLoaderWin::Create(network_task_runner, kRegistryKey)));
207 #elif defined(OS_MACOSX)
208 CFStringRef bundle_id = CFSTR("com.google.Chrome");
209 DCHECK(policy_service == nullptr);
210 return CreateFromPolicyLoader(
211 network_task_runner,
212 make_scoped_ptr(new PolicyLoaderMac(
213 network_task_runner,
214 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id),
215 new MacPreferences(), bundle_id)));
216 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
217 DCHECK(policy_service == nullptr);
218 // Always read the Chrome policies (even on Chromium) so that policy
219 // enforcement can't be bypassed by running Chromium.
220 static const base::FilePath::CharType kPolicyDir[] =
221 FILE_PATH_LITERAL("/etc/opt/chrome/policies");
222 return CreateFromPolicyLoader(
223 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader(
224 network_task_runner, base::FilePath(kPolicyDir),
225 POLICY_SCOPE_MACHINE)));
226 #else
227 #error OS that is not yet supported by PolicyWatcher code.
228 #endif
87 } 229 }
88 230
89 } // namespace policy_hack 231 } // namespace policy_hack
90 } // namespace remoting 232 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698