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/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "chrome/browser/chromeos/lock_screen_apps/app_manager_impl.h" | 13 #include "chrome/browser/chromeos/lock_screen_apps/app_manager_impl.h" |
14 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 14 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chromeos/chromeos_switches.h" | 16 #include "chromeos/chromeos_switches.h" |
17 #include "components/session_manager/core/session_manager.h" | 17 #include "components/session_manager/core/session_manager.h" |
18 #include "content/public/common/service_manager_connection.h" | 18 #include "content/public/common/service_manager_connection.h" |
19 #include "extensions/browser/app_window/app_window.h" | |
20 #include "extensions/browser/app_window/native_app_window.h" | |
21 #include "extensions/common/extension.h" | |
19 #include "services/service_manager/public/cpp/connector.h" | 22 #include "services/service_manager/public/cpp/connector.h" |
20 | 23 |
21 using ash::mojom::TrayActionState; | 24 using ash::mojom::TrayActionState; |
22 | 25 |
23 namespace lock_screen_apps { | 26 namespace lock_screen_apps { |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 StateController* g_instance = nullptr; | 30 StateController* g_instance = nullptr; |
28 | 31 |
29 } // namespace | 32 } // namespace |
30 | 33 |
31 // static | 34 // static |
32 bool StateController::IsEnabled() { | 35 bool StateController::IsEnabled() { |
33 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 36 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
34 chromeos::switches::kEnableLockScreenApps); | 37 chromeos::switches::kEnableLockScreenApps); |
35 } | 38 } |
36 | 39 |
37 // static | 40 // static |
38 StateController* StateController::Get() { | 41 StateController* StateController::Get() { |
39 DCHECK(g_instance || !IsEnabled()); | 42 DCHECK(g_instance || !IsEnabled()); |
40 return g_instance; | 43 return g_instance; |
41 } | 44 } |
42 | 45 |
43 StateController::StateController() : binding_(this), session_observer_(this) { | 46 StateController::StateController() |
47 : binding_(this), app_window_observer_(this), session_observer_(this) { | |
44 DCHECK(!g_instance); | 48 DCHECK(!g_instance); |
45 DCHECK(IsEnabled()); | 49 DCHECK(IsEnabled()); |
46 | 50 |
47 g_instance = this; | 51 g_instance = this; |
48 } | 52 } |
49 | 53 |
50 StateController::~StateController() { | 54 StateController::~StateController() { |
51 DCHECK_EQ(g_instance, this); | 55 DCHECK_EQ(g_instance, this); |
52 g_instance = nullptr; | 56 g_instance = nullptr; |
53 } | 57 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 // Update state to launching even if app fails to launch - this is to notify | 117 // Update state to launching even if app fails to launch - this is to notify |
114 // listeners that a lock screen note request was handled. | 118 // listeners that a lock screen note request was handled. |
115 UpdateLockScreenNoteState(TrayActionState::kLaunching); | 119 UpdateLockScreenNoteState(TrayActionState::kLaunching); |
116 if (!app_manager_->LaunchNoteTaking()) | 120 if (!app_manager_->LaunchNoteTaking()) |
117 UpdateLockScreenNoteState(TrayActionState::kAvailable); | 121 UpdateLockScreenNoteState(TrayActionState::kAvailable); |
118 } | 122 } |
119 | 123 |
120 void StateController::OnSessionStateChanged() { | 124 void StateController::OnSessionStateChanged() { |
121 if (!session_manager::SessionManager::Get()->IsScreenLocked()) { | 125 if (!session_manager::SessionManager::Get()->IsScreenLocked()) { |
122 app_manager_->Stop(); | 126 app_manager_->Stop(); |
123 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); | 127 ResetNoteTakingWindowAndMoveToNextState(true /* close_window */); |
124 return; | 128 return; |
125 } | 129 } |
126 | 130 |
127 // base::Unretained is safe here because |app_manager_| is owned by |this|, | 131 // base::Unretained is safe here because |app_manager_| is owned by |this|, |
128 // and the callback will not be invoked after |app_manager_| goes out of | 132 // and the callback will not be invoked after |app_manager_| goes out of |
129 // scope. | 133 // scope. |
130 app_manager_->Start( | 134 app_manager_->Start( |
131 base::Bind(&StateController::OnNoteTakingAvailabilityChanged, | 135 base::Bind(&StateController::OnNoteTakingAvailabilityChanged, |
132 base::Unretained(this))); | 136 base::Unretained(this))); |
133 OnNoteTakingAvailabilityChanged(); | 137 OnNoteTakingAvailabilityChanged(); |
134 } | 138 } |
135 | 139 |
140 void StateController::OnAppWindowRemoved(extensions::AppWindow* app_window) { | |
141 if (note_app_window_ != app_window) | |
142 return; | |
143 ResetNoteTakingWindowAndMoveToNextState(false /* close_window */); | |
144 } | |
145 | |
146 extensions::AppWindow* StateController::CreateAppWindowForLockScreenAction( | |
147 content::BrowserContext* context, | |
148 const extensions::Extension* extension, | |
149 extensions::api::app_runtime::ActionType action, | |
150 std::unique_ptr<extensions::AppDelegate> app_delegate) { | |
151 if (action != extensions::api::app_runtime::ACTION_TYPE_NEW_NOTE) | |
152 return nullptr; | |
153 | |
154 if (GetLockScreenNoteState() != TrayActionState::kLaunching) | |
Devlin
2017/06/16 16:14:57
why don't we just use lock_screen_note_state_?
tbarzic
2017/06/19 05:21:23
Done.
| |
155 return nullptr; | |
156 | |
157 if (!chromeos::ProfileHelper::GetSigninProfile()->IsSameProfile( | |
158 Profile::FromBrowserContext(context))) { | |
159 return nullptr; | |
160 } | |
161 | |
162 if (!extension || app_manager_->GetNoteTakingAppId() != extension->id()) | |
163 return nullptr; | |
164 | |
165 note_app_window_ = | |
166 new extensions::AppWindow(context, app_delegate.release(), extension); | |
Devlin
2017/06/16 16:14:57
What's the lifetime management of this?
tbarzic
2017/06/19 05:21:23
the same as the windows returned by ChromeAppWindo
| |
167 app_window_observer_.Add(extensions::AppWindowRegistry::Get( | |
168 chromeos::ProfileHelper::GetSigninProfile())); | |
169 UpdateLockScreenNoteState(TrayActionState::kActive); | |
170 return note_app_window_; | |
171 } | |
172 | |
136 void StateController::MoveToBackground() { | 173 void StateController::MoveToBackground() { |
137 TrayActionState state = GetLockScreenNoteState(); | 174 if (GetLockScreenNoteState() != TrayActionState::kActive) |
138 if (state != TrayActionState::kActive && state != TrayActionState::kLaunching) | |
139 return; | 175 return; |
140 UpdateLockScreenNoteState(TrayActionState::kBackground); | 176 UpdateLockScreenNoteState(TrayActionState::kBackground); |
141 } | 177 } |
142 | 178 |
143 void StateController::MoveToForeground() { | 179 void StateController::MoveToForeground() { |
144 if (GetLockScreenNoteState() != TrayActionState::kBackground) | 180 if (GetLockScreenNoteState() != TrayActionState::kBackground) |
145 return; | 181 return; |
146 UpdateLockScreenNoteState(TrayActionState::kActive); | 182 UpdateLockScreenNoteState(TrayActionState::kActive); |
147 } | 183 } |
148 | 184 |
149 void StateController::SetLockScreenNoteStateForTesting( | |
150 ash::mojom::TrayActionState state) { | |
151 lock_screen_note_state_ = state; | |
152 } | |
153 | |
154 void StateController::OnNoteTakingAvailabilityChanged() { | 185 void StateController::OnNoteTakingAvailabilityChanged() { |
155 if (!app_manager_->IsNoteTakingAppAvailable()) { | 186 if (!app_manager_->IsNoteTakingAppAvailable() || |
156 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); | 187 (note_app_window_ && note_app_window_->GetExtension()->id() != |
188 app_manager_->GetNoteTakingAppId())) { | |
189 ResetNoteTakingWindowAndMoveToNextState(true /* close_window */); | |
157 return; | 190 return; |
158 } | 191 } |
159 | 192 |
160 if (GetLockScreenNoteState() == TrayActionState::kNotAvailable) | 193 if (GetLockScreenNoteState() == TrayActionState::kNotAvailable) |
161 UpdateLockScreenNoteState(TrayActionState::kAvailable); | 194 UpdateLockScreenNoteState(TrayActionState::kAvailable); |
162 } | 195 } |
163 | 196 |
197 void StateController::ResetNoteTakingWindowAndMoveToNextState( | |
198 bool close_window) { | |
199 app_window_observer_.RemoveAll(); | |
200 | |
201 if (note_app_window_) { | |
202 if (close_window && note_app_window_->GetBaseWindow()) | |
203 note_app_window_->GetBaseWindow()->Close(); | |
204 note_app_window_ = nullptr; | |
205 } | |
206 | |
207 UpdateLockScreenNoteState(app_manager_->IsNoteTakingAppAvailable() | |
208 ? TrayActionState::kAvailable | |
209 : TrayActionState::kNotAvailable); | |
210 } | |
211 | |
164 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { | 212 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { |
165 const TrayActionState old_state = GetLockScreenNoteState(); | 213 const TrayActionState old_state = GetLockScreenNoteState(); |
166 if (old_state == state) | 214 if (old_state == state) |
167 return false; | 215 return false; |
168 | 216 |
169 lock_screen_note_state_ = state; | 217 lock_screen_note_state_ = state; |
170 NotifyLockScreenNoteStateChanged(); | 218 NotifyLockScreenNoteStateChanged(); |
171 return true; | 219 return true; |
172 } | 220 } |
173 | 221 |
174 void StateController::NotifyLockScreenNoteStateChanged() { | 222 void StateController::NotifyLockScreenNoteStateChanged() { |
175 for (auto& observer : observers_) | 223 for (auto& observer : observers_) |
176 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); | 224 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
177 | 225 |
178 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); | 226 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); |
179 } | 227 } |
180 | 228 |
181 } // namespace lock_screen_apps | 229 } // namespace lock_screen_apps |
OLD | NEW |