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/signin/screenlock_bridge.h"
7 #include "base/logging.h"
8 #include "chrome/browser/profiles/profile_window.h"
9 #include "chrome/browser/signin/signin_manager_factory.h"
10 #include "components/signin/core/browser/signin_manager.h"
12 #if defined(OS_CHROMEOS)
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/session_manager_client.h"
19 base::LazyInstance
<ScreenlockBridge
> g_screenlock_bridge_bridge_instance
=
20 LAZY_INSTANCE_INITIALIZER
;
25 ScreenlockBridge
* ScreenlockBridge::Get() {
26 return g_screenlock_bridge_bridge_instance
.Pointer();
30 std::string
ScreenlockBridge::GetAuthenticatedUserEmail(Profile
* profile
) {
31 // |profile| has to be a signed-in profile with SigninManager already
32 // created. Otherwise, just crash to collect stack.
33 SigninManagerBase
* signin_manager
=
34 SigninManagerFactory::GetForProfileIfExists(profile
);
35 return signin_manager
->GetAuthenticatedUsername();
38 ScreenlockBridge::ScreenlockBridge() : lock_handler_(NULL
) {
41 ScreenlockBridge::~ScreenlockBridge() {
44 void ScreenlockBridge::SetLockHandler(LockHandler
* lock_handler
) {
45 DCHECK(lock_handler_
== NULL
|| lock_handler
== NULL
);
46 lock_handler_
= lock_handler
;
48 FOR_EACH_OBSERVER(Observer
, observers_
, OnScreenDidLock());
50 FOR_EACH_OBSERVER(Observer
, observers_
, OnScreenDidUnlock());
53 bool ScreenlockBridge::IsLocked() const {
54 return lock_handler_
!= NULL
;
57 void ScreenlockBridge::Lock(Profile
* profile
) {
58 #if defined(OS_CHROMEOS)
59 chromeos::SessionManagerClient
* session_manager
=
60 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
61 session_manager
->RequestLockScreen();
63 profiles::LockProfile(profile
);
67 void ScreenlockBridge::Unlock(Profile
* profile
) {
69 lock_handler_
->Unlock(GetAuthenticatedUserEmail(profile
));
72 void ScreenlockBridge::AddObserver(Observer
* observer
) {
73 observers_
.AddObserver(observer
);
76 void ScreenlockBridge::RemoveObserver(Observer
* observer
) {
77 observers_
.RemoveObserver(observer
);