summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/profile_policy_connector.cc
blob: f7e0e5790ea5eff7536cd3e422a01ad7a3cf3668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/profile_policy_connector.h"

#include <vector>

#include "base/logging.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/browser_policy_connector.h"
#include "chrome/browser/policy/configuration_policy_provider.h"
#include "chrome/browser/policy/policy_service_impl.h"

#if defined(ENABLE_MANAGED_USERS)
#include "chrome/browser/policy/managed_mode_policy_provider.h"
#endif

#if defined(OS_CHROMEOS)
#include "base/bind.h"
#include "base/message_loop.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/chromeos/login/user.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
#include "chrome/browser/chromeos/policy/network_configuration_updater.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/common/pref_names.h"
#include "chromeos/dbus/cryptohome_client.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#else
#include "chrome/browser/policy/cloud/user_cloud_policy_manager.h"
#include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
#endif

namespace policy {

#if defined(OS_CHROMEOS)
class ProfilePolicyConnector::PolicyInitializationObserver
    : public PolicyService::Observer {
 public:
  PolicyInitializationObserver(const base::Closure& closure,
                               PolicyService* policy_service)
      : closure_(closure),
        policy_service_(policy_service),
        weak_ptr_factory_(this) {
    if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME))
      OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME);
    else
      policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this);
  }

  virtual ~PolicyInitializationObserver() {
    policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
  }

  // PolicyService::Observer overrides.
  virtual void OnPolicyUpdated(const PolicyNamespace& ns,
                               const PolicyMap& previous,
                               const PolicyMap& current) OVERRIDE {
    // Ignore policy changes.
  }

  virtual void OnPolicyServiceInitialized(PolicyDomain domain) OVERRIDE {
    if (domain != POLICY_DOMAIN_CHROME)
      return;
    policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
    // Delay one cycle, so that the policies are propagated to the device policy
    // service before calling the closure.
    base::MessageLoop::current()->PostTask(
        FROM_HERE, base::Bind(&PolicyInitializationObserver::RunClosure,
                              weak_ptr_factory_.GetWeakPtr()));
  }

  void RunClosure() {
    closure_.Run();
  }

 private:
  base::Closure closure_;
  PolicyService* policy_service_;
  base::WeakPtrFactory<PolicyInitializationObserver> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(PolicyInitializationObserver);
};
#endif

ProfilePolicyConnector::ProfilePolicyConnector(Profile* profile)
    : profile_(profile),
#if defined(OS_CHROMEOS)
      is_primary_user_(false),
#endif
      weak_ptr_factory_(this) {}

ProfilePolicyConnector::~ProfilePolicyConnector() {}

void ProfilePolicyConnector::Init(
    bool force_immediate_load,
    base::SequencedTaskRunner* sequenced_task_runner) {
  BrowserPolicyConnector* connector =
      g_browser_process->browser_policy_connector();
  // |providers| contains a list of the policy providers available for the
  // PolicyService of this connector.
  std::vector<ConfigurationPolicyProvider*> providers;

#if defined(OS_CHROMEOS)
  UserCloudPolicyManagerChromeOS* cloud_policy_manager =
      UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile_);
  if (cloud_policy_manager)
    providers.push_back(cloud_policy_manager);

  bool is_managed = false;
  std::string username;
  if (!chromeos::ProfileHelper::IsSigninProfile(profile_)) {
    // |user| should never be NULL except for the signin profile.
    // TODO(joaodasilva): get the |user| that corresponds to the |profile_|
    // from the ProfileHelper, once that's ready.
    chromeos::UserManager* user_manager = chromeos::UserManager::Get();
    chromeos::User* user = user_manager->GetActiveUser();
    CHECK(user);
    // Check if |user| is managed, and if it's a public account.
    username = user->email();
    is_managed =
        connector->GetUserAffiliation(username) == USER_AFFILIATION_MANAGED;
    is_primary_user_ =
        chromeos::UserManager::Get()->GetLoggedInUsers().size() == 1;
    if (user->GetType() == chromeos::User::USER_TYPE_PUBLIC_ACCOUNT)
      InitializeDeviceLocalAccountPolicyProvider(username);
    if (device_local_account_policy_provider_)
      providers.push_back(device_local_account_policy_provider_.get());
  }
