Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" | 5 #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "ash/public/interfaces/constants.mojom.h" | 9 #include "ash/public/interfaces/constants.mojom.h" |
| 10 #include "base/bind.h" | |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "chrome/browser/chromeos/lock_screen_apps/app_manager_impl.h" | |
| 14 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chromeos/chromeos_switches.h" | 16 #include "chromeos/chromeos_switches.h" |
| 17 #include "components/session_manager/core/session_manager.h" | |
| 12 #include "content/public/common/service_manager_connection.h" | 18 #include "content/public/common/service_manager_connection.h" |
| 13 #include "services/service_manager/public/cpp/connector.h" | 19 #include "services/service_manager/public/cpp/connector.h" |
| 14 | 20 |
| 15 using ash::mojom::TrayActionState; | 21 using ash::mojom::TrayActionState; |
| 16 | 22 |
| 17 namespace lock_screen_apps { | 23 namespace lock_screen_apps { |
| 18 | 24 |
| 19 namespace { | 25 namespace { |
| 20 | 26 |
| 21 StateController* g_instance = nullptr; | 27 StateController* g_instance = nullptr; |
| 22 | 28 |
| 23 } // namespace | 29 } // namespace |
| 24 | 30 |
| 25 // static | 31 // static |
| 26 bool StateController::IsEnabled() { | 32 bool StateController::IsEnabled() { |
| 27 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 33 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 28 chromeos::switches::kEnableLockScreenApps); | 34 chromeos::switches::kEnableLockScreenApps); |
| 29 } | 35 } |
| 30 | 36 |
| 31 // static | 37 // static |
| 32 StateController* StateController::Get() { | 38 StateController* StateController::Get() { |
| 33 DCHECK(g_instance || !IsEnabled()); | 39 DCHECK(g_instance || !IsEnabled()); |
| 34 return g_instance; | 40 return g_instance; |
| 35 } | 41 } |
| 36 | 42 |
| 37 StateController::StateController() : binding_(this) { | 43 StateController::StateController() : binding_(this), session_observer_(this) { |
| 38 DCHECK(!g_instance); | 44 DCHECK(!g_instance); |
| 39 DCHECK(IsEnabled()); | 45 DCHECK(IsEnabled()); |
| 40 | 46 |
| 41 g_instance = this; | 47 g_instance = this; |
| 42 } | 48 } |
| 43 | 49 |
| 44 StateController::~StateController() { | 50 StateController::~StateController() { |
| 45 DCHECK_EQ(g_instance, this); | 51 DCHECK_EQ(g_instance, this); |
| 46 g_instance = nullptr; | 52 g_instance = nullptr; |
| 47 } | 53 } |
| 48 | 54 |
| 49 void StateController::SetTrayActionPtrForTesting( | 55 void StateController::SetTrayActionPtrForTesting( |
| 50 ash::mojom::TrayActionPtr tray_action_ptr) { | 56 ash::mojom::TrayActionPtr tray_action_ptr) { |
| 51 tray_action_ptr_ = std::move(tray_action_ptr); | 57 tray_action_ptr_ = std::move(tray_action_ptr); |
| 52 } | 58 } |
| 53 | 59 |
| 54 void StateController::FlushTrayActionForTesting() { | 60 void StateController::FlushTrayActionForTesting() { |
| 55 tray_action_ptr_.FlushForTesting(); | 61 tray_action_ptr_.FlushForTesting(); |
| 56 } | 62 } |
| 57 | 63 |
| 64 void StateController::SetAppManagerForTesting( | |
| 65 std::unique_ptr<AppManager> app_manager) { | |
| 66 app_manager_ = std::move(app_manager); | |
|
Devlin
2017/06/05 16:09:35
nit: DCHECK(!app_manager_)?
tbarzic
2017/06/06 01:54:55
Done.
| |
| 67 } | |
| 68 | |
| 58 void StateController::Initialize() { | 69 void StateController::Initialize() { |
| 59 // The tray action ptr might be set previously if the client was being created | 70 // The tray action ptr might be set previously if the client was being created |
| 60 // for testing. | 71 // for testing. |
| 61 if (!tray_action_ptr_) { | 72 if (!tray_action_ptr_) { |
| 62 service_manager::Connector* connector = | 73 service_manager::Connector* connector = |
| 63 content::ServiceManagerConnection::GetForProcess()->GetConnector(); | 74 content::ServiceManagerConnection::GetForProcess()->GetConnector(); |
| 64 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); | 75 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); |
| 65 } | 76 } |
| 66 tray_action_ptr_->SetClient(binding_.CreateInterfacePtrAndBind(), | 77 tray_action_ptr_->SetClient(binding_.CreateInterfacePtrAndBind(), |
| 67 lock_screen_note_state_); | 78 lock_screen_note_state_); |
| 68 } | 79 } |
| 69 | 80 |
| 81 void StateController::SetPrimaryProfile(Profile* profile) { | |
| 82 // App manager might have been set previously by a test. | |
| 83 if (!app_manager_) | |
| 84 app_manager_ = base::MakeUnique<AppManagerImpl>(); | |
| 85 app_manager_->Initialize(profile, | |
| 86 chromeos::ProfileHelper::GetSigninProfile()); | |
| 87 | |
| 88 session_observer_.Add(session_manager::SessionManager::Get()); | |
| 89 OnSessionStateChanged(); | |
| 90 } | |
| 91 | |
| 70 void StateController::AddObserver(StateObserver* observer) { | 92 void StateController::AddObserver(StateObserver* observer) { |
| 71 observers_.AddObserver(observer); | 93 observers_.AddObserver(observer); |
| 72 } | 94 } |
| 73 | 95 |
| 74 void StateController::RemoveObserver(StateObserver* observer) { | 96 void StateController::RemoveObserver(StateObserver* observer) { |
| 75 observers_.RemoveObserver(observer); | 97 observers_.RemoveObserver(observer); |
| 76 } | 98 } |
| 77 | 99 |
| 78 TrayActionState StateController::GetLockScreenNoteState() const { | 100 TrayActionState StateController::GetLockScreenNoteState() const { |
| 79 return lock_screen_note_state_; | 101 return lock_screen_note_state_; |
| 80 } | 102 } |
| 81 | 103 |
| 82 void StateController::RequestNewLockScreenNote() { | 104 void StateController::RequestNewLockScreenNote() { |
| 83 if (lock_screen_note_state_ != TrayActionState::kAvailable) { | 105 if (lock_screen_note_state_ != TrayActionState::kAvailable) |
| 106 return; | |
| 107 | |
| 108 DCHECK(app_manager_->IsNoteTakingAppAvailable()); | |
| 109 | |
| 110 // Update state to launching even if app fails to launch - this is to notify | |
| 111 // listeners that a lock screen note request was handled. | |
| 112 UpdateLockScreenNoteState(TrayActionState::kLaunching); | |
| 113 if (!app_manager_->LaunchNoteTaking()) | |
| 114 UpdateLockScreenNoteState(TrayActionState::kAvailable); | |
| 115 } | |
| 116 | |
| 117 void StateController::OnSessionStateChanged() { | |
| 118 if (!session_manager::SessionManager::Get()->IsScreenLocked()) { | |
| 119 app_manager_->Stop(); | |
| 120 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); | |
| 84 return; | 121 return; |
| 85 } | 122 } |
| 86 | 123 |
| 87 // TODO(tbarzic): Implement this properly. | 124 // base::Unretained is safe here because |app_manager_| is owned by |this|, |
| 88 UpdateLockScreenNoteState(TrayActionState::kActive); | 125 // and the callback will not be invoked after |app_manager_| goes out of |
| 126 // scope. | |
| 127 app_manager_->Start( | |
| 128 base::Bind(&StateController::OnNoteTakingAvailabilityChanged, | |
| 129 base::Unretained(this))); | |
| 130 OnNoteTakingAvailabilityChanged(); | |
| 89 } | 131 } |
| 90 | 132 |
| 91 void StateController::MoveToBackground() { | 133 void StateController::MoveToBackground() { |
| 92 if (GetLockScreenNoteState() != TrayActionState::kActive) | 134 TrayActionState state = GetLockScreenNoteState(); |
| 135 if (state != TrayActionState::kActive && state != TrayActionState::kLaunching) | |
| 93 return; | 136 return; |
| 94 UpdateLockScreenNoteState(TrayActionState::kBackground); | 137 UpdateLockScreenNoteState(TrayActionState::kBackground); |
| 95 } | 138 } |
| 96 | 139 |
| 97 void StateController::MoveToForeground() { | 140 void StateController::MoveToForeground() { |
| 98 if (GetLockScreenNoteState() != TrayActionState::kBackground) | 141 if (GetLockScreenNoteState() != TrayActionState::kBackground) |
| 99 return; | 142 return; |
| 100 UpdateLockScreenNoteState(TrayActionState::kActive); | 143 UpdateLockScreenNoteState(TrayActionState::kActive); |
| 101 } | 144 } |
| 102 | 145 |
| 103 void StateController::SetLockScreenNoteStateForTesting( | 146 void StateController::SetLockScreenNoteStateForTesting( |
| 104 ash::mojom::TrayActionState state) { | 147 ash::mojom::TrayActionState state) { |
| 105 lock_screen_note_state_ = state; | 148 lock_screen_note_state_ = state; |
| 106 } | 149 } |
| 107 | 150 |
| 151 void StateController::OnNoteTakingAvailabilityChanged() { | |
| 152 if (!app_manager_->IsNoteTakingAppAvailable()) { | |
| 153 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); | |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 if (GetLockScreenNoteState() == TrayActionState::kNotAvailable) | |
| 158 UpdateLockScreenNoteState(TrayActionState::kAvailable); | |
| 159 } | |
| 160 | |
| 108 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { | 161 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { |
| 109 const TrayActionState old_state = GetLockScreenNoteState(); | 162 const TrayActionState old_state = GetLockScreenNoteState(); |
| 110 if (old_state == state) | 163 if (old_state == state) |
| 111 return false; | 164 return false; |
| 112 | 165 |
| 113 lock_screen_note_state_ = state; | 166 lock_screen_note_state_ = state; |
| 114 NotifyLockScreenNoteStateChanged(); | 167 NotifyLockScreenNoteStateChanged(); |
| 115 return true; | 168 return true; |
| 116 } | 169 } |
| 117 | 170 |
| 118 void StateController::NotifyLockScreenNoteStateChanged() { | 171 void StateController::NotifyLockScreenNoteStateChanged() { |
| 119 for (auto& observer : observers_) | 172 for (auto& observer : observers_) |
| 120 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); | 173 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
| 121 | 174 |
| 122 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); | 175 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); |
| 123 } | 176 } |
| 124 | 177 |
| 125 } // namespace lock_screen_apps | 178 } // namespace lock_screen_apps |
| OLD | NEW |