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
16class Profile;
17
18namespace extensions {
19class WebstoreStandaloneInstaller;
20}
21
22namespace chromeos {
23
24// Launches the app at startup. The flow roughly looks like this:
25// First call Initialize():
26// - Checks if the app is installed in user profile (aka app profile);
27// - If the app is installed, launch it and finish the flow;
28// - If not installed, prepare to start install by checking network online
29//   state;
30// - If network gets online, start to install the app from web store;
31// Report OnLauncherInitialized() or OnLaunchFailed() to observers:
32// - If all goes good, launches the app and finish the flow;
33class StartupAppLauncher
34    : public base::SupportsWeakPtr<StartupAppLauncher>,
35      public OAuth2TokenService::Observer {
36 public:
37  class Delegate {
38   public:
39    // Invoked to perform actual network initialization work. Note the app
40    // launch flow is paused until ContinueWithNetworkReady is called.
41    virtual void InitializeNetwork() = 0;
42
43    virtual void OnLoadingOAuthFile() = 0;
44    virtual void OnInitializingTokenService() = 0;
45    virtual void OnInstallingApp() = 0;
46    virtual void OnReadyToLaunch() = 0;
47    virtual void OnLaunchSucceeded() = 0;
48    virtual void OnLaunchFailed(KioskAppLaunchError::Error error) = 0;
49
50   protected:
51    virtual ~Delegate() {}
52  };
53
54  StartupAppLauncher(Profile* profile,
55                     const std::string& app_id,
56                     Delegate* delegate);
57
58  virtual ~StartupAppLauncher();
59
60  // Prepares the environment for an app launch.
61  void Initialize();
62
63  // Continues the initialization after network is ready.
64  void ContinueWithNetworkReady();
65
66  // Launches the app after the initialization is successful.
67  void LaunchApp();
68
69 private:
70  // OAuth parameters from /home/chronos/kiosk_auth file.
71  struct KioskOAuthParams {
72    std::string refresh_token;
73    std::string client_id;
74    std::string client_secret;
75  };
76
77  void OnLaunchSuccess();
78  void OnLaunchFailure(KioskAppLaunchError::Error error);
79
80  void MaybeInstall();
81
82  // Callbacks from ExtensionUpdater.
83  void OnUpdateCheckFinished();
84
85  void BeginInstall();
86  void InstallCallback(bool success, const std::string& error);
87  void OnReadyToLaunch();
88  void UpdateAppData();
89
90  void InitializeTokenService();
91  void InitializeNetwork();
92
93  void StartLoadingOAuthFile();
94  static void LoadOAuthFileOnBlockingPool(KioskOAuthParams* auth_params);
95  void OnOAuthFileLoaded(KioskOAuthParams* auth_params);
96
97  // OAuth2TokenService::Observer overrides.
98  virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
99  virtual void OnRefreshTokensLoaded() OVERRIDE;
100
101  Profile* profile_;
102  const std::string app_id_;
103  Delegate* delegate_;
104  bool install_attempted_;
105  bool ready_to_launch_;
106
107  scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
108  KioskOAuthParams auth_params_;
109
110  DISALLOW_COPY_AND_ASSIGN(StartupAppLauncher);
111};
112
113}  // namespace chromeos
114
115#endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_STARTUP_APP_LAUNCHER_H_
116