Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / policy / profile_policy_connector.cc
blob52c7f91ee281c932cf9be8a38b4b2875cef9c48c
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/policy/profile_policy_connector.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/values.h"
12 #include "chrome/browser/browser_process.h"
13 #include "components/policy/core/browser/browser_policy_connector.h"
14 #include "components/policy/core/common/cloud/cloud_policy_core.h"
15 #include "components/policy/core/common/cloud/cloud_policy_manager.h"
16 #include "components/policy/core/common/cloud/cloud_policy_store.h"
17 #include "components/policy/core/common/configuration_policy_provider.h"
18 #include "components/policy/core/common/forwarding_policy_provider.h"
19 #include "components/policy/core/common/policy_service_impl.h"
20 #include "google_apis/gaia/gaia_auth_util.h"
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/chromeos/login/user.h"
24 #include "chrome/browser/chromeos/login/user_manager.h"
25 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
26 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
27 #include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
28 #include "chrome/browser/chromeos/policy/login_profile_policy_provider.h"
29 #endif
31 namespace policy {
33 ProfilePolicyConnector::ProfilePolicyConnector()
34 #if defined(OS_CHROMEOS)
35 : is_primary_user_(false),
36 user_cloud_policy_manager_(NULL)
37 #else
38 : user_cloud_policy_manager_(NULL)
39 #endif
42 ProfilePolicyConnector::~ProfilePolicyConnector() {}
44 void ProfilePolicyConnector::Init(
45 bool force_immediate_load,
46 #if defined(OS_CHROMEOS)
47 const chromeos::User* user,
48 #endif
49 SchemaRegistry* schema_registry,
50 CloudPolicyManager* user_cloud_policy_manager) {
51 user_cloud_policy_manager_ = user_cloud_policy_manager;
53 // |providers| contains a list of the policy providers available for the
54 // PolicyService of this connector, in decreasing order of priority.
56 // Note: all the providers appended to this vector must eventually become
57 // initialized for every policy domain, otherwise some subsystems will never
58 // use the policies exposed by the PolicyService!
59 // The default ConfigurationPolicyProvider::IsInitializationComplete()
60 // result is true, so take care if a provider overrides that.
61 std::vector<ConfigurationPolicyProvider*> providers;
63 #if defined(OS_CHROMEOS)
64 BrowserPolicyConnectorChromeOS* connector =
65 g_browser_process->platform_part()->browser_policy_connector_chromeos();
66 #else
67 BrowserPolicyConnector* connector =
68 g_browser_process->browser_policy_connector();
69 #endif
71 if (connector->GetPlatformProvider()) {
72 forwarding_policy_provider_.reset(
73 new ForwardingPolicyProvider(connector->GetPlatformProvider()));
74 forwarding_policy_provider_->Init(schema_registry);
75 providers.push_back(forwarding_policy_provider_.get());
78 #if defined(OS_CHROMEOS)
79 if (connector->GetDeviceCloudPolicyManager())
80 providers.push_back(connector->GetDeviceCloudPolicyManager());
81 #endif
83 if (user_cloud_policy_manager)
84 providers.push_back(user_cloud_policy_manager);
86 #if defined(OS_CHROMEOS)
87 if (!user) {
88 DCHECK(schema_registry);
89 // This case occurs for the signin profile.
90 special_user_policy_provider_.reset(
91 new LoginProfilePolicyProvider(connector->GetPolicyService()));
92 } else {
93 // |user| should never be NULL except for the signin profile.
94 is_primary_user_ = user == chromeos::UserManager::Get()->GetPrimaryUser();
95 special_user_policy_provider_ = DeviceLocalAccountPolicyProvider::Create(
96 user->email(),
97 connector->GetDeviceLocalAccountPolicyService());
99 if (special_user_policy_provider_) {
100 special_user_policy_provider_->Init(schema_registry);
101 providers.push_back(special_user_policy_provider_.get());
103 #endif
105 policy_service_.reset(new PolicyServiceImpl(providers));
107 #if defined(OS_CHROMEOS)
108 if (is_primary_user_) {
109 if (user_cloud_policy_manager)
110 connector->SetUserPolicyDelegate(user_cloud_policy_manager);
111 else if (special_user_policy_provider_)
112 connector->SetUserPolicyDelegate(special_user_policy_provider_.get());
114 #endif
117 void ProfilePolicyConnector::InitForTesting(scoped_ptr<PolicyService> service) {
118 policy_service_ = service.Pass();
121 void ProfilePolicyConnector::Shutdown() {
122 #if defined(OS_CHROMEOS)
123 BrowserPolicyConnectorChromeOS* connector =
124 g_browser_process->platform_part()->browser_policy_connector_chromeos();
125 if (is_primary_user_)
126 connector->SetUserPolicyDelegate(NULL);
127 if (special_user_policy_provider_)
128 special_user_policy_provider_->Shutdown();
129 #endif
130 if (forwarding_policy_provider_)
131 forwarding_policy_provider_->Shutdown();
134 bool ProfilePolicyConnector::IsManaged() const {
135 return !GetManagementDomain().empty();
138 std::string ProfilePolicyConnector::GetManagementDomain() const {
139 if (!user_cloud_policy_manager_)
140 return "";
141 CloudPolicyStore* store = user_cloud_policy_manager_->core()->store();
142 if (store && store->is_managed() && store->policy()->has_username())
143 return gaia::ExtractDomainName(store->policy()->username());
144 return "";
147 } // namespace policy