Chromium Code Reviews| 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 "" /* suporevised_user_id */); | |
|
xiyuan
2017/06/22 20:15:25
nit: suporevised_user_id -> supervised_user_id
tbarzic
2017/06/22 20:51:15
Done.
| |
| 108 } | |
| 109 | |
| 110 void StateController::OnProfilesReady(Profile* primary_profile, | |
| 111 Profile* lock_screen_profile, | |
| 112 Profile::CreateStatus status) { | |
| 113 if (status != Profile::CREATE_STATUS_INITIALIZED) { | |
| 114 if (status != Profile::CREATE_STATUS_CREATED) | |
| 115 LOG(ERROR) << "Failed to create profile for lock screen apps."; | |
| 116 return; | |
| 117 } | |
| 118 DCHECK(!lock_screen_profile_); | |
| 119 | |
| 120 lock_screen_profile_ = lock_screen_profile; | |
| 121 lock_screen_profile_->GetPrefs()->SetBoolean(prefs::kForceEphemeralProfiles, | |
| 122 true); | |
| 123 | |
| 88 // App manager might have been set previously by a test. | 124 // App manager might have been set previously by a test. |
| 89 if (!app_manager_) | 125 if (!app_manager_) |
| 90 app_manager_ = base::MakeUnique<AppManagerImpl>(); | 126 app_manager_ = base::MakeUnique<AppManagerImpl>(); |
| 91 app_manager_->Initialize( | 127 app_manager_->Initialize(primary_profile, lock_screen_profile); |
|
xiyuan
2017/06/22 20:15:25
OnProfilesReady will be called twice. First time w
tbarzic
2017/06/22 20:51:15
We already do - the method bails out early if sta
xiyuan
2017/06/22 20:52:59
Ah, missed that.
| |
| 92 profile, | |
| 93 chromeos::ProfileHelper::GetSigninProfile()->GetOriginalProfile()); | |
| 94 | 128 |
| 95 session_observer_.Add(session_manager::SessionManager::Get()); | 129 session_observer_.Add(session_manager::SessionManager::Get()); |
| 96 OnSessionStateChanged(); | 130 OnSessionStateChanged(); |
| 131 | |
| 132 // SessionController is fully initialized at this point. | |
| 133 if (!ready_callback_.is_null()) { | |
| 134 ready_callback_.Run(); | |
| 135 ready_callback_.Reset(); | |
| 136 } | |
| 97 } | 137 } |
| 98 | 138 |
| 99 void StateController::AddObserver(StateObserver* observer) { | 139 void StateController::AddObserver(StateObserver* observer) { |
| 100 observers_.AddObserver(observer); | 140 observers_.AddObserver(observer); |
| 101 } | 141 } |
| 102 | 142 |
| 103 void StateController::RemoveObserver(StateObserver* observer) { | 143 void StateController::RemoveObserver(StateObserver* observer) { |
| 104 observers_.RemoveObserver(observer); | 144 observers_.RemoveObserver(observer); |
| 105 } | 145 } |
| 106 | 146 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 content::BrowserContext* context, | 187 content::BrowserContext* context, |
| 148 const extensions::Extension* extension, | 188 const extensions::Extension* extension, |
| 149 extensions::api::app_runtime::ActionType action, | 189 extensions::api::app_runtime::ActionType action, |
| 150 std::unique_ptr<extensions::AppDelegate> app_delegate) { | 190 std::unique_ptr<extensions::AppDelegate> app_delegate) { |
| 151 if (action != extensions::api::app_runtime::ACTION_TYPE_NEW_NOTE) | 191 if (action != extensions::api::app_runtime::ACTION_TYPE_NEW_NOTE) |
| 152 return nullptr; | 192 return nullptr; |
| 153 | 193 |
| 154 if (lock_screen_note_state_ != TrayActionState::kLaunching) | 194 if (lock_screen_note_state_ != TrayActionState::kLaunching) |
| 155 return nullptr; | 195 return nullptr; |
| 156 | 196 |
| 157 if (!chromeos::ProfileHelper::GetSigninProfile()->IsSameProfile( | 197 if (!lock_screen_profile_->IsSameProfile( |
| 158 Profile::FromBrowserContext(context))) { | 198 Profile::FromBrowserContext(context))) { |
| 159 return nullptr; | 199 return nullptr; |
| 160 } | 200 } |
| 161 | 201 |
| 162 if (!extension || app_manager_->GetNoteTakingAppId() != extension->id()) | 202 if (!extension || app_manager_->GetNoteTakingAppId() != extension->id()) |
| 163 return nullptr; | 203 return nullptr; |
| 164 | 204 |
| 165 // The ownership of the window is passed to the caller of this method. | 205 // The ownership of the window is passed to the caller of this method. |
| 166 note_app_window_ = | 206 note_app_window_ = |
| 167 new extensions::AppWindow(context, app_delegate.release(), extension); | 207 new extensions::AppWindow(context, app_delegate.release(), extension); |
| 168 app_window_observer_.Add(extensions::AppWindowRegistry::Get( | 208 app_window_observer_.Add( |
| 169 chromeos::ProfileHelper::GetSigninProfile())); | 209 extensions::AppWindowRegistry::Get(lock_screen_profile_)); |
| 170 UpdateLockScreenNoteState(TrayActionState::kActive); | 210 UpdateLockScreenNoteState(TrayActionState::kActive); |
| 171 return note_app_window_; | 211 return note_app_window_; |
| 172 } | 212 } |
| 173 | 213 |
| 174 void StateController::MoveToBackground() { | 214 void StateController::MoveToBackground() { |
| 175 if (GetLockScreenNoteState() != TrayActionState::kActive) | 215 if (GetLockScreenNoteState() != TrayActionState::kActive) |
| 176 return; | 216 return; |
| 177 UpdateLockScreenNoteState(TrayActionState::kBackground); | 217 UpdateLockScreenNoteState(TrayActionState::kBackground); |
| 178 } | 218 } |
| 179 | 219 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 } | 261 } |
| 222 | 262 |
| 223 void StateController::NotifyLockScreenNoteStateChanged() { | 263 void StateController::NotifyLockScreenNoteStateChanged() { |
| 224 for (auto& observer : observers_) | 264 for (auto& observer : observers_) |
| 225 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); | 265 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); |
| 226 | 266 |
| 227 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); | 267 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); |
| 228 } | 268 } |
| 229 | 269 |
| 230 } // namespace lock_screen_apps | 270 } // namespace lock_screen_apps |
| OLD | NEW |