startup_app_launcher.h revision 3551c9c881056c480085172ff9840cab31610854
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 "base/observer_list.h"
14#include "base/timer/timer.h"
15#include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
16#include "chrome/browser/signin/oauth2_token_service.h"
17#include "net/base/network_change_notifier.h"
18
19class Profile;
20
21namespace extensions {
22class WebstoreStandaloneInstaller;
23}
24
25namespace chromeos {
26
27// Launches the app at startup. The flow roughly looks like this:
28// - Checks if the app is installed in user profile (aka app profile);
29// - If the app is installed, launch it and finish the flow;
30// - If not installed, prepare to start install by checking network online
31//   state;
32// - If network gets online in time, start to install the app from web store;
33// - If all goes good, launches the app and finish the flow;
34class StartupAppLauncher
35    : public base::SupportsWeakPtr<StartupAppLauncher>,
36      public OAuth2TokenService::Observer,
37      public net::NetworkChangeNotifier::NetworkChangeObserver {
38 public:
39  class Observer {
40   public:
41    virtual void OnLoadingOAuthFile() = 0;
42    virtual void OnInitializingTokenService() = 0;
43    virtual void OnInitializingNetwork() = 0;
44    virtual void OnNetworkWaitTimedout() = 0;
45    virtual void OnInstallingApp() = 0;
46    virtual void OnLaunchSucceeded() = 0;
47    virtual void OnLaunchFailed(KioskAppLaunchError::Error error) = 0;
48
49   protected:
50    virtual ~Observer() {}
51  };
52
53  StartupAppLauncher(Profile* profile, const std::string& app_id);
54
55  virtual ~StartupAppLauncher();
56
57  // Starts app launcher. If |skip_auth_setup| is set, we will skip
58  // TokenService initialization.
59  void Start();
60
61  // Add and remove observers for app launch procedure.
62  void AddObserver(Observer* observer);
63  void RemoveObserver(Observer* observer);
64
65 private:
66  // OAuth parameters from /home/chronos/kiosk_auth file.
67  struct KioskOAuthParams {
68    std::string refresh_token;
69    std::string client_id;
70    std::string client_secret;
71  };
72
73  void OnLaunchSuccess();
74  void OnLaunchFailure(KioskAppLaunchError::Error error);
75
76  void Launch();
77
78  void BeginInstall();
79  void InstallCallback(bool success, const std::string& error);
80
81  void InitializeTokenService();
82  void InitializeNetwork();
83
84  void OnNetworkWaitTimedout();
85
86  void StartLoadingOAuthFile();
87  static void LoadOAuthFileOnBlockingPool(KioskOAuthParams* auth_params);
88  void OnOAuthFileLoaded(KioskOAuthParams* auth_params);
89
90  // OAuth2TokenService::Observer overrides.
91  virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
92  virtual void OnRefreshTokensLoaded() OVERRIDE;
93
94  // net::NetworkChangeNotifier::NetworkChangeObserver overrides:
95  virtual void OnNetworkChanged(
96      net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
97
98  Profile* profile_;
99  const std::string app_id_;
100  ObserverList<Observer> observer_list_;
101
102  scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
103  base::OneShotTimer<StartupAppLauncher> network_wait_timer_;
104  KioskOAuthParams auth_params_;
105
106  DISALLOW_COPY_AND_ASSIGN(StartupAppLauncher);
107};
108
109}  // namespace chromeos
110
111#endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_STARTUP_APP_LAUNCHER_H_
112