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 "base/bind.h" | 7 #include <utility> |
| 8 | |
| 9 #include "ash/public/interfaces/constants.mojom.h" | |
| 8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 9 #include "base/location.h" | |
| 10 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 12 #include "chromeos/chromeos_switches.h" | 13 #include "chromeos/chromeos_switches.h" |
| 14 #include "content/public/common/service_manager_connection.h" | |
| 15 #include "services/service_manager/public/cpp/connector.h" | |
| 16 | |
| 17 using ash::mojom::TrayActionState; | |
| 13 | 18 |
| 14 namespace lock_screen_apps { | 19 namespace lock_screen_apps { |
| 15 | 20 |
| 16 namespace { | 21 namespace { |
| 17 | 22 |
| 18 base::LazyInstance<StateController>::Leaky g_instance = | 23 StateController* g_instance = nullptr; |
| 19 LAZY_INSTANCE_INITIALIZER; | |
| 20 | 24 |
| 21 } // namespace | 25 } // namespace |
| 22 | 26 |
| 23 // static | 27 // static |
| 28 bool StateController::IsEnabled() { | |
| 29 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 30 chromeos::switches::kEnableLockScreenApps); | |
| 31 } | |
| 32 | |
| 33 // static | |
| 24 StateController* StateController::Get() { | 34 StateController* StateController::Get() { |
| 25 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 35 CHECK(g_instance || !IsEnabled()); |
| 26 chromeos::switches::kEnableLockScreenApps)) { | 36 return g_instance; |
| 27 return nullptr; | 37 } |
| 28 } | 38 |
| 29 return g_instance.Pointer(); | 39 // static |
| 40 std::unique_ptr<StateController> StateController::Create() { | |
| 41 CHECK(!g_instance); | |
| 42 CHECK(IsEnabled()); | |
| 43 | |
| 44 std::unique_ptr<StateController> instance(new StateController); | |
| 45 instance->RegisterAsAshTrayActionClient(); | |
| 46 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.
| |
| 47 return instance; | |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 std::unique_ptr<StateController> StateController::CreateForTesting( | |
| 52 ash::mojom::TrayActionPtr tray_action_ptr) { | |
| 53 DCHECK(tray_action_ptr); | |
| 54 CHECK(!g_instance); | |
| 55 CHECK(IsEnabled()); | |
| 56 | |
| 57 std::unique_ptr<StateController> instance(new StateController); | |
| 58 instance->tray_action_ptr_ = std::move(tray_action_ptr); | |
| 59 instance->RegisterAsAshTrayActionClient(); | |
| 60 instance->tray_action_ptr_.FlushForTesting(); | |
| 61 | |
| 62 g_instance = instance.get(); | |
| 63 return instance; | |
| 64 } | |
| 65 | |
| 66 // static | |
|
xiyuan
2017/05/04 21:19:16
nit: remove ?
tbarzic
2017/05/04 23:27:18
Done.
| |
| 67 StateController::~StateController() { | |
| 68 CHECK(g_instance); | |
| 69 g_instance = nullptr; | |
| 30 } | 70 } |
| 31 | 71 |
| 32 void StateController::AddObserver(StateObserver* observer) { | 72 void StateController::AddObserver(StateObserver* observer) { |
| 33 observers_.AddObserver(observer); | 73 observers_.AddObserver(observer); |
| 34 } | 74 } |
| 35 | 75 |
| 36 void StateController::RemoveObserver(StateObserver* observer) { | 76 void StateController::RemoveObserver(StateObserver* observer) { |
| 37 observers_.RemoveObserver(observer); | 77 observers_.RemoveObserver(observer); |
| 38 } | 78 } |
| 39 | 79 |
| 40 ActionState StateController::GetActionState(Action action) const { | 80 TrayActionState StateController::GetLockScreenNoteState() const { |
| 41 DCHECK_EQ(Action::kNewNote, action); | 81 return lock_screen_note_state_; |
| 42 return new_note_state_; | |
| 43 } | 82 } |
| 44 | 83 |
| 45 bool StateController::HandleAction(Action action) { | 84 void StateController::RequestNewLockScreenNote() { |
| 46 DCHECK_EQ(Action::kNewNote, action); | 85 if (lock_screen_note_state_ != TrayActionState::kAvailable && |
| 47 | 86 lock_screen_note_state_ != TrayActionState::kBackground) { |
| 48 if (new_note_state_ != ActionState::kAvailable && | 87 return; |
| 49 new_note_state_ != ActionState::kHidden) { | |
| 50 return false; | |
| 51 } | 88 } |
| 52 | 89 |
| 53 // TODO(tbarzic): Implement this. | 90 // TODO(tbarzic): Implement this properly. |
| 54 NOTIMPLEMENTED(); | 91 UpdateLockScreenNoteState(TrayActionState::kActive); |
| 92 } | |
| 93 | |
| 94 void StateController::MoveToBackground() { | |
| 95 UpdateLockScreenNoteState(TrayActionState::kBackground); | |
| 96 } | |
| 97 | |
| 98 void StateController::SetLockScreenNoteStateForTesting( | |
| 99 ash::mojom::TrayActionState state) { | |
| 100 lock_screen_note_state_ = state; | |
| 101 } | |
| 102 | |
| 103 StateController::StateController() : binding_(this) {} | |
| 104 | |
| 105 void StateController::RegisterAsAshTrayActionClient() { | |
| 106 // The tray action ptr might be set previously if the client was being created | |
| 107 // for testing. | |
| 108 if (!tray_action_ptr_) { | |
| 109 service_manager::Connector* connector = | |
| 110 content::ServiceManagerConnection::GetForProcess()->GetConnector(); | |
| 111 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); | |
| 112 } | |
| 113 tray_action_ptr_->SetClient(binding_.CreateInterfacePtrAndBind()); | |
| 114 } | |
| 115 | |
| 116 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { | |
| 117 const TrayActionState old_state = GetLockScreenNoteState(); | |
| 118 if (old_state == state) | |
| 119 return false; | |
| 120 | |
| 121 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.
| |
| 122 old_state != TrayActionState::kActive) | |
| 123 return false; | |
| 124 | |
| 125 lock_screen_note_state_ = state; | |
| 126 NotifyLockScreenNoteStateChanged(); | |
| 55 return true; | 127 return true; |
| 56 } | 128 } |
| 57 | 129 |
| 58 void StateController::MoveToBackground() { | 130 void StateController::NotifyLockScreenNoteStateChanged() { |
| 59 UpdateActionState(Action::kNewNote, ActionState::kHidden); | 131 for (auto& observer : observers_) |
| 60 } | 132 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
| 61 | 133 |
| 62 StateController::StateController() {} | 134 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); |
| 63 | |
| 64 StateController::~StateController() {} | |
| 65 | |
| 66 bool StateController::UpdateActionState(Action action, | |
| 67 ActionState action_state) { | |
| 68 DCHECK_EQ(Action::kNewNote, action); | |
| 69 | |
| 70 const ActionState old_state = GetActionState(action); | |
| 71 if (old_state == action_state) | |
| 72 return false; | |
| 73 | |
| 74 if (action_state == ActionState::kHidden && old_state != ActionState::kActive) | |
| 75 return false; | |
| 76 | |
| 77 new_note_state_ = action_state; | |
| 78 NotifyStateChanged(Action::kNewNote); | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 void StateController::NotifyStateChanged(Action action) { | |
| 83 DCHECK_EQ(Action::kNewNote, action); | |
| 84 | |
| 85 for (auto& observer : observers_) | |
| 86 observer.OnLockScreenAppsStateChanged(Action::kNewNote, new_note_state_); | |
| 87 } | 135 } |
| 88 | 136 |
| 89 } // namespace lock_screen_apps | 137 } // namespace lock_screen_apps |
| OLD | NEW |