screenlock_bridge.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include "ash/components/proximity_auth/screenlock_bridge.h"
  5. #include <string>
  6. #include <utility>
  7. #include <memory>
  8. #include "ash/components/multidevice/logging/logging.h"
  9. #include "build/build_config.h"
  10. #include "chromeos/ash/components/dbus/session_manager/session_manager_client.h"
  11. namespace proximity_auth {
  12. namespace {
  13. base::LazyInstance<ScreenlockBridge>::DestructorAtExit
  14. g_screenlock_bridge_instance = LAZY_INSTANCE_INITIALIZER;
  15. // Ids for the icons that are supported by lock screen and signin screen
  16. // account picker as user pod custom icons.
  17. // The id's should be kept in sync with values used by user_pod_row.js.
  18. const char kLockedUserPodCustomIconId[] = "locked";
  19. const char kLockedToBeActivatedUserPodCustomIconId[] = "locked-to-be-activated";
  20. const char kLockedWithProximityHintUserPodCustomIconId[] =
  21. "locked-with-proximity-hint";
  22. const char kUnlockedUserPodCustomIconId[] = "unlocked";
  23. const char kHardlockedUserPodCustomIconId[] = "hardlocked";
  24. const char kSpinnerUserPodCustomIconId[] = "spinner";
  25. // Given the user pod icon, returns its id as used by the user pod UI code.
  26. std::string GetIdForIcon(ScreenlockBridge::UserPodCustomIcon icon) {
  27. switch (icon) {
  28. case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED:
  29. return kLockedUserPodCustomIconId;
  30. case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED_TO_BE_ACTIVATED:
  31. return kLockedToBeActivatedUserPodCustomIconId;
  32. case ScreenlockBridge::USER_POD_CUSTOM_ICON_LOCKED_WITH_PROXIMITY_HINT:
  33. return kLockedWithProximityHintUserPodCustomIconId;
  34. case ScreenlockBridge::USER_POD_CUSTOM_ICON_UNLOCKED:
  35. return kUnlockedUserPodCustomIconId;
  36. case ScreenlockBridge::USER_POD_CUSTOM_ICON_HARDLOCKED:
  37. return kHardlockedUserPodCustomIconId;
  38. case ScreenlockBridge::USER_POD_CUSTOM_ICON_SPINNER:
  39. return kSpinnerUserPodCustomIconId;
  40. default:
  41. return "";
  42. }
  43. }
  44. } // namespace
  45. ScreenlockBridge::UserPodCustomIconInfo::UserPodCustomIconInfo()
  46. : autoshow_tooltip_(false), hardlock_on_click_(false) {}
  47. ScreenlockBridge::UserPodCustomIconInfo::~UserPodCustomIconInfo() {}
  48. std::unique_ptr<base::DictionaryValue>
  49. ScreenlockBridge::UserPodCustomIconInfo::ToDictionaryValue() const {
  50. auto result = std::make_unique<base::DictionaryValue>();
  51. result->SetStringKey("id", GetIDString());
  52. if (!tooltip_.empty()) {
  53. base::DictionaryValue tooltip_options;
  54. tooltip_options.SetStringKey("text", tooltip_);
  55. tooltip_options.SetBoolKey("autoshow", autoshow_tooltip_);
  56. result->SetKey("tooltip", std::move(tooltip_options));
  57. }
  58. if (!aria_label_.empty())
  59. result->SetStringKey("ariaLabel", aria_label_);
  60. if (hardlock_on_click_)
  61. result->SetBoolKey("hardlockOnClick", true);
  62. return result;
  63. }
  64. void ScreenlockBridge::UserPodCustomIconInfo::SetIcon(
  65. ScreenlockBridge::UserPodCustomIcon icon) {
  66. icon_ = icon;
  67. }
  68. void ScreenlockBridge::UserPodCustomIconInfo::SetTooltip(
  69. const std::u16string& tooltip,
  70. bool autoshow) {
  71. tooltip_ = tooltip;
  72. autoshow_tooltip_ = autoshow;
  73. }
  74. void ScreenlockBridge::UserPodCustomIconInfo::SetAriaLabel(
  75. const std::u16string& aria_label) {
  76. aria_label_ = aria_label;
  77. }
  78. void ScreenlockBridge::UserPodCustomIconInfo::SetHardlockOnClick() {
  79. hardlock_on_click_ = true;
  80. }
  81. std::string ScreenlockBridge::UserPodCustomIconInfo::GetIDString() const {
  82. return GetIdForIcon(icon_);
  83. }
  84. // static
  85. ScreenlockBridge* ScreenlockBridge::Get() {
  86. return g_screenlock_bridge_instance.Pointer();
  87. }
  88. void ScreenlockBridge::SetLockHandler(LockHandler* lock_handler) {
  89. // Don't notify observers if there is no change -- i.e. if the screen was
  90. // already unlocked, and is remaining unlocked.
  91. if (lock_handler == lock_handler_)
  92. return;
  93. DCHECK(lock_handler_ == nullptr || lock_handler == nullptr);
  94. // TODO(isherman): If |lock_handler| is null, then |lock_handler_| might have
  95. // been freed. Cache the screen type rather than querying it below.
  96. LockHandler::ScreenType screen_type;
  97. if (lock_handler_)
  98. screen_type = lock_handler_->GetScreenType();
  99. else
  100. screen_type = lock_handler->GetScreenType();
  101. lock_handler_ = lock_handler;
  102. if (lock_handler_) {
  103. for (auto& observer : observers_)
  104. observer.OnScreenDidLock(screen_type);
  105. } else {
  106. focused_account_id_ = EmptyAccountId();
  107. for (auto& observer : observers_)
  108. observer.OnScreenDidUnlock(screen_type);
  109. }
  110. }
  111. void ScreenlockBridge::SetFocusedUser(const AccountId& account_id) {
  112. if (account_id == focused_account_id_)
  113. return;
  114. focused_account_id_ = account_id;
  115. for (auto& observer : observers_)
  116. observer.OnFocusedUserChanged(account_id);
  117. }
  118. bool ScreenlockBridge::IsLocked() const {
  119. return lock_handler_ != nullptr;
  120. }
  121. void ScreenlockBridge::Lock() {
  122. ash::SessionManagerClient::Get()->RequestLockScreen();
  123. }
  124. void ScreenlockBridge::Unlock(const AccountId& account_id) {
  125. if (lock_handler_)
  126. lock_handler_->Unlock(account_id);
  127. }
  128. void ScreenlockBridge::AddObserver(Observer* observer) {
  129. observers_.AddObserver(observer);
  130. }
  131. void ScreenlockBridge::RemoveObserver(Observer* observer) {
  132. observers_.RemoveObserver(observer);
  133. }
  134. ScreenlockBridge::ScreenlockBridge()
  135. : lock_handler_(nullptr), focused_account_id_(EmptyAccountId()) {}
  136. ScreenlockBridge::~ScreenlockBridge() {}
  137. } // namespace proximity_auth