OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/policy/core/common/policy_loader_mac.h" | 5 #include "components/policy/core/common/policy_loader_mac.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/files/file_path.h" | |
10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
11 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
12 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
13 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
14 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "components/policy/core/common/external_data_fetcher.h" | 17 #include "components/policy/core/common/external_data_fetcher.h" |
17 #include "components/policy/core/common/mac_util.h" | 18 #include "components/policy/core/common/mac_util.h" |
18 #include "components/policy/core/common/policy_bundle.h" | 19 #include "components/policy/core/common/policy_bundle.h" |
19 #include "components/policy/core/common/policy_load_status.h" | 20 #include "components/policy/core/common/policy_load_status.h" |
20 #include "components/policy/core/common/policy_map.h" | 21 #include "components/policy/core/common/policy_map.h" |
21 #include "components/policy/core/common/preferences_mac.h" | 22 #include "components/policy/core/common/preferences_mac.h" |
22 #include "components/policy/core/common/schema.h" | 23 #include "components/policy/core/common/schema.h" |
23 #include "components/policy/core/common/schema_map.h" | 24 #include "components/policy/core/common/schema_map.h" |
24 | 25 |
25 using base::ScopedCFTypeRef; | 26 using base::ScopedCFTypeRef; |
26 | 27 |
27 namespace policy { | 28 namespace policy { |
28 | 29 |
29 PolicyLoaderMac::PolicyLoaderMac( | 30 PolicyLoaderMac::PolicyLoaderMac( |
30 scoped_refptr<base::SequencedTaskRunner> task_runner, | 31 scoped_refptr<base::SequencedTaskRunner> task_runner, |
31 const base::FilePath& managed_policy_path, | 32 const base::FilePath& managed_policy_path, |
32 MacPreferences* preferences) | 33 MacPreferences* preferences) |
33 : AsyncPolicyLoader(task_runner), | 34 : AsyncPolicyLoader(task_runner), |
34 preferences_(preferences), | 35 preferences_(preferences), |
35 managed_policy_path_(managed_policy_path) {} | 36 managed_policy_path_(managed_policy_path), |
37 application_id_(kCFPreferencesCurrentApplication) { | |
38 } | |
39 | |
40 PolicyLoaderMac::PolicyLoaderMac( | |
41 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
42 const base::FilePath& managed_policy_path, | |
43 MacPreferences* preferences, | |
44 CFStringRef application_id) | |
45 : AsyncPolicyLoader(task_runner), | |
46 preferences_(preferences), | |
47 managed_policy_path_(managed_policy_path), | |
48 application_id_(application_id) { | |
49 } | |
36 | 50 |
37 PolicyLoaderMac::~PolicyLoaderMac() {} | 51 PolicyLoaderMac::~PolicyLoaderMac() {} |
38 | 52 |
39 void PolicyLoaderMac::InitOnBackgroundThread() { | 53 void PolicyLoaderMac::InitOnBackgroundThread() { |
40 if (!managed_policy_path_.empty()) { | 54 if (!managed_policy_path_.empty()) { |
41 watcher_.Watch( | 55 watcher_.Watch( |
42 managed_policy_path_, false, | 56 managed_policy_path_, false, |
43 base::Bind(&PolicyLoaderMac::OnFileUpdated, base::Unretained(this))); | 57 base::Bind(&PolicyLoaderMac::OnFileUpdated, base::Unretained(this))); |
44 } | 58 } |
45 } | 59 } |
46 | 60 |
47 scoped_ptr<PolicyBundle> PolicyLoaderMac::Load() { | 61 scoped_ptr<PolicyBundle> PolicyLoaderMac::Load() { |
48 preferences_->AppSynchronize(kCFPreferencesCurrentApplication); | 62 preferences_->AppSynchronize(application_id_); |
49 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | 63 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); |
50 | 64 |
51 // Load Chrome's policy. | 65 // Load Chrome's policy. |
52 PolicyMap& chrome_policy = | 66 PolicyMap& chrome_policy = |
53 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | 67 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); |
54 | 68 |
55 PolicyLoadStatusSample status; | 69 PolicyLoadStatusSample status; |
56 bool policy_present = false; | 70 bool policy_present = false; |
57 const Schema* schema = | 71 const Schema* schema = |
58 schema_map()->GetSchema(PolicyNamespace(POLICY_DOMAIN_CHROME, "")); | 72 schema_map()->GetSchema(PolicyNamespace(POLICY_DOMAIN_CHROME, "")); |
59 for (Schema::Iterator it = schema->GetPropertiesIterator(); | 73 for (Schema::Iterator it = schema->GetPropertiesIterator(); |
60 !it.IsAtEnd(); it.Advance()) { | 74 !it.IsAtEnd(); it.Advance()) { |
61 base::ScopedCFTypeRef<CFStringRef> name( | 75 base::ScopedCFTypeRef<CFStringRef> name( |
62 base::SysUTF8ToCFStringRef(it.key())); | 76 base::SysUTF8ToCFStringRef(it.key())); |
63 base::ScopedCFTypeRef<CFPropertyListRef> value( | 77 base::ScopedCFTypeRef<CFPropertyListRef> value( |
64 preferences_->CopyAppValue(name, kCFPreferencesCurrentApplication)); | 78 preferences_->CopyAppValue(name, application_id_)); |
65 if (!value.get()) | 79 if (!value.get()) |
66 continue; | 80 continue; |
67 policy_present = true; | 81 policy_present = true; |
68 bool forced = | 82 bool forced = preferences_->AppValueIsForced(name, application_id_); |
69 preferences_->AppValueIsForced(name, kCFPreferencesCurrentApplication); | |
70 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : | 83 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : |
71 POLICY_LEVEL_RECOMMENDED; | 84 POLICY_LEVEL_RECOMMENDED; |
72 // TODO(joaodasilva): figure the policy scope. | 85 // TODO(joaodasilva): figure the policy scope. |
73 scoped_ptr<base::Value> policy = PropertyToValue(value); | 86 scoped_ptr<base::Value> policy = PropertyToValue(value); |
74 if (policy) { | 87 if (policy) { |
75 chrome_policy.Set( | 88 chrome_policy.Set( |
76 it.key(), level, POLICY_SCOPE_USER, policy.release(), NULL); | 89 it.key(), level, POLICY_SCOPE_USER, policy.release(), NULL); |
77 } else { | 90 } else { |
78 status.Add(POLICY_LOAD_STATUS_PARSE_ERROR); | 91 status.Add(POLICY_LOAD_STATUS_PARSE_ERROR); |
79 } | 92 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 policy_value.release(), NULL); | 163 policy_value.release(), NULL); |
151 } | 164 } |
152 } | 165 } |
153 } | 166 } |
154 | 167 |
155 void PolicyLoaderMac::OnFileUpdated(const base::FilePath& path, bool error) { | 168 void PolicyLoaderMac::OnFileUpdated(const base::FilePath& path, bool error) { |
156 if (!error) | 169 if (!error) |
157 Reload(false); | 170 Reload(false); |
158 } | 171 } |
159 | 172 |
173 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
174 | |
175 base::FilePath PolicyLoaderMac::GetManagedPolicyPath(CFStringRef bundle_id) { | |
Mattias Nissler (ping if slow)
2015/01/08 09:58:37
nit: Please move this up to make the ordering of f
Łukasz Anforowicz
2015/01/08 23:09:27
Done.
| |
176 // This constructs the path to the plist file in which Mac OS X stores the | |
177 // managed preference for the application. This is undocumented and therefore | |
178 // fragile, but if it doesn't work out, AsyncPolicyLoader has a task that | |
179 // polls periodically in order to reload managed preferences later even if we | |
180 // missed the change. | |
181 | |
182 base::FilePath path; | |
183 if (!base::mac::GetLocalDirectory(NSLibraryDirectory, &path)) { | |
Mattias Nissler (ping if slow)
2015/01/08 09:58:36
nit: no curly braces
Łukasz Anforowicz
2015/01/08 23:09:27
Ok. I'll make this consistent with the rest of co
| |
184 return base::FilePath(); | |
185 } | |
186 path = path.Append(FILE_PATH_LITERAL("Managed Preferences")); | |
187 char* login = getlogin(); | |
188 if (!login) { | |
Mattias Nissler (ping if slow)
2015/01/08 09:58:36
ditto
Łukasz Anforowicz
2015/01/08 23:09:27
Done.
| |
189 return base::FilePath(); | |
190 } | |
191 path = path.AppendASCII(login); | |
192 if (!base::PathExists(path)) { // We don't want to create this. | |
Mattias Nissler (ping if slow)
2015/01/08 09:58:36
As explained in my other reply, I think we should
Łukasz Anforowicz
2015/01/08 23:09:27
Done.
| |
193 return base::FilePath(); | |
194 } | |
195 | |
196 return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist"); | |
197 } | |
198 | |
199 #endif | |
200 | |
160 } // namespace policy | 201 } // namespace policy |
OLD | NEW |