OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/app_mode/startup_app_launcher.h" | 5 #include "chrome/browser/chromeos/app_mode/startup_app_launcher.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/json/json_file_value_serializer.h" | 9 #include "base/json/json_file_value_serializer.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 bool IsAppInstalled(Profile* profile, const std::string& app_id) { | 48 bool IsAppInstalled(Profile* profile, const std::string& app_id) { |
49 return extensions::ExtensionSystem::Get(profile)->extension_service()-> | 49 return extensions::ExtensionSystem::Get(profile)->extension_service()-> |
50 GetInstalledExtension(app_id); | 50 GetInstalledExtension(app_id); |
51 } | 51 } |
52 | 52 |
53 } // namespace | 53 } // namespace |
54 | 54 |
55 | 55 |
56 StartupAppLauncher::StartupAppLauncher(Profile* profile, | 56 StartupAppLauncher::StartupAppLauncher(Profile* profile, |
57 const std::string& app_id) | 57 const std::string& app_id, |
| 58 StartupAppLauncher::Delegate* delegate) |
58 : profile_(profile), | 59 : profile_(profile), |
59 app_id_(app_id), | 60 app_id_(app_id), |
| 61 delegate_(delegate), |
| 62 install_attempted_(false), |
60 ready_to_launch_(false) { | 63 ready_to_launch_(false) { |
61 DCHECK(profile_); | 64 DCHECK(profile_); |
62 DCHECK(Extension::IdIsValid(app_id_)); | 65 DCHECK(Extension::IdIsValid(app_id_)); |
63 } | 66 } |
64 | 67 |
65 StartupAppLauncher::~StartupAppLauncher() { | 68 StartupAppLauncher::~StartupAppLauncher() { |
66 // StartupAppLauncher can be deleted at anytime during the launch process | 69 // StartupAppLauncher can be deleted at anytime during the launch process |
67 // through a user bailout shortcut. | 70 // through a user bailout shortcut. |
68 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_) | 71 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_) |
69 ->RemoveObserver(this); | 72 ->RemoveObserver(this); |
70 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | |
71 } | 73 } |
72 | 74 |
73 void StartupAppLauncher::Initialize() { | 75 void StartupAppLauncher::Initialize() { |
74 DVLOG(1) << "Starting... connection = " | |
75 << net::NetworkChangeNotifier::GetConnectionType(); | |
76 StartLoadingOAuthFile(); | 76 StartLoadingOAuthFile(); |
77 } | 77 } |
78 | 78 |
79 void StartupAppLauncher::AddObserver(Observer* observer) { | 79 void StartupAppLauncher::ContinueWithNetworkReady() { |
80 observer_list_.AddObserver(observer); | 80 // Starts install if it is not started. |
81 } | 81 if (!install_attempted_) { |
82 | 82 install_attempted_ = true; |
83 void StartupAppLauncher::RemoveObserver(Observer* observer) { | 83 BeginInstall(); |
84 observer_list_.RemoveObserver(observer); | 84 } |
85 } | 85 } |
86 | 86 |
87 void StartupAppLauncher::StartLoadingOAuthFile() { | 87 void StartupAppLauncher::StartLoadingOAuthFile() { |
88 FOR_EACH_OBSERVER(Observer, observer_list_, OnLoadingOAuthFile()); | 88 delegate_->OnLoadingOAuthFile(); |
89 | 89 |
90 KioskOAuthParams* auth_params = new KioskOAuthParams(); | 90 KioskOAuthParams* auth_params = new KioskOAuthParams(); |
91 BrowserThread::PostBlockingPoolTaskAndReply( | 91 BrowserThread::PostBlockingPoolTaskAndReply( |
92 FROM_HERE, | 92 FROM_HERE, |
93 base::Bind(&StartupAppLauncher::LoadOAuthFileOnBlockingPool, | 93 base::Bind(&StartupAppLauncher::LoadOAuthFileOnBlockingPool, |
94 auth_params), | 94 auth_params), |
95 base::Bind(&StartupAppLauncher::OnOAuthFileLoaded, | 95 base::Bind(&StartupAppLauncher::OnOAuthFileLoaded, |
96 AsWeakPtr(), | 96 AsWeakPtr(), |
97 base::Owned(auth_params))); | 97 base::Owned(auth_params))); |
98 } | 98 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 auth_params_.client_id, | 130 auth_params_.client_id, |
131 auth_params_.client_secret); | 131 auth_params_.client_secret); |
132 } | 132 } |
133 | 133 |
134 // If we are restarting chrome (i.e. on crash), we need to initialize | 134 // If we are restarting chrome (i.e. on crash), we need to initialize |
135 // OAuth2TokenService as well. | 135 // OAuth2TokenService as well. |
136 InitializeTokenService(); | 136 InitializeTokenService(); |
137 } | 137 } |
138 | 138 |
139 void StartupAppLauncher::InitializeNetwork() { | 139 void StartupAppLauncher::InitializeNetwork() { |
140 FOR_EACH_OBSERVER(Observer, observer_list_, OnInitializingNetwork()); | 140 delegate_->InitializeNetwork(); |
141 | |
142 // TODO(tengs): Use NetworkStateInformer instead because it can handle | |
143 // portal and proxy detection. We will need to do some refactoring to | |
144 // make NetworkStateInformer more independent from the WebUI handlers. | |
145 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | |
146 OnNetworkChanged(net::NetworkChangeNotifier::GetConnectionType()); | |
147 } | 141 } |
148 | 142 |
149 void StartupAppLauncher::InitializeTokenService() { | 143 void StartupAppLauncher::InitializeTokenService() { |
150 FOR_EACH_OBSERVER(Observer, observer_list_, OnInitializingTokenService()); | 144 delegate_->OnInitializingTokenService(); |
151 | 145 |
152 ProfileOAuth2TokenService* profile_token_service = | 146 ProfileOAuth2TokenService* profile_token_service = |
153 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | 147 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
154 if (profile_token_service->RefreshTokenIsAvailable( | 148 if (profile_token_service->RefreshTokenIsAvailable( |
155 profile_token_service->GetPrimaryAccountId()) || | 149 profile_token_service->GetPrimaryAccountId()) || |
156 auth_params_.refresh_token.empty()) { | 150 auth_params_.refresh_token.empty()) { |
157 InitializeNetwork(); | 151 InitializeNetwork(); |
158 } else { | 152 } else { |
159 // Pass oauth2 refresh token from the auth file. | 153 // Pass oauth2 refresh token from the auth file. |
160 // TODO(zelidrag): We should probably remove this option after M27. | 154 // TODO(zelidrag): We should probably remove this option after M27. |
(...skipping 22 matching lines...) Expand all Loading... |
183 InitializeNetwork(); | 177 InitializeNetwork(); |
184 } | 178 } |
185 | 179 |
186 void StartupAppLauncher::OnRefreshTokensLoaded() { | 180 void StartupAppLauncher::OnRefreshTokensLoaded() { |
187 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_) | 181 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_) |
188 ->RemoveObserver(this); | 182 ->RemoveObserver(this); |
189 InitializeNetwork(); | 183 InitializeNetwork(); |
190 } | 184 } |
191 | 185 |
192 void StartupAppLauncher::OnLaunchSuccess() { | 186 void StartupAppLauncher::OnLaunchSuccess() { |
193 FOR_EACH_OBSERVER(Observer, observer_list_, OnLaunchSucceeded()); | 187 delegate_->OnLaunchSucceeded(); |
194 } | 188 } |
195 | 189 |
196 void StartupAppLauncher::OnLaunchFailure(KioskAppLaunchError::Error error) { | 190 void StartupAppLauncher::OnLaunchFailure(KioskAppLaunchError::Error error) { |
197 LOG(ERROR) << "App launch failed, error: " << error; | 191 LOG(ERROR) << "App launch failed, error: " << error; |
198 DCHECK_NE(KioskAppLaunchError::NONE, error); | 192 DCHECK_NE(KioskAppLaunchError::NONE, error); |
199 | 193 |
200 FOR_EACH_OBSERVER(Observer, observer_list_, OnLaunchFailed(error)); | 194 delegate_->OnLaunchFailed(error); |
201 } | 195 } |
202 | 196 |
203 void StartupAppLauncher::LaunchApp() { | 197 void StartupAppLauncher::LaunchApp() { |
204 if (!ready_to_launch_) { | 198 if (!ready_to_launch_) { |
205 NOTREACHED(); | 199 NOTREACHED(); |
206 LOG(ERROR) << "LaunchApp() called but launcher is not initialized."; | 200 LOG(ERROR) << "LaunchApp() called but launcher is not initialized."; |
207 } | 201 } |
208 | 202 |
209 const Extension* extension = extensions::ExtensionSystem::Get(profile_)-> | 203 const Extension* extension = extensions::ExtensionSystem::Get(profile_)-> |
210 extension_service()->GetInstalledExtension(app_id_); | 204 extension_service()->GetInstalledExtension(app_id_); |
(...skipping 13 matching lines...) Expand all Loading... |
224 | 218 |
225 content::NotificationService::current()->Notify( | 219 content::NotificationService::current()->Notify( |
226 chrome::NOTIFICATION_KIOSK_APP_LAUNCHED, | 220 chrome::NOTIFICATION_KIOSK_APP_LAUNCHED, |
227 content::NotificationService::AllSources(), | 221 content::NotificationService::AllSources(), |
228 content::NotificationService::NoDetails()); | 222 content::NotificationService::NoDetails()); |
229 | 223 |
230 OnLaunchSuccess(); | 224 OnLaunchSuccess(); |
231 } | 225 } |
232 | 226 |
233 void StartupAppLauncher::BeginInstall() { | 227 void StartupAppLauncher::BeginInstall() { |
234 FOR_EACH_OBSERVER(Observer, observer_list_, OnInstallingApp()); | 228 delegate_->OnInstallingApp(); |
235 | |
236 DVLOG(1) << "BeginInstall... connection = " | |
237 << net::NetworkChangeNotifier::GetConnectionType(); | |
238 | 229 |
239 if (IsAppInstalled(profile_, app_id_)) { | 230 if (IsAppInstalled(profile_, app_id_)) { |
240 OnReadyToLaunch(); | 231 OnReadyToLaunch(); |
241 return; | 232 return; |
242 } | 233 } |
243 | 234 |
244 installer_ = new WebstoreStartupInstaller( | 235 installer_ = new WebstoreStartupInstaller( |
245 app_id_, | 236 app_id_, |
246 profile_, | 237 profile_, |
247 false, | 238 false, |
(...skipping 14 matching lines...) Expand all Loading... |
262 AsWeakPtr())); | 253 AsWeakPtr())); |
263 return; | 254 return; |
264 } | 255 } |
265 | 256 |
266 LOG(ERROR) << "App install failed: " << error; | 257 LOG(ERROR) << "App install failed: " << error; |
267 OnLaunchFailure(KioskAppLaunchError::UNABLE_TO_INSTALL); | 258 OnLaunchFailure(KioskAppLaunchError::UNABLE_TO_INSTALL); |
268 } | 259 } |
269 | 260 |
270 void StartupAppLauncher::OnReadyToLaunch() { | 261 void StartupAppLauncher::OnReadyToLaunch() { |
271 ready_to_launch_ = true; | 262 ready_to_launch_ = true; |
272 FOR_EACH_OBSERVER(Observer, observer_list_, OnReadyToLaunch()); | 263 delegate_->OnReadyToLaunch(); |
273 } | |
274 | |
275 void StartupAppLauncher::OnNetworkChanged( | |
276 net::NetworkChangeNotifier::ConnectionType type) { | |
277 DVLOG(1) << "OnNetworkChanged... connection = " | |
278 << net::NetworkChangeNotifier::GetConnectionType(); | |
279 if (!net::NetworkChangeNotifier::IsOffline()) { | |
280 DVLOG(1) << "Network up and running!"; | |
281 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | |
282 | |
283 BeginInstall(); | |
284 } else { | |
285 DVLOG(1) << "Network not running yet!"; | |
286 } | |
287 } | 264 } |
288 | 265 |
289 } // namespace chromeos | 266 } // namespace chromeos |
OLD | NEW |