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
3 // found in the LICENSE file.
5 #include "chrome/browser/managed_mode/chromeos/managed_user_password_service.h"
8 #include "base/values.h"
9 #include "chrome/browser/chromeos/login/managed/supervised_user_authentication.h"
10 #include "chrome/browser/chromeos/login/supervised_user_manager.h"
11 #include "chrome/browser/chromeos/login/user_manager.h"
12 #include "chrome/browser/managed_mode/managed_user_constants.h"
13 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
15 ManagedUserPasswordService::ManagedUserPasswordService()
16 : weak_ptr_factory_(this) {}
18 ManagedUserPasswordService::~ManagedUserPasswordService() {}
20 void ManagedUserPasswordService::Init(
21 const std::string& user_id,
22 ManagedUserSharedSettingsService* shared_settings_service) {
24 settings_service_ = shared_settings_service;
25 settings_service_subscription_ = settings_service_->Subscribe(
26 base::Bind(&ManagedUserPasswordService::OnSharedSettingsChange,
27 weak_ptr_factory_.GetWeakPtr()));
29 // Force value check in case we have missed some notification.
31 chromeos::SupervisedUserManager* supervised_user_manager =
32 chromeos::UserManager::Get()->GetSupervisedUserManager();
34 OnSharedSettingsChange(supervised_user_manager->GetUserSyncId(user_id),
35 managed_users::kUserPasswordRecord);
38 void ManagedUserPasswordService::OnSharedSettingsChange(
39 const std::string& mu_id,
40 const std::string& key) {
41 if (key != managed_users::kUserPasswordRecord)
43 chromeos::SupervisedUserManager* supervised_user_manager =
44 chromeos::UserManager::Get()->GetSupervisedUserManager();
45 const chromeos::User* user = supervised_user_manager->FindBySyncId(mu_id);
47 LOG(WARNING) << "Got notification for user not on device.";
50 DCHECK(user_id_ == user->email());
51 if (user_id_ != user->email())
53 const base::Value* value = settings_service_->GetValue(mu_id, key);
55 LOG(WARNING) << "Got empty value from sync.";
58 const base::DictionaryValue* dict;
59 if (!value->GetAsDictionary(&dict)) {
60 LOG(WARNING) << "Got non-dictionary value from sync.";
63 chromeos::SupervisedUserAuthentication* auth =
64 supervised_user_manager->GetAuthentication();
65 if (!auth->NeedPasswordChange(user_id_, dict))
67 auth->ScheduleSupervisedPasswordChange(user_id_, dict);
70 void ManagedUserPasswordService::Shutdown() {
71 settings_service_subscription_.reset();