#else
  UserCloudPolicyManager* cloud_policy_manager =
      UserCloudPolicyManagerFactory::GetForProfile(profile_);
  if (cloud_policy_manager)
    providers.push_back(cloud_policy_manager);
#endif

#if defined(ENABLE_MANAGED_USERS)
  managed_mode_policy_provider_ = ManagedModePolicyProvider::Create(
      profile_, sequenced_task_runner, force_immediate_load);
  managed_mode_policy_provider_->Init();
  providers.push_back(managed_mode_policy_provider_.get());
#endif

  policy_service_ = connector->CreatePolicyService(providers);

#if defined(OS_CHROMEOS)
  if (is_primary_user_) {
    if (cloud_policy_manager) {
      connector->SetUserPolicyDelegate(cloud_policy_manager);
    } else if (device_local_account_policy_provider_) {
      connector->SetUserPolicyDelegate(
          device_local_account_policy_provider_.get());
    }

    chromeos::CryptohomeClient* cryptohome_client =
        chromeos::DBusThreadManager::Get()->GetCryptohomeClient();
    cryptohome_client->GetSanitizedUsername(
        username,
        base::Bind(
            &ProfilePolicyConnector::InitializeNetworkConfigurationUpdater,
            weak_ptr_factory_.GetWeakPtr(),
            is_managed));
  }
#endif
}

void ProfilePolicyConnector::InitForTesting(scoped_ptr<PolicyService> service) {
  policy_service_ = service.Pass();
}

void ProfilePolicyConnector::Shutdown() {
#if defined(OS_CHROMEOS)
  if (is_primary_user_) {
    g_browser_process->browser_policy_connector()->SetUserPolicyDelegate(NULL);
    user_policy_init_observer_.reset();
  }
  if (device_local_account_policy_provider_)
    device_local_account_policy_provider_->Shutdown();
#endif

#if defined(ENABLE_MANAGED_USERS)
  if (managed_mode_policy_provider_)
    managed_mode_policy_provider_->Shutdown();
#endif
}

bool ProfilePolicyConnector::UsedPolicyCertificates() {
#if defined(OS_CHROMEOS)
  return profile_->GetPrefs()->GetBoolean(prefs::kUsedPolicyCertificatesOnce);
#else
  return false;
#endif
}

#if defined(OS_CHROMEOS)
void ProfilePolicyConnector::InitializeDeviceLocalAccountPolicyProvider(
    const std::string& username) {
  BrowserPolicyConnector* connector =
      g_browser_process->browser_policy_connector();
  DeviceLocalAccountPolicyService* device_local_account_policy_service =
      connector->GetDeviceLocalAccountPolicyService();
  if (!device_local_account_policy_service)
    return;
  device_local_account_policy_provider_.reset(
      new DeviceLocalAccountPolicyProvider(
          username, device_local_account_policy_service));
  device_local_account_policy_provider_->Init();
}

void ProfilePolicyConnector::InitializeNetworkConfigurationUpdater(
    bool is_managed,
    chromeos::DBusMethodCallStatus status,
    const std::string& hashed_username) {
  // TODO(joaodasilva): create the NetworkConfigurationUpdater for user ONC
  // here, after splitting that class into an instance for device policy and
  // another per profile for user policy.
  BrowserPolicyConnector* connector =
      g_browser_process->browser_policy_connector();
  NetworkConfigurationUpdater* network_updater =
      connector->GetNetworkConfigurationUpdater();

  user_policy_init_observer_.reset(new PolicyInitializationObserver(
      base::Bind(&NetworkConfigurationUpdater::OnUserPolicyInitialized,
                 base::Unretained(network_updater),
                 is_managed,
                 hashed_username),
      policy_service()));
}
#endif

}  // namespace policy