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 <utility> |
8 | 8 |
9 #include "ash/public/interfaces/constants.mojom.h" | 9 #include "ash/public/interfaces/constants.mojom.h" |
| 10 #include "base/bind.h" |
10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "chrome/browser/chromeos/lock_screen_apps/app_manager_impl.h" |
| 14 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
11 #include "chromeos/chromeos_switches.h" | 16 #include "chromeos/chromeos_switches.h" |
| 17 #include "components/session_manager/core/session_manager.h" |
12 #include "content/public/common/service_manager_connection.h" | 18 #include "content/public/common/service_manager_connection.h" |
13 #include "services/service_manager/public/cpp/connector.h" | 19 #include "services/service_manager/public/cpp/connector.h" |
14 | 20 |
15 using ash::mojom::TrayActionState; | 21 using ash::mojom::TrayActionState; |
16 | 22 |
17 namespace lock_screen_apps { | 23 namespace lock_screen_apps { |
18 | 24 |
19 namespace { | 25 namespace { |
20 | 26 |
21 StateController* g_instance = nullptr; | 27 StateController* g_instance = nullptr; |
22 | 28 |
23 } // namespace | 29 } // namespace |
24 | 30 |
25 // static | 31 // static |
26 bool StateController::IsEnabled() { | 32 bool StateController::IsEnabled() { |
27 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 33 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
28 chromeos::switches::kEnableLockScreenApps); | 34 chromeos::switches::kEnableLockScreenApps); |
29 } | 35 } |
30 | 36 |
31 // static | 37 // static |
32 StateController* StateController::Get() { | 38 StateController* StateController::Get() { |
33 DCHECK(g_instance || !IsEnabled()); | 39 DCHECK(g_instance || !IsEnabled()); |
34 return g_instance; | 40 return g_instance; |
35 } | 41 } |
36 | 42 |
37 StateController::StateController() : binding_(this) { | 43 StateController::StateController() : binding_(this), session_observer_(this) { |
38 DCHECK(!g_instance); | 44 DCHECK(!g_instance); |
39 DCHECK(IsEnabled()); | 45 DCHECK(IsEnabled()); |
40 | 46 |
41 g_instance = this; | 47 g_instance = this; |
42 } | 48 } |
43 | 49 |
44 StateController::~StateController() { | 50 StateController::~StateController() { |
45 DCHECK_EQ(g_instance, this); | 51 DCHECK_EQ(g_instance, this); |
46 g_instance = nullptr; | 52 g_instance = nullptr; |
47 } | 53 } |
48 | 54 |
49 void StateController::SetTrayActionPtrForTesting( | 55 void StateController::SetTrayActionPtrForTesting( |
50 ash::mojom::TrayActionPtr tray_action_ptr) { | 56 ash::mojom::TrayActionPtr tray_action_ptr) { |
51 tray_action_ptr_ = std::move(tray_action_ptr); | 57 tray_action_ptr_ = std::move(tray_action_ptr); |
52 } | 58 } |
53 | 59 |
54 void StateController::FlushTrayActionForTesting() { | 60 void StateController::FlushTrayActionForTesting() { |
55 tray_action_ptr_.FlushForTesting(); | 61 tray_action_ptr_.FlushForTesting(); |
56 } | 62 } |
57 | 63 |
| 64 void StateController::SetAppManagerForTesting( |
| 65 std::unique_ptr<AppManager> app_manager) { |
| 66 app_manager_ = std::move(app_manager); |
| 67 } |
| 68 |
58 void StateController::Initialize() { | 69 void StateController::Initialize() { |
59 // The tray action ptr might be set previously if the client was being created | 70 // The tray action ptr might be set previously if the client was being created |
60 // for testing. | 71 // for testing. |
61 if (!tray_action_ptr_) { | 72 if (!tray_action_ptr_) { |
62 service_manager::Connector* connector = | 73 service_manager::Connector* connector = |
63 content::ServiceManagerConnection::GetForProcess()->GetConnector(); | 74 content::ServiceManagerConnection::GetForProcess()->GetConnector(); |
64 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); | 75 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); |
65 } | 76 } |
66 tray_action_ptr_->SetClient(binding_.CreateInterfacePtrAndBind(), | 77 tray_action_ptr_->SetClient(binding_.CreateInterfacePtrAndBind(), |
67 lock_screen_note_state_); | 78 lock_screen_note_state_); |
68 } | 79 } |
69 | 80 |
| 81 void StateController::SetPrimaryProfile(Profile* profile) { |
| 82 // App manager might have been set previously by a test. |
| 83 if (!app_manager_) |
| 84 app_manager_ = base::MakeUnique<AppManagerImpl>(); |
| 85 app_manager_->Initialize(profile, |
| 86 chromeos::ProfileHelper::GetSigninProfile()); |
| 87 |
| 88 session_observer_.Add(session_manager::SessionManager::Get()); |
| 89 OnSessionStateChanged(); |
| 90 } |
| 91 |
70 void StateController::AddObserver(StateObserver* observer) { | 92 void StateController::AddObserver(StateObserver* observer) { |
71 observers_.AddObserver(observer); | 93 observers_.AddObserver(observer); |
72 } | 94 } |
73 | 95 |
74 void StateController::RemoveObserver(StateObserver* observer) { | 96 void StateController::RemoveObserver(StateObserver* observer) { |
75 observers_.RemoveObserver(observer); | 97 observers_.RemoveObserver(observer); |
76 } | 98 } |
77 | 99 |
78 TrayActionState StateController::GetLockScreenNoteState() const { | 100 TrayActionState StateController::GetLockScreenNoteState() const { |
79 return lock_screen_note_state_; | 101 return lock_screen_note_state_; |
80 } | 102 } |
81 | 103 |
82 void StateController::RequestNewLockScreenNote() { | 104 void StateController::RequestNewLockScreenNote() { |
83 if (lock_screen_note_state_ != TrayActionState::kAvailable) { | 105 if (lock_screen_note_state_ != TrayActionState::kAvailable) |
| 106 return; |
| 107 |
| 108 DCHECK(app_manager_->IsNoteTakingAppAvailable()); |
| 109 |
| 110 // Update state to launching even if app fails to launch - this is to notify |
| 111 // listeners that a lock screen note request was handled. |
| 112 UpdateLockScreenNoteState(TrayActionState::kLaunching); |
| 113 if (!app_manager_->LaunchNoteTaking()) |
| 114 UpdateLockScreenNoteState(TrayActionState::kAvailable); |
| 115 } |
| 116 |
| 117 void StateController::OnSessionStateChanged() { |
| 118 if (!session_manager::SessionManager::Get()->IsScreenLocked()) { |
| 119 app_manager_->Stop(); |
| 120 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); |
84 return; | 121 return; |
85 } | 122 } |
86 | 123 |
87 // TODO(tbarzic): Implement this properly. | 124 app_manager_->Start(this); |
88 UpdateLockScreenNoteState(TrayActionState::kActive); | 125 OnNoteTakingAvailabilityChanged(); |
| 126 } |
| 127 |
| 128 void StateController::OnNoteTakingAvailabilityChanged() { |
| 129 if (!app_manager_->IsNoteTakingAppAvailable()) { |
| 130 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); |
| 131 return; |
| 132 } |
| 133 |
| 134 if (GetLockScreenNoteState() == TrayActionState::kNotAvailable) |
| 135 UpdateLockScreenNoteState(TrayActionState::kAvailable); |
89 } | 136 } |
90 | 137 |
91 void StateController::MoveToBackground() { | 138 void StateController::MoveToBackground() { |
92 if (GetLockScreenNoteState() != TrayActionState::kActive) | 139 TrayActionState state = GetLockScreenNoteState(); |
| 140 if (state != TrayActionState::kActive && state != TrayActionState::kLaunching) |
93 return; | 141 return; |
94 UpdateLockScreenNoteState(TrayActionState::kBackground); | 142 UpdateLockScreenNoteState(TrayActionState::kBackground); |
95 } | 143 } |
96 | 144 |
97 void StateController::MoveToForeground() { | 145 void StateController::MoveToForeground() { |
98 if (GetLockScreenNoteState() != TrayActionState::kBackground) | 146 if (GetLockScreenNoteState() != TrayActionState::kBackground) |
99 return; | 147 return; |
100 UpdateLockScreenNoteState(TrayActionState::kActive); | 148 UpdateLockScreenNoteState(TrayActionState::kActive); |
101 } | 149 } |
102 | 150 |
(...skipping 13 matching lines...) Expand all Loading... |
116 } | 164 } |
117 | 165 |
118 void StateController::NotifyLockScreenNoteStateChanged() { | 166 void StateController::NotifyLockScreenNoteStateChanged() { |
119 for (auto& observer : observers_) | 167 for (auto& observer : observers_) |
120 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); | 168 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
121 | 169 |
122 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); | 170 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); |
123 } | 171 } |
124 | 172 |
125 } // namespace lock_screen_apps | 173 } // namespace lock_screen_apps |
OLD | NEW |