Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / app_mode / startup_app_launcher.h
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
54    protected:
55     virtual ~Delegate() {}
56   };
57
58   StartupAppLauncher(Profile* profile,
59                      const std::string& app_id,
60                      bool diagnostic_mode,
61                      Delegate* delegate);
62
63   virtual ~StartupAppLauncher();
64
65   // Prepares the environment for an app launch.
66   void Initialize();
67
68   // Continues the initialization after network is ready.
69   void ContinueWithNetworkReady();
70
71   // Launches the app after the initialization is successful.
72   void LaunchApp();
73
74  private:
75   // OAuth parameters from /home/chronos/kiosk_auth file.
76   struct KioskOAuthParams {
77     std::string refresh_token;
78     std::string client_id;
79     std::string client_secret;
80   };
81
82   void OnLaunchSuccess();
83   void OnLaunchFailure(KioskAppLaunchError::Error error);
84
85   void MaybeInstall();
86
87   // Callbacks from ExtensionUpdater.
88   void OnUpdateCheckFinished();
89
90   void BeginInstall();
91   void InstallCallback(bool success, const std::string& error);
92   void OnReadyToLaunch();
93   void UpdateAppData();
94
95   void InitializeTokenService();
96   void MaybeInitializeNetwork();
97
98   void StartLoadingOAuthFile();
99   static void LoadOAuthFileOnBlockingPool(KioskOAuthParams* auth_params);
100   void OnOAuthFileLoaded(KioskOAuthParams* auth_params);
101
102   // OAuth2TokenService::Observer overrides.
103   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
104   virtual void OnRefreshTokensLoaded() OVERRIDE;
105
106   Profile* profile_;
107   const std::string app_id_;
108   const bool diagnostic_mode_;
109   Delegate* delegate_;
110   bool install_attempted_;
111   bool ready_to_launch_;
112
113   scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
114   KioskOAuthParams auth_params_;
115
116   DISALLOW_COPY_AND_ASSIGN(StartupAppLauncher);
117 };
118
119 }  // namespace chromeos
120
121 #endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_STARTUP_APP_LAUNCHER_H_