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 "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 // TODO(lukasza): Merge PolicyServiceWatcher with PolicyWatcher class |
| 38 // (after removing other classes derived from PolicyWatcher - i.e. after |
| 39 // removing FakePolicyWatcher class and replacing it with mocks of classes |
| 40 // from components/policy instead). |
| 41 |
| 42 // PolicyServiceWatcher is a concrete implementation of PolicyWatcher that wraps |
| 43 // an instance of PolicyService. |
| 44 class PolicyServiceWatcher : public PolicyWatcher, |
| 45 public PolicyService::Observer { |
| 46 public: |
| 47 // Constructor for the case when |policy_service| is borrowed. |
| 48 // |
| 49 // |policy_service_task_runner| is the task runner where it is safe |
| 50 // to call |policy_service| methods and where we expect to get callbacks |
| 51 // from |policy_service|. |
| 52 PolicyServiceWatcher(const scoped_refptr<base::SingleThreadTaskRunner>& |
| 53 policy_service_task_runner, |
| 54 PolicyService* policy_service); |
| 55 |
| 56 // Constructor for the case when |policy_service| is owned (and uses also |
| 57 // owned |owned_policy_provider| and |owned_schema_registry|. |
| 58 // |
| 59 // |policy_service_task_runner| is the task runner where it is safe |
| 60 // to call |policy_service| methods and where we expect to get callbacks |
| 61 // from |policy_service|. |
| 62 PolicyServiceWatcher( |
| 63 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 64 policy_service_task_runner, |
| 65 scoped_ptr<PolicyService> owned_policy_service, |
| 66 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider, |
| 67 scoped_ptr<SchemaRegistry> owned_schema_registry); |
| 68 |
| 69 ~PolicyServiceWatcher() override; |
| 70 |
| 71 // PolicyService::Observer interface. |
| 72 void OnPolicyUpdated(const PolicyNamespace& ns, |
| 73 const PolicyMap& previous, |
| 74 const PolicyMap& current) override; |
| 75 void OnPolicyServiceInitialized(PolicyDomain domain) override; |
| 76 |
| 77 protected: |
| 78 // PolicyWatcher overrides. |
| 79 void StartWatchingInternal() override; |
| 80 void StopWatchingInternal() override; |
| 81 |
| 82 private: |
| 83 PolicyService* policy_service_; |
| 84 |
| 85 // Order of fields below is important to ensure destruction takes object |
| 86 // dependencies into account: |
| 87 // - |owned_policy_service_| uses |owned_policy_provider_| |
| 88 // - |owned_policy_provider_| uses |owned_schema_registry_| |
| 89 scoped_ptr<SchemaRegistry> owned_schema_registry_; |
| 90 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider_; |
| 91 scoped_ptr<PolicyService> owned_policy_service_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(PolicyServiceWatcher); |
| 94 }; |
| 95 |
| 96 namespace { |
| 97 |
| 98 PolicyNamespace GetPolicyNamespace() { |
| 99 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()); |
| 100 } |
| 101 |
| 102 } // anonymous namespace |
| 103 |
| 104 PolicyServiceWatcher::PolicyServiceWatcher( |
| 105 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 106 policy_service_task_runner, |
| 107 PolicyService* policy_service) |
| 108 : PolicyWatcher(policy_service_task_runner) { |
| 109 policy_service_ = policy_service; |
| 110 } |
| 111 |
| 112 PolicyServiceWatcher::PolicyServiceWatcher( |
| 113 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 114 policy_service_task_runner, |
| 115 scoped_ptr<PolicyService> owned_policy_service, |
| 116 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider, |
| 117 scoped_ptr<SchemaRegistry> owned_schema_registry) |
| 118 : PolicyWatcher(policy_service_task_runner), |
| 119 owned_schema_registry_(owned_schema_registry.Pass()), |
| 120 owned_policy_provider_(owned_policy_provider.Pass()), |
| 121 owned_policy_service_(owned_policy_service.Pass()) { |
| 122 policy_service_ = owned_policy_service_.get(); |
| 123 } |
| 124 |
| 125 PolicyServiceWatcher::~PolicyServiceWatcher() { |
| 126 if (owned_policy_provider_) { |
| 127 owned_policy_provider_->Shutdown(); |
| 128 } |
| 129 } |
| 130 |
| 131 void PolicyServiceWatcher::OnPolicyUpdated(const PolicyNamespace& ns, |
| 132 const PolicyMap& previous, |
| 133 const PolicyMap& current) { |
| 134 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue()); |
| 135 for (PolicyMap::const_iterator it = current.begin(); it != current.end(); |
| 136 it++) { |
| 137 // TODO(lukasza): Use policy::Schema::Normalize() for schema verification. |
| 138 policy_dict->Set(it->first, it->second.value->DeepCopy()); |
| 139 } |
| 140 UpdatePolicies(policy_dict.get()); |
| 141 } |
| 142 |
| 143 void PolicyServiceWatcher::OnPolicyServiceInitialized(PolicyDomain domain) { |
| 144 PolicyNamespace ns = GetPolicyNamespace(); |
| 145 const PolicyMap& current = policy_service_->GetPolicies(ns); |
| 146 OnPolicyUpdated(ns, current, current); |
| 147 } |
| 148 |
| 149 void PolicyServiceWatcher::StartWatchingInternal() { |
| 150 // Listen for future policy changes. |
| 151 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this); |
| 152 |
| 153 // Process current policy state. |
| 154 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) { |
| 155 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME); |
| 156 } |
| 157 } |
| 158 |
| 159 void PolicyServiceWatcher::StopWatchingInternal() { |
| 160 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); |
| 161 } |
| 162 |
| 163 #if !defined(OS_CHROMEOS) |
| 164 |
| 165 // Creates PolicyServiceWatcher that wraps the owned |async_policy_loader| |
| 166 // with an appropriate PolicySchema. |
| 167 // |
| 168 // |policy_service_task_runner| is passed through to the constructor |
| 169 // of PolicyServiceWatcher. |
| 170 scoped_ptr<PolicyServiceWatcher> CreateFromPolicyLoader( |
| 171 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 172 policy_service_task_runner, |
| 173 scoped_ptr<AsyncPolicyLoader> async_policy_loader) { |
| 174 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific |
| 175 // policies (expecting perf and maintanability improvement, but no functional |
| 176 // impact). |
| 177 Schema schema = Schema::Wrap(GetChromeSchemaData()); |
| 178 |
| 179 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry()); |
| 180 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); |
| 181 |
| 182 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider( |
| 183 schema_registry.get(), async_policy_loader.Pass())); |
| 184 policy_provider->Init(schema_registry.get()); |
| 185 |
| 186 PolicyServiceImpl::Providers providers; |
| 187 providers.push_back(policy_provider.get()); |
| 188 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers)); |
| 189 |
| 190 return make_scoped_ptr(new PolicyServiceWatcher( |
| 191 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(), |
| 192 schema_registry.Pass())); |
| 193 } |
| 194 |
| 195 #endif |
| 196 |
| 197 } // anonymous namespace |
| 198 |
| 199 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( |
| 200 policy::PolicyService* policy_service, |
| 201 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) { |
| 202 #if defined(OS_CHROMEOS) |
| 203 DCHECK(policy_service); |
| 204 return make_scoped_ptr(new PolicyServiceWatcher( |
| 205 content::BrowserThread::GetMessageLoopProxyForThread( |
| 206 content::BrowserThread::UI), |
| 207 policy_service)); |
| 208 #elif defined(OS_WIN) |
| 209 DCHECK(!policy_service); |
| 210 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; |
| 211 return CreateFromPolicyLoader( |
| 212 network_task_runner, |
| 213 PolicyLoaderWin::Create(network_task_runner, kRegistryKey)); |
| 214 #elif defined(OS_MACOSX) |
| 215 CFStringRef bundle_id = CFSTR("com.google.Chrome"); |
| 216 DCHECK(!policy_service); |
| 217 return CreateFromPolicyLoader( |
| 218 network_task_runner, |
| 219 make_scoped_ptr(new PolicyLoaderMac( |
| 220 network_task_runner, |
| 221 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), |
| 222 new MacPreferences(), bundle_id))); |
| 223 #elif defined(OS_POSIX) && !defined(OS_ANDROID) |
| 224 DCHECK(!policy_service); |
| 225 // Always read the Chrome policies (even on Chromium) so that policy |
| 226 // enforcement can't be bypassed by running Chromium. |
| 227 static const base::FilePath::CharType kPolicyDir[] = |
| 228 FILE_PATH_LITERAL("/etc/opt/chrome/policies"); |
| 229 return CreateFromPolicyLoader( |
| 230 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader( |
| 231 network_task_runner, base::FilePath(kPolicyDir), |
| 232 POLICY_SCOPE_MACHINE))); |
| 233 #else |
| 234 #error OS that is not yet supported by PolicyWatcher code. |
| 235 #endif |
| 236 } |
| 237 |
| 238 } // namespace policy_hack |
| 239 } // namespace remoting |
OLD | NEW |