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_(
     21             new StartupAppLauncher(profile,
     22                                    app_id,
     23                                    false /* diagnostic_mode */,
     24                                    this)) {}
     25 
     26   void Start() {
     27     startup_app_launcher_->Initialize();
     28   }
     29 
     30  private:
     31   virtual ~AppLaunchManager() {}
     32 
     33   void Cleanup() { delete this; }
     34 
     35   // StartupAppLauncher::Delegate overrides:
     36   virtual void InitializeNetwork() OVERRIDE {
     37     // This is on crash-restart path and assumes network is online.
     38     // TODO(xiyuan): Remove the crash-restart path for kiosk or add proper
     39     // network configure handling.
     40     startup_app_launcher_->ContinueWithNetworkReady();
     41   }
     42   virtual bool IsNetworkReady() OVERRIDE {
     43     // See comments above. Network is assumed to be online here.
     44     return true;
     45   }
     46   virtual void OnLoadingOAuthFile() OVERRIDE {}
     47   virtual void OnInitializingTokenService() OVERRIDE {}
     48   virtual void OnInstallingApp() OVERRIDE {}
     49   virtual void OnReadyToLaunch() OVERRIDE {
     50     startup_app_launcher_->LaunchApp();
     51   }
     52   virtual void OnLaunchSucceeded() OVERRIDE { Cleanup(); }
     53   virtual void OnLaunchFailed(KioskAppLaunchError::Error error) OVERRIDE {
     54     KioskAppLaunchError::Save(error);
     55     chrome::AttemptUserExit();
     56     Cleanup();
     57   }
     58   virtual bool IsShowingNetworkConfigScreen() OVERRIDE { return false; }
     59 
     60   scoped_ptr<StartupAppLauncher> startup_app_launcher_;
     61 
     62   DISALLOW_COPY_AND_ASSIGN(AppLaunchManager);
     63 };
     64 
     65 void LaunchAppOrDie(Profile* profile, const std::string& app_id) {
     66   // AppLaunchManager manages its own lifetime.
     67   (new AppLaunchManager(profile, app_id))->Start();
     68 }
     69 
     70 }  // namespace chromeos
     71