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"
17 ManagedUserPasswordService::ManagedUserPasswordService()
18 : weak_ptr_factory_(this) {}
20 ManagedUserPasswordService::~ManagedUserPasswordService() {}
22 void ManagedUserPasswordService::Init(
23 const std::string& user_id,
24 ManagedUserSharedSettingsService* shared_settings_service) {
26 settings_service_ = shared_settings_service;
27 settings_service_subscription_ = settings_service_->Subscribe(
28 base::Bind(&ManagedUserPasswordService::OnSharedSettingsChange,
29 weak_ptr_factory_.GetWeakPtr()));
31 // Force value check in case we have missed some notification.
33 chromeos::SupervisedUserManager* supervised_user_manager =
34 chromeos::UserManager::Get()->GetSupervisedUserManager();
36 OnSharedSettingsChange(supervised_user_manager->GetUserSyncId(user_id),
37 managed_users::kChromeOSPasswordData);
40 void ManagedUserPasswordService::OnSharedSettingsChange(
41 const std::string& mu_id,
42 const std::string& key) {
43 if (key != managed_users::kChromeOSPasswordData)
45 chromeos::SupervisedUserManager* supervised_user_manager =
46 chromeos::UserManager::Get()->GetSupervisedUserManager();
47 const chromeos::User* user = supervised_user_manager->FindBySyncId(mu_id);
49 LOG(WARNING) << "Got notification for user not on device.";
52 DCHECK(user_id_ == user->email());
53 if (user_id_ != user->email())
55 const base::Value* value = settings_service_->GetValue(mu_id, key);
57 LOG(WARNING) << "Got empty value from sync.";
60 const base::DictionaryValue* dict;
61 if (!value->GetAsDictionary(&dict)) {
62 LOG(WARNING) << "Got non-dictionary value from sync.";
65 chromeos::SupervisedUserAuthentication* auth =
66 supervised_user_manager->GetAuthentication();
67 if (!auth->NeedPasswordChange(user_id_, dict))
69 auth->ScheduleSupervisedPasswordChange(user_id_, dict);
72 void ManagedUserPasswordService::Shutdown() {
73 settings_service_subscription_.reset();
76 } // namespace chromeos