OLD | NEW |
| (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 "remoting/host/policy_hack/policy_service_watcher.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/values.h" | |
9 #include "components/policy/core/common/async_policy_loader.h" | |
10 #include "components/policy/core/common/async_policy_provider.h" | |
11 #include "components/policy/core/common/policy_namespace.h" | |
12 #include "components/policy/core/common/policy_service_impl.h" | |
13 #include "components/policy/core/common/schema.h" | |
14 #include "components/policy/core/common/schema_registry.h" | |
15 #include "policy/policy_constants.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 namespace { | |
34 | |
35 PolicyNamespace GetPolicyNamespace() { | |
36 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()); | |
37 } | |
38 | |
39 } // namespace | |
40 | |
41 PolicyServiceWatcher::PolicyServiceWatcher( | |
42 const scoped_refptr<base::SingleThreadTaskRunner>& | |
43 policy_service_task_runner, | |
44 PolicyService* policy_service) | |
45 : PolicyWatcher(policy_service_task_runner) { | |
46 policy_service_ = policy_service; | |
47 } | |
48 | |
49 PolicyServiceWatcher::PolicyServiceWatcher( | |
50 const scoped_refptr<base::SingleThreadTaskRunner>& | |
51 policy_service_task_runner, | |
52 scoped_ptr<PolicyService> owned_policy_service, | |
53 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider, | |
54 scoped_ptr<SchemaRegistry> owned_schema_registry) | |
55 : PolicyWatcher(policy_service_task_runner), | |
56 owned_schema_registry_(owned_schema_registry.Pass()), | |
57 owned_policy_provider_(owned_policy_provider.Pass()), | |
58 owned_policy_service_(owned_policy_service.Pass()) { | |
59 policy_service_ = owned_policy_service_.get(); | |
60 } | |
61 | |
62 PolicyServiceWatcher::~PolicyServiceWatcher() { | |
63 if (owned_policy_provider_) { | |
64 owned_policy_provider_->Shutdown(); | |
65 } | |
66 } | |
67 | |
68 void PolicyServiceWatcher::OnPolicyUpdated(const PolicyNamespace& ns, | |
69 const PolicyMap& previous, | |
70 const PolicyMap& current) { | |
71 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue()); | |
72 for (PolicyMap::const_iterator it = current.begin(); it != current.end(); | |
73 it++) { | |
74 // TODO(lukasza): Use policy::Schema::Normalize() for schema verification. | |
75 policy_dict->Set(it->first, it->second.value->DeepCopy()); | |
76 } | |
77 UpdatePolicies(policy_dict.get()); | |
78 } | |
79 | |
80 void PolicyServiceWatcher::OnPolicyServiceInitialized(PolicyDomain domain) { | |
81 PolicyNamespace ns = GetPolicyNamespace(); | |
82 const PolicyMap& current = policy_service_->GetPolicies(ns); | |
83 OnPolicyUpdated(ns, current, current); | |
84 } | |
85 | |
86 void PolicyServiceWatcher::StartWatchingInternal() { | |
87 // Listen for future policy changes. | |
88 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this); | |
89 | |
90 // Process current policy state. | |
91 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) { | |
92 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME); | |
93 } | |
94 } | |
95 | |
96 void PolicyServiceWatcher::StopWatchingInternal() { | |
97 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); | |
98 } | |
99 | |
100 scoped_ptr<PolicyServiceWatcher> PolicyServiceWatcher::CreateFromPolicyLoader( | |
101 const scoped_refptr<base::SingleThreadTaskRunner>& | |
102 policy_service_task_runner, | |
103 scoped_ptr<AsyncPolicyLoader> async_policy_loader) { | |
104 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific | |
105 // policies (expecting perf and maintanability improvement, but no functional | |
106 // impact). | |
107 Schema schema = Schema::Wrap(GetChromeSchemaData()); | |
108 | |
109 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry()); | |
110 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); | |
111 | |
112 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider( | |
113 schema_registry.get(), async_policy_loader.Pass())); | |
114 policy_provider->Init(schema_registry.get()); | |
115 | |
116 PolicyServiceImpl::Providers providers; | |
117 providers.push_back(policy_provider.get()); | |
118 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers)); | |
119 | |
120 return make_scoped_ptr(new PolicyServiceWatcher( | |
121 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(), | |
122 schema_registry.Pass())); | |
123 } | |
124 | |
125 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( | |
126 policy::PolicyService* policy_service, | |
127 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) { | |
128 #if defined(OS_CHROMEOS) | |
129 DCHECK(policy_service); | |
130 return make_scoped_ptr(new PolicyServiceWatcher( | |
131 content::BrowserThread::GetMessageLoopProxyForThread( | |
132 content::BrowserThread::UI), | |
133 policy_service)); | |
134 #elif defined(OS_WIN) | |
135 DCHECK(!policy_service); | |
136 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; | |
137 return PolicyServiceWatcher::CreateFromPolicyLoader( | |
138 network_task_runner, | |
139 PolicyLoaderWin::Create(network_task_runner, kRegistryKey)); | |
140 #elif defined(OS_MACOSX) | |
141 CFStringRef bundle_id = CFSTR("com.google.Chrome"); | |
142 DCHECK(!policy_service); | |
143 return PolicyServiceWatcher::CreateFromPolicyLoader( | |
144 network_task_runner, | |
145 make_scoped_ptr(new PolicyLoaderMac( | |
146 network_task_runner, | |
147 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), | |
148 new MacPreferences(), bundle_id))); | |
149 #elif defined(OS_POSIX) && !defined(OS_ANDROID) | |
150 DCHECK(!policy_service); | |
151 // Always read the Chrome policies (even on Chromium) so that policy | |
152 // enforcement can't be bypassed by running Chromium. | |
153 static const base::FilePath::CharType kPolicyDir[] = | |
154 FILE_PATH_LITERAL("/etc/opt/chrome/policies"); | |
155 return PolicyServiceWatcher::CreateFromPolicyLoader( | |
156 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader( | |
157 network_task_runner, base::FilePath(kPolicyDir), | |
158 POLICY_SCOPE_MACHINE))); | |
159 #else | |
160 #error OS that is not yet supported by PolicyWatcher code. | |
161 #endif | |
162 } | |
163 | |
164 } // namespace policy_hack | |
165 } // namespace remoting | |
OLD | NEW |