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; |
|
James Cook
2017/05/05 02:55:45
The way you're handling instance creation and sing
tbarzic
2017/05/05 04:46:27
This pretty much what I'm doing now, but I have th
| |
| 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()); |
|
James Cook
2017/05/05 02:55:45
Do all these really need to be CHECK or would DCHE
tbarzic
2017/05/05 04:46:27
I guess it could be dcheck
| |
| 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 return instance; | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 std::unique_ptr<StateController> StateController::CreateForTesting( | |
| 51 ash::mojom::TrayActionPtr tray_action_ptr) { | |
| 52 DCHECK(tray_action_ptr); | |
| 53 CHECK(!g_instance); | |
| 54 CHECK(IsEnabled()); | |
| 55 | |
| 56 std::unique_ptr<StateController> instance(new StateController); | |
| 57 instance->tray_action_ptr_ = std::move(tray_action_ptr); | |
| 58 instance->RegisterAsAshTrayActionClient(); | |
| 59 instance->tray_action_ptr_.FlushForTesting(); | |
| 60 return instance; | |
| 61 } | |
| 62 | |
| 63 StateController::~StateController() { | |
| 64 CHECK(g_instance); | |
| 65 g_instance = nullptr; | |
|
James Cook
2017/05/05 02:55:45
Nice that you reset the pointer at the start of th
tbarzic
2017/05/05 04:46:27
Acknowledged.
| |
| 66 tray_action_ptr_->SetClient(nullptr, TrayActionState::kNotSupported); | |
| 30 } | 67 } |
| 31 | 68 |
| 32 void StateController::AddObserver(StateObserver* observer) { | 69 void StateController::AddObserver(StateObserver* observer) { |
| 33 observers_.AddObserver(observer); | 70 observers_.AddObserver(observer); |
| 34 } | 71 } |
| 35 | 72 |
| 36 void StateController::RemoveObserver(StateObserver* observer) { | 73 void StateController::RemoveObserver(StateObserver* observer) { |
| 37 observers_.RemoveObserver(observer); | 74 observers_.RemoveObserver(observer); |
| 38 } | 75 } |
| 39 | 76 |
| 40 ActionState StateController::GetActionState(Action action) const { | 77 TrayActionState StateController::GetLockScreenNoteState() const { |
| 41 DCHECK_EQ(Action::kNewNote, action); | 78 return lock_screen_note_state_; |
| 42 return new_note_state_; | |
| 43 } | 79 } |
| 44 | 80 |
| 45 bool StateController::HandleAction(Action action) { | 81 void StateController::RequestNewLockScreenNote() { |
| 46 DCHECK_EQ(Action::kNewNote, action); | 82 if (lock_screen_note_state_ != TrayActionState::kAvailable && |
| 47 | 83 lock_screen_note_state_ != TrayActionState::kBackground) { |
| 48 if (new_note_state_ != ActionState::kAvailable && | 84 return; |
| 49 new_note_state_ != ActionState::kHidden) { | |
| 50 return false; | |
| 51 } | 85 } |
| 52 | 86 |
| 53 // TODO(tbarzic): Implement this. | 87 // TODO(tbarzic): Implement this properly. |
| 54 NOTIMPLEMENTED(); | 88 UpdateLockScreenNoteState(TrayActionState::kActive); |
| 89 } | |
| 90 | |
| 91 void StateController::MoveToBackground() { | |
| 92 UpdateLockScreenNoteState(TrayActionState::kBackground); | |
| 93 } | |
| 94 | |
| 95 void StateController::SetLockScreenNoteStateForTesting( | |
| 96 ash::mojom::TrayActionState state) { | |
| 97 lock_screen_note_state_ = state; | |
| 98 } | |
| 99 | |
| 100 StateController::StateController() : binding_(this) { | |
| 101 g_instance = this; | |
| 102 } | |
| 103 | |
| 104 void StateController::RegisterAsAshTrayActionClient() { | |
|
James Cook
2017/05/05 02:55:45
This could become the Init() method.
tbarzic
2017/05/05 04:46:27
Done.
| |
| 105 // The tray action ptr might be set previously if the client was being created | |
| 106 // for testing. | |
| 107 if (!tray_action_ptr_) { | |
| 108 service_manager::Connector* connector = | |
| 109 content::ServiceManagerConnection::GetForProcess()->GetConnector(); | |
| 110 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); | |
| 111 } | |
| 112 tray_action_ptr_->SetClient(binding_.CreateInterfacePtrAndBind(), | |
| 113 lock_screen_note_state_); | |
| 114 } | |
| 115 | |
| 116 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { | |
| 117 const TrayActionState old_state = GetLockScreenNoteState(); | |
| 118 if (old_state == state) | |
| 119 return false; | |
| 120 | |
| 121 // Action state can be moved to background only if the action is currently | |
| 122 // active. | |
| 123 if (state == TrayActionState::kBackground && | |
| 124 old_state != TrayActionState::kActive) | |
| 125 return false; | |
| 126 | |
| 127 lock_screen_note_state_ = state; | |
| 128 NotifyLockScreenNoteStateChanged(); | |
| 55 return true; | 129 return true; |
| 56 } | 130 } |
| 57 | 131 |
| 58 void StateController::MoveToBackground() { | 132 void StateController::NotifyLockScreenNoteStateChanged() { |
| 59 UpdateActionState(Action::kNewNote, ActionState::kHidden); | 133 for (auto& observer : observers_) |
| 60 } | 134 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
| 61 | 135 |
| 62 StateController::StateController() {} | 136 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 } | 137 } |
| 88 | 138 |
| 89 } // namespace lock_screen_apps | 139 } // namespace lock_screen_apps |
| OLD | NEW |