Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: chrome/browser/policy/policy_loader_mac.cc

Issue 10496013: Implement the mac policy provider based on the AsyncPolicyLoader. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: . Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/policy/configuration_policy_provider_mac.h" 5 #include "chrome/browser/policy/policy_loader_mac.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/file_path.h"
10 #include "base/file_util.h" 9 #include "base/file_util.h"
11 #include "base/mac/foundation_util.h" 10 #include "base/mac/foundation_util.h"
12 #include "base/mac/scoped_cftyperef.h" 11 #include "base/mac/scoped_cftyperef.h"
13 #include "base/path_service.h" 12 #include "base/path_service.h"
14 #include "base/platform_file.h" 13 #include "base/platform_file.h"
15 #include "base/sys_string_conversions.h" 14 #include "base/sys_string_conversions.h"
16 #include "base/values.h" 15 #include "base/values.h"
17 #include "chrome/browser/policy/policy_bundle.h" 16 #include "chrome/browser/policy/policy_bundle.h"
18 #include "chrome/browser/policy/policy_map.h" 17 #include "chrome/browser/policy/policy_map.h"
19 #include "chrome/browser/preferences_mac.h" 18 #include "chrome/browser/preferences_mac.h"
20 #include "chrome/common/chrome_paths.h" 19 #include "chrome/common/chrome_paths.h"
21 #include "policy/policy_constants.h" 20 #include "policy/policy_constants.h"
22 21
23 using base::mac::CFCast; 22 using base::mac::CFCast;
24 using base::mac::ScopedCFTypeRef; 23 using base::mac::ScopedCFTypeRef;
25 24
26 namespace policy { 25 namespace policy {
27 26
28 namespace { 27 namespace {
29 28
30 FilePath GetManagedPolicyPath() { 29 FilePath GetManagedPolicyPath() {
31 // This constructs the path to the plist file in which Mac OS X stores the 30 // This constructs the path to the plist file in which Mac OS X stores the
32 // managed preference for the application. This is undocumented and therefore 31 // managed preference for the application. This is undocumented and therefore
33 // fragile, but if it doesn't work out, FileBasedPolicyLoader has a task that 32 // fragile, but if it doesn't work out, AsyncPolicyLoader has a task that
34 // polls periodically in order to reload managed preferences later even if we 33 // polls periodically in order to reload managed preferences later even if we
35 // missed the change. 34 // missed the change.
36 FilePath path; 35 FilePath path;
37 if (!PathService::Get(chrome::DIR_MANAGED_PREFS, &path)) 36 if (!PathService::Get(chrome::DIR_MANAGED_PREFS, &path))
38 return FilePath(); 37 return FilePath();
39 38
40 CFBundleRef bundle(CFBundleGetMainBundle()); 39 CFBundleRef bundle(CFBundleGetMainBundle());
41 if (!bundle) 40 if (!bundle)
42 return FilePath(); 41 return FilePath();
43 42
44 CFStringRef bundle_id = CFBundleGetIdentifier(bundle); 43 CFStringRef bundle_id = CFBundleGetIdentifier(bundle);
45 if (!bundle_id) 44 if (!bundle_id)
46 return FilePath(); 45 return FilePath();
47 46
48 return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist"); 47 return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist");
49 } 48 }
50 49
51 // Callback function for CFDictionaryApplyFunction. |key| and |value| are an 50 // Callback function for CFDictionaryApplyFunction. |key| and |value| are an
52 // entry of the CFDictionary that should be converted into an equivalent entry 51 // entry of the CFDictionary that should be converted into an equivalent entry
53 // in the DictionaryValue in |context|. 52 // in the DictionaryValue in |context|.
54 void DictionaryEntryToValue(const void* key, const void* value, void* context) { 53 void DictionaryEntryToValue(const void* key, const void* value, void* context) {
55 if (CFStringRef cf_key = CFCast<CFStringRef>(key)) { 54 if (CFStringRef cf_key = CFCast<CFStringRef>(key)) {
56 base::Value* converted = 55 base::Value* converted =
57 MacPreferencesPolicyProviderDelegate::CreateValueFromProperty( 56 MacPolicyLoader::CreateValueFromProperty(
58 static_cast<CFPropertyListRef>(value)); 57 static_cast<CFPropertyListRef>(value));
59 if (converted) { 58 if (converted) {
60 const std::string string = base::SysCFStringRefToUTF8(cf_key); 59 const std::string string = base::SysCFStringRefToUTF8(cf_key);
61 static_cast<base::DictionaryValue *>(context)->Set(string, converted); 60 static_cast<base::DictionaryValue *>(context)->Set(string, converted);
62 } 61 }
63 } 62 }
64 } 63 }
65 64
66 // Callback function for CFArrayApplyFunction. |value| is an entry of the 65 // Callback function for CFArrayApplyFunction. |value| is an entry of the
67 // CFArray that should be converted into an equivalent entry in the ListValue 66 // CFArray that should be converted into an equivalent entry in the ListValue
68 // in |context|. 67 // in |context|.
69 void ArrayEntryToValue(const void* value, void* context) { 68 void ArrayEntryToValue(const void* value, void* context) {
70 base::Value* converted = 69 base::Value* converted =
71 MacPreferencesPolicyProviderDelegate::CreateValueFromProperty( 70 MacPolicyLoader::CreateValueFromProperty(
72 static_cast<CFPropertyListRef>(value)); 71 static_cast<CFPropertyListRef>(value));
73 if (converted) 72 if (converted)
74 static_cast<base::ListValue *>(context)->Append(converted); 73 static_cast<base::ListValue *>(context)->Append(converted);
75 } 74 }
76 75
77 } // namespace 76 } // namespace
78 77
79 MacPreferencesPolicyProviderDelegate::MacPreferencesPolicyProviderDelegate( 78 MacPolicyLoader::MacPolicyLoader(const PolicyDefinitionList* policy_list,
80 MacPreferences* preferences, 79 MacPreferences* preferences)
81 const PolicyDefinitionList* policy_list) 80 : policy_list_(policy_list),
82 : FileBasedPolicyProvider::ProviderDelegate(GetManagedPolicyPath()), 81 preferences_(preferences),
83 policy_list_(policy_list), 82 managed_policy_path_(GetManagedPolicyPath()) {}
84 preferences_(preferences) {}
85 83
86 MacPreferencesPolicyProviderDelegate::~MacPreferencesPolicyProviderDelegate() {} 84 MacPolicyLoader::~MacPolicyLoader() {}
87 85
88 scoped_ptr<PolicyBundle> MacPreferencesPolicyProviderDelegate::Load() { 86 // static
87 AsyncPolicyProvider* MacPolicyLoader::CreateProvider(
88 const PolicyDefinitionList* policy_list) {
89 MacPolicyLoader* loader =
90 new MacPolicyLoader(policy_list, new MacPreferences());
91 return new AsyncPolicyProvider(policy_list, loader);
Mattias Nissler (ping if slow) 2012/06/04 09:19:02 It seems this function will look pretty much the s
Joao da Silva 2012/06/04 17:55:14 Right, I think even CreateLoader() would be a very
92 }
93
94 void MacPolicyLoader::InitOnFile() {
95 if (!managed_policy_path_.empty())
96 WatchPath(managed_policy_path_);
97 }
98
99 scoped_ptr<PolicyBundle> MacPolicyLoader::Load() {
89 preferences_->AppSynchronize(kCFPreferencesCurrentApplication); 100 preferences_->AppSynchronize(kCFPreferencesCurrentApplication);
90 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); 101 scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
91 PolicyMap& chrome_policy = bundle->Get(POLICY_DOMAIN_CHROME, std::string()); 102 PolicyMap& chrome_policy = bundle->Get(POLICY_DOMAIN_CHROME, std::string());
92 103
93 const PolicyDefinitionList::Entry* current; 104 const PolicyDefinitionList::Entry* current;
94 for (current = policy_list_->begin; current != policy_list_->end; ++current) { 105 for (current = policy_list_->begin; current != policy_list_->end; ++current) {
95 base::mac::ScopedCFTypeRef<CFStringRef> name( 106 base::mac::ScopedCFTypeRef<CFStringRef> name(
96 base::SysUTF8ToCFStringRef(current->name)); 107 base::SysUTF8ToCFStringRef(current->name));
97 base::mac::ScopedCFTypeRef<CFPropertyListRef> value( 108 base::mac::ScopedCFTypeRef<CFPropertyListRef> value(
98 preferences_->CopyAppValue(name, kCFPreferencesCurrentApplication)); 109 preferences_->CopyAppValue(name, kCFPreferencesCurrentApplication));
99 if (!value.get()) 110 if (!value.get())
100 continue; 111 continue;
101 bool forced = 112 bool forced =
102 preferences_->AppValueIsForced(name, kCFPreferencesCurrentApplication); 113 preferences_->AppValueIsForced(name, kCFPreferencesCurrentApplication);
103 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : 114 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY :
104 POLICY_LEVEL_RECOMMENDED; 115 POLICY_LEVEL_RECOMMENDED;
105 // TODO(joaodasilva): figure the policy scope. 116 // TODO(joaodasilva): figure the policy scope.
106 base::Value* policy = CreateValueFromProperty(value); 117 base::Value* policy = CreateValueFromProperty(value);
107 if (policy) 118 if (policy)
108 chrome_policy.Set(current->name, level, POLICY_SCOPE_USER, policy); 119 chrome_policy.Set(current->name, level, POLICY_SCOPE_USER, policy);
109 } 120 }
110 121
111 return bundle.Pass(); 122 return bundle.Pass();
112 } 123 }
113 124
114 base::Time MacPreferencesPolicyProviderDelegate::GetLastModification() { 125 base::Time MacPolicyLoader::LastModificationTime() {
115 base::PlatformFileInfo file_info; 126 base::PlatformFileInfo file_info;
116 if (!file_util::GetFileInfo(config_file_path(), &file_info) || 127 if (!file_util::GetFileInfo(managed_policy_path_, &file_info) ||
117 file_info.is_directory) { 128 file_info.is_directory) {
118 return base::Time(); 129 return base::Time();
119 } 130 }
120 131
121 return file_info.last_modified; 132 return file_info.last_modified;
122 } 133 }
123 134
124 // static 135 // static
125 base::Value* MacPreferencesPolicyProviderDelegate::CreateValueFromProperty( 136 base::Value* MacPolicyLoader::CreateValueFromProperty(
126 CFPropertyListRef property) { 137 CFPropertyListRef property) {
127 if (CFCast<CFNullRef>(property)) 138 if (CFCast<CFNullRef>(property))
128 return base::Value::CreateNullValue(); 139 return base::Value::CreateNullValue();
129 140
130 if (CFBooleanRef boolean = CFCast<CFBooleanRef>(property)) 141 if (CFBooleanRef boolean = CFCast<CFBooleanRef>(property))
131 return base::Value::CreateBooleanValue(CFBooleanGetValue(boolean)); 142 return base::Value::CreateBooleanValue(CFBooleanGetValue(boolean));
132 143
133 if (CFNumberRef number = CFCast<CFNumberRef>(property)) { 144 if (CFNumberRef number = CFCast<CFNumberRef>(property)) {
134 // CFNumberGetValue() converts values implicitly when the conversion is not 145 // CFNumberGetValue() converts values implicitly when the conversion is not
135 // lossy. Check the type before trying to convert. 146 // lossy. Check the type before trying to convert.
(...skipping 22 matching lines...) Expand all
158 CFArrayApplyFunction(array, 169 CFArrayApplyFunction(array,
159 CFRangeMake(0, CFArrayGetCount(array)), 170 CFRangeMake(0, CFArrayGetCount(array)),
160 ArrayEntryToValue, 171 ArrayEntryToValue,
161 list_value); 172 list_value);
162 return list_value; 173 return list_value;
163 } 174 }
164 175
165 return NULL; 176 return NULL;
166 } 177 }
167 178
168 ConfigurationPolicyProviderMac::ConfigurationPolicyProviderMac(
169 const PolicyDefinitionList* policy_list)
170 : FileBasedPolicyProvider(
171 policy_list,
172 new MacPreferencesPolicyProviderDelegate(new MacPreferences(),
173 policy_list)) {}
174
175 ConfigurationPolicyProviderMac::ConfigurationPolicyProviderMac(
176 const PolicyDefinitionList* policy_list,
177 MacPreferences* preferences)
178 : FileBasedPolicyProvider(
179 policy_list,
180 new MacPreferencesPolicyProviderDelegate(preferences,
181 policy_list)) {}
182
183 } // namespace policy 179 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698