Home | History | Annotate | Download | only in app_mode
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/chromeos/app_mode/app_launch_utils.h"
      6 
      7 #include "base/timer/timer.h"
      8 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
      9 #include "chrome/browser/chromeos/app_mode/startup_app_launcher.h"
     10 #include "chrome/browser/lifetime/application_lifetime.h"
     11 
     12 namespace chromeos {
     13 
     14 // A simple manager for the app launch that starts the launch
     15 // and deletes itself when the launch finishes. On launch failure,
     16 // it exits the browser process.
     17 class AppLaunchManager : public StartupAppLauncher::Delegate {
     18  public:
     19   AppLaunchManager(Profile* profile, const std::string& app_id)
     20       : startup_app_launcher_(new StartupAppLauncher(profile, app_id, this)) {
     21   }
     22 
     23   void Start() {
     24     startup_app_launcher_->Initialize();
     25   }
     26 
     27  private:
     28   virtual ~AppLaunchManager() {}
     29 
     30   void Cleanup() { delete this; }
     31 
     32   // StartupAppLauncher::Delegate overrides:
     33   virtual void InitializeNetwork() OVERRIDE {
     34     // This is on crash-restart path and assumes network is online.
     35     // TODO(xiyuan): Remove the crash-restart path for kiosk or add proper
     36     // network configure handling.
     37     startup_app_launcher_->ContinueWithNetworkReady();
     38   }
     39   virtual void OnLoadingOAuthFile() OVERRIDE {}
     40   virtual void OnInitializingTokenService() OVERRIDE {}
     41   virtual void OnInstallingApp() OVERRIDE {}
     42   virtual void OnReadyToLaunch() OVERRIDE {
     43     startup_app_launcher_->LaunchApp();
     44   }
     45   virtual void OnLaunchSucceeded() OVERRIDE { Cleanup(); }
     46   virtual void OnLaunchFailed(KioskAppLaunchError::Error error) OVERRIDE {
     47     KioskAppLaunchError::Save(error);
     48     chrome::AttemptUserExit();
     49     Cleanup();
     50   }
     51 
     52   scoped_ptr<StartupAppLauncher> startup_app_launcher_;
     53 
     54   DISALLOW_COPY_AND_ASSIGN(AppLaunchManager);
     55 };
     56 
     57 void LaunchAppOrDie(Profile* profile, const std::string& app_id) {
     58   // AppLaunchManager manages its own lifetime.
     59   (new AppLaunchManager(profile, app_id))->Start();
     60 }
     61 
     62 }  // namespace chromeos
     63