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/timer/timer.h"
14#include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
15#include "chrome/browser/signin/oauth2_token_service.h"
16#include "net/base/network_change_notifier.h"
17#include "ui/base/events/event_handler.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// - Starts the app launch splash screen;
29// - Checks if the app is installed in user profile (aka app profile);
30// - If the app is installed, launch it and finish the flow;
31// - If not installed, prepare to start install by checking network online
32//   state;
33// - If network gets online in time, start to install the app from web store;
34// - If all goes good, launches the app and finish the flow;
35// If anything goes wrong, it exits app mode and goes back to login screen.
36class StartupAppLauncher
37    : public base::SupportsWeakPtr<StartupAppLauncher>,
38      public OAuth2TokenService::Observer,
39      public net::NetworkChangeNotifier::NetworkChangeObserver,
40      public ui::EventHandler {
41 public:
42  StartupAppLauncher(Profile* profile, const std::string& app_id);
43
44  // Starts app launcher. If |skip_auth_setup| is set, we will skip
45  // TokenService initialization.
46  void Start();
47
48 private:
49  // OAuth parameters from /home/chronos/kiosk_auth file.
50  struct KioskOAuthParams {
51    std::string refresh_token;
52    std::string client_id;
53    std::string client_secret;
54  };
55
56  // Private dtor because this class manages its own lifetime.
57  virtual ~StartupAppLauncher();
58
59  void Cleanup();
60  void OnLaunchSuccess();
61  void OnLaunchFailure(KioskAppLaunchError::Error error);
62
63  void Launch();
64
65  void BeginInstall();
66  void InstallCallback(bool success, const std::string& error);
67
68  void InitializeTokenService();
69  void InitializeNetwork();
70
71  void OnNetworkWaitTimedout();
72
73  void StartLoadingOAuthFile();
74  static void LoadOAuthFileOnBlockingPool(KioskOAuthParams* auth_params);
75  void OnOAuthFileLoaded(KioskOAuthParams* auth_params);
76
77  // OAuth2TokenService::Observer overrides.
78  virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
79  virtual void OnRefreshTokensLoaded() OVERRIDE;
80
81  // net::NetworkChangeNotifier::NetworkChangeObserver overrides:
82  virtual void OnNetworkChanged(
83      net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
84
85  // ui::EventHandler overrides:
86  virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
87
88  Profile* profile_;
89  const std::string app_id_;
90
91  int64 launch_splash_start_time_;
92
93  scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
94  base::OneShotTimer<StartupAppLauncher> network_wait_timer_;
95  KioskOAuthParams auth_params_;
96
97  DISALLOW_COPY_AND_ASSIGN(StartupAppLauncher);
98};
99
100}  // namespace chromeos
101
102#endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_STARTUP_APP_LAUNCHER_H_
103