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.
5 #include "remoting/host/policy_hack/policy_watcher.h"
7 #include "components/policy/core/common/policy_service.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "remoting/base/auto_thread_task_runner.h"
11 using namespace policy
;
14 namespace policy_hack
{
18 class PolicyWatcherChromeOS
: public PolicyWatcher
,
19 public PolicyService::Observer
{
21 PolicyWatcherChromeOS(scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
22 PolicyService
* policy_service
);
24 ~PolicyWatcherChromeOS() override
;
26 // PolicyService::Observer interface.
27 void OnPolicyUpdated(const PolicyNamespace
& ns
,
28 const PolicyMap
& previous
,
29 const PolicyMap
& current
) override
;
32 // PolicyWatcher interface.
33 void Reload() override
;
34 void StartWatchingInternal() override
;
35 void StopWatchingInternal() override
;
38 PolicyService
* policy_service_
;
40 DISALLOW_COPY_AND_ASSIGN(PolicyWatcherChromeOS
);
43 PolicyWatcherChromeOS::PolicyWatcherChromeOS(
44 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
45 PolicyService
* policy_service
)
46 : PolicyWatcher(task_runner
), policy_service_(policy_service
) {
49 PolicyWatcherChromeOS::~PolicyWatcherChromeOS() {
52 void PolicyWatcherChromeOS::OnPolicyUpdated(const PolicyNamespace
& ns
,
53 const PolicyMap
& previous
,
54 const PolicyMap
& current
) {
55 scoped_ptr
<base::DictionaryValue
> policy_dict(new base::DictionaryValue());
56 for (PolicyMap::const_iterator it
= current
.begin(); it
!= current
.end();
58 policy_dict
->Set(it
->first
, it
->second
.value
->DeepCopy());
60 UpdatePolicies(policy_dict
.get());
63 void PolicyWatcherChromeOS::Reload() {
64 PolicyNamespace
ns(POLICY_DOMAIN_CHROME
, std::string());
65 const PolicyMap
& current
= policy_service_
->GetPolicies(ns
);
66 OnPolicyUpdated(ns
, current
, current
);
69 void PolicyWatcherChromeOS::StartWatchingInternal() {
70 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, this);
74 void PolicyWatcherChromeOS::StopWatchingInternal() {
75 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, this);
80 scoped_ptr
<PolicyWatcher
> PolicyWatcher::Create(
81 policy::PolicyService
* policy_service
,
82 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
) {
83 return make_scoped_ptr(new PolicyWatcherChromeOS(
84 content::BrowserThread::GetMessageLoopProxyForThread(
85 content::BrowserThread::UI
),
89 } // namespace policy_hack
90 } // namespace remoting