| 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/time/time.h" | |
| 12 #include "chromeos/chromeos_switches.h" | 11 #include "chromeos/chromeos_switches.h" |
| 12 #include "content/public/common/service_manager_connection.h" |
| 13 #include "services/service_manager/public/cpp/connector.h" |
| 14 |
| 15 using ash::mojom::TrayActionState; |
| 13 | 16 |
| 14 namespace lock_screen_apps { | 17 namespace lock_screen_apps { |
| 15 | 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 base::LazyInstance<StateController>::Leaky g_instance = | 21 StateController* g_instance = nullptr; |
| 19 LAZY_INSTANCE_INITIALIZER; | |
| 20 | 22 |
| 21 } // namespace | 23 } // namespace |
| 22 | 24 |
| 23 // static | 25 // static |
| 26 bool StateController::IsEnabled() { |
| 27 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 28 chromeos::switches::kEnableLockScreenApps); |
| 29 } |
| 30 |
| 31 // static |
| 24 StateController* StateController::Get() { | 32 StateController* StateController::Get() { |
| 25 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 33 DCHECK(g_instance || !IsEnabled()); |
| 26 chromeos::switches::kEnableLockScreenApps)) { | 34 return g_instance; |
| 27 return nullptr; | 35 } |
| 36 |
| 37 StateController::StateController() : binding_(this) { |
| 38 DCHECK(!g_instance); |
| 39 DCHECK(IsEnabled()); |
| 40 |
| 41 g_instance = this; |
| 42 } |
| 43 |
| 44 StateController::~StateController() { |
| 45 DCHECK_EQ(g_instance, this); |
| 46 g_instance = nullptr; |
| 47 } |
| 48 |
| 49 void StateController::SetTrayActionPtrForTesting( |
| 50 ash::mojom::TrayActionPtr tray_action_ptr) { |
| 51 tray_action_ptr_ = std::move(tray_action_ptr); |
| 52 } |
| 53 |
| 54 void StateController::FlushTrayActionForTesting() { |
| 55 tray_action_ptr_.FlushForTesting(); |
| 56 } |
| 57 |
| 58 void StateController::Initialize() { |
| 59 // The tray action ptr might be set previously if the client was being created |
| 60 // for testing. |
| 61 if (!tray_action_ptr_) { |
| 62 service_manager::Connector* connector = |
| 63 content::ServiceManagerConnection::GetForProcess()->GetConnector(); |
| 64 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); |
| 28 } | 65 } |
| 29 return g_instance.Pointer(); | 66 tray_action_ptr_->SetClient(binding_.CreateInterfacePtrAndBind(), |
| 67 lock_screen_note_state_); |
| 30 } | 68 } |
| 31 | 69 |
| 32 void StateController::AddObserver(StateObserver* observer) { | 70 void StateController::AddObserver(StateObserver* observer) { |
| 33 observers_.AddObserver(observer); | 71 observers_.AddObserver(observer); |
| 34 } | 72 } |
| 35 | 73 |
| 36 void StateController::RemoveObserver(StateObserver* observer) { | 74 void StateController::RemoveObserver(StateObserver* observer) { |
| 37 observers_.RemoveObserver(observer); | 75 observers_.RemoveObserver(observer); |
| 38 } | 76 } |
| 39 | 77 |
| 40 ActionState StateController::GetActionState(Action action) const { | 78 TrayActionState StateController::GetLockScreenNoteState() const { |
| 41 DCHECK_EQ(Action::kNewNote, action); | 79 return lock_screen_note_state_; |
| 42 return new_note_state_; | |
| 43 } | 80 } |
| 44 | 81 |
| 45 bool StateController::HandleAction(Action action) { | 82 void StateController::RequestNewLockScreenNote() { |
| 46 DCHECK_EQ(Action::kNewNote, action); | 83 if (lock_screen_note_state_ != TrayActionState::kAvailable) { |
| 47 | 84 return; |
| 48 if (new_note_state_ != ActionState::kAvailable && | |
| 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 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { |
| 101 const TrayActionState old_state = GetLockScreenNoteState(); |
| 102 if (old_state == state) |
| 103 return false; |
| 104 |
| 105 // Action state can be moved to background only if the action is currently |
| 106 // active. |
| 107 if (state == TrayActionState::kBackground && |
| 108 old_state != TrayActionState::kActive) |
| 109 return false; |
| 110 |
| 111 lock_screen_note_state_ = state; |
| 112 NotifyLockScreenNoteStateChanged(); |
| 55 return true; | 113 return true; |
| 56 } | 114 } |
| 57 | 115 |
| 58 void StateController::MoveToBackground() { | 116 void StateController::NotifyLockScreenNoteStateChanged() { |
| 59 UpdateActionState(Action::kNewNote, ActionState::kHidden); | 117 for (auto& observer : observers_) |
| 60 } | 118 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
| 61 | 119 |
| 62 StateController::StateController() {} | 120 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 } | 121 } |
| 88 | 122 |
| 89 } // namespace lock_screen_apps | 123 } // namespace lock_screen_apps |
| OLD | NEW |