| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_pref_store.h" | 5 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 6 | 6 |
| 7 #include <map> | |
| 8 #include <string> | 7 #include <string> |
| 9 #include <vector> | 8 #include <vector> |
| 10 | 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/stl_util.h" | |
| 13 #include "base/string16.h" | 12 #include "base/string16.h" |
| 14 #include "base/string_util.h" | 13 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/download/download_util.h" | |
| 17 #include "chrome/browser/policy/browser_policy_connector.h" | 15 #include "chrome/browser/policy/browser_policy_connector.h" |
| 16 #include "chrome/browser/policy/configuration_policy_handler.h" |
| 17 #include "chrome/browser/policy/policy_error_map.h" |
| 18 #include "chrome/browser/policy/policy_map.h" | 18 #include "chrome/browser/policy/policy_map.h" |
| 19 #include "chrome/browser/policy/policy_path_parser.h" | |
| 20 #include "chrome/browser/prefs/incognito_mode_prefs.h" | |
| 21 #include "chrome/browser/prefs/pref_value_map.h" | 19 #include "chrome/browser/prefs/pref_value_map.h" |
| 22 #include "chrome/browser/prefs/proxy_config_dictionary.h" | 20 #include "content/browser/browser_thread.h" |
| 23 #include "chrome/browser/search_engines/search_terms_data.h" | |
| 24 #include "chrome/browser/search_engines/template_url.h" | |
| 25 #include "chrome/common/content_settings.h" | |
| 26 #include "chrome/common/pref_names.h" | |
| 27 #include "policy/policy_constants.h" | 21 #include "policy/policy_constants.h" |
| 28 | 22 |
| 29 namespace policy { | 23 namespace policy { |
| 30 | 24 |
| 31 // Accepts policy settings from a ConfigurationPolicyProvider, converts them | 25 namespace { |
| 32 // to preferences and caches the result. | |
| 33 class ConfigurationPolicyPrefKeeper { | |
| 34 public: | |
| 35 explicit ConfigurationPolicyPrefKeeper(ConfigurationPolicyProvider* provider); | |
| 36 virtual ~ConfigurationPolicyPrefKeeper(); | |
| 37 | 26 |
| 38 // Get a preference value. | 27 // Policies are loaded early on startup, before PolicyErrorMaps are ready to |
| 39 PrefStore::ReadResult GetValue(const std::string& key, | 28 // be retrieved. This function is posted to UI to log any errors found on |
| 40 const Value** result) const; | 29 // Refresh below. |
| 41 | 30 void LogErrors(PolicyErrorMap* errors) { |
| 42 // Compute the set of preference names that are different in |keeper|. This | 31 PolicyErrorMap::const_iterator iter; |
| 43 // includes preferences that are missing in either one. | 32 for (iter = errors->begin(); iter != errors->end(); ++iter) { |
| 44 void GetDifferingPrefPaths(const ConfigurationPolicyPrefKeeper* other, | 33 string16 policy = ASCIIToUTF16(GetPolicyName(iter->first)); |
| 45 std::vector<std::string>* differing_prefs) const; | 34 DLOG(WARNING) << "Policy " << policy << ": " << iter->second; |
| 46 | |
| 47 private: | |
| 48 // Apply the policy settings stored in |policies|. | |
| 49 void Apply(PolicyMap* policies); | |
| 50 | |
| 51 // Policies that map to a single preference are handled | |
| 52 // by an automated converter. Each one of these policies | |
| 53 // has an entry in |simple_policy_map_| with the following type. | |
| 54 struct PolicyToPreferenceMapEntry { | |
| 55 base::Value::Type value_type; | |
| 56 ConfigurationPolicyType policy_type; | |
| 57 const char* preference_path; // A DictionaryValue path, not a file path. | |
| 58 }; | |
| 59 | |
| 60 // Remove the preferences found in the map from |prefs_|. Returns true if any | |
| 61 // such preferences were found and removed. | |
| 62 bool RemovePreferencesOfMap(const PolicyToPreferenceMapEntry* map, | |
| 63 int table_size); | |
| 64 | |
| 65 bool ApplyPolicyFromMap(ConfigurationPolicyType policy, | |
| 66 Value* value, | |
| 67 const PolicyToPreferenceMapEntry* map, | |
| 68 int size); | |
| 69 | |
| 70 // Processes proxy-specific policies. Returns true if the specified policy | |
| 71 // is a proxy-related policy. ApplyProxyPolicy assumes the ownership | |
| 72 // of |value| in the case that the policy is proxy-specific. | |
| 73 bool ApplyProxyPolicy(ConfigurationPolicyType policy, Value* value); | |
| 74 | |
| 75 // Handles sync-related policies. Returns true if the policy was handled. | |
| 76 // Assumes ownership of |value| in that case. | |
| 77 bool ApplySyncPolicy(ConfigurationPolicyType policy, Value* value); | |
| 78 | |
| 79 // Handles policies that affect Autofill. Returns true if the policy was | |
| 80 // handled and assumes ownership of |value| in that case. | |
| 81 bool ApplyAutofillPolicy(ConfigurationPolicyType policy, Value* value); | |
| 82 | |
| 83 // Processes download directory policy. Returns true if the specified policy | |
| 84 // is the download directory policy. ApplyDownloadDirPolicy assumes the | |
| 85 // ownership of |value| in the case that the policy is recognized. | |
| 86 bool ApplyDownloadDirPolicy(ConfigurationPolicyType policy, Value* value); | |
| 87 | |
| 88 // Processes disk cache directory policy. Returns true if the specified policy | |
| 89 // is the right one. ApplyDiskCacheDirPolicy assumes the | |
| 90 // ownership of |value| in the case that the policy is recognized. | |
| 91 bool ApplyDiskCacheDirPolicy(ConfigurationPolicyType policy, Value* value); | |
| 92 | |
| 93 // Processes file-selection dialogs policy. Returns true if the specified | |
| 94 // policy is the file-selection dialogs policy. | |
| 95 // ApplyFileSelectionDialogsPolicy assumes the ownership of |value| in the | |
| 96 // case that the policy is recognized. | |
| 97 bool ApplyFileSelectionDialogsPolicy(ConfigurationPolicyType policy, | |
| 98 Value* value); | |
| 99 | |
| 100 // Processes default search provider policies. Returns true if the specified | |
| 101 // policy is a default search provider related policy. In that case, | |
| 102 // ApplyDefaultSearchPolicy takes ownership of |value|. | |
| 103 bool ApplyDefaultSearchPolicy(ConfigurationPolicyType policy, Value* value); | |
| 104 | |
| 105 // Processes incognito mode availability related policies. Returns true if the | |
| 106 // specified policy is pertinent to incognito mode availability. In that case, | |
| 107 // the function takes ownership of |value|. | |
| 108 bool ApplyIncognitoModePolicy(ConfigurationPolicyType policy, Value* value); | |
| 109 | |
| 110 // Processes policies that determine whether Javascript can be enabled. | |
| 111 // Returns true if the specified policy was processed. | |
| 112 bool ApplyJavascriptPolicy(ConfigurationPolicyType policy, Value* value); | |
| 113 | |
| 114 // Make sure that the |path| if present in |prefs_|. If not, set it to | |
| 115 // a blank string. | |
| 116 void EnsureStringPrefExists(const std::string& path); | |
| 117 | |
| 118 // If the required entries for default search are specified and valid, | |
| 119 // finalizes the policy-specified configuration by initializing the | |
| 120 // unspecified map entries. Otherwise wipes all default search related | |
| 121 // map entries from |prefs_|. | |
| 122 void FinalizeDefaultSearchPolicySettings(); | |
| 123 | |
| 124 // If the required entries for the proxy settings are specified and valid, | |
| 125 // finalizes the policy-specified configuration by initializing the | |
| 126 // respective values in |prefs_|. | |
| 127 void FinalizeProxyPolicySettings(); | |
| 128 | |
| 129 // If the required entries for the Incognito mode availability settings | |
| 130 // are specified and valid, finalizes the policy-specified configuration | |
| 131 // by initializing the respective values in |prefs_|. | |
| 132 void FinalizeIncognitoModeSettings(); | |
| 133 | |
| 134 // Returns true if the policy values stored in proxy_* represent a valid proxy | |
| 135 // configuration, including the case in which there is no configuration at | |
| 136 // all. | |
| 137 bool CheckProxySettings(); | |
| 138 | |
| 139 // Assumes CheckProxySettings returns true and applies the values stored | |
| 140 // in proxy_*. | |
| 141 void ApplyProxySettings(); | |
| 142 | |
| 143 bool HasProxyPolicy(ConfigurationPolicyType policy) const; | |
| 144 | |
| 145 // Temporary cache that stores values until FinalizeProxyPolicySettings() | |
| 146 // is called. | |
| 147 std::map<ConfigurationPolicyType, Value*> proxy_policies_; | |
| 148 | |
| 149 // Saved state of the deprecated kPolicyIncognitoEnabled. It is still used for | |
| 150 // backward compatibility to set the new kIncognitoAvailabilityMode pref in | |
| 151 // case the corresponding policy for the latter is not specified. | |
| 152 scoped_ptr<Value> deprecated_incognito_enabled_; | |
| 153 | |
| 154 PrefValueMap prefs_; | |
| 155 | |
| 156 static const PolicyToPreferenceMapEntry kSimplePolicyMap[]; | |
| 157 static const PolicyToPreferenceMapEntry kDefaultSearchPolicyMap[]; | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyPrefKeeper); | |
| 160 }; | |
| 161 | |
| 162 const ConfigurationPolicyPrefKeeper::PolicyToPreferenceMapEntry | |
| 163 ConfigurationPolicyPrefKeeper::kSimplePolicyMap[] = { | |
| 164 { Value::TYPE_STRING, kPolicyHomepageLocation, prefs::kHomePage }, | |
| 165 { Value::TYPE_BOOLEAN, kPolicyHomepageIsNewTabPage, | |
| 166 prefs::kHomePageIsNewTabPage }, | |
| 167 { Value::TYPE_INTEGER, kPolicyRestoreOnStartup, | |
| 168 prefs::kRestoreOnStartup}, | |
| 169 { Value::TYPE_LIST, kPolicyRestoreOnStartupURLs, | |
| 170 prefs::kURLsToRestoreOnStartup }, | |
| 171 { Value::TYPE_BOOLEAN, kPolicyAlternateErrorPagesEnabled, | |
| 172 prefs::kAlternateErrorPagesEnabled }, | |
| 173 { Value::TYPE_BOOLEAN, kPolicySearchSuggestEnabled, | |
| 174 prefs::kSearchSuggestEnabled }, | |
| 175 { Value::TYPE_BOOLEAN, kPolicyDnsPrefetchingEnabled, | |
| 176 prefs::kNetworkPredictionEnabled }, | |
| 177 { Value::TYPE_BOOLEAN, kPolicyDisableSpdy, | |
| 178 prefs::kDisableSpdy }, | |
| 179 { Value::TYPE_LIST, kPolicyDisabledSchemes, | |
| 180 prefs::kDisabledSchemes }, | |
| 181 { Value::TYPE_BOOLEAN, kPolicySafeBrowsingEnabled, | |
| 182 prefs::kSafeBrowsingEnabled }, | |
| 183 { Value::TYPE_BOOLEAN, kPolicyPasswordManagerEnabled, | |
| 184 prefs::kPasswordManagerEnabled }, | |
| 185 { Value::TYPE_BOOLEAN, kPolicyPasswordManagerAllowShowPasswords, | |
| 186 prefs::kPasswordManagerAllowShowPasswords }, | |
| 187 { Value::TYPE_BOOLEAN, kPolicyPrintingEnabled, | |
| 188 prefs::kPrintingEnabled }, | |
| 189 { Value::TYPE_BOOLEAN, kPolicyMetricsReportingEnabled, | |
| 190 prefs::kMetricsReportingEnabled }, | |
| 191 { Value::TYPE_STRING, kPolicyApplicationLocaleValue, | |
| 192 prefs::kApplicationLocale}, | |
| 193 { Value::TYPE_LIST, kPolicyExtensionInstallWhitelist, | |
| 194 prefs::kExtensionInstallAllowList}, | |
| 195 { Value::TYPE_LIST, kPolicyExtensionInstallBlacklist, | |
| 196 prefs::kExtensionInstallDenyList}, | |
| 197 { Value::TYPE_LIST, kPolicyExtensionInstallForcelist, | |
| 198 prefs::kExtensionInstallForceList}, | |
| 199 { Value::TYPE_LIST, kPolicyDisabledPlugins, | |
| 200 prefs::kPluginsDisabledPlugins}, | |
| 201 { Value::TYPE_LIST, kPolicyDisabledPluginsExceptions, | |
| 202 prefs::kPluginsDisabledPluginsExceptions}, | |
| 203 { Value::TYPE_LIST, kPolicyEnabledPlugins, | |
| 204 prefs::kPluginsEnabledPlugins}, | |
| 205 { Value::TYPE_BOOLEAN, kPolicyShowHomeButton, | |
| 206 prefs::kShowHomeButton }, | |
| 207 { Value::TYPE_BOOLEAN, kPolicySavingBrowserHistoryDisabled, | |
| 208 prefs::kSavingBrowserHistoryDisabled }, | |
| 209 { Value::TYPE_BOOLEAN, kPolicyClearSiteDataOnExit, | |
| 210 prefs::kClearSiteDataOnExit }, | |
| 211 { Value::TYPE_BOOLEAN, kPolicyDeveloperToolsDisabled, | |
| 212 prefs::kDevToolsDisabled }, | |
| 213 { Value::TYPE_BOOLEAN, kPolicyBlockThirdPartyCookies, | |
| 214 prefs::kBlockThirdPartyCookies }, | |
| 215 { Value::TYPE_INTEGER, kPolicyDefaultCookiesSetting, | |
| 216 prefs::kManagedDefaultCookiesSetting }, | |
| 217 { Value::TYPE_INTEGER, kPolicyDefaultImagesSetting, | |
| 218 prefs::kManagedDefaultImagesSetting }, | |
| 219 { Value::TYPE_INTEGER, kPolicyDefaultPluginsSetting, | |
| 220 prefs::kManagedDefaultPluginsSetting }, | |
| 221 { Value::TYPE_INTEGER, kPolicyDefaultPopupsSetting, | |
| 222 prefs::kManagedDefaultPopupsSetting }, | |
| 223 { Value::TYPE_LIST, kPolicyAutoSelectCertificateForUrls, | |
| 224 prefs::kManagedAutoSelectCertificateForUrls}, | |
| 225 { Value::TYPE_LIST, kPolicyCookiesAllowedForUrls, | |
| 226 prefs::kManagedCookiesAllowedForUrls }, | |
| 227 { Value::TYPE_LIST, kPolicyCookiesBlockedForUrls, | |
| 228 prefs::kManagedCookiesBlockedForUrls }, | |
| 229 { Value::TYPE_LIST, kPolicyCookiesSessionOnlyForUrls, | |
| 230 prefs::kManagedCookiesSessionOnlyForUrls }, | |
| 231 { Value::TYPE_LIST, kPolicyImagesAllowedForUrls, | |
| 232 prefs::kManagedImagesAllowedForUrls }, | |
| 233 { Value::TYPE_LIST, kPolicyImagesBlockedForUrls, | |
| 234 prefs::kManagedImagesBlockedForUrls }, | |
| 235 { Value::TYPE_LIST, kPolicyJavaScriptAllowedForUrls, | |
| 236 prefs::kManagedJavaScriptAllowedForUrls }, | |
| 237 { Value::TYPE_LIST, kPolicyJavaScriptBlockedForUrls, | |
| 238 prefs::kManagedJavaScriptBlockedForUrls }, | |
| 239 { Value::TYPE_LIST, kPolicyPluginsAllowedForUrls, | |
| 240 prefs::kManagedPluginsAllowedForUrls }, | |
| 241 { Value::TYPE_LIST, kPolicyPluginsBlockedForUrls, | |
| 242 prefs::kManagedPluginsBlockedForUrls }, | |
| 243 { Value::TYPE_LIST, kPolicyPopupsAllowedForUrls, | |
| 244 prefs::kManagedPopupsAllowedForUrls }, | |
| 245 { Value::TYPE_LIST, kPolicyPopupsBlockedForUrls, | |
| 246 prefs::kManagedPopupsBlockedForUrls }, | |
| 247 { Value::TYPE_LIST, kPolicyNotificationsAllowedForUrls, | |
| 248 prefs::kManagedNotificationsAllowedForUrls }, | |
| 249 { Value::TYPE_LIST, kPolicyNotificationsBlockedForUrls, | |
| 250 prefs::kManagedNotificationsBlockedForUrls }, | |
| 251 { Value::TYPE_INTEGER, kPolicyDefaultNotificationsSetting, | |
| 252 prefs::kManagedDefaultNotificationsSetting }, | |
| 253 { Value::TYPE_INTEGER, kPolicyDefaultGeolocationSetting, | |
| 254 prefs::kManagedDefaultGeolocationSetting }, | |
| 255 { Value::TYPE_STRING, kPolicyAuthSchemes, | |
| 256 prefs::kAuthSchemes }, | |
| 257 { Value::TYPE_BOOLEAN, kPolicyDisableAuthNegotiateCnameLookup, | |
| 258 prefs::kDisableAuthNegotiateCnameLookup }, | |
| 259 { Value::TYPE_BOOLEAN, kPolicyEnableAuthNegotiatePort, | |
| 260 prefs::kEnableAuthNegotiatePort }, | |
| 261 { Value::TYPE_STRING, kPolicyAuthServerWhitelist, | |
| 262 prefs::kAuthServerWhitelist }, | |
| 263 { Value::TYPE_STRING, kPolicyAuthNegotiateDelegateWhitelist, | |
| 264 prefs::kAuthNegotiateDelegateWhitelist }, | |
| 265 { Value::TYPE_STRING, kPolicyGSSAPILibraryName, | |
| 266 prefs::kGSSAPILibraryName }, | |
| 267 { Value::TYPE_BOOLEAN, kPolicyAllowCrossOriginAuthPrompt, | |
| 268 prefs::kAllowCrossOriginAuthPrompt }, | |
| 269 { Value::TYPE_BOOLEAN, kPolicyDisable3DAPIs, | |
| 270 prefs::kDisable3DAPIs }, | |
| 271 { Value::TYPE_BOOLEAN, kPolicyDisablePluginFinder, | |
| 272 prefs::kDisablePluginFinder }, | |
| 273 { Value::TYPE_INTEGER, kPolicyPolicyRefreshRate, | |
| 274 prefs::kUserPolicyRefreshRate }, | |
| 275 { Value::TYPE_INTEGER, kPolicyDevicePolicyRefreshRate, | |
| 276 prefs::kDevicePolicyRefreshRate }, | |
| 277 { Value::TYPE_BOOLEAN, kPolicyInstantEnabled, prefs::kInstantEnabled }, | |
| 278 { Value::TYPE_BOOLEAN, kPolicyDefaultBrowserSettingEnabled, | |
| 279 prefs::kDefaultBrowserSettingEnabled }, | |
| 280 { Value::TYPE_BOOLEAN, kPolicyRemoteAccessHostFirewallTraversal, | |
| 281 prefs::kRemoteAccessHostFirewallTraversal }, | |
| 282 { Value::TYPE_BOOLEAN, kPolicyCloudPrintProxyEnabled, | |
| 283 prefs::kCloudPrintProxyEnabled }, | |
| 284 { Value::TYPE_BOOLEAN, kPolicyTranslateEnabled, prefs::kEnableTranslate }, | |
| 285 { Value::TYPE_BOOLEAN, kPolicyAllowOutdatedPlugins, | |
| 286 prefs::kPluginsAllowOutdated }, | |
| 287 { Value::TYPE_BOOLEAN, kPolicyAlwaysAuthorizePlugins, | |
| 288 prefs::kPluginsAlwaysAuthorize }, | |
| 289 { Value::TYPE_BOOLEAN, kPolicyBookmarkBarEnabled, | |
| 290 prefs::kShowBookmarkBar }, | |
| 291 { Value::TYPE_BOOLEAN, kPolicyEditBookmarksEnabled, | |
| 292 prefs::kEditBookmarksEnabled }, | |
| 293 { Value::TYPE_BOOLEAN, kPolicyAllowFileSelectionDialogs, | |
| 294 prefs::kAllowFileSelectionDialogs }, | |
| 295 { Value::TYPE_BOOLEAN, kPolicyImportBookmarks, | |
| 296 prefs::kImportBookmarks}, | |
| 297 { Value::TYPE_BOOLEAN, kPolicyImportHistory, | |
| 298 prefs::kImportHistory}, | |
| 299 { Value::TYPE_BOOLEAN, kPolicyImportHomepage, | |
| 300 prefs::kImportHomepage}, | |
| 301 { Value::TYPE_BOOLEAN, kPolicyImportSearchEngine, | |
| 302 prefs::kImportSearchEngine }, | |
| 303 { Value::TYPE_BOOLEAN, kPolicyImportSavedPasswords, | |
| 304 prefs::kImportSavedPasswords }, | |
| 305 { Value::TYPE_INTEGER, kPolicyMaxConnectionsPerProxy, | |
| 306 prefs::kMaxConnectionsPerProxy }, | |
| 307 { Value::TYPE_BOOLEAN, kPolicyHideWebStorePromo, | |
| 308 prefs::kNTPHideWebStorePromo }, | |
| 309 { Value::TYPE_LIST, kPolicyURLBlacklist, | |
| 310 prefs::kUrlBlacklist }, | |
| 311 { Value::TYPE_LIST, kPolicyURLWhitelist, | |
| 312 prefs::kUrlWhitelist }, | |
| 313 | |
| 314 #if defined(OS_CHROMEOS) | |
| 315 { Value::TYPE_BOOLEAN, kPolicyChromeOsLockOnIdleSuspend, | |
| 316 prefs::kEnableScreenLock }, | |
| 317 { Value::TYPE_STRING, kPolicyChromeOsReleaseChannel, | |
| 318 prefs::kChromeOsReleaseChannel }, | |
| 319 #endif | |
| 320 }; | |
| 321 | |
| 322 const ConfigurationPolicyPrefKeeper::PolicyToPreferenceMapEntry | |
| 323 ConfigurationPolicyPrefKeeper::kDefaultSearchPolicyMap[] = { | |
| 324 { Value::TYPE_BOOLEAN, kPolicyDefaultSearchProviderEnabled, | |
| 325 prefs::kDefaultSearchProviderEnabled }, | |
| 326 { Value::TYPE_STRING, kPolicyDefaultSearchProviderName, | |
| 327 prefs::kDefaultSearchProviderName }, | |
| 328 { Value::TYPE_STRING, kPolicyDefaultSearchProviderKeyword, | |
| 329 prefs::kDefaultSearchProviderKeyword }, | |
| 330 { Value::TYPE_STRING, kPolicyDefaultSearchProviderSearchURL, | |
| 331 prefs::kDefaultSearchProviderSearchURL }, | |
| 332 { Value::TYPE_STRING, kPolicyDefaultSearchProviderSuggestURL, | |
| 333 prefs::kDefaultSearchProviderSuggestURL }, | |
| 334 { Value::TYPE_STRING, kPolicyDefaultSearchProviderInstantURL, | |
| 335 prefs::kDefaultSearchProviderInstantURL }, | |
| 336 { Value::TYPE_STRING, kPolicyDefaultSearchProviderIconURL, | |
| 337 prefs::kDefaultSearchProviderIconURL }, | |
| 338 { Value::TYPE_LIST, kPolicyDefaultSearchProviderEncodings, | |
| 339 prefs::kDefaultSearchProviderEncodings }, | |
| 340 }; | |
| 341 | |
| 342 ConfigurationPolicyPrefKeeper::ConfigurationPolicyPrefKeeper( | |
| 343 ConfigurationPolicyProvider* provider) { | |
| 344 scoped_ptr<PolicyMap> policies(new PolicyMap()); | |
| 345 if (!provider->Provide(policies.get())) | |
| 346 LOG(WARNING) << "Failed to get policy from provider."; | |
| 347 | |
| 348 Apply(policies.get()); | |
| 349 FinalizeProxyPolicySettings(); | |
| 350 FinalizeDefaultSearchPolicySettings(); | |
| 351 FinalizeIncognitoModeSettings(); | |
| 352 } | |
| 353 | |
| 354 ConfigurationPolicyPrefKeeper::~ConfigurationPolicyPrefKeeper() { | |
| 355 DCHECK(proxy_policies_.empty()); | |
| 356 } | |
| 357 | |
| 358 PrefStore::ReadResult | |
| 359 ConfigurationPolicyPrefKeeper::GetValue(const std::string& key, | |
| 360 const Value** result) const { | |
| 361 const Value* stored_value = NULL; | |
| 362 if (!prefs_.GetValue(key, &stored_value)) | |
| 363 return PrefStore::READ_NO_VALUE; | |
| 364 | |
| 365 // Check whether there's a default value, which indicates READ_USE_DEFAULT | |
| 366 // should be returned. | |
| 367 if (stored_value->IsType(Value::TYPE_NULL)) | |
| 368 return PrefStore::READ_USE_DEFAULT; | |
| 369 | |
| 370 if (result) | |
| 371 *result = stored_value; | |
| 372 return PrefStore::READ_OK; | |
| 373 } | |
| 374 | |
| 375 void ConfigurationPolicyPrefKeeper::GetDifferingPrefPaths( | |
| 376 const ConfigurationPolicyPrefKeeper* other, | |
| 377 std::vector<std::string>* differing_prefs) const { | |
| 378 prefs_.GetDifferingKeys(&other->prefs_, differing_prefs); | |
| 379 } | |
| 380 | |
| 381 void ConfigurationPolicyPrefKeeper::Apply(PolicyMap* policies) { | |
| 382 PolicyMap::const_iterator current = policies->begin(); | |
| 383 for ( ; current != policies->end(); ++current) { | |
| 384 // TODO(simo) Use a separate ConfigurationPolicyHandler class to apply | |
| 385 // policy settings. | |
| 386 Value* value = current->second->DeepCopy(); | |
| 387 if (ApplyProxyPolicy(current->first, value) || | |
| 388 ApplySyncPolicy(current->first, value) || | |
| 389 ApplyAutofillPolicy(current->first, value) || | |
| 390 ApplyDownloadDirPolicy(current->first, value) || | |
| 391 ApplyDiskCacheDirPolicy(current->first, value) || | |
| 392 ApplyFileSelectionDialogsPolicy(current->first, value) || | |
| 393 ApplyDefaultSearchPolicy(current->first, value) || | |
| 394 ApplyIncognitoModePolicy(current->first, value) || | |
| 395 ApplyJavascriptPolicy(current->first, value) || | |
| 396 ApplyPolicyFromMap(current->first, value, | |
| 397 kSimplePolicyMap, arraysize(kSimplePolicyMap))) { | |
| 398 continue; | |
| 399 } | |
| 400 delete value; | |
| 401 | |
| 402 // Other policy implementations should go into the conditional above. | |
| 403 NOTIMPLEMENTED(); | |
| 404 } | 35 } |
| 405 } | 36 } |
| 406 | 37 |
| 407 bool ConfigurationPolicyPrefKeeper::RemovePreferencesOfMap( | |
| 408 const PolicyToPreferenceMapEntry* map, int table_size) { | |
| 409 bool found_any = false; | |
| 410 for (int i = 0; i < table_size; ++i) { | |
| 411 if (prefs_.RemoveValue(map[i].preference_path)) | |
| 412 found_any = true; | |
| 413 } | |
| 414 return found_any; | |
| 415 } | |
| 416 | |
| 417 bool ConfigurationPolicyPrefKeeper::ApplyPolicyFromMap( | |
| 418 ConfigurationPolicyType policy, | |
| 419 Value* value, | |
| 420 const PolicyToPreferenceMapEntry* map, | |
| 421 int size) { | |
| 422 for (int current = 0; current < size; ++current) { | |
| 423 if (map[current].policy_type == policy) { | |
| 424 DCHECK_EQ(map[current].value_type, value->GetType()) | |
| 425 << "mismatch in provided and expected policy value for preferences " | |
| 426 << map[current].preference_path << ". expected = " | |
| 427 << map[current].value_type << ", actual = "<< value->GetType(); | |
| 428 prefs_.SetValue(map[current].preference_path, value); | |
| 429 return true; | |
| 430 } | |
| 431 } | |
| 432 return false; | |
| 433 } | |
| 434 | |
| 435 bool ConfigurationPolicyPrefKeeper::ApplyProxyPolicy( | |
| 436 ConfigurationPolicyType policy, | |
| 437 Value* value) { | |
| 438 // We only collect the values until we have sufficient information when | |
| 439 // FinalizeProxyPolicySettings() is called to determine whether the presented | |
| 440 // values were correct and apply them in that case. | |
| 441 if (ConfigurationPolicyPrefStore::IsProxyPolicy(policy)) { | |
| 442 delete proxy_policies_[policy]; | |
| 443 proxy_policies_[policy] = value; | |
| 444 return true; | |
| 445 } | |
| 446 // We are not interested in this policy. | |
| 447 return false; | |
| 448 } | |
| 449 | |
| 450 bool ConfigurationPolicyPrefKeeper::ApplySyncPolicy( | |
| 451 ConfigurationPolicyType policy, Value* value) { | |
| 452 if (policy == kPolicySyncDisabled) { | |
| 453 bool disable_sync; | |
| 454 if (value->GetAsBoolean(&disable_sync) && disable_sync) | |
| 455 prefs_.SetValue(prefs::kSyncManaged, value); | |
| 456 else | |
| 457 delete value; | |
| 458 return true; | |
| 459 } | |
| 460 return false; | |
| 461 } | |
| 462 | |
| 463 bool ConfigurationPolicyPrefKeeper::ApplyAutofillPolicy( | |
| 464 ConfigurationPolicyType policy, Value* value) { | |
| 465 if (policy == kPolicyAutoFillEnabled) { | |
| 466 bool auto_fill_enabled; | |
| 467 if (value->GetAsBoolean(&auto_fill_enabled) && !auto_fill_enabled) | |
| 468 prefs_.SetValue(prefs::kAutofillEnabled, | |
| 469 Value::CreateBooleanValue(false)); | |
| 470 delete value; | |
| 471 return true; | |
| 472 } | |
| 473 return false; | |
| 474 } | |
| 475 | |
| 476 bool ConfigurationPolicyPrefKeeper::ApplyDownloadDirPolicy( | |
| 477 ConfigurationPolicyType policy, | |
| 478 Value* value) { | |
| 479 // Replace the policy string which might contain some user variables to an | |
| 480 // expanded string. | |
| 481 if (policy == kPolicyDownloadDirectory) { | |
| 482 // This policy is ignored on ChromeOS because the download path there is | |
| 483 // fixed and can not be configured by the user. | |
| 484 #if !defined(OS_CHROMEOS) | |
| 485 FilePath::StringType string_value; | |
| 486 bool result = value->GetAsString(&string_value); | |
| 487 DCHECK(result); | |
| 488 FilePath::StringType expanded_value = | |
| 489 policy::path_parser::ExpandPathVariables(string_value); | |
| 490 // Leaving the policy empty would revert to the default download location | |
| 491 // else we would point in an undefined location. We do this after the | |
| 492 // path expansion because it might lead to an empty string(e.g. for "\"\""). | |
| 493 if (expanded_value.empty()) | |
| 494 expanded_value = download_util::GetDefaultDownloadDirectory().value(); | |
| 495 prefs_.SetValue(prefs::kDownloadDefaultDirectory, | |
| 496 Value::CreateStringValue(expanded_value)); | |
| 497 prefs_.SetValue(prefs::kPromptForDownload, | |
| 498 Value::CreateBooleanValue(false)); | |
| 499 #endif // !defined(OS_CHROMEOS) | |
| 500 delete value; | |
| 501 return true; | |
| 502 } | |
| 503 // We are not interested in this policy. | |
| 504 return false; | |
| 505 } | |
| 506 | |
| 507 bool ConfigurationPolicyPrefKeeper::ApplyDiskCacheDirPolicy( | |
| 508 ConfigurationPolicyType policy, | |
| 509 Value* value) { | |
| 510 // Replace the policy string which might contain some user variables to an | |
| 511 // expanded string. | |
| 512 if (policy == kPolicyDiskCacheDir) { | |
| 513 FilePath::StringType string_value; | |
| 514 bool result = value->GetAsString(&string_value); | |
| 515 DCHECK(result); | |
| 516 FilePath::StringType expanded_value = | |
| 517 policy::path_parser::ExpandPathVariables(string_value); | |
| 518 prefs_.SetValue(prefs::kDiskCacheDir, | |
| 519 Value::CreateStringValue(expanded_value)); | |
| 520 delete value; | |
| 521 return true; | |
| 522 } | |
| 523 // We are not interested in this policy. | |
| 524 return false; | |
| 525 } | |
| 526 | |
| 527 bool ConfigurationPolicyPrefKeeper::ApplyFileSelectionDialogsPolicy( | |
| 528 ConfigurationPolicyType policy, | |
| 529 Value* value) { | |
| 530 if (policy == kPolicyAllowFileSelectionDialogs) { | |
| 531 prefs_.SetValue(prefs::kAllowFileSelectionDialogs, value); | |
| 532 // If file-selection dialogs are not allowed we forbid the user to be | |
| 533 // prompted for the download location, since this would end up in an Infobar | |
| 534 // explaining that file-selection dialogs are forbidden anyways. | |
| 535 bool allow_file_selection_dialogs = true; | |
| 536 bool result = value->GetAsBoolean(&allow_file_selection_dialogs); | |
| 537 DCHECK(result); | |
| 538 if (!allow_file_selection_dialogs) { | |
| 539 prefs_.SetValue(prefs::kPromptForDownload, | |
| 540 Value::CreateBooleanValue(false)); | |
| 541 } | |
| 542 return true; | |
| 543 } | |
| 544 // We are not interested in this policy. | |
| 545 return false; | |
| 546 } | |
| 547 | |
| 548 bool ConfigurationPolicyPrefKeeper::ApplyDefaultSearchPolicy( | |
| 549 ConfigurationPolicyType policy, | |
| 550 Value* value) { | |
| 551 // The DefaultSearchProviderEncodings policy has type list, but the related | |
| 552 // preference has type string. Convert one into the other here, using | |
| 553 // ';' as a separator. | |
| 554 if (policy == kPolicyDefaultSearchProviderEncodings) { | |
| 555 ListValue* list; | |
| 556 if (!value->GetAsList(&list)) { | |
| 557 NOTREACHED() | |
| 558 << "mismatch in provided and expected policy value for preferences " | |
| 559 << prefs::kDefaultSearchProviderEncodings << ". expected = " | |
| 560 << Value::TYPE_LIST << ", actual = "<< value->GetType(); | |
| 561 return false; | |
| 562 } | |
| 563 ListValue::const_iterator iter(list->begin()); | |
| 564 ListValue::const_iterator end(list->end()); | |
| 565 std::string encodings; | |
| 566 for (; iter != end; ++iter) { | |
| 567 std::string s; | |
| 568 if ((*iter)->GetAsString(&s)) { | |
| 569 if (!encodings.empty()) | |
| 570 encodings.push_back(';'); | |
| 571 encodings.append(s); | |
| 572 } else { | |
| 573 NOTREACHED(); | |
| 574 } | |
| 575 } | |
| 576 // We own |value|. | |
| 577 delete value; | |
| 578 prefs_.SetValue(prefs::kDefaultSearchProviderEncodings, | |
| 579 Value::CreateStringValue(encodings)); | |
| 580 return true; | |
| 581 } | |
| 582 | |
| 583 if (ApplyPolicyFromMap(policy, value, kDefaultSearchPolicyMap, | |
| 584 arraysize(kDefaultSearchPolicyMap))) { | |
| 585 return true; | |
| 586 } | |
| 587 return false; | |
| 588 } | |
| 589 | |
| 590 bool ConfigurationPolicyPrefKeeper::ApplyIncognitoModePolicy( | |
| 591 ConfigurationPolicyType policy, | |
| 592 Value* value) { | |
| 593 if (policy == kPolicyIncognitoModeAvailability) { | |
| 594 int availability = IncognitoModePrefs::ENABLED; | |
| 595 bool result = value->GetAsInteger(&availability); | |
| 596 delete value; | |
| 597 if (result) { | |
| 598 IncognitoModePrefs::Availability availability_enum_value; | |
| 599 if (IncognitoModePrefs::IntToAvailability(availability, | |
| 600 &availability_enum_value)) { | |
| 601 prefs_.SetValue(prefs::kIncognitoModeAvailability, | |
| 602 Value::CreateIntegerValue(availability_enum_value)); | |
| 603 } else { | |
| 604 LOG(WARNING) << "IncognitoModeAvailability policy value is " | |
| 605 << "out of range " << availability; | |
| 606 } | |
| 607 } else { | |
| 608 LOG(WARNING) << "IncognitoModeAvailability policy value could not be " | |
| 609 << "parsed"; | |
| 610 } | |
| 611 return true; | |
| 612 } | |
| 613 if (policy == kPolicyIncognitoEnabled) { | |
| 614 deprecated_incognito_enabled_.reset(value); | |
| 615 return true; | |
| 616 } | |
| 617 // The policy is not relevant to incognito. | |
| 618 return false; | |
| 619 } | |
| 620 | |
| 621 bool ConfigurationPolicyPrefKeeper::ApplyJavascriptPolicy( | |
| 622 ConfigurationPolicyType policy, | |
| 623 Value* value) { | |
| 624 if (policy == kPolicyJavascriptEnabled) { | |
| 625 bool enabled; | |
| 626 if (value->GetAsBoolean(&enabled) && !enabled) { | |
| 627 // Force the content setting policy for javascript to BLOCK too. | |
| 628 prefs_.SetValue(prefs::kManagedDefaultJavaScriptSetting, | |
| 629 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK)); | |
| 630 } | |
| 631 prefs_.SetValue(prefs::kWebKitJavascriptEnabled, value); | |
| 632 return true; | |
| 633 } | |
| 634 if (policy == kPolicyDefaultJavaScriptSetting) { | |
| 635 // Ignore this policy if it has been overridden by kPolicyJavascriptEnabled. | |
| 636 Value* existing_value; | |
| 637 if (prefs_.GetValue(prefs::kManagedDefaultJavaScriptSetting, | |
| 638 &existing_value)) { | |
| 639 delete value; | |
| 640 } else { | |
| 641 prefs_.SetValue(prefs::kManagedDefaultJavaScriptSetting, value); | |
| 642 } | |
| 643 return true; | |
| 644 } | |
| 645 return false; | |
| 646 } | |
| 647 | |
| 648 void ConfigurationPolicyPrefKeeper::EnsureStringPrefExists( | |
| 649 const std::string& path) { | |
| 650 std::string value; | |
| 651 if (!prefs_.GetString(path, &value)) | |
| 652 prefs_.SetString(path, value); | |
| 653 } | |
| 654 | |
| 655 namespace { | |
| 656 | |
| 657 // Implementation of SearchTermsData just for validation. | |
| 658 class SearchTermsDataForValidation : public SearchTermsData { | |
| 659 public: | |
| 660 SearchTermsDataForValidation() {} | |
| 661 | |
| 662 // Implementation of SearchTermsData. | |
| 663 virtual std::string GoogleBaseURLValue() const { | |
| 664 return "http://www.google.com/"; | |
| 665 } | |
| 666 virtual std::string GetApplicationLocale() const { | |
| 667 return "en"; | |
| 668 } | |
| 669 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) | |
| 670 virtual string16 GetRlzParameterValue() const { | |
| 671 return string16(); | |
| 672 } | |
| 673 #endif | |
| 674 private: | |
| 675 DISALLOW_COPY_AND_ASSIGN(SearchTermsDataForValidation); | |
| 676 }; | |
| 677 | |
| 678 } // namespace | 38 } // namespace |
| 679 | 39 |
| 680 void ConfigurationPolicyPrefKeeper::FinalizeDefaultSearchPolicySettings() { | |
| 681 bool enabled = true; | |
| 682 if (prefs_.GetBoolean(prefs::kDefaultSearchProviderEnabled, &enabled) && | |
| 683 !enabled) { | |
| 684 // If default search is disabled, we ignore the other fields. | |
| 685 prefs_.SetString(prefs::kDefaultSearchProviderName, std::string()); | |
| 686 prefs_.SetString(prefs::kDefaultSearchProviderSearchURL, std::string()); | |
| 687 prefs_.SetString(prefs::kDefaultSearchProviderSuggestURL, std::string()); | |
| 688 prefs_.SetString(prefs::kDefaultSearchProviderIconURL, std::string()); | |
| 689 prefs_.SetString(prefs::kDefaultSearchProviderEncodings, std::string()); | |
| 690 prefs_.SetString(prefs::kDefaultSearchProviderKeyword, std::string()); | |
| 691 prefs_.SetString(prefs::kDefaultSearchProviderInstantURL, std::string()); | |
| 692 return; | |
| 693 } | |
| 694 std::string search_url; | |
| 695 // The search URL is required. | |
| 696 if (prefs_.GetString(prefs::kDefaultSearchProviderSearchURL, &search_url) && | |
| 697 !search_url.empty()) { | |
| 698 SearchTermsDataForValidation search_terms_data; | |
| 699 const TemplateURLRef search_url_ref(search_url, 0, 0); | |
| 700 // It must support replacement (which implies it is valid). | |
| 701 if (search_url_ref.SupportsReplacementUsingTermsData(search_terms_data)) { | |
| 702 // The other entries are optional. Just make sure that they are all | |
| 703 // specified via policy, so that we don't use regular prefs. | |
| 704 EnsureStringPrefExists(prefs::kDefaultSearchProviderSuggestURL); | |
| 705 EnsureStringPrefExists(prefs::kDefaultSearchProviderIconURL); | |
| 706 EnsureStringPrefExists(prefs::kDefaultSearchProviderEncodings); | |
| 707 EnsureStringPrefExists(prefs::kDefaultSearchProviderKeyword); | |
| 708 EnsureStringPrefExists(prefs::kDefaultSearchProviderInstantURL); | |
| 709 | |
| 710 // For the name, default to the host if not specified. | |
| 711 std::string name; | |
| 712 if (!prefs_.GetString(prefs::kDefaultSearchProviderName, &name) || | |
| 713 name.empty()) | |
| 714 prefs_.SetString(prefs::kDefaultSearchProviderName, | |
| 715 GURL(search_url).host()); | |
| 716 | |
| 717 // And clear the IDs since these are not specified via policy. | |
| 718 prefs_.SetString(prefs::kDefaultSearchProviderID, std::string()); | |
| 719 prefs_.SetString(prefs::kDefaultSearchProviderPrepopulateID, | |
| 720 std::string()); | |
| 721 return; | |
| 722 } | |
| 723 } | |
| 724 // Required entries are not there. Remove any related entries. | |
| 725 RemovePreferencesOfMap(kDefaultSearchPolicyMap, | |
| 726 arraysize(kDefaultSearchPolicyMap)); | |
| 727 } | |
| 728 | |
| 729 void ConfigurationPolicyPrefKeeper::FinalizeIncognitoModeSettings() { | |
| 730 int int_value; | |
| 731 if (!prefs_.GetInteger(prefs::kIncognitoModeAvailability, &int_value)) { | |
| 732 // If kPolicyIncognitoModeAvailability is not specified, check the obsolete | |
| 733 // kPolicyIncognitoEnabled. | |
| 734 if (deprecated_incognito_enabled_.get()) { | |
| 735 bool enabled = true; | |
| 736 if (deprecated_incognito_enabled_->GetAsBoolean(&enabled)) { | |
| 737 prefs_.SetInteger( | |
| 738 prefs::kIncognitoModeAvailability, | |
| 739 enabled ? IncognitoModePrefs::ENABLED : | |
| 740 IncognitoModePrefs::DISABLED); | |
| 741 } else { | |
| 742 LOG(WARNING) << "IncognitoEnabled policy value could not be parsed"; | |
| 743 } | |
| 744 } | |
| 745 } | |
| 746 } | |
| 747 | |
| 748 void ConfigurationPolicyPrefKeeper::FinalizeProxyPolicySettings() { | |
| 749 if (CheckProxySettings()) | |
| 750 ApplyProxySettings(); | |
| 751 | |
| 752 STLDeleteContainerPairSecondPointers(proxy_policies_.begin(), | |
| 753 proxy_policies_.end()); | |
| 754 proxy_policies_.clear(); | |
| 755 } | |
| 756 | |
| 757 bool ConfigurationPolicyPrefKeeper::CheckProxySettings() { | |
| 758 bool mode = HasProxyPolicy(kPolicyProxyMode); | |
| 759 bool server_mode = HasProxyPolicy(kPolicyProxyServerMode); // deprecated | |
| 760 bool server = HasProxyPolicy(kPolicyProxyServer); | |
| 761 bool pac_url = HasProxyPolicy(kPolicyProxyPacUrl); | |
| 762 bool bypass_list = HasProxyPolicy(kPolicyProxyBypassList); | |
| 763 | |
| 764 if ((server || pac_url || bypass_list) && !(mode || server_mode)) { | |
| 765 LOG(WARNING) << "A centrally-administered policy defines proxy setting" | |
| 766 << " details without setting a proxy mode."; | |
| 767 return false; | |
| 768 } | |
| 769 | |
| 770 // If there's a server mode, convert it into a mode. | |
| 771 std::string mode_value; | |
| 772 if (mode) { | |
| 773 if (server_mode) | |
| 774 LOG(WARNING) << "Both ProxyMode and ProxyServerMode policies defined, " | |
| 775 << "ignoring ProxyMode."; | |
| 776 if (!proxy_policies_[kPolicyProxyMode]->GetAsString(&mode_value)) { | |
| 777 LOG(WARNING) << "Invalid ProxyMode value."; | |
| 778 return false; | |
| 779 } | |
| 780 } else if (server_mode) { | |
| 781 int server_mode_value; | |
| 782 if (!proxy_policies_[kPolicyProxyServerMode]->GetAsInteger( | |
| 783 &server_mode_value)) { | |
| 784 LOG(WARNING) << "Invalid ProxyServerMode value."; | |
| 785 return false; | |
| 786 } | |
| 787 | |
| 788 switch (server_mode_value) { | |
| 789 case kPolicyNoProxyServerMode: | |
| 790 mode_value = ProxyPrefs::kDirectProxyModeName; | |
| 791 break; | |
| 792 case kPolicyAutoDetectProxyServerMode: | |
| 793 mode_value = ProxyPrefs::kAutoDetectProxyModeName; | |
| 794 break; | |
| 795 case kPolicyManuallyConfiguredProxyServerMode: | |
| 796 if (server && pac_url) { | |
| 797 LOG(WARNING) << "A centrally-administered policy dictates that" | |
| 798 << " both fixed proxy servers and a .pac url. should" | |
| 799 << " be used for proxy configuration."; | |
| 800 return false; | |
| 801 } | |
| 802 if (!server && !pac_url) { | |
| 803 LOG(WARNING) << "A centrally-administered policy dictates that the" | |
| 804 << " proxy settings should use either fixed proxy" | |
| 805 << " servers or a .pac url, but specifies neither."; | |
| 806 return false; | |
| 807 } | |
| 808 if (pac_url) | |
| 809 mode_value = ProxyPrefs::kPacScriptProxyModeName; | |
| 810 else | |
| 811 mode_value = ProxyPrefs::kFixedServersProxyModeName; | |
| 812 break; | |
| 813 case kPolicyUseSystemProxyServerMode: | |
| 814 mode_value = ProxyPrefs::kSystemProxyModeName; | |
| 815 break; | |
| 816 default: | |
| 817 LOG(WARNING) << "Invalid proxy mode " << server_mode_value; | |
| 818 return false; | |
| 819 } | |
| 820 } | |
| 821 | |
| 822 // If neither ProxyMode nor ProxyServerMode are specified, mode_value will be | |
| 823 // empty and the proxy shouldn't be configured at all. | |
| 824 if (mode_value.empty()) | |
| 825 return true; | |
| 826 | |
| 827 if (mode_value == ProxyPrefs::kDirectProxyModeName) { | |
| 828 if (server || pac_url || bypass_list) { | |
| 829 LOG(WARNING) << "A centrally-administered policy disables the use of" | |
| 830 << " a proxy but also specifies an explicit proxy" | |
| 831 << " configuration."; | |
| 832 return false; | |
| 833 } | |
| 834 } else if (mode_value == ProxyPrefs::kAutoDetectProxyModeName) { | |
| 835 if (server || bypass_list || pac_url) { | |
| 836 LOG(WARNING) << "A centrally-administered policy dictates that a proxy" | |
| 837 << " shall be auto configured but specifies fixed proxy" | |
| 838 << " servers, a by-pass list or a .pac script URL."; | |
| 839 return false; | |
| 840 } | |
| 841 } else if (mode_value == ProxyPrefs::kPacScriptProxyModeName) { | |
| 842 if (server || bypass_list) { | |
| 843 LOG(WARNING) << "A centrally-administered policy dictates that a .pac" | |
| 844 << " script URL should be used for proxy configuration but" | |
| 845 << " also specifies policies required only for fixed" | |
| 846 << " proxy servers."; | |
| 847 return false; | |
| 848 } | |
| 849 } else if (mode_value == ProxyPrefs::kFixedServersProxyModeName) { | |
| 850 if (pac_url) { | |
| 851 LOG(WARNING) << "A centrally-administered policy dictates that" | |
| 852 << " fixed proxy servers should be used but also" | |
| 853 << " specifies a .pac script URL."; | |
| 854 return false; | |
| 855 } | |
| 856 } else if (mode_value == ProxyPrefs::kSystemProxyModeName) { | |
| 857 if (server || pac_url || bypass_list) { | |
| 858 LOG(WARNING) << "A centrally-administered policy dictates that the" | |
| 859 << " system proxy settings should be used but also " | |
| 860 << " specifies an explicit proxy configuration."; | |
| 861 return false; | |
| 862 } | |
| 863 } else { | |
| 864 LOG(WARNING) << "Invalid proxy mode " << mode_value; | |
| 865 return false; | |
| 866 } | |
| 867 return true; | |
| 868 } | |
| 869 | |
| 870 void ConfigurationPolicyPrefKeeper::ApplyProxySettings() { | |
| 871 ProxyPrefs::ProxyMode mode; | |
| 872 if (HasProxyPolicy(kPolicyProxyMode)) { | |
| 873 std::string string_mode; | |
| 874 CHECK(proxy_policies_[kPolicyProxyMode]->GetAsString(&string_mode)); | |
| 875 if (!ProxyPrefs::StringToProxyMode(string_mode, &mode)) { | |
| 876 LOG(WARNING) << "A centrally-administered policy specifies a value for " | |
| 877 << "the ProxyMode policy that isn't recognized."; | |
| 878 return; | |
| 879 } | |
| 880 } else if (HasProxyPolicy(kPolicyProxyServerMode)) { | |
| 881 int int_mode = 0; | |
| 882 CHECK(proxy_policies_[kPolicyProxyServerMode]->GetAsInteger(&int_mode)); | |
| 883 switch (int_mode) { | |
| 884 case kPolicyNoProxyServerMode: | |
| 885 mode = ProxyPrefs::MODE_DIRECT; | |
| 886 break; | |
| 887 case kPolicyAutoDetectProxyServerMode: | |
| 888 mode = ProxyPrefs::MODE_AUTO_DETECT; | |
| 889 break; | |
| 890 case kPolicyManuallyConfiguredProxyServerMode: | |
| 891 mode = ProxyPrefs::MODE_FIXED_SERVERS; | |
| 892 if (HasProxyPolicy(kPolicyProxyPacUrl)) | |
| 893 mode = ProxyPrefs::MODE_PAC_SCRIPT; | |
| 894 break; | |
| 895 case kPolicyUseSystemProxyServerMode: | |
| 896 mode = ProxyPrefs::MODE_SYSTEM; | |
| 897 break; | |
| 898 default: | |
| 899 mode = ProxyPrefs::MODE_DIRECT; | |
| 900 NOTREACHED(); | |
| 901 } | |
| 902 } else { | |
| 903 return; | |
| 904 } | |
| 905 switch (mode) { | |
| 906 case ProxyPrefs::MODE_DIRECT: | |
| 907 prefs_.SetValue(prefs::kProxy, ProxyConfigDictionary::CreateDirect()); | |
| 908 break; | |
| 909 case ProxyPrefs::MODE_AUTO_DETECT: | |
| 910 prefs_.SetValue(prefs::kProxy, ProxyConfigDictionary::CreateAutoDetect()); | |
| 911 break; | |
| 912 case ProxyPrefs::MODE_PAC_SCRIPT: { | |
| 913 if (!HasProxyPolicy(kPolicyProxyPacUrl)) { | |
| 914 LOG(WARNING) << "A centrally-administered policy specifies to use a " | |
| 915 << "PAC script, but doesn't supply the PAC script URL."; | |
| 916 return; | |
| 917 } | |
| 918 std::string pac_url; | |
| 919 proxy_policies_[kPolicyProxyPacUrl]->GetAsString(&pac_url); | |
| 920 prefs_.SetValue(prefs::kProxy, | |
| 921 ProxyConfigDictionary::CreatePacScript(pac_url, false)); | |
| 922 break; | |
| 923 } | |
| 924 case ProxyPrefs::MODE_FIXED_SERVERS: { | |
| 925 if (!HasProxyPolicy(kPolicyProxyServer)) { | |
| 926 LOG(WARNING) << "A centrally-administered policy specifies to use a " | |
| 927 << "fixed server, but doesn't supply the server address."; | |
| 928 return; | |
| 929 } | |
| 930 std::string proxy_server; | |
| 931 proxy_policies_[kPolicyProxyServer]->GetAsString(&proxy_server); | |
| 932 std::string bypass_list; | |
| 933 if (HasProxyPolicy(kPolicyProxyBypassList)) | |
| 934 proxy_policies_[kPolicyProxyBypassList]->GetAsString(&bypass_list); | |
| 935 prefs_.SetValue(prefs::kProxy, | |
| 936 ProxyConfigDictionary::CreateFixedServers(proxy_server, | |
| 937 bypass_list)); | |
| 938 break; | |
| 939 } | |
| 940 case ProxyPrefs::MODE_SYSTEM: | |
| 941 prefs_.SetValue(prefs::kProxy, | |
| 942 ProxyConfigDictionary::CreateSystem()); | |
| 943 break; | |
| 944 case ProxyPrefs::kModeCount: | |
| 945 NOTREACHED(); | |
| 946 } | |
| 947 } | |
| 948 | |
| 949 bool ConfigurationPolicyPrefKeeper::HasProxyPolicy( | |
| 950 ConfigurationPolicyType policy) const { | |
| 951 std::map<ConfigurationPolicyType, Value*>::const_iterator iter; | |
| 952 iter = proxy_policies_.find(policy); | |
| 953 std::string tmp; | |
| 954 if (iter == proxy_policies_.end() || | |
| 955 !iter->second || | |
| 956 iter->second->IsType(Value::TYPE_NULL) || | |
| 957 (iter->second->IsType(Value::TYPE_STRING) && | |
| 958 iter->second->GetAsString(&tmp) && | |
| 959 tmp.empty())) { | |
| 960 return false; | |
| 961 } | |
| 962 return true; | |
| 963 } | |
| 964 | |
| 965 ConfigurationPolicyPrefStore::ConfigurationPolicyPrefStore( | 40 ConfigurationPolicyPrefStore::ConfigurationPolicyPrefStore( |
| 966 ConfigurationPolicyProvider* provider) | 41 ConfigurationPolicyProvider* provider) |
| 967 : provider_(provider), | 42 : provider_(provider), |
| 968 initialization_complete_(false) { | 43 initialization_complete_(false) { |
| 969 if (provider_) { | 44 if (provider_) { |
| 970 // Read initial policy. | 45 // Read initial policy. |
| 971 policy_keeper_.reset(new ConfigurationPolicyPrefKeeper(provider_)); | 46 prefs_.reset(CreatePreferencesFromPolicies()); |
| 972 registrar_.Init(provider_, this); | 47 registrar_.Init(provider_, this); |
| 973 initialization_complete_ = provider_->IsInitializationComplete(); | 48 initialization_complete_ = provider_->IsInitializationComplete(); |
| 974 } else { | 49 } else { |
| 975 initialization_complete_ = true; | 50 initialization_complete_ = true; |
| 976 } | 51 } |
| 977 } | 52 } |
| 978 | 53 |
| 979 ConfigurationPolicyPrefStore::~ConfigurationPolicyPrefStore() { | 54 ConfigurationPolicyPrefStore::~ConfigurationPolicyPrefStore() { |
| 980 } | 55 } |
| 981 | 56 |
| 982 void ConfigurationPolicyPrefStore::AddObserver(PrefStore::Observer* observer) { | 57 void ConfigurationPolicyPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 983 observers_.AddObserver(observer); | 58 observers_.AddObserver(observer); |
| 984 } | 59 } |
| 985 | 60 |
| 986 void ConfigurationPolicyPrefStore::RemoveObserver( | 61 void ConfigurationPolicyPrefStore::RemoveObserver( |
| 987 PrefStore::Observer* observer) { | 62 PrefStore::Observer* observer) { |
| 988 observers_.RemoveObserver(observer); | 63 observers_.RemoveObserver(observer); |
| 989 } | 64 } |
| 990 | 65 |
| 991 bool ConfigurationPolicyPrefStore::IsInitializationComplete() const { | 66 bool ConfigurationPolicyPrefStore::IsInitializationComplete() const { |
| 992 return initialization_complete_; | 67 return initialization_complete_; |
| 993 } | 68 } |
| 994 | 69 |
| 995 PrefStore::ReadResult | 70 PrefStore::ReadResult |
| 996 ConfigurationPolicyPrefStore::GetValue(const std::string& key, | 71 ConfigurationPolicyPrefStore::GetValue(const std::string& key, |
| 997 const Value** value) const { | 72 const Value** value) const { |
| 998 if (policy_keeper_.get()) | 73 const Value* stored_value = NULL; |
| 999 return policy_keeper_->GetValue(key, value); | 74 if (!prefs_.get() || !prefs_->GetValue(key, &stored_value)) |
| 75 return PrefStore::READ_NO_VALUE; |
| 1000 | 76 |
| 1001 return PrefStore::READ_NO_VALUE; | 77 if (value) |
| 78 *value = stored_value; |
| 79 return PrefStore::READ_OK; |
| 1002 } | 80 } |
| 1003 | 81 |
| 1004 void ConfigurationPolicyPrefStore::OnUpdatePolicy() { | 82 void ConfigurationPolicyPrefStore::OnUpdatePolicy() { |
| 1005 Refresh(); | 83 Refresh(); |
| 1006 } | 84 } |
| 1007 | 85 |
| 1008 void ConfigurationPolicyPrefStore::OnProviderGoingAway() { | 86 void ConfigurationPolicyPrefStore::OnProviderGoingAway() { |
| 1009 provider_ = NULL; | 87 provider_ = NULL; |
| 1010 } | 88 } |
| 1011 | 89 |
| 1012 // static | 90 // static |
| 1013 ConfigurationPolicyPrefStore* | 91 ConfigurationPolicyPrefStore* |
| 1014 ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore() { | 92 ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore() { |
| 1015 BrowserPolicyConnector* connector = | |
| 1016 g_browser_process->browser_policy_connector(); | |
| 1017 return new ConfigurationPolicyPrefStore( | 93 return new ConfigurationPolicyPrefStore( |
| 1018 connector->GetManagedPlatformProvider()); | 94 g_browser_process->browser_policy_connector()-> |
| 95 GetManagedPlatformProvider()); |
| 1019 } | 96 } |
| 1020 | 97 |
| 1021 // static | 98 // static |
| 1022 ConfigurationPolicyPrefStore* | 99 ConfigurationPolicyPrefStore* |
| 1023 ConfigurationPolicyPrefStore::CreateManagedCloudPolicyPrefStore() { | 100 ConfigurationPolicyPrefStore::CreateManagedCloudPolicyPrefStore() { |
| 1024 return new ConfigurationPolicyPrefStore( | 101 return new ConfigurationPolicyPrefStore( |
| 1025 g_browser_process->browser_policy_connector()->GetManagedCloudProvider()); | 102 g_browser_process->browser_policy_connector()->GetManagedCloudProvider()); |
| 1026 } | 103 } |
| 1027 | 104 |
| 1028 // static | 105 // static |
| 1029 ConfigurationPolicyPrefStore* | 106 ConfigurationPolicyPrefStore* |
| 1030 ConfigurationPolicyPrefStore::CreateRecommendedPlatformPolicyPrefStore() { | 107 ConfigurationPolicyPrefStore::CreateRecommendedPlatformPolicyPrefStore() { |
| 1031 BrowserPolicyConnector* connector = | |
| 1032 g_browser_process->browser_policy_connector(); | |
| 1033 return new ConfigurationPolicyPrefStore( | 108 return new ConfigurationPolicyPrefStore( |
| 1034 connector->GetRecommendedPlatformProvider()); | 109 g_browser_process->browser_policy_connector()-> |
| 110 GetRecommendedPlatformProvider()); |
| 1035 } | 111 } |
| 1036 | 112 |
| 1037 // static | 113 // static |
| 1038 ConfigurationPolicyPrefStore* | 114 ConfigurationPolicyPrefStore* |
| 1039 ConfigurationPolicyPrefStore::CreateRecommendedCloudPolicyPrefStore() { | 115 ConfigurationPolicyPrefStore::CreateRecommendedCloudPolicyPrefStore() { |
| 1040 return new ConfigurationPolicyPrefStore( | 116 return new ConfigurationPolicyPrefStore( |
| 1041 g_browser_process->browser_policy_connector()-> | 117 g_browser_process->browser_policy_connector()-> |
| 1042 GetRecommendedCloudProvider()); | 118 GetRecommendedCloudProvider()); |
| 1043 } | 119 } |
| 1044 | 120 |
| 1045 bool | 121 bool |
| 1046 ConfigurationPolicyPrefStore::IsProxyPolicy(ConfigurationPolicyType policy) { | 122 ConfigurationPolicyPrefStore::IsProxyPolicy(ConfigurationPolicyType policy) { |
| 1047 return policy == kPolicyProxyMode || | 123 return policy == kPolicyProxyMode || |
| 1048 policy == kPolicyProxyServerMode || | 124 policy == kPolicyProxyServerMode || |
| 1049 policy == kPolicyProxyServer || | 125 policy == kPolicyProxyServer || |
| 1050 policy == kPolicyProxyPacUrl || | 126 policy == kPolicyProxyPacUrl || |
| 1051 policy == kPolicyProxyBypassList; | 127 policy == kPolicyProxyBypassList; |
| 1052 } | 128 } |
| 1053 | 129 |
| 1054 void ConfigurationPolicyPrefStore::Refresh() { | 130 void ConfigurationPolicyPrefStore::Refresh() { |
| 1055 if (!provider_) | 131 if (!provider_) |
| 1056 return; | 132 return; |
| 1057 | 133 scoped_ptr<PrefValueMap> new_prefs(CreatePreferencesFromPolicies()); |
| 1058 // Construct a new keeper, determine what changed and swap the keeper in. | |
| 1059 scoped_ptr<ConfigurationPolicyPrefKeeper> new_keeper( | |
| 1060 new ConfigurationPolicyPrefKeeper(provider_)); | |
| 1061 std::vector<std::string> changed_prefs; | 134 std::vector<std::string> changed_prefs; |
| 1062 new_keeper->GetDifferingPrefPaths(policy_keeper_.get(), &changed_prefs); | 135 new_prefs->GetDifferingKeys(prefs_.get(), &changed_prefs); |
| 1063 policy_keeper_.swap(new_keeper); | 136 prefs_.swap(new_prefs); |
| 1064 | 137 |
| 1065 // Send out change notifications. | 138 // Send out change notifications. |
| 1066 for (std::vector<std::string>::const_iterator pref(changed_prefs.begin()); | 139 for (std::vector<std::string>::const_iterator pref(changed_prefs.begin()); |
| 1067 pref != changed_prefs.end(); | 140 pref != changed_prefs.end(); |
| 1068 ++pref) { | 141 ++pref) { |
| 1069 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, | 142 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, |
| 1070 OnPrefValueChanged(*pref)); | 143 OnPrefValueChanged(*pref)); |
| 1071 } | 144 } |
| 1072 | 145 |
| 1073 // Update the initialization flag. | 146 // Update the initialization flag. |
| 1074 if (!initialization_complete_ && | 147 if (!initialization_complete_ && |
| 1075 provider_->IsInitializationComplete()) { | 148 provider_->IsInitializationComplete()) { |
| 1076 initialization_complete_ = true; | 149 initialization_complete_ = true; |
| 1077 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, | 150 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, |
| 1078 OnInitializationCompleted(true)); | 151 OnInitializationCompleted(true)); |
| 1079 } | 152 } |
| 1080 } | 153 } |
| 1081 | 154 |
| 155 PrefValueMap* ConfigurationPolicyPrefStore::CreatePreferencesFromPolicies() { |
| 156 scoped_ptr<PrefValueMap> prefs(new PrefValueMap); |
| 157 |
| 158 PolicyMap policies; |
| 159 if (!provider_->Provide(&policies)) |
| 160 DLOG(WARNING) << "Failed to get policy from provider."; |
| 161 |
| 162 const ConfigurationPolicyHandler::HandlerList* handlers = |
| 163 g_browser_process->browser_policy_connector()-> |
| 164 GetConfigurationPolicyHandlerList(); |
| 165 |
| 166 scoped_ptr<PolicyErrorMap> errors(new PolicyErrorMap); |
| 167 ConfigurationPolicyHandler::HandlerList::const_iterator handler; |
| 168 for (handler = handlers->begin(); handler != handlers->end(); ++handler) { |
| 169 if ((*handler)->CheckPolicySettings(&policies, errors.get())) |
| 170 (*handler)->ApplyPolicySettings(&policies, prefs.get()); |
| 171 } |
| 172 |
| 173 // Retrieve and log the errors once the UI loop is ready. This is only an |
| 174 // issue during startup. |
| 175 BrowserThread::PostTask(BrowserThread::UI, |
| 176 FROM_HERE, |
| 177 base::Bind(&LogErrors, |
| 178 base::Owned(errors.release()))); |
| 179 |
| 180 return prefs.release(); |
| 181 } |
| 182 |
| 1082 } // namespace policy | 183 } // namespace policy |
| OLD | NEW |