| 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 (lock_screen_note_state_ != TrayActionState::kLaunching) |
| 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 // The ownership of the window is passed to the caller of this method. |
| 166 note_app_window_ = |
| 167 new extensions::AppWindow(context, app_delegate.release(), extension); |
| 168 app_window_observer_.Add(extensions::AppWindowRegistry::Get( |
| 169 chromeos::ProfileHelper::GetSigninProfile())); |
| 170 UpdateLockScreenNoteState(TrayActionState::kActive); |
| 171 return note_app_window_; |
| 172 } |
| 173 |
| 136 void StateController::MoveToBackground() { | 174 void StateController::MoveToBackground() { |
| 137 TrayActionState state = GetLockScreenNoteState(); | 175 if (GetLockScreenNoteState() != TrayActionState::kActive) |
| 138 if (state != TrayActionState::kActive && state != TrayActionState::kLaunching) | |
| 139 return; | 176 return; |
| 140 UpdateLockScreenNoteState(TrayActionState::kBackground); | 177 UpdateLockScreenNoteState(TrayActionState::kBackground); |
| 141 } | 178 } |
| 142 | 179 |
| 143 void StateController::MoveToForeground() { | 180 void StateController::MoveToForeground() { |
| 144 if (GetLockScreenNoteState() != TrayActionState::kBackground) | 181 if (GetLockScreenNoteState() != TrayActionState::kBackground) |
| 145 return; | 182 return; |
| 146 UpdateLockScreenNoteState(TrayActionState::kActive); | 183 UpdateLockScreenNoteState(TrayActionState::kActive); |
| 147 } | 184 } |
| 148 | 185 |
| 149 void StateController::SetLockScreenNoteStateForTesting( | |
| 150 ash::mojom::TrayActionState state) { | |
| 151 lock_screen_note_state_ = state; | |
| 152 } | |
| 153 | |
| 154 void StateController::OnNoteTakingAvailabilityChanged() { | 186 void StateController::OnNoteTakingAvailabilityChanged() { |
| 155 if (!app_manager_->IsNoteTakingAppAvailable()) { | 187 if (!app_manager_->IsNoteTakingAppAvailable() || |
| 156 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); | 188 (note_app_window_ && note_app_window_->GetExtension()->id() != |
| 189 app_manager_->GetNoteTakingAppId())) { |
| 190 ResetNoteTakingWindowAndMoveToNextState(true /* close_window */); |
| 157 return; | 191 return; |
| 158 } | 192 } |
| 159 | 193 |
| 160 if (GetLockScreenNoteState() == TrayActionState::kNotAvailable) | 194 if (GetLockScreenNoteState() == TrayActionState::kNotAvailable) |
| 161 UpdateLockScreenNoteState(TrayActionState::kAvailable); | 195 UpdateLockScreenNoteState(TrayActionState::kAvailable); |
| 162 } | 196 } |
| 163 | 197 |
| 198 void StateController::ResetNoteTakingWindowAndMoveToNextState( |
| 199 bool close_window) { |
| 200 app_window_observer_.RemoveAll(); |
| 201 |
| 202 if (note_app_window_) { |
| 203 if (close_window && note_app_window_->GetBaseWindow()) |
| 204 note_app_window_->GetBaseWindow()->Close(); |
| 205 note_app_window_ = nullptr; |
| 206 } |
| 207 |
| 208 UpdateLockScreenNoteState(app_manager_->IsNoteTakingAppAvailable() |
| 209 ? TrayActionState::kAvailable |
| 210 : TrayActionState::kNotAvailable); |
| 211 } |
| 212 |
| 164 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { | 213 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { |
| 165 const TrayActionState old_state = GetLockScreenNoteState(); | 214 const TrayActionState old_state = GetLockScreenNoteState(); |
| 166 if (old_state == state) | 215 if (old_state == state) |
| 167 return false; | 216 return false; |
| 168 | 217 |
| 169 lock_screen_note_state_ = state; | 218 lock_screen_note_state_ = state; |
| 170 NotifyLockScreenNoteStateChanged(); | 219 NotifyLockScreenNoteStateChanged(); |
| 171 return true; | 220 return true; |
| 172 } | 221 } |
| 173 | 222 |
| 174 void StateController::NotifyLockScreenNoteStateChanged() { | 223 void StateController::NotifyLockScreenNoteStateChanged() { |
| 175 for (auto& observer : observers_) | 224 for (auto& observer : observers_) |
| 176 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); | 225 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
| 177 | 226 |
| 178 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); | 227 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); |
| 179 } | 228 } |
| 180 | 229 |
| 181 } // namespace lock_screen_apps | 230 } // namespace lock_screen_apps |
| OLD | NEW |