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 DCHECK(!app_manager_); |
| 67 app_manager_ = std::move(app_manager); |
| 68 } |
| 69 |
58 void StateController::Initialize() { | 70 void StateController::Initialize() { |
59 // The tray action ptr might be set previously if the client was being created | 71 // The tray action ptr might be set previously if the client was being created |
60 // for testing. | 72 // for testing. |
61 if (!tray_action_ptr_) { | 73 if (!tray_action_ptr_) { |
62 service_manager::Connector* connector = | 74 service_manager::Connector* connector = |
63 content::ServiceManagerConnection::GetForProcess()->GetConnector(); | 75 content::ServiceManagerConnection::GetForProcess()->GetConnector(); |
64 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); | 76 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); |
65 } | 77 } |
66 ash::mojom::TrayActionClientPtr client; | 78 ash::mojom::TrayActionClientPtr client; |
67 binding_.Bind(mojo::MakeRequest(&client)); | 79 binding_.Bind(mojo::MakeRequest(&client)); |
68 tray_action_ptr_->SetClient(std::move(client), lock_screen_note_state_); | 80 tray_action_ptr_->SetClient(std::move(client), lock_screen_note_state_); |
69 } | 81 } |
70 | 82 |
| 83 void StateController::SetPrimaryProfile(Profile* profile) { |
| 84 // App manager might have been set previously by a test. |
| 85 if (!app_manager_) |
| 86 app_manager_ = base::MakeUnique<AppManagerImpl>(); |
| 87 app_manager_->Initialize( |
| 88 profile, |
| 89 chromeos::ProfileHelper::GetSigninProfile()->GetOriginalProfile()); |
| 90 |
| 91 session_observer_.Add(session_manager::SessionManager::Get()); |
| 92 OnSessionStateChanged(); |
| 93 } |
| 94 |
71 void StateController::AddObserver(StateObserver* observer) { | 95 void StateController::AddObserver(StateObserver* observer) { |
72 observers_.AddObserver(observer); | 96 observers_.AddObserver(observer); |
73 } | 97 } |
74 | 98 |
75 void StateController::RemoveObserver(StateObserver* observer) { | 99 void StateController::RemoveObserver(StateObserver* observer) { |
76 observers_.RemoveObserver(observer); | 100 observers_.RemoveObserver(observer); |
77 } | 101 } |
78 | 102 |
79 TrayActionState StateController::GetLockScreenNoteState() const { | 103 TrayActionState StateController::GetLockScreenNoteState() const { |
80 return lock_screen_note_state_; | 104 return lock_screen_note_state_; |
81 } | 105 } |
82 | 106 |
83 void StateController::RequestNewLockScreenNote() { | 107 void StateController::RequestNewLockScreenNote() { |
84 if (lock_screen_note_state_ != TrayActionState::kAvailable) { | 108 if (lock_screen_note_state_ != TrayActionState::kAvailable) |
| 109 return; |
| 110 |
| 111 DCHECK(app_manager_->IsNoteTakingAppAvailable()); |
| 112 |
| 113 // Update state to launching even if app fails to launch - this is to notify |
| 114 // listeners that a lock screen note request was handled. |
| 115 UpdateLockScreenNoteState(TrayActionState::kLaunching); |
| 116 if (!app_manager_->LaunchNoteTaking()) |
| 117 UpdateLockScreenNoteState(TrayActionState::kAvailable); |
| 118 } |
| 119 |
| 120 void StateController::OnSessionStateChanged() { |
| 121 if (!session_manager::SessionManager::Get()->IsScreenLocked()) { |
| 122 app_manager_->Stop(); |
| 123 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); |
85 return; | 124 return; |
86 } | 125 } |
87 | 126 |
88 // TODO(tbarzic): Implement this properly. | 127 // base::Unretained is safe here because |app_manager_| is owned by |this|, |
89 UpdateLockScreenNoteState(TrayActionState::kActive); | 128 // and the callback will not be invoked after |app_manager_| goes out of |
| 129 // scope. |
| 130 app_manager_->Start( |
| 131 base::Bind(&StateController::OnNoteTakingAvailabilityChanged, |
| 132 base::Unretained(this))); |
| 133 OnNoteTakingAvailabilityChanged(); |
90 } | 134 } |
91 | 135 |
92 void StateController::MoveToBackground() { | 136 void StateController::MoveToBackground() { |
93 if (GetLockScreenNoteState() != TrayActionState::kActive) | 137 TrayActionState state = GetLockScreenNoteState(); |
| 138 if (state != TrayActionState::kActive && state != TrayActionState::kLaunching) |
94 return; | 139 return; |
95 UpdateLockScreenNoteState(TrayActionState::kBackground); | 140 UpdateLockScreenNoteState(TrayActionState::kBackground); |
96 } | 141 } |
97 | 142 |
98 void StateController::MoveToForeground() { | 143 void StateController::MoveToForeground() { |
99 if (GetLockScreenNoteState() != TrayActionState::kBackground) | 144 if (GetLockScreenNoteState() != TrayActionState::kBackground) |
100 return; | 145 return; |
101 UpdateLockScreenNoteState(TrayActionState::kActive); | 146 UpdateLockScreenNoteState(TrayActionState::kActive); |
102 } | 147 } |
103 | 148 |
104 void StateController::SetLockScreenNoteStateForTesting( | 149 void StateController::SetLockScreenNoteStateForTesting( |
105 ash::mojom::TrayActionState state) { | 150 ash::mojom::TrayActionState state) { |
106 lock_screen_note_state_ = state; | 151 lock_screen_note_state_ = state; |
107 } | 152 } |
108 | 153 |
| 154 void StateController::OnNoteTakingAvailabilityChanged() { |
| 155 if (!app_manager_->IsNoteTakingAppAvailable()) { |
| 156 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); |
| 157 return; |
| 158 } |
| 159 |
| 160 if (GetLockScreenNoteState() == TrayActionState::kNotAvailable) |
| 161 UpdateLockScreenNoteState(TrayActionState::kAvailable); |
| 162 } |
| 163 |
109 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { | 164 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { |
110 const TrayActionState old_state = GetLockScreenNoteState(); | 165 const TrayActionState old_state = GetLockScreenNoteState(); |
111 if (old_state == state) | 166 if (old_state == state) |
112 return false; | 167 return false; |
113 | 168 |
114 lock_screen_note_state_ = state; | 169 lock_screen_note_state_ = state; |
115 NotifyLockScreenNoteStateChanged(); | 170 NotifyLockScreenNoteStateChanged(); |
116 return true; | 171 return true; |
117 } | 172 } |
118 | 173 |
119 void StateController::NotifyLockScreenNoteStateChanged() { | 174 void StateController::NotifyLockScreenNoteStateChanged() { |
120 for (auto& observer : observers_) | 175 for (auto& observer : observers_) |
121 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); | 176 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
122 | 177 |
123 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); | 178 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); |
124 } | 179 } |
125 | 180 |
126 } // namespace lock_screen_apps | 181 } // namespace lock_screen_apps |
OLD | NEW |