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 namespace { |
| 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() { |
| 95 return PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()); |
| 96 } |
| 97 |
| 98 PolicyServiceWatcher::PolicyServiceWatcher( |
| 99 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 100 policy_service_task_runner, |
| 101 PolicyService* policy_service) |
| 102 : PolicyWatcher(policy_service_task_runner) { |
| 103 policy_service_ = policy_service; |
| 104 } |
| 105 |
| 106 PolicyServiceWatcher::PolicyServiceWatcher( |
| 107 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 108 policy_service_task_runner, |
| 109 scoped_ptr<PolicyService> owned_policy_service, |
| 110 scoped_ptr<ConfigurationPolicyProvider> owned_policy_provider, |
| 111 scoped_ptr<SchemaRegistry> owned_schema_registry) |
| 112 : PolicyWatcher(policy_service_task_runner), |
| 113 owned_schema_registry_(owned_schema_registry.Pass()), |
| 114 owned_policy_provider_(owned_policy_provider.Pass()), |
| 115 owned_policy_service_(owned_policy_service.Pass()) { |
| 116 policy_service_ = owned_policy_service_.get(); |
| 117 } |
| 118 |
| 119 PolicyServiceWatcher::~PolicyServiceWatcher() { |
| 120 if (owned_policy_provider_) { |
| 121 owned_policy_provider_->Shutdown(); |
| 122 } |
| 123 } |
| 124 |
| 125 void PolicyServiceWatcher::OnPolicyUpdated(const PolicyNamespace& ns, |
| 126 const PolicyMap& previous, |
| 127 const PolicyMap& current) { |
| 128 scoped_ptr<base::DictionaryValue> policy_dict(new base::DictionaryValue()); |
| 129 for (PolicyMap::const_iterator it = current.begin(); it != current.end(); |
| 130 it++) { |
| 131 // TODO(lukasza): Use policy::Schema::Normalize() for schema verification. |
| 132 policy_dict->Set(it->first, it->second.value->DeepCopy()); |
| 133 } |
| 134 UpdatePolicies(policy_dict.get()); |
| 135 } |
| 136 |
| 137 void PolicyServiceWatcher::OnPolicyServiceInitialized(PolicyDomain domain) { |
| 138 PolicyNamespace ns = GetPolicyNamespace(); |
| 139 const PolicyMap& current = policy_service_->GetPolicies(ns); |
| 140 OnPolicyUpdated(ns, current, current); |
| 141 } |
| 142 |
| 143 void PolicyServiceWatcher::StartWatchingInternal() { |
| 144 // Listen for future policy changes. |
| 145 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this); |
| 146 |
| 147 // Process current policy state. |
| 148 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) { |
| 149 OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME); |
| 150 } |
| 151 } |
| 152 |
| 153 void PolicyServiceWatcher::StopWatchingInternal() { |
| 154 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); |
| 155 } |
| 156 |
| 157 #if !defined(OS_CHROMEOS) |
| 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>& |
| 166 policy_service_task_runner, |
| 167 scoped_ptr<AsyncPolicyLoader> async_policy_loader) { |
| 168 // TODO(lukasza): Schema below should ideally only cover Chromoting-specific |
| 169 // policies (expecting perf and maintanability improvement, but no functional |
| 170 // impact). |
| 171 Schema schema = Schema::Wrap(GetChromeSchemaData()); |
| 172 |
| 173 scoped_ptr<SchemaRegistry> schema_registry(new SchemaRegistry()); |
| 174 schema_registry->RegisterComponent(GetPolicyNamespace(), schema); |
| 175 |
| 176 scoped_ptr<AsyncPolicyProvider> policy_provider(new AsyncPolicyProvider( |
| 177 schema_registry.get(), async_policy_loader.Pass())); |
| 178 policy_provider->Init(schema_registry.get()); |
| 179 |
| 180 PolicyServiceImpl::Providers providers; |
| 181 providers.push_back(policy_provider.get()); |
| 182 scoped_ptr<PolicyService> policy_service(new PolicyServiceImpl(providers)); |
| 183 |
| 184 return make_scoped_ptr(new PolicyServiceWatcher( |
| 185 policy_service_task_runner, policy_service.Pass(), policy_provider.Pass(), |
| 186 schema_registry.Pass())); |
| 187 } |
| 188 |
| 189 #endif |
| 190 |
| 191 } // anonymous namespace |
| 192 |
| 193 scoped_ptr<PolicyWatcher> PolicyWatcher::Create( |
| 194 policy::PolicyService* policy_service, |
| 195 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) { |
| 196 #if defined(OS_CHROMEOS) |
| 197 DCHECK(policy_service); |
| 198 return make_scoped_ptr(new PolicyServiceWatcher( |
| 199 content::BrowserThread::GetMessageLoopProxyForThread( |
| 200 content::BrowserThread::UI), |
| 201 policy_service)); |
| 202 #elif defined(OS_WIN) |
| 203 DCHECK(!policy_service); |
| 204 static const wchar_t kRegistryKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; |
| 205 return CreateFromPolicyLoader( |
| 206 network_task_runner, |
| 207 PolicyLoaderWin::Create(network_task_runner, kRegistryKey)); |
| 208 #elif defined(OS_MACOSX) |
| 209 CFStringRef bundle_id = CFSTR("com.google.Chrome"); |
| 210 DCHECK(!policy_service); |
| 211 return CreateFromPolicyLoader( |
| 212 network_task_runner, |
| 213 make_scoped_ptr(new PolicyLoaderMac( |
| 214 network_task_runner, |
| 215 policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), |
| 216 new MacPreferences(), bundle_id))); |
| 217 #elif defined(OS_POSIX) && !defined(OS_ANDROID) |
| 218 DCHECK(!policy_service); |
| 219 // Always read the Chrome policies (even on Chromium) so that policy |
| 220 // enforcement can't be bypassed by running Chromium. |
| 221 static const base::FilePath::CharType kPolicyDir[] = |
| 222 FILE_PATH_LITERAL("/etc/opt/chrome/policies"); |
| 223 return CreateFromPolicyLoader( |
| 224 network_task_runner, make_scoped_ptr(new ConfigDirPolicyLoader( |
| 225 network_task_runner, base::FilePath(kPolicyDir), |
| 226 POLICY_SCOPE_MACHINE))); |
| 227 #else |
| 228 #error OS that is not yet supported by PolicyWatcher code. |
| 229 #endif |
| 230 } |
| 231 |
| 232 } // namespace policy_hack |
| 233 } // namespace remoting |
OLD | NEW |