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/signin/screenlock_bridge.h" 6 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" 11 12 #if defined(OS_CHROMEOS) 13 #include "chromeos/dbus/dbus_thread_manager.h" 14 #include "chromeos/dbus/session_manager_client.h" 15 #endif 16 17 namespace { 18 19 base::LazyInstance<ScreenlockBridge> g_screenlock_bridge_bridge_instance = 20 LAZY_INSTANCE_INITIALIZER; 21 22 } // namespace 23 24 // static 25 ScreenlockBridge* ScreenlockBridge::Get() { 26 return g_screenlock_bridge_bridge_instance.Pointer(); 27 } 28 29 // static 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(); 36 } 37 38 ScreenlockBridge::ScreenlockBridge() : lock_handler_(NULL) { 39 } 40 41 ScreenlockBridge::~ScreenlockBridge() { 42 } 43 44 void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) { 45 DCHECK(lock_handler_ == NULL || lock_handler == NULL); 46 lock_handler_ = lock_handler; 47 if (lock_handler_) 48 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidLock()); 49 else 50 FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidUnlock()); 51 } 52 53 bool ScreenlockBridge::IsLocked() const { 54 return lock_handler_ != NULL; 55 } 56 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(); 62 #else 63 profiles::LockProfile(profile); 64 #endif 65 } 66 67 void ScreenlockBridge::Unlock(Profile* profile) { 68 if (lock_handler_) 69 lock_handler_->Unlock(GetAuthenticatedUserEmail(profile)); 70 } 71 72 void ScreenlockBridge::AddObserver(Observer* observer) { 73 observers_.AddObserver(observer); 74 } 75 76 void ScreenlockBridge::RemoveObserver(Observer* observer) { 77 observers_.RemoveObserver(observer); 78 } 79