Chromium Code Reviews| 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 "chrome/browser/policy/configuration_policy_pref_store.h" | 5 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/prefs/pref_value_map.h" | 13 #include "base/prefs/pref_value_map.h" |
| 13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/browser/browser_process.h" | |
| 16 #include "chrome/browser/policy/browser_policy_connector.h" | |
| 17 #include "chrome/browser/policy/configuration_policy_handler_list.h" | 16 #include "chrome/browser/policy/configuration_policy_handler_list.h" |
| 18 #include "chrome/browser/policy/policy_error_map.h" | 17 #include "chrome/browser/policy/policy_error_map.h" |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "policy/policy_constants.h" | 18 #include "policy/policy_constants.h" |
| 21 | 19 |
| 22 using content::BrowserThread; | |
| 23 | |
| 24 namespace policy { | 20 namespace policy { |
| 25 | 21 |
| 26 namespace { | 22 namespace { |
| 27 | 23 |
| 28 // Policies are loaded early on startup, before PolicyErrorMaps are ready to | 24 // Policies are loaded early on startup, before PolicyErrorMaps are ready to |
| 29 // be retrieved. This function is posted to UI to log any errors found on | 25 // be retrieved. This function is posted to UI to log any errors found on |
| 30 // Refresh below. | 26 // Refresh below. |
| 31 void LogErrors(PolicyErrorMap* errors) { | 27 void LogErrors(PolicyErrorMap* errors) { |
| 32 PolicyErrorMap::const_iterator iter; | 28 PolicyErrorMap::const_iterator iter; |
| 33 for (iter = errors->begin(); iter != errors->end(); ++iter) { | 29 for (iter = errors->begin(); iter != errors->end(); ++iter) { |
| 34 string16 policy = ASCIIToUTF16(iter->first); | 30 string16 policy = ASCIIToUTF16(iter->first); |
| 35 DLOG(WARNING) << "Policy " << policy << ": " << iter->second; | 31 DLOG(WARNING) << "Policy " << policy << ": " << iter->second; |
| 36 } | 32 } |
| 37 } | 33 } |
| 38 | 34 |
| 39 } // namespace | 35 } // namespace |
| 40 | 36 |
| 41 ConfigurationPolicyPrefStore::ConfigurationPolicyPrefStore( | 37 ConfigurationPolicyPrefStore::ConfigurationPolicyPrefStore( |
| 42 PolicyService* service, | 38 PolicyService* service, |
| 39 const ConfigurationPolicyHandlerList* handler_list, | |
| 43 PolicyLevel level) | 40 PolicyLevel level) |
| 44 : policy_service_(service), | 41 : policy_service_(service), |
| 42 handler_list_(handler_list), | |
| 45 level_(level) { | 43 level_(level) { |
| 46 // Read initial policy. | 44 // Read initial policy. |
| 47 prefs_.reset(CreatePreferencesFromPolicies()); | 45 prefs_.reset(CreatePreferencesFromPolicies()); |
| 48 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this); | 46 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this); |
| 49 } | 47 } |
| 50 | 48 |
| 51 void ConfigurationPolicyPrefStore::AddObserver(PrefStore::Observer* observer) { | 49 void ConfigurationPolicyPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 52 observers_.AddObserver(observer); | 50 observers_.AddObserver(observer); |
| 53 } | 51 } |
| 54 | 52 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 } | 84 } |
| 87 | 85 |
| 88 void ConfigurationPolicyPrefStore::OnPolicyServiceInitialized( | 86 void ConfigurationPolicyPrefStore::OnPolicyServiceInitialized( |
| 89 PolicyDomain domain) { | 87 PolicyDomain domain) { |
| 90 if (domain == POLICY_DOMAIN_CHROME) { | 88 if (domain == POLICY_DOMAIN_CHROME) { |
| 91 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, | 89 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, |
| 92 OnInitializationCompleted(true)); | 90 OnInitializationCompleted(true)); |
| 93 } | 91 } |
| 94 } | 92 } |
| 95 | 93 |
| 96 // static | |
| 97 ConfigurationPolicyPrefStore* | |
| 98 ConfigurationPolicyPrefStore::CreateMandatoryPolicyPrefStore( | |
| 99 PolicyService* policy_service) { | |
| 100 return new ConfigurationPolicyPrefStore(policy_service, | |
| 101 POLICY_LEVEL_MANDATORY); | |
| 102 } | |
| 103 | |
| 104 // static | |
| 105 ConfigurationPolicyPrefStore* | |
| 106 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore( | |
| 107 PolicyService* policy_service) { | |
| 108 return new ConfigurationPolicyPrefStore(policy_service, | |
| 109 POLICY_LEVEL_RECOMMENDED); | |
| 110 } | |
| 111 | |
| 112 ConfigurationPolicyPrefStore::~ConfigurationPolicyPrefStore() { | 94 ConfigurationPolicyPrefStore::~ConfigurationPolicyPrefStore() { |
| 113 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); | 95 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); |
| 114 } | 96 } |
| 115 | 97 |
| 116 void ConfigurationPolicyPrefStore::Refresh() { | 98 void ConfigurationPolicyPrefStore::Refresh() { |
| 117 scoped_ptr<PrefValueMap> new_prefs(CreatePreferencesFromPolicies()); | 99 scoped_ptr<PrefValueMap> new_prefs(CreatePreferencesFromPolicies()); |
| 118 std::vector<std::string> changed_prefs; | 100 std::vector<std::string> changed_prefs; |
| 119 new_prefs->GetDifferingKeys(prefs_.get(), &changed_prefs); | 101 new_prefs->GetDifferingKeys(prefs_.get(), &changed_prefs); |
| 120 prefs_.swap(new_prefs); | 102 prefs_.swap(new_prefs); |
| 121 | 103 |
| 122 // Send out change notifications. | 104 // Send out change notifications. |
| 123 for (std::vector<std::string>::const_iterator pref(changed_prefs.begin()); | 105 for (std::vector<std::string>::const_iterator pref(changed_prefs.begin()); |
| 124 pref != changed_prefs.end(); | 106 pref != changed_prefs.end(); |
| 125 ++pref) { | 107 ++pref) { |
| 126 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, | 108 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, |
| 127 OnPrefValueChanged(*pref)); | 109 OnPrefValueChanged(*pref)); |
| 128 } | 110 } |
| 129 } | 111 } |
| 130 | 112 |
| 131 PrefValueMap* ConfigurationPolicyPrefStore::CreatePreferencesFromPolicies() { | 113 PrefValueMap* ConfigurationPolicyPrefStore::CreatePreferencesFromPolicies() { |
| 132 scoped_ptr<PrefValueMap> prefs(new PrefValueMap); | 114 scoped_ptr<PrefValueMap> prefs(new PrefValueMap); |
| 133 PolicyMap filtered_policies; | 115 PolicyMap filtered_policies; |
| 134 filtered_policies.CopyFrom(policy_service_->GetPolicies( | 116 filtered_policies.CopyFrom(policy_service_->GetPolicies( |
| 135 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))); | 117 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))); |
| 136 filtered_policies.FilterLevel(level_); | 118 filtered_policies.FilterLevel(level_); |
| 137 | 119 |
| 138 scoped_ptr<PolicyErrorMap> errors(new PolicyErrorMap); | 120 scoped_ptr<PolicyErrorMap> errors(new PolicyErrorMap); |
| 139 | 121 |
| 140 const ConfigurationPolicyHandlerList* handler_list = | 122 handler_list_->ApplyPolicySettings(filtered_policies, |
| 141 g_browser_process->browser_policy_connector()->GetHandlerList(); | 123 prefs.get(), |
| 142 handler_list->ApplyPolicySettings(filtered_policies, | 124 errors.get()); |
| 143 prefs.get(), | |
| 144 errors.get()); | |
| 145 | 125 |
| 146 // Retrieve and log the errors once the UI loop is ready. This is only an | 126 base::MessageLoop::current()->PostTask( |
|
bartfab (slow)
2013/08/12 13:27:53
Nit: Retain the comment explaining why a post is n
Joao da Silva
2013/08/12 15:41:55
Done.
| |
| 147 // issue during startup. | 127 FROM_HERE, base::Bind(&LogErrors, base::Owned(errors.release()))); |
| 148 BrowserThread::PostTask(BrowserThread::UI, | |
| 149 FROM_HERE, | |
| 150 base::Bind(&LogErrors, | |
| 151 base::Owned(errors.release()))); | |
| 152 | 128 |
| 153 return prefs.release(); | 129 return prefs.release(); |
| 154 } | 130 } |
| 155 | 131 |
| 156 } // namespace policy | 132 } // namespace policy |
| OLD | NEW |