Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: chrome/browser/chromeos/lock_screen_apps/state_controller.cc

Issue 2934513003: Changes in app.window and app.runtime to support lock screen note taking (Closed)
Patch Set: split out browsertests Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 // Update state to launching even if app fails to launch - this is to notify 116 // Update state to launching even if app fails to launch - this is to notify
113 // listeners that a lock screen note request was handled. 117 // listeners that a lock screen note request was handled.
114 UpdateLockScreenNoteState(TrayActionState::kLaunching); 118 UpdateLockScreenNoteState(TrayActionState::kLaunching);
115 if (!app_manager_->LaunchNoteTaking()) 119 if (!app_manager_->LaunchNoteTaking())
116 UpdateLockScreenNoteState(TrayActionState::kAvailable); 120 UpdateLockScreenNoteState(TrayActionState::kAvailable);
117 } 121 }
118 122
119 void StateController::OnSessionStateChanged() { 123 void StateController::OnSessionStateChanged() {
120 if (!session_manager::SessionManager::Get()->IsScreenLocked()) { 124 if (!session_manager::SessionManager::Get()->IsScreenLocked()) {
121 app_manager_->Stop(); 125 app_manager_->Stop();
122 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); 126 ResetNoteTakingWindowAndMoveToNextState(true);
benwells 2017/06/13 10:30:46 Nit: true /* close_window */
tbarzic 2017/06/13 19:17:59 Done.
123 return; 127 return;
124 } 128 }
125 129
126 // base::Unretained is safe here because |app_manager_| is owned by |this|, 130 // base::Unretained is safe here because |app_manager_| is owned by |this|,
127 // and the callback will not be invoked after |app_manager_| goes out of 131 // and the callback will not be invoked after |app_manager_| goes out of
128 // scope. 132 // scope.
129 app_manager_->Start( 133 app_manager_->Start(
130 base::Bind(&StateController::OnNoteTakingAvailabilityChanged, 134 base::Bind(&StateController::OnNoteTakingAvailabilityChanged,
131 base::Unretained(this))); 135 base::Unretained(this)));
132 OnNoteTakingAvailabilityChanged(); 136 OnNoteTakingAvailabilityChanged();
133 } 137 }
134 138
139 void StateController::OnAppWindowRemoved(extensions::AppWindow* app_window) {
140 if (note_app_window_ != app_window)
141 return;
142 ResetNoteTakingWindowAndMoveToNextState(false);
benwells 2017/06/13 10:30:46 Nit: false /* close_window */
tbarzic 2017/06/13 19:17:59 Done.
143 }
144
145 bool StateController::CanCreateAppWindowForAction(
benwells 2017/06/13 10:30:46 This function name is kind of confusing. It is che
tbarzic 2017/06/13 19:17:59 So, the main purpose of this method is for ChromeA
146 content::BrowserContext* context,
147 const extensions::Extension* extension,
148 extensions::api::app_runtime::ActionType action) const {
149 if (action != extensions::api::app_runtime::ACTION_TYPE_NEW_NOTE)
150 return false;
151
152 if (GetLockScreenNoteState() != TrayActionState::kLaunching)
153 return false;
154
155 if (!chromeos::ProfileHelper::GetSigninProfile()->IsSameProfile(
156 Profile::FromBrowserContext(context))) {
157 return false;
158 }
159
160 return extension && app_manager_->GetNoteTakingAppId() == extension->id();
161 }
162
163 bool StateController::RegisterAppWindowForAction(
164 extensions::AppWindow* window,
165 extensions::api::app_runtime::ActionType action) {
benwells 2017/06/13 10:30:46 Is ACTION_TYPE_NEW_NOTE the only type of action th
tbarzic 2017/06/13 19:17:59 Currently, yes (as NEW_NOTE is the only action tha
166 if (!CanCreateAppWindowForAction(window->browser_context(),
167 window->GetExtension(), action)) {
168 return false;
169 }
170
171 note_app_window_ = window;
172 app_window_observer_.Add(extensions::AppWindowRegistry::Get(
173 chromeos::ProfileHelper::GetSigninProfile()));
174 UpdateLockScreenNoteState(TrayActionState::kActive);
175 return true;
176 }
177
135 void StateController::MoveToBackground() { 178 void StateController::MoveToBackground() {
136 TrayActionState state = GetLockScreenNoteState(); 179 if (GetLockScreenNoteState() != TrayActionState::kActive)
137 if (state != TrayActionState::kActive && state != TrayActionState::kLaunching)
138 return; 180 return;
139 UpdateLockScreenNoteState(TrayActionState::kBackground); 181 UpdateLockScreenNoteState(TrayActionState::kBackground);
140 } 182 }
141 183
142 void StateController::MoveToForeground() { 184 void StateController::MoveToForeground() {
143 if (GetLockScreenNoteState() != TrayActionState::kBackground) 185 if (GetLockScreenNoteState() != TrayActionState::kBackground)
144 return; 186 return;
145 UpdateLockScreenNoteState(TrayActionState::kActive); 187 UpdateLockScreenNoteState(TrayActionState::kActive);
146 } 188 }
147 189
148 void StateController::SetLockScreenNoteStateForTesting(
149 ash::mojom::TrayActionState state) {
150 lock_screen_note_state_ = state;
151 }
152
153 void StateController::OnNoteTakingAvailabilityChanged() { 190 void StateController::OnNoteTakingAvailabilityChanged() {
154 if (!app_manager_->IsNoteTakingAppAvailable()) { 191 if (!app_manager_->IsNoteTakingAppAvailable() ||
155 UpdateLockScreenNoteState(TrayActionState::kNotAvailable); 192 (note_app_window_ && note_app_window_->GetExtension()->id() !=
193 app_manager_->GetNoteTakingAppId())) {
194 ResetNoteTakingWindowAndMoveToNextState(true);
156 return; 195 return;
157 } 196 }
158 197
159 if (GetLockScreenNoteState() == TrayActionState::kNotAvailable) 198 if (GetLockScreenNoteState() == TrayActionState::kNotAvailable)
160 UpdateLockScreenNoteState(TrayActionState::kAvailable); 199 UpdateLockScreenNoteState(TrayActionState::kAvailable);
161 } 200 }
162 201
202 void StateController::ResetNoteTakingWindowAndMoveToNextState(
203 bool close_window) {
204 app_window_observer_.RemoveAll();
205
206 if (note_app_window_) {
207 if (close_window && note_app_window_->GetBaseWindow())
208 note_app_window_->GetBaseWindow()->Close();
209 note_app_window_ = nullptr;
210 }
211
212 UpdateLockScreenNoteState(app_manager_->IsNoteTakingAppAvailable()
213 ? TrayActionState::kAvailable
214 : TrayActionState::kNotAvailable);
215 }
216
163 bool StateController::UpdateLockScreenNoteState(TrayActionState state) { 217 bool StateController::UpdateLockScreenNoteState(TrayActionState state) {
164 const TrayActionState old_state = GetLockScreenNoteState(); 218 const TrayActionState old_state = GetLockScreenNoteState();
165 if (old_state == state) 219 if (old_state == state)
166 return false; 220 return false;
167 221
168 lock_screen_note_state_ = state; 222 lock_screen_note_state_ = state;
169 NotifyLockScreenNoteStateChanged(); 223 NotifyLockScreenNoteStateChanged();
170 return true; 224 return true;
171 } 225 }
172 226
173 void StateController::NotifyLockScreenNoteStateChanged() { 227 void StateController::NotifyLockScreenNoteStateChanged() {
174 for (auto& observer : observers_) 228 for (auto& observer : observers_)
175 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_); 229 observer.OnLockScreenNoteStateChanged(lock_screen_note_state_);
176 230
177 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_); 231 tray_action_ptr_->UpdateLockScreenNoteState(lock_screen_note_state_);
178 } 232 }
179 233
180 } // namespace lock_screen_apps 234 } // namespace lock_screen_apps
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698