| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/browser/browser_policy_connector.h" | 5 #include "components/policy/core/browser/browser_policy_connector.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "components/policy/core/common/policy_switches.h" | 24 #include "components/policy/core/common/policy_switches.h" |
| 25 #include "google_apis/gaia/gaia_auth_util.h" | 25 #include "google_apis/gaia/gaia_auth_util.h" |
| 26 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
| 27 #include "policy/policy_constants.h" | 27 #include "policy/policy_constants.h" |
| 28 #include "third_party/icu/source/i18n/unicode/regex.h" | 28 #include "third_party/icu/source/i18n/unicode/regex.h" |
| 29 | 29 |
| 30 namespace policy { | 30 namespace policy { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // The URL for the device management server. | 34 // The URL for the enterprise device management server. |
| 35 const char kDefaultDeviceManagementServerUrl[] = | 35 const char kDefaultEnterpriseDeviceManagementServerUrl[] = |
| 36 "https://m.google.com/devicemanagement/data/api"; |
| 37 |
| 38 // TODO(davidyu): use the right URL once the server is ready. |
| 39 // The URL for the consumer device management server. |
| 40 const char kDefaultConsumerDeviceManagementServerUrl[] = |
| 36 "https://m.google.com/devicemanagement/data/api"; | 41 "https://m.google.com/devicemanagement/data/api"; |
| 37 | 42 |
| 38 // Used in BrowserPolicyConnector::SetPolicyProviderForTesting. | 43 // Used in BrowserPolicyConnector::SetPolicyProviderForTesting. |
| 39 bool g_created_policy_service = false; | 44 bool g_created_policy_service = false; |
| 40 ConfigurationPolicyProvider* g_testing_provider = NULL; | 45 ConfigurationPolicyProvider* g_testing_provider = NULL; |
| 41 | 46 |
| 42 // Returns true if |domain| matches the regex |pattern|. | 47 // Returns true if |domain| matches the regex |pattern|. |
| 43 bool MatchDomain(const base::string16& domain, const base::string16& pattern) { | 48 bool MatchDomain(const base::string16& domain, const base::string16& pattern) { |
| 44 UErrorCode status = U_ZERO_ERROR; | 49 UErrorCode status = U_ZERO_ERROR; |
| 45 const icu::UnicodeString icu_pattern(pattern.data(), pattern.length()); | 50 const icu::UnicodeString icu_pattern(pattern.data(), pattern.length()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // This usually means it's an early shutdown and | 83 // This usually means it's an early shutdown and |
| 79 // BrowserProcessImpl::StartTearDown() wasn't invoked. | 84 // BrowserProcessImpl::StartTearDown() wasn't invoked. |
| 80 // Cleanup properly in those cases and avoid crashing the ToastCrasher test. | 85 // Cleanup properly in those cases and avoid crashing the ToastCrasher test. |
| 81 Shutdown(); | 86 Shutdown(); |
| 82 } | 87 } |
| 83 } | 88 } |
| 84 | 89 |
| 85 void BrowserPolicyConnector::Init( | 90 void BrowserPolicyConnector::Init( |
| 86 PrefService* local_state, | 91 PrefService* local_state, |
| 87 scoped_refptr<net::URLRequestContextGetter> request_context, | 92 scoped_refptr<net::URLRequestContextGetter> request_context, |
| 88 scoped_ptr<DeviceManagementService> device_management_service) { | 93 scoped_ptr<DeviceManagementService> enterprise_device_management_service, |
| 94 scoped_ptr<DeviceManagementService> consumer_device_management_service) { |
| 89 DCHECK(!is_initialized()); | 95 DCHECK(!is_initialized()); |
| 90 | 96 |
| 91 device_management_service_ = device_management_service.Pass(); | 97 enterprise_device_management_service_ = |
| 98 enterprise_device_management_service.Pass(); |
| 99 consumer_device_management_service_ = |
| 100 consumer_device_management_service.Pass(); |
| 92 | 101 |
| 93 if (g_testing_provider) | 102 if (g_testing_provider) |
| 94 g_testing_provider->Init(GetSchemaRegistry()); | 103 g_testing_provider->Init(GetSchemaRegistry()); |
| 95 for (size_t i = 0; i < policy_providers_.size(); ++i) | 104 for (size_t i = 0; i < policy_providers_.size(); ++i) |
| 96 policy_providers_[i]->Init(GetSchemaRegistry()); | 105 policy_providers_[i]->Init(GetSchemaRegistry()); |
| 97 | 106 |
| 98 policy_statistics_collector_.reset( | 107 policy_statistics_collector_.reset( |
| 99 new policy::PolicyStatisticsCollector( | 108 new policy::PolicyStatisticsCollector( |
| 100 base::Bind(&GetChromePolicyDetails), | 109 base::Bind(&GetChromePolicyDetails), |
| 101 GetChromeSchema(), | 110 GetChromeSchema(), |
| 102 GetPolicyService(), | 111 GetPolicyService(), |
| 103 local_state, | 112 local_state, |
| 104 base::MessageLoop::current()->message_loop_proxy())); | 113 base::MessageLoop::current()->message_loop_proxy())); |
| 105 policy_statistics_collector_->Initialize(); | 114 policy_statistics_collector_->Initialize(); |
| 106 | 115 |
| 107 is_initialized_ = true; | 116 is_initialized_ = true; |
| 108 } | 117 } |
| 109 | 118 |
| 110 void BrowserPolicyConnector::Shutdown() { | 119 void BrowserPolicyConnector::Shutdown() { |
| 111 is_initialized_ = false; | 120 is_initialized_ = false; |
| 112 if (g_testing_provider) | 121 if (g_testing_provider) |
| 113 g_testing_provider->Shutdown(); | 122 g_testing_provider->Shutdown(); |
| 114 for (size_t i = 0; i < policy_providers_.size(); ++i) | 123 for (size_t i = 0; i < policy_providers_.size(); ++i) |
| 115 policy_providers_[i]->Shutdown(); | 124 policy_providers_[i]->Shutdown(); |
| 116 // Drop g_testing_provider so that tests executed with --single_process can | 125 // Drop g_testing_provider so that tests executed with --single_process can |
| 117 // call SetPolicyProviderForTesting() again. It is still owned by the test. | 126 // call SetPolicyProviderForTesting() again. It is still owned by the test. |
| 118 g_testing_provider = NULL; | 127 g_testing_provider = NULL; |
| 119 device_management_service_.reset(); | 128 enterprise_device_management_service_.reset(); |
| 129 consumer_device_management_service_.reset(); |
| 120 } | 130 } |
| 121 | 131 |
| 122 PolicyService* BrowserPolicyConnector::GetPolicyService() { | 132 PolicyService* BrowserPolicyConnector::GetPolicyService() { |
| 123 if (!policy_service_) { | 133 if (!policy_service_) { |
| 124 g_created_policy_service = true; | 134 g_created_policy_service = true; |
| 125 std::vector<ConfigurationPolicyProvider*> providers; | 135 std::vector<ConfigurationPolicyProvider*> providers; |
| 126 if (g_testing_provider) { | 136 if (g_testing_provider) { |
| 127 providers.push_back(g_testing_provider); | 137 providers.push_back(g_testing_provider); |
| 128 } else { | 138 } else { |
| 129 providers.resize(policy_providers_.size()); | 139 providers.resize(policy_providers_.size()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 147 } | 157 } |
| 148 | 158 |
| 149 CombinedSchemaRegistry* BrowserPolicyConnector::GetSchemaRegistry() { | 159 CombinedSchemaRegistry* BrowserPolicyConnector::GetSchemaRegistry() { |
| 150 return &schema_registry_; | 160 return &schema_registry_; |
| 151 } | 161 } |
| 152 | 162 |
| 153 void BrowserPolicyConnector::ScheduleServiceInitialization( | 163 void BrowserPolicyConnector::ScheduleServiceInitialization( |
| 154 int64 delay_milliseconds) { | 164 int64 delay_milliseconds) { |
| 155 // Skip device initialization if the BrowserPolicyConnector was never | 165 // Skip device initialization if the BrowserPolicyConnector was never |
| 156 // initialized (unit tests). | 166 // initialized (unit tests). |
| 157 if (device_management_service_) | 167 if (enterprise_device_management_service_) |
| 158 device_management_service_->ScheduleInitialization(delay_milliseconds); | 168 enterprise_device_management_service_-> |
| 169 ScheduleInitialization(delay_milliseconds); |
| 170 if (consumer_device_management_service_) |
| 171 consumer_device_management_service_-> |
| 172 ScheduleInitialization(delay_milliseconds); |
| 159 } | 173 } |
| 160 | 174 |
| 161 const ConfigurationPolicyHandlerList* | 175 const ConfigurationPolicyHandlerList* |
| 162 BrowserPolicyConnector::GetHandlerList() const { | 176 BrowserPolicyConnector::GetHandlerList() const { |
| 163 return handler_list_.get(); | 177 return handler_list_.get(); |
| 164 } | 178 } |
| 165 | 179 |
| 180 DeviceManagementService* BrowserPolicyConnector::GetDeviceManagementService( |
| 181 ManagementMode management_mode) { |
| 182 switch (management_mode) { |
| 183 case ENTERPRISE_MANAGED: |
| 184 return enterprise_device_management_service_.get(); |
| 185 |
| 186 case CONSUMER_MANAGED: |
| 187 return consumer_device_management_service_.get(); |
| 188 |
| 189 default: |
| 190 NOTREACHED(); |
| 191 return NULL; |
| 192 } |
| 193 } |
| 194 |
| 166 // static | 195 // static |
| 167 void BrowserPolicyConnector::SetPolicyProviderForTesting( | 196 void BrowserPolicyConnector::SetPolicyProviderForTesting( |
| 168 ConfigurationPolicyProvider* provider) { | 197 ConfigurationPolicyProvider* provider) { |
| 169 // If this function is used by a test then it must be called before the | 198 // If this function is used by a test then it must be called before the |
| 170 // browser is created, and GetPolicyService() gets called. | 199 // browser is created, and GetPolicyService() gets called. |
| 171 CHECK(!g_created_policy_service); | 200 CHECK(!g_created_policy_service); |
| 172 DCHECK(!g_testing_provider); | 201 DCHECK(!g_testing_provider); |
| 173 g_testing_provider = provider; | 202 g_testing_provider = provider; |
| 174 } | 203 } |
| 175 | 204 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 201 gaia::ExtractDomainName(gaia::CanonicalizeEmail(username))); | 230 gaia::ExtractDomainName(gaia::CanonicalizeEmail(username))); |
| 202 for (size_t i = 0; i < arraysize(kNonManagedDomainPatterns); i++) { | 231 for (size_t i = 0; i < arraysize(kNonManagedDomainPatterns); i++) { |
| 203 base::string16 pattern = base::WideToUTF16(kNonManagedDomainPatterns[i]); | 232 base::string16 pattern = base::WideToUTF16(kNonManagedDomainPatterns[i]); |
| 204 if (MatchDomain(domain, pattern)) | 233 if (MatchDomain(domain, pattern)) |
| 205 return true; | 234 return true; |
| 206 } | 235 } |
| 207 return false; | 236 return false; |
| 208 } | 237 } |
| 209 | 238 |
| 210 // static | 239 // static |
| 211 std::string BrowserPolicyConnector::GetDeviceManagementUrl() { | 240 std::string BrowserPolicyConnector::GetDeviceManagementUrl( |
| 241 ManagementMode management_mode) { |
| 212 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 242 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 213 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) | 243 switch (management_mode) { |
| 214 return command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl); | 244 case ENTERPRISE_MANAGED: |
| 215 else | 245 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) |
| 216 return kDefaultDeviceManagementServerUrl; | 246 return command_line->GetSwitchValueASCII( |
| 247 switches::kDeviceManagementUrl); |
| 248 else |
| 249 return kDefaultEnterpriseDeviceManagementServerUrl; |
| 250 |
| 251 case CONSUMER_MANAGED: |
| 252 if (command_line->HasSwitch(switches::kConsumerDeviceManagementUrl)) |
| 253 return command_line->GetSwitchValueASCII( |
| 254 switches::kConsumerDeviceManagementUrl); |
| 255 else |
| 256 return kDefaultConsumerDeviceManagementServerUrl; |
| 257 |
| 258 default: |
| 259 NOTREACHED(); |
| 260 return std::string(); |
| 261 } |
| 217 } | 262 } |
| 218 | 263 |
| 219 // static | 264 // static |
| 220 void BrowserPolicyConnector::RegisterPrefs(PrefRegistrySimple* registry) { | 265 void BrowserPolicyConnector::RegisterPrefs(PrefRegistrySimple* registry) { |
| 221 registry->RegisterIntegerPref( | 266 registry->RegisterIntegerPref( |
| 222 policy_prefs::kUserPolicyRefreshRate, | 267 policy_prefs::kUserPolicyRefreshRate, |
| 223 CloudPolicyRefreshScheduler::kDefaultRefreshDelayMs); | 268 CloudPolicyRefreshScheduler::kDefaultRefreshDelayMs); |
| 224 } | 269 } |
| 225 | 270 |
| 226 void BrowserPolicyConnector::AddPolicyProvider( | 271 void BrowserPolicyConnector::AddPolicyProvider( |
| 227 scoped_ptr<ConfigurationPolicyProvider> provider) { | 272 scoped_ptr<ConfigurationPolicyProvider> provider) { |
| 228 policy_providers_.push_back(provider.release()); | 273 policy_providers_.push_back(provider.release()); |
| 229 } | 274 } |
| 230 | 275 |
| 231 void BrowserPolicyConnector::SetPlatformPolicyProvider( | 276 void BrowserPolicyConnector::SetPlatformPolicyProvider( |
| 232 scoped_ptr<ConfigurationPolicyProvider> provider) { | 277 scoped_ptr<ConfigurationPolicyProvider> provider) { |
| 233 CHECK(!platform_policy_provider_); | 278 CHECK(!platform_policy_provider_); |
| 234 platform_policy_provider_ = provider.get(); | 279 platform_policy_provider_ = provider.get(); |
| 235 AddPolicyProvider(provider.Pass()); | 280 AddPolicyProvider(provider.Pass()); |
| 236 } | 281 } |
| 237 | 282 |
| 238 } // namespace policy | 283 } // namespace policy |
| OLD | NEW |