Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / managed_mode / chromeos / managed_user_password_service.cc
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.
4
5 #include "chrome/browser/managed_mode/chromeos/managed_user_password_service.h"
6
7 #include "base/bind.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"
14
15 ManagedUserPasswordService::ManagedUserPasswordService()
16     : weak_ptr_factory_(this) {}
17
18 ManagedUserPasswordService::~ManagedUserPasswordService() {}
19
20 void ManagedUserPasswordService::Init(
21     const std::string& user_id,
22     ManagedUserSharedSettingsService* shared_settings_service) {
23   user_id_ = user_id;
24   settings_service_ = shared_settings_service;
25   settings_service_subscription_ = settings_service_->Subscribe(
26       base::Bind(&ManagedUserPasswordService::OnSharedSettingsChange,
27                  weak_ptr_factory_.GetWeakPtr()));
28
29   // Force value check in case we have missed some notification.
30
31   chromeos::SupervisedUserManager* supervised_user_manager =
32       chromeos::UserManager::Get()->GetSupervisedUserManager();
33
34   OnSharedSettingsChange(supervised_user_manager->GetUserSyncId(user_id),
35                          managed_users::kUserPasswordRecord);
36 }
37
38 void ManagedUserPasswordService::OnSharedSettingsChange(
39     const std::string& mu_id,
40     const std::string& key) {
41   if (key != managed_users::kUserPasswordRecord)
42     return;
43   chromeos::SupervisedUserManager* supervised_user_manager =
44       chromeos::UserManager::Get()->GetSupervisedUserManager();
45   const chromeos::User* user = supervised_user_manager->FindBySyncId(mu_id);
46   if (user == NULL) {
47     LOG(WARNING) << "Got notification for user not on device.";
48     return;
49   }
50   DCHECK(user_id_ == user->email());
51   if (user_id_ != user->email())
52     return;
53   const base::Value* value = settings_service_->GetValue(mu_id, key);
54   if (value == NULL) {
55     LOG(WARNING) << "Got empty value from sync.";
56     return;
57   }
58   const base::DictionaryValue* dict;
59   if (!value->GetAsDictionary(&dict)) {
60     LOG(WARNING) << "Got non-dictionary value from sync.";
61     return;
62   }
63   chromeos::SupervisedUserAuthentication* auth =
64       supervised_user_manager->GetAuthentication();
65   if (!auth->NeedPasswordChange(user_id_, dict))
66     return;
67   auth->ScheduleSupervisedPasswordChange(user_id_, dict);
68 }
69
70 void ManagedUserPasswordService::Shutdown() {
71     settings_service_subscription_.reset();
72 }