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 #ifndef CHROME_BROWSER_CHROMEOS_APP_MODE_STARTUP_APP_LAUNCHER_H_
      6 #define CHROME_BROWSER_CHROMEOS_APP_MODE_STARTUP_APP_LAUNCHER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
     14 #include "google_apis/gaia/oauth2_token_service.h"
     15 
     16 class Profile;
     17 
     18 namespace extensions {
     19 class WebstoreStandaloneInstaller;
     20 }
     21 
     22 namespace chromeos {
     23 
     24 // Launches the app at startup. The flow roughly looks like this:
     25 // First call Initialize():
     26 // - Attempts to load oauth token file. Stores the loaded tokens in
     27 //   |auth_params_|.
     28 // - Initialize token service and inject |auth_params_| if needed.
     29 // - Initialize network if app is not installed or not offline_enabled.
     30 // - If network is online, install or update the app as needed.
     31 // - After the app is installed/updated, launch it and finish the flow;
     32 // Report OnLauncherInitialized() or OnLaunchFailed() to observers:
     33 // - If all goes good, launches the app and finish the flow;
     34 class StartupAppLauncher
     35     : public base::SupportsWeakPtr<StartupAppLauncher>,
     36       public OAuth2TokenService::Observer {
     37  public:
     38   class Delegate {
     39    public:
     40     // Invoked to perform actual network initialization work. Note the app
     41     // launch flow is paused until ContinueWithNetworkReady is called.
     42     virtual void InitializeNetwork() = 0;
     43 
     44     // Returns true if Internet is online.
     45     virtual bool IsNetworkReady() = 0;
     46 
     47     virtual void OnLoadingOAuthFile() = 0;
     48     virtual void OnInitializingTokenService() = 0;
     49     virtual void OnInstallingApp() = 0;
     50     virtual void OnReadyToLaunch() = 0;
     51     virtual void OnLaunchSucceeded() = 0;
     52     virtual void OnLaunchFailed(KioskAppLaunchError::Error error) = 0;
     53     virtual bool IsShowingNetworkConfigScreen() = 0;
     54 
     55    protected:
     56     virtual ~Delegate() {}
     57   };
     58 
     59   StartupAppLauncher(Profile* profile,
     60                      const std::string& app_id,
     61                      bool diagnostic_mode,
     62                      Delegate* delegate);
     63 
     64   virtual ~StartupAppLauncher();
     65 
     66   // Prepares the environment for an app launch.
     67   void Initialize();
     68 
     69   // Continues the initialization after network is ready.
     70   void ContinueWithNetworkReady();
     71 
     72   // Launches the app after the initialization is successful.
     73   void LaunchApp();
     74 
     75   // Restarts launcher;
     76   void RestartLauncher();
     77 
     78  private:
     79   // OAuth parameters from /home/chronos/kiosk_auth file.
     80   struct KioskOAuthParams {
     81     std::string refresh_token;
     82     std::string client_id;
     83     std::string client_secret;
     84   };
     85 
     86   void OnLaunchSuccess();
     87   void OnLaunchFailure(KioskAppLaunchError::Error error);
     88 
     89   void MaybeInstall();
     90 
     91   // Callbacks from ExtensionUpdater.
     92   void OnUpdateCheckFinished();
     93 
     94   void BeginInstall();
     95   void InstallCallback(bool success, const std::string& error);
     96   void OnReadyToLaunch();
     97   void UpdateAppData();
     98 
     99   void InitializeTokenService();
    100   void MaybeInitializeNetwork();
    101 
    102   void StartLoadingOAuthFile();
    103   static void LoadOAuthFileOnBlockingPool(KioskOAuthParams* auth_params);
    104   void OnOAuthFileLoaded(KioskOAuthParams* auth_params);
    105 
    106   // OAuth2TokenService::Observer overrides.
    107   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
    108   virtual void OnRefreshTokensLoaded() OVERRIDE;
    109 
    110   Profile* profile_;
    111   const std::string app_id_;
    112   const bool diagnostic_mode_;
    113   Delegate* delegate_;
    114   bool network_ready_handled_;
    115   int install_attempt_;
    116   bool ready_to_launch_;
    117 
    118   scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
    119   KioskOAuthParams auth_params_;
    120 
    121   DISALLOW_COPY_AND_ASSIGN(StartupAppLauncher);
    122 };
    123 
    124 }  // namespace chromeos
    125 
    126 #endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_STARTUP_APP_LAUNCHER_H_
    127