13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// found in the LICENSE file.
43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/chromeos/app_mode/app_launch_utils.h"
63551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/timer/timer.h"
83551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
93551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/chromeos/app_mode/startup_app_launcher.h"
103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/lifetime/application_lifetime.h"
113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)namespace chromeos {
133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// A simple manager for the app launch that starts the launch
153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// and deletes itself when the launch finishes. On launch failure,
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// it exits the browser process.
17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class AppLaunchManager : public StartupAppLauncher::Delegate {
183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  AppLaunchManager(Profile* profile, const std::string& app_id)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : startup_app_launcher_(
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            new StartupAppLauncher(profile,
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   app_id,
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   false /* diagnostic_mode */,
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   this)) {}
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  void Start() {
2758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    startup_app_launcher_->Initialize();
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) private:
313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual ~AppLaunchManager() {}
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  void Cleanup() { delete this; }
343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // StartupAppLauncher::Delegate overrides:
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void InitializeNetwork() OVERRIDE {
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // This is on crash-restart path and assumes network is online.
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // TODO(xiyuan): Remove the crash-restart path for kiosk or add proper
39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // network configure handling.
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    startup_app_launcher_->ContinueWithNetworkReady();
4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool IsNetworkReady() OVERRIDE {
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // See comments above. Network is assumed to be online here.
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void OnLoadingOAuthFile() OVERRIDE {}
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void OnInitializingTokenService() OVERRIDE {}
48a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void OnInstallingApp() OVERRIDE {}
4958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual void OnReadyToLaunch() OVERRIDE {
5058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    startup_app_launcher_->LaunchApp();
5158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void OnLaunchSucceeded() OVERRIDE { Cleanup(); }
533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void OnLaunchFailed(KioskAppLaunchError::Error error) OVERRIDE {
543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    KioskAppLaunchError::Save(error);
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    chrome::AttemptUserExit();
563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    Cleanup();
573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual bool IsShowingNetworkConfigScreen() OVERRIDE { return false; }
593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_ptr<StartupAppLauncher> startup_app_launcher_;
613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AppLaunchManager);
633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void LaunchAppOrDie(Profile* profile, const std::string& app_id) {
663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // AppLaunchManager manages its own lifetime.
673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  (new AppLaunchManager(profile, app_id))->Start();
683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}  // namespace chromeos
71