Chromium Code Reviews| Index: chrome/browser/chromeos/lock_screen_apps/state_controller.cc |
| diff --git a/chrome/browser/chromeos/lock_screen_apps/state_controller.cc b/chrome/browser/chromeos/lock_screen_apps/state_controller.cc |
| index 12562dbf722bd7f02d1fd6db1d2d38cf4678e2af..fcacc0c05ef49416e81961acbced569b993f4bcc 100644 |
| --- a/chrome/browser/chromeos/lock_screen_apps/state_controller.cc |
| +++ b/chrome/browser/chromeos/lock_screen_apps/state_controller.cc |
| @@ -4,29 +4,69 @@ |
| #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" |
| -#include "base/bind.h" |
| +#include <utility> |
| + |
| +#include "ash/public/interfaces/constants.mojom.h" |
| #include "base/command_line.h" |
| -#include "base/location.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "base/time/time.h" |
| #include "chromeos/chromeos_switches.h" |
| +#include "content/public/common/service_manager_connection.h" |
| +#include "services/service_manager/public/cpp/connector.h" |
| + |
| +using ash::mojom::TrayActionState; |
| namespace lock_screen_apps { |
| namespace { |
| -base::LazyInstance<StateController>::Leaky g_instance = |
| - LAZY_INSTANCE_INITIALIZER; |
| +StateController* g_instance = nullptr; |
| } // namespace |
| // static |
| +bool StateController::IsEnabled() { |
| + return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + chromeos::switches::kEnableLockScreenApps); |
| +} |
| + |
| +// static |
| StateController* StateController::Get() { |
| - if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| - chromeos::switches::kEnableLockScreenApps)) { |
| - return nullptr; |
| - } |
| - return g_instance.Pointer(); |
| + CHECK(g_instance || !IsEnabled()); |
| + return g_instance; |
| +} |
| + |
| +// static |
| +std::unique_ptr<StateController> StateController::Create() { |
| + CHECK(!g_instance); |
| + CHECK(IsEnabled()); |
| + |
| + std::unique_ptr<StateController> instance(new StateController); |
| + instance->RegisterAsAshTrayActionClient(); |
| + g_instance = instance.get(); |
|
xiyuan
2017/05/04 21:19:16
nit: move this to ctor and rremove the one in Crea
tbarzic
2017/05/04 23:27:18
Done.
|
| + return instance; |
| +} |
| + |
| +// static |
| +std::unique_ptr<StateController> StateController::CreateForTesting( |
| + ash::mojom::TrayActionPtr tray_action_ptr) { |
| + DCHECK(tray_action_ptr); |
| + CHECK(!g_instance); |
| + CHECK(IsEnabled()); |
| + |
| + std::unique_ptr<StateController> instance(new StateController); |
| + instance->tray_action_ptr_ = std::move(tray_action_ptr); |
| + instance->RegisterAsAshTrayActionClient(); |
| + instance->tray_action_ptr_.FlushForTesting(); |
| + |
| + g_instance = instance.get(); |
| + return instance; |
| +} |
| + |
| +// static |
|
xiyuan
2017/05/04 21:19:16
nit: remove ?
tbarzic
2017/05/04 23:27:18
Done.
|
| +StateController::~StateController() { |
| + CHECK(g_instance); |
| + g_instance = nullptr; |
| } |
| void StateController::AddObserver(StateObserver* observer) { |
| @@ -37,53 +77,61 @@ void StateController::RemoveObserver(StateObserver* observer) { |
| observers_.RemoveObserver(observer); |
| } |
| -ActionState StateController::GetActionState(Action action) const { |
| - DCHECK_EQ(Action::kNewNote, action); |
| - return new_note_state_; |
| +TrayActionState StateController::GetLockScreenNoteState() const { |
| + return lock_screen_note_state_; |
| } |
| -bool StateController::HandleAction(Action action) { |
| - DCHECK_EQ(Action::kNewNote, action); |
| - |
| - if (new_note_state_ != ActionState::kAvailable && |
| - new_note_state_ != ActionState::kHidden) { |
| - return false; |
| +void StateController::RequestNewLockScreenNote() { |
| + if (lock_screen_note_state_ != TrayActionState::kAvailable && |
| + lock_screen_note_state_ != TrayActionState::kBackground) { |
| + return; |
| } |
| - // TODO(tbarzic): Implement this. |
| - NOTIMPLEMENTED(); |
| - return true; |
| + // TODO(tbarzic): Implement this properly. |
| + UpdateLockScreenNoteState(TrayActionState::kActive); |
| } |
| void StateController::MoveToBackground() { |
| - UpdateActionState(Action::kNewNote, ActionState::kHidden); |
| + UpdateLockScreenNoteState(TrayActionState::kBackground); |
| } |
| -StateController::StateController() {} |
| +void StateController::SetLockScreenNoteStateForTesting( |
| + ash::mojom::TrayActionState state) { |
| + lock_screen_note_state_ = state; |
| +} |
| -StateController::~StateController() {} |
| +StateController::StateController() : binding_(this) {} |
| -bool StateController::UpdateActionState(Action action, |
| - ActionState action_state) { |
| - DCHECK_EQ(Action::kNewNote, action); |
| +void StateController::RegisterAsAshTrayActionClient() { |
| + // The tray action ptr might be set previously if the client was being created |
| + // for testing. |
| + if (!tray_action_ptr_) { |
| + service_manager::Connector* connector = |
| + content::ServiceManagerConnection::GetForProcess()->GetConnector(); |
| + connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); |
| + } |
| + tray_action_ptr_->SetClient(binding_.CreateInterfacePtrAndBind()); |
| +} |
| - const ActionState old_state = GetActionState(action); |
| - if (old_state == action_state) |
| +bool StateController::UpdateLockScreenNoteState(TrayActionState state) { |
| + const TrayActionState old_state = GetLockScreenNoteState(); |
| + if (old_state == state) |
| return false; |
| - if (action_state == ActionState::kHidden && old_state != ActionState::kActive) |
| + if (state == TrayActionState::kBackground && |
|
xiyuan
2017/05/04 21:19:16
nit: comment that we only allow go to kBackground
tbarzic
2017/05/04 23:27:18
Done.
|
| + old_state != TrayActionState::kActive) |
| return false; |
| - new_note_state_ = action_state; |
| - NotifyStateChanged(Action::kNewNote); |
| + lock_screen_note_state_ = state; |
| + NotifyLockScreenNoteStateChanged(); |
| return true; |
| } |
| -void StateController::NotifyStateChanged(Action action) { |
| - DCHECK_EQ(Action::kNewNote, action); |
| - |
| +void StateController::NotifyLockScreenNoteStateChanged() { |
| for (auto& observer : observers_) |
| - observer.OnLockScreenAppsStateChanged(Action::kNewNote, new_note_state_); |
| + observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
| + |
| + tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); |
| } |
| } // namespace lock_screen_apps |