| 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/app_launch_utils.h" | 5 #include "chrome/browser/chromeos/app_mode/app_launch_utils.h" |
| 6 | 6 |
| 7 #include "base/timer/timer.h" | 7 #include "base/timer/timer.h" |
| 8 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h" | 8 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h" |
| 9 #include "chrome/browser/chromeos/app_mode/startup_app_launcher.h" | 9 #include "chrome/browser/chromeos/app_mode/startup_app_launcher.h" |
| 10 #include "chrome/browser/lifetime/application_lifetime.h" | 10 #include "chrome/browser/lifetime/application_lifetime.h" |
| 11 | 11 |
| 12 namespace chromeos { | 12 namespace chromeos { |
| 13 | 13 |
| 14 // A simple manager for the app launch that starts the launch | 14 // A simple manager for the app launch that starts the launch |
| 15 // and deletes itself when the launch finishes. On launch failure, | 15 // and deletes itself when the launch finishes. On launch failure, |
| 16 // it exits the browser process. | 16 // it exits the browser process. |
| 17 class AppLaunchManager : public StartupAppLauncher::Observer { | 17 class AppLaunchManager : public StartupAppLauncher::Delegate { |
| 18 public: | 18 public: |
| 19 AppLaunchManager(Profile* profile, const std::string& app_id) | 19 AppLaunchManager(Profile* profile, const std::string& app_id) |
| 20 : waiting_for_network_(false) { | 20 : startup_app_launcher_(new StartupAppLauncher(profile, app_id, this)) { |
| 21 startup_app_launcher_.reset(new StartupAppLauncher(profile, app_id)); | |
| 22 } | 21 } |
| 23 | 22 |
| 24 void Start() { | 23 void Start() { |
| 25 startup_app_launcher_->AddObserver(this); | |
| 26 startup_app_launcher_->Initialize(); | 24 startup_app_launcher_->Initialize(); |
| 27 } | 25 } |
| 28 | 26 |
| 29 private: | 27 private: |
| 30 virtual ~AppLaunchManager() {} | 28 virtual ~AppLaunchManager() {} |
| 31 | 29 |
| 32 void Cleanup() { delete this; } | 30 void Cleanup() { delete this; } |
| 33 | 31 |
| 34 void OnNetworkWaitTimedout() { | 32 // StartupAppLauncher::Delegate overrides: |
| 35 DCHECK(waiting_for_network_); | 33 virtual void InitializeNetwork() OVERRIDE { |
| 36 LOG(ERROR) << "Timed out while waiting for network during app launch."; | 34 // This is on crash-restart path and assumes network is online. |
| 37 OnLaunchFailed(KioskAppLaunchError::UNABLE_TO_INSTALL); | 35 // TODO(xiyuan): Remove the crash-restart path for kiosk or add proper |
| 36 // network configure handling. |
| 37 startup_app_launcher_->ContinueWithNetworkReady(); |
| 38 } | 38 } |
| 39 | |
| 40 // StartupAppLauncher::Observer overrides: | |
| 41 virtual void OnLoadingOAuthFile() OVERRIDE {} | 39 virtual void OnLoadingOAuthFile() OVERRIDE {} |
| 42 virtual void OnInitializingTokenService() OVERRIDE {} | 40 virtual void OnInitializingTokenService() OVERRIDE {} |
| 43 | 41 virtual void OnInstallingApp() OVERRIDE {} |
| 44 virtual void OnInitializingNetwork() OVERRIDE { | |
| 45 waiting_for_network_ = true; | |
| 46 const int kMaxNetworkWaitSeconds = 5 * 60; | |
| 47 network_wait_timer_.Start( | |
| 48 FROM_HERE, | |
| 49 base::TimeDelta::FromSeconds(kMaxNetworkWaitSeconds), | |
| 50 this, &AppLaunchManager::OnNetworkWaitTimedout); | |
| 51 } | |
| 52 | |
| 53 virtual void OnInstallingApp() OVERRIDE { | |
| 54 waiting_for_network_ = false; | |
| 55 network_wait_timer_.Stop(); | |
| 56 } | |
| 57 | |
| 58 virtual void OnReadyToLaunch() OVERRIDE { | 42 virtual void OnReadyToLaunch() OVERRIDE { |
| 59 startup_app_launcher_->LaunchApp(); | 43 startup_app_launcher_->LaunchApp(); |
| 60 } | 44 } |
| 61 | |
| 62 virtual void OnLaunchSucceeded() OVERRIDE { Cleanup(); } | 45 virtual void OnLaunchSucceeded() OVERRIDE { Cleanup(); } |
| 63 virtual void OnLaunchFailed(KioskAppLaunchError::Error error) OVERRIDE { | 46 virtual void OnLaunchFailed(KioskAppLaunchError::Error error) OVERRIDE { |
| 64 KioskAppLaunchError::Save(error); | 47 KioskAppLaunchError::Save(error); |
| 65 chrome::AttemptUserExit(); | 48 chrome::AttemptUserExit(); |
| 66 Cleanup(); | 49 Cleanup(); |
| 67 } | 50 } |
| 68 | 51 |
| 69 base::OneShotTimer<AppLaunchManager> network_wait_timer_; | |
| 70 bool waiting_for_network_; | |
| 71 scoped_ptr<StartupAppLauncher> startup_app_launcher_; | 52 scoped_ptr<StartupAppLauncher> startup_app_launcher_; |
| 72 | 53 |
| 73 DISALLOW_COPY_AND_ASSIGN(AppLaunchManager); | 54 DISALLOW_COPY_AND_ASSIGN(AppLaunchManager); |
| 74 }; | 55 }; |
| 75 | 56 |
| 76 void LaunchAppOrDie(Profile* profile, const std::string& app_id) { | 57 void LaunchAppOrDie(Profile* profile, const std::string& app_id) { |
| 77 // AppLaunchManager manages its own lifetime. | 58 // AppLaunchManager manages its own lifetime. |
| 78 (new AppLaunchManager(profile, app_id))->Start(); | 59 (new AppLaunchManager(profile, app_id))->Start(); |
| 79 } | 60 } |
| 80 | 61 |
| 81 } // namespace chromeos | 62 } // namespace chromeos |
| OLD | NEW |