| 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 "base/strings/string16.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/chromeos/lock_screen_apps/app_manager_impl.h" | 15 #include "chrome/browser/chromeos/lock_screen_apps/app_manager_impl.h" |
| 14 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 16 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 18 #include "chrome/common/pref_names.h" |
| 16 #include "chromeos/chromeos_switches.h" | 19 #include "chromeos/chromeos_switches.h" |
| 20 #include "components/prefs/pref_service.h" |
| 17 #include "components/session_manager/core/session_manager.h" | 21 #include "components/session_manager/core/session_manager.h" |
| 18 #include "content/public/common/service_manager_connection.h" | 22 #include "content/public/common/service_manager_connection.h" |
| 19 #include "extensions/browser/app_window/app_window.h" | 23 #include "extensions/browser/app_window/app_window.h" |
| 20 #include "extensions/browser/app_window/native_app_window.h" | 24 #include "extensions/browser/app_window/native_app_window.h" |
| 21 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
| 22 #include "services/service_manager/public/cpp/connector.h" | 26 #include "services/service_manager/public/cpp/connector.h" |
| 23 | 27 |
| 24 using ash::mojom::TrayActionState; | 28 using ash::mojom::TrayActionState; |
| 25 | 29 |
| 26 namespace lock_screen_apps { | 30 namespace lock_screen_apps { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 37 chromeos::switches::kEnableLockScreenApps); | 41 chromeos::switches::kEnableLockScreenApps); |
| 38 } | 42 } |
| 39 | 43 |
| 40 // static | 44 // static |
| 41 StateController* StateController::Get() { | 45 StateController* StateController::Get() { |
| 42 DCHECK(g_instance || !IsEnabled()); | 46 DCHECK(g_instance || !IsEnabled()); |
| 43 return g_instance; | 47 return g_instance; |
| 44 } | 48 } |
| 45 | 49 |
| 46 StateController::StateController() | 50 StateController::StateController() |
| 47 : binding_(this), app_window_observer_(this), session_observer_(this) { | 51 : binding_(this), |
| 52 app_window_observer_(this), |
| 53 session_observer_(this), |
| 54 weak_ptr_factory_(this) { |
| 48 DCHECK(!g_instance); | 55 DCHECK(!g_instance); |
| 49 DCHECK(IsEnabled()); | 56 DCHECK(IsEnabled()); |
| 50 | 57 |
| 51 g_instance = this; | 58 g_instance = this; |
| 52 } | 59 } |
| 53 | 60 |
| 54 StateController::~StateController() { | 61 StateController::~StateController() { |
| 55 DCHECK_EQ(g_instance, this); | 62 DCHECK_EQ(g_instance, this); |
| 56 g_instance = nullptr; | 63 g_instance = nullptr; |
| 57 } | 64 } |
| 58 | 65 |
| 59 void StateController::SetTrayActionPtrForTesting( | 66 void StateController::SetTrayActionPtrForTesting( |
| 60 ash::mojom::TrayActionPtr tray_action_ptr) { | 67 ash::mojom::TrayActionPtr tray_action_ptr) { |
| 61 tray_action_ptr_ = std::move(tray_action_ptr); | 68 tray_action_ptr_ = std::move(tray_action_ptr); |
| 62 } | 69 } |
| 63 | 70 |
| 64 void StateController::FlushTrayActionForTesting() { | 71 void StateController::FlushTrayActionForTesting() { |
| 65 tray_action_ptr_.FlushForTesting(); | 72 tray_action_ptr_.FlushForTesting(); |
| 66 } | 73 } |
| 67 | 74 |
| 75 void StateController::SetReadyCallbackForTesting( |
| 76 const base::Closure& ready_callback) { |
| 77 DCHECK(ready_callback_.is_null()); |
| 78 |
| 79 ready_callback_ = ready_callback; |
| 80 } |
| 81 |
| 68 void StateController::SetAppManagerForTesting( | 82 void StateController::SetAppManagerForTesting( |
| 69 std::unique_ptr<AppManager> app_manager) { | 83 std::unique_ptr<AppManager> app_manager) { |
| 70 DCHECK(!app_manager_); | 84 DCHECK(!app_manager_); |
| 71 app_manager_ = std::move(app_manager); | 85 app_manager_ = std::move(app_manager); |
| 72 } | 86 } |
| 73 | 87 |
| 74 void StateController::Initialize() { | 88 void StateController::Initialize() { |
| 75 // The tray action ptr might be set previously if the client was being created | 89 // The tray action ptr might be set previously if the client was being created |
| 76 // for testing. | 90 // for testing. |
| 77 if (!tray_action_ptr_) { | 91 if (!tray_action_ptr_) { |
| 78 service_manager::Connector* connector = | 92 service_manager::Connector* connector = |
| 79 content::ServiceManagerConnection::GetForProcess()->GetConnector(); | 93 content::ServiceManagerConnection::GetForProcess()->GetConnector(); |
| 80 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); | 94 connector->BindInterface(ash::mojom::kServiceName, &tray_action_ptr_); |
| 81 } | 95 } |
| 82 ash::mojom::TrayActionClientPtr client; | 96 ash::mojom::TrayActionClientPtr client; |
| 83 binding_.Bind(mojo::MakeRequest(&client)); | 97 binding_.Bind(mojo::MakeRequest(&client)); |
| 84 tray_action_ptr_->SetClient(std::move(client), lock_screen_note_state_); | 98 tray_action_ptr_->SetClient(std::move(client), lock_screen_note_state_); |
| 85 } | 99 } |
| 86 | 100 |
| 87 void StateController::SetPrimaryProfile(Profile* profile) { | 101 void StateController::SetPrimaryProfile(Profile* profile) { |
| 102 g_browser_process->profile_manager()->CreateProfileAsync( |
| 103 chromeos::ProfileHelper::GetLockScreenAppProfilePath(), |
| 104 base::Bind(&StateController::OnProfilesReady, |
| 105 weak_ptr_factory_.GetWeakPtr(), profile), |
| 106 base::string16() /* name */, "" /* icon_url*/, |
| 107 "" /* supervised_user_id */); |
| 108 } |
| 109 |
| 110 void StateController::OnProfilesReady(Profile* primary_profile, |
| 111 Profile* lock_screen_profile, |
| 112 Profile::CreateStatus status) { |
| 113 // Ignore CREATED status - wait for profile to be initialized before |
| 114 // continuing. |
| 115 if (status == Profile::CREATE_STATUS_CREATED) |
| 116 return; |
| 117 |
| 118 // On error, bail out - this will cause the lock screen apps to remain |
| 119 // unavailable on the device. |
| 120 if (status != Profile::CREATE_STATUS_INITIALIZED) { |
| 121 LOG(ERROR) << "Failed to create profile for lock screen apps."; |
| 122 return; |
| 123 } |
| 124 |
| 125 DCHECK(!lock_screen_profile_); |
| 126 |
| 127 lock_screen_profile_ = lock_screen_profile; |
| 128 lock_screen_profile_->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, |
| 129 true); |
| 130 |
| 88 // App manager might have been set previously by a test. | 131 // App manager might have been set previously by a test. |
| 89 if (!app_manager_) | 132 if (!app_manager_) |
| 90 app_manager_ = base::MakeUnique<AppManagerImpl>(); | 133 app_manager_ = base::MakeUnique<AppManagerImpl>(); |
| 91 app_manager_->Initialize( | 134 app_manager_->Initialize(primary_profile, lock_screen_profile); |
| 92 profile, | |
| 93 chromeos::ProfileHelper::GetSigninProfile()->GetOriginalProfile()); | |
| 94 | 135 |
| 95 session_observer_.Add(session_manager::SessionManager::Get()); | 136 session_observer_.Add(session_manager::SessionManager::Get()); |
| 96 OnSessionStateChanged(); | 137 OnSessionStateChanged(); |
| 138 |
| 139 // SessionController is fully initialized at this point. |
| 140 if (!ready_callback_.is_null()) { |
| 141 ready_callback_.Run(); |
| 142 ready_callback_.Reset(); |
| 143 } |
| 97 } | 144 } |
| 98 | 145 |
| 99 void StateController::AddObserver(StateObserver* observer) { | 146 void StateController::AddObserver(StateObserver* observer) { |
| 100 observers_.AddObserver(observer); | 147 observers_.AddObserver(observer); |
| 101 } | 148 } |
| 102 | 149 |
| 103 void StateController::RemoveObserver(StateObserver* observer) { | 150 void StateController::RemoveObserver(StateObserver* observer) { |
| 104 observers_.RemoveObserver(observer); | 151 observers_.RemoveObserver(observer); |
| 105 } | 152 } |
| 106 | 153 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 content::BrowserContext* context, | 194 content::BrowserContext* context, |
| 148 const extensions::Extension* extension, | 195 const extensions::Extension* extension, |
| 149 extensions::api::app_runtime::ActionType action, | 196 extensions::api::app_runtime::ActionType action, |
| 150 std::unique_ptr<extensions::AppDelegate> app_delegate) { | 197 std::unique_ptr<extensions::AppDelegate> app_delegate) { |
| 151 if (action != extensions::api::app_runtime::ACTION_TYPE_NEW_NOTE) | 198 if (action != extensions::api::app_runtime::ACTION_TYPE_NEW_NOTE) |
| 152 return nullptr; | 199 return nullptr; |
| 153 | 200 |
| 154 if (lock_screen_note_state_ != TrayActionState::kLaunching) | 201 if (lock_screen_note_state_ != TrayActionState::kLaunching) |
| 155 return nullptr; | 202 return nullptr; |
| 156 | 203 |
| 157 if (!chromeos::ProfileHelper::GetSigninProfile()->IsSameProfile( | 204 if (!lock_screen_profile_->IsSameProfile( |
| 158 Profile::FromBrowserContext(context))) { | 205 Profile::FromBrowserContext(context))) { |
| 159 return nullptr; | 206 return nullptr; |
| 160 } | 207 } |
| 161 | 208 |
| 162 if (!extension || app_manager_->GetNoteTakingAppId() != extension->id()) | 209 if (!extension || app_manager_->GetNoteTakingAppId() != extension->id()) |
| 163 return nullptr; | 210 return nullptr; |
| 164 | 211 |
| 165 // The ownership of the window is passed to the caller of this method. | 212 // The ownership of the window is passed to the caller of this method. |
| 166 note_app_window_ = | 213 note_app_window_ = |
| 167 new extensions::AppWindow(context, app_delegate.release(), extension); | 214 new extensions::AppWindow(context, app_delegate.release(), extension); |
| 168 app_window_observer_.Add(extensions::AppWindowRegistry::Get( | 215 app_window_observer_.Add( |
| 169 chromeos::ProfileHelper::GetSigninProfile())); | 216 extensions::AppWindowRegistry::Get(lock_screen_profile_)); |
| 170 UpdateLockScreenNoteState(TrayActionState::kActive); | 217 UpdateLockScreenNoteState(TrayActionState::kActive); |
| 171 return note_app_window_; | 218 return note_app_window_; |
| 172 } | 219 } |
| 173 | 220 |
| 174 void StateController::MoveToBackground() { | 221 void StateController::MoveToBackground() { |
| 175 if (GetLockScreenNoteState() != TrayActionState::kActive) | 222 if (GetLockScreenNoteState() != TrayActionState::kActive) |
| 176 return; | 223 return; |
| 177 UpdateLockScreenNoteState(TrayActionState::kBackground); | 224 UpdateLockScreenNoteState(TrayActionState::kBackground); |
| 178 } | 225 } |
| 179 | 226 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 } | 268 } |
| 222 | 269 |
| 223 void StateController::NotifyLockScreenNoteStateChanged() { | 270 void StateController::NotifyLockScreenNoteStateChanged() { |
| 224 for (auto& observer : observers_) | 271 for (auto& observer : observers_) |
| 225 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); | 272 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
| 226 | 273 |
| 227 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); | 274 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); |
| 228 } | 275 } |
| 229 | 276 |
| 230 } // namespace lock_screen_apps | 277 } // namespace lock_screen_apps |
| OLD | NEW |