screenlock_bridge.cc revision ab8f6f0bd665d3c1ff476eb06c58c42630e462d4
1010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// found in the LICENSE file.
4010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
5010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "chrome/browser/signin/screenlock_bridge.h"
6010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/logging.h"
86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/strings/string16.h"
9010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "chrome/browser/profiles/profile_window.h"
10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "components/signin/core/browser/signin_manager.h"
12010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#if defined(OS_CHROMEOS)
14010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "chromeos/dbus/dbus_thread_manager.h"
15010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "chromeos/dbus/session_manager_client.h"
16010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#endif
17010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
18010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)namespace {
19010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
20010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)base::LazyInstance<ScreenlockBridge> g_screenlock_bridge_bridge_instance =
21010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
22010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
2334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)// Ids for the icons that are supported by lock screen and signin screen
2434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)// account picker as user pod custom icons.
2534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)// The id's should be kept in sync with values used by user_pod_row.js.
2634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)const char kLockedUserPodCustomIconId[] = "locked";
2734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)const char kUnlockedUserPodCustomIconId[] = "unlocked";
2834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)const char kHardlockedUserPodCustomIconId[] = "hardlocked";
2934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)const char kSpinnerUserPodCustomIconId[] = "spinner";
3034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
3134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)// Given the user pod icon, returns its id as used by the user pod UI code.
3234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)std::string GetIdForIcon(ScreenlockBridge::UserPodCustomIcon icon) {
3334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  switch (icon) {
3434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED:
3534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      return kLockedUserPodCustomIconId;
3634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    case ScreenlockBridge::USER_POD_CUSTOM_ICON_UNLOCKED:
3734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      return kUnlockedUserPodCustomIconId;
3834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    case ScreenlockBridge::USER_POD_CUSTOM_ICON_HARDLOCKED:
3934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      return kHardlockedUserPodCustomIconId;
4034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    case ScreenlockBridge::USER_POD_CUSTOM_ICON_SPINNER:
4134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      return kSpinnerUserPodCustomIconId;
4234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    default:
4334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)      return "";
4434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  }
4534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)}
4634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
47010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}  // namespace
48010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
49010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// static
50010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ScreenlockBridge* ScreenlockBridge::Get() {
51010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return g_screenlock_bridge_bridge_instance.Pointer();
52010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
53010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)ScreenlockBridge::UserPodCustomIconOptions::UserPodCustomIconOptions()
5534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    : autoshow_tooltip_(false),
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      hardlock_on_click_(false) {
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)ScreenlockBridge::UserPodCustomIconOptions::~UserPodCustomIconOptions() {}
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)scoped_ptr<base::DictionaryValue>
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)ScreenlockBridge::UserPodCustomIconOptions::ToDictionaryValue() const {
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
6434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  std::string icon_id = GetIdForIcon(icon_);
6534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  if (icon_id.empty())
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return result.Pass();
676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  result->SetString("id", icon_id);
696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!tooltip_.empty()) {
716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    base::DictionaryValue* tooltip_options = new base::DictionaryValue();
726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    tooltip_options->SetString("text", tooltip_);
736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    tooltip_options->SetBoolean("autoshow", autoshow_tooltip_);
746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    result->Set("tooltip", tooltip_options);
756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
77ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch  if (!aria_label_.empty())
78ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch    result->SetString("ariaLabel", aria_label_);
79ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch
8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (hardlock_on_click_)
8103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    result->SetBoolean("hardlockOnClick", true);
8203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return result.Pass();
846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)void ScreenlockBridge::UserPodCustomIconOptions::SetIcon(
8734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)    ScreenlockBridge::UserPodCustomIcon icon) {
8834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)  icon_ = icon;
896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void ScreenlockBridge::UserPodCustomIconOptions::SetTooltip(
926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    const base::string16& tooltip,
936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool autoshow) {
946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  tooltip_ = tooltip;
956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  autoshow_tooltip_ = autoshow;
966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
98ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdochvoid ScreenlockBridge::UserPodCustomIconOptions::SetAriaLabel(
99ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch    const base::string16& aria_label) {
100ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch  aria_label_ = aria_label;
101ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch}
102ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch
10303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void ScreenlockBridge::UserPodCustomIconOptions::SetHardlockOnClick() {
10403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  hardlock_on_click_ = true;
10503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
10603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
107010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// static
108010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)std::string ScreenlockBridge::GetAuthenticatedUserEmail(Profile* profile) {
109010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // |profile| has to be a signed-in profile with SigninManager already
110010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // created. Otherwise, just crash to collect stack.
111010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  SigninManagerBase* signin_manager =
112010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      SigninManagerFactory::GetForProfileIfExists(profile);
113010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return signin_manager->GetAuthenticatedUsername();
114010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
115010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ScreenlockBridge::ScreenlockBridge() : lock_handler_(NULL) {
117010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
118010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
119010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ScreenlockBridge::~ScreenlockBridge() {
120010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
121010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
122010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) {
123010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(lock_handler_ == NULL || lock_handler == NULL);
124010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  lock_handler_ = lock_handler;
125010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (lock_handler_)
126010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidLock());
127010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  else
128010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    FOR_EACH_OBSERVER(Observer, observers_, OnScreenDidUnlock());
129010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
130010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ScreenlockBridge::SetFocusedUser(const std::string& user_id) {
1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (user_id == focused_user_id_)
1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return;
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  focused_user_id_ = user_id;
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  FOR_EACH_OBSERVER(Observer, observers_, OnFocusedUserChanged(user_id));
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
138010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ScreenlockBridge::IsLocked() const {
139010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return lock_handler_ != NULL;
140010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
141010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
142010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void ScreenlockBridge::Lock(Profile* profile) {
143010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#if defined(OS_CHROMEOS)
144010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  chromeos::SessionManagerClient* session_manager =
145010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
146010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  session_manager->RequestLockScreen();
147010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#else
148010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  profiles::LockProfile(profile);
149010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#endif
150010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
151010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
152010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void ScreenlockBridge::Unlock(Profile* profile) {
153010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (lock_handler_)
154010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    lock_handler_->Unlock(GetAuthenticatedUserEmail(profile));
155010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
156010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
157010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void ScreenlockBridge::AddObserver(Observer* observer) {
158010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  observers_.AddObserver(observer);
159010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
160010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
161010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void ScreenlockBridge::RemoveObserver(Observer* observer) {
162010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  observers_.RemoveObserver(observer);
163010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
164