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#ifndef CHROME_BROWSER_CHROMEOS_FIRST_RUN_DRIVE_FIRST_RUN_CONTROLLER_H_
5#define CHROME_BROWSER_CHROMEOS_FIRST_RUN_DRIVE_FIRST_RUN_CONTROLLER_H_
6
7#include "base/basictypes.h"
8#include "base/observer_list.h"
9#include "base/timer/timer.h"
10#include "chrome/browser/profiles/profile.h"
11
12namespace chromeos {
13
14class DriveWebContentsManager;
15
16// This class is responsible for kicking off the Google Drive offline
17// initialization process. There is an initial delay to avoid contention when
18// the session starts. DriveFirstRunController will manage its own lifetime and
19// destroy itself when the initialization succeeds or fails.
20class DriveFirstRunController {
21 public:
22  // This enum is used for UMA metrics. Keep the fields in the same order as
23  // the "CrosEnableDriveOfflineOutcome" enum in histograms.xml.
24  enum UMAOutcome {
25    OUTCOME_OFFLINE_ENABLED,
26    OUTCOME_WEB_CONTENTS_TIMED_OUT,
27    OUTCOME_WEB_CONTENTS_LOAD_FAILED,
28    OUTCOME_WRONG_USER_TYPE,
29    OUTCOME_APP_NOT_INSTALLED,
30    OUTCOME_BACKGROUND_PAGE_EXISTS,
31    OUTCOME_MAX,
32  };
33
34  class Observer {
35   public:
36    // Called when enabling offline mode times out. OnCompletion will be called
37    // immediately afterwards.
38    virtual void OnTimedOut() = 0;
39
40    // Called when the first run flow finishes, informing the observer of
41    // success or failure.
42    virtual void OnCompletion(bool success) = 0;
43
44   protected:
45    virtual ~Observer() {}
46  };
47
48  explicit DriveFirstRunController(Profile* profile);
49  ~DriveFirstRunController();
50
51  // Starts the process to enable offline mode for the user's Drive account.
52  void EnableOfflineMode();
53
54  // Manages observers of the first run flow.
55  void AddObserver(Observer* observer);
56  void RemoveObserver(Observer* observer);
57
58  // Set delay times for testing purposes.
59  void SetDelaysForTest(int initial_delay_secs, int timeout_secs);
60
61  // Set the app id and endpoint url for testing purposes.
62  void SetAppInfoForTest(const std::string& app_id,
63                         const std::string& endpoint_url);
64
65 private:
66  // Used as a callback to indicate whether the offline initialization
67  // succeeds or fails.
68  void OnOfflineInit(bool success, UMAOutcome outcome);
69
70  // Called when timed out waiting for offline initialization to complete.
71  void OnWebContentsTimedOut();
72
73  // Creates and shows a system notification when enable offline succeeds.
74  void ShowNotification();
75
76  // Cleans up internal state and schedules self for deletion.
77  void CleanUp();
78
79  Profile* profile_;
80  scoped_ptr<DriveWebContentsManager> web_contents_manager_;
81  base::OneShotTimer<DriveFirstRunController> web_contents_timer_;
82  base::OneShotTimer<DriveFirstRunController> initial_delay_timer_;
83  bool started_;
84  ObserverList<Observer> observer_list_;
85
86  int initial_delay_secs_;
87  int web_contents_timeout_secs_;
88  std::string drive_offline_endpoint_url_;
89  std::string drive_hosted_app_id_;
90
91  DISALLOW_COPY_AND_ASSIGN(DriveFirstRunController);
92};
93
94}  // namespace chromeos
95
96#endif  // CHROME_BROWSER_CHROMEOS_FIRST_RUN_DRIVE_FIRST_RUN_CONTROLLER_H_
97