summaryrefslogtreecommitdiffstats
path: root/remoting/host/policy_hack/policy_watcher_mac.mm
blob: d0dd47322168ec526d3bb623391853d5c18d00ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/policy_hack/policy_watcher.h"

#include <CoreFoundation/CoreFoundation.h>

#include "base/compiler_specific.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/sys_string_conversions.h"
#include "base/values.h"

namespace remoting {
namespace policy_hack {

// The MacOS version does not watch files because it is accepted
// practice on the Mac that the user must logout/login for policies to be
// applied. This will actually pick up policies every
// |kFallbackReloadDelayMinutes| which is sufficient for right now.
class PolicyWatcherMac : public PolicyWatcher {
 public:
  explicit PolicyWatcherMac(
      scoped_refptr<base::SingleThreadTaskRunner> task_runner)
    : PolicyWatcher(task_runner) {
  }

  virtual ~PolicyWatcherMac() {
  }

 protected:
  virtual void StartWatchingInternal() OVERRIDE {
    Reload();
  }

  virtual void StopWatchingInternal() OVERRIDE {
  }

  virtual void Reload() OVERRIDE {
    DCHECK(OnPolicyWatcherThread());
    base::DictionaryValue policy;

    CFStringRef policy_bundle_id = CFSTR("com.google.Chrome");
    if (CFPreferencesAppSynchronize(policy_bundle_id)) {
      for (int i = 0; i < kBooleanPolicyNamesNum; ++i) {
        const char* policy_name = kBooleanPolicyNames[i];
        base::mac::ScopedCFTypeRef<CFStringRef> policy_key(
            base::SysUTF8ToCFStringRef(policy_name));
        Boolean valid = false;
        bool allowed = CFPreferencesGetAppBooleanValue(policy_key,
                                                       policy_bundle_id,
                                                       &valid);
        if (valid) {
          policy.SetBoolean(policy_name, allowed);
        }
      }
      for (int i = 0; i < kStringPolicyNamesNum; ++i) {
        const char* policy_name = kStringPolicyNames[i];
        base::mac::ScopedCFTypeRef<CFStringRef> policy_key(
            base::SysUTF8ToCFStringRef(policy_name));
        base::mac::ScopedCFTypeRef<CFPropertyListRef> property_list(
            CFPreferencesCopyAppValue(policy_key, policy_bundle_id));
        if (property_list.get() != NULL) {
          CFStringRef policy_value = base::mac::CFCast<CFStringRef>(
              property_list.get());
          if (policy_value != NULL) {
            policy.SetString(policy_name,
                             base::SysCFStringRefToUTF8(policy_value));
          } else {
            LOG(WARNING) << "Policy " << policy_name << ": value not a string.";
          }
        }
      }
    }

    // Set policy. Policy must be set (even if it is empty) so that the
    // default policy is picked up the first time reload is called.
    UpdatePolicies(&policy);

    // Reschedule task.
    ScheduleFallbackReloadTask();
  }
};

PolicyWatcher* PolicyWatcher::Create(
        scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
  return new PolicyWatcherMac(task_runner);
}

}  // namespace policy_hack
}  // namespace remoting