| 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 "base/bind.h" |
| 8 | |
| 9 #include "ash/public/interfaces/constants.mojom.h" | |
| 10 #include "base/command_line.h" | 8 #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" |
| 11 #include "chromeos/chromeos_switches.h" | 12 #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; | |
| 16 | 13 |
| 17 namespace lock_screen_apps { | 14 namespace lock_screen_apps { |
| 18 | 15 |
| 19 namespace { | 16 namespace { |
| 20 | 17 |
| 21 StateController* g_instance = nullptr; | 18 base::LazyInstance<StateController>::Leaky g_instance = |
| 19 LAZY_INSTANCE_INITIALIZER; |
| 22 | 20 |
| 23 } // namespace | 21 } // namespace |
| 24 | 22 |
| 25 // static | 23 // static |
| 26 bool StateController::IsEnabled() { | |
| 27 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 28 chromeos::switches::kEnableLockScreenApps); | |
| 29 } | |
| 30 | |
| 31 // static | |
| 32 StateController* StateController::Get() { | 24 StateController* StateController::Get() { |
| 33 DCHECK(g_instance || !IsEnabled()); | 25 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 34 return g_instance; | 26 chromeos::switches::kEnableLockScreenApps)) { |
| 35 } | 27 return nullptr; |
| 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_); | |
| 65 } | 28 } |
| 66 tray_action_ptr_->SetClient(binding_.CreateInterfacePtrAndBind(), | 29 return g_instance.Pointer(); |
| 67 lock_screen_note_state_); | |
| 68 } | 30 } |
| 69 | 31 |
| 70 void StateController::AddObserver(StateObserver* observer) { | 32 void StateController::AddObserver(StateObserver* observer) { |
| 71 observers_.AddObserver(observer); | 33 observers_.AddObserver(observer); |
| 72 } | 34 } |
| 73 | 35 |
| 74 void StateController::RemoveObserver(StateObserver* observer) { | 36 void StateController::RemoveObserver(StateObserver* observer) { |
| 75 observers_.RemoveObserver(observer); | 37 observers_.RemoveObserver(observer); |
| 76 } | 38 } |
| 77 | 39 |
| 78 TrayActionState StateController::GetLockScreenNoteState() const { | 40 ActionState StateController::GetActionState(Action action) const { |
| 79 return lock_screen_note_state_; | 41 DCHECK_EQ(Action::kNewNote, action); |
| 42 return new_note_state_; |
| 80 } | 43 } |
| 81 | 44 |
| 82 void StateController::RequestNewLockScreenNote() { | 45 bool StateController::HandleAction(Action action) { |
| 83 if (lock_screen_note_state_ != TrayActionState::kAvailable) { | 46 DCHECK_EQ(Action::kNewNote, action); |
| 84 return; | 47 |
| 48 if (new_note_state_ != ActionState::kAvailable && |
| 49 new_note_state_ != ActionState::kHidden) { |
| 50 return false; |
| 85 } | 51 } |
| 86 | 52 |
| 87 // TODO(tbarzic): Implement this properly. | 53 // TODO(tbarzic): Implement this. |
| 88 UpdateLockScreenNoteState(TrayActionState::kActive); | 54 NOTIMPLEMENTED(); |
| 55 return true; |
| 89 } | 56 } |
| 90 | 57 |
| 91 void StateController::MoveToBackground() { | 58 void StateController::MoveToBackground() { |
| 92 UpdateLockScreenNoteState(TrayActionState::kBackground); | 59 UpdateActionState(Action::kNewNote, ActionState::kHidden); |
| 93 } | 60 } |
| 94 | 61 |
| 95 void StateController::SetLockScreenNoteStateForTesting( | 62 StateController::StateController() {} |
| 96 ash::mojom::TrayActionState state) { | |
| 97 lock_screen_note_state_ = state; | |
| 98 } | |
| 99 | 63 |
| 100 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { | 64 StateController::~StateController() {} |
| 101 const TrayActionState old_state = GetLockScreenNoteState(); | 65 |
| 102 if (old_state == state) | 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) |
| 103 return false; | 72 return false; |
| 104 | 73 |
| 105 // Action state can be moved to background only if the action is currently | 74 if (action_state == ActionState::kHidden && old_state != ActionState::kActive) |
| 106 // active. | |
| 107 if (state == TrayActionState::kBackground && | |
| 108 old_state != TrayActionState::kActive) | |
| 109 return false; | 75 return false; |
| 110 | 76 |
| 111 lock_screen_note_state_ = state; | 77 new_note_state_ = action_state; |
| 112 NotifyLockScreenNoteStateChanged(); | 78 NotifyStateChanged(Action::kNewNote); |
| 113 return true; | 79 return true; |
| 114 } | 80 } |
| 115 | 81 |
| 116 void StateController::NotifyLockScreenNoteStateChanged() { | 82 void StateController::NotifyStateChanged(Action action) { |
| 83 DCHECK_EQ(Action::kNewNote, action); |
| 84 |
| 117 for (auto& observer : observers_) | 85 for (auto& observer : observers_) |
| 118 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); | 86 observer.OnLockScreenAppsStateChanged(Action::kNewNote, new_note_state_); |
| 119 | |
| 120 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); | |
| 121 } | 87 } |
| 122 | 88 |
| 123 } // namespace lock_screen_apps | 89 } // namespace lock_screen_apps |
| OLD | NEW |