update_screen.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright (c) 2012 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_LOGIN_SCREENS_UPDATE_SCREEN_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_UPDATE_SCREEN_H_
7
8#include <set>
9
10#include "base/callback.h"
11#include "base/compiler_specific.h"
12#include "base/gtest_prod_util.h"
13#include "base/memory/weak_ptr.h"
14#include "base/time/time.h"
15#include "base/timer/timer.h"
16#include "chrome/browser/chromeos/login/screens/update_screen_actor.h"
17#include "chrome/browser/chromeos/login/screens/wizard_screen.h"
18#include "chromeos/dbus/update_engine_client.h"
19#include "chromeos/network/portal_detector/network_portal_detector.h"
20
21namespace chromeos {
22
23class ErrorScreen;
24class ErrorScreensHistogramHelper;
25class NetworkState;
26class ScreenManager;
27class ScreenObserver;
28
29// Controller for the update screen. It does not depend on the specific
30// implementation of the screen showing (Views of WebUI based), the dependency
31// is moved to the UpdateScreenActor instead.
32class UpdateScreen: public UpdateEngineClient::Observer,
33                    public UpdateScreenActor::Delegate,
34                    public WizardScreen,
35                    public NetworkPortalDetector::Observer {
36 public:
37  UpdateScreen(ScreenObserver* screen_observer, UpdateScreenActor* actor);
38  virtual ~UpdateScreen();
39
40  static UpdateScreen* Get(ScreenManager* manager);
41
42  // Overridden from WizardScreen.
43  virtual void PrepareToShow() OVERRIDE;
44  virtual void Show() OVERRIDE;
45  virtual void Hide() OVERRIDE;
46  virtual std::string GetName() const OVERRIDE;
47
48  // UpdateScreenActor::Delegate implementation:
49  virtual void CancelUpdate() OVERRIDE;
50  virtual void OnActorDestroyed(UpdateScreenActor* actor) OVERRIDE;
51  virtual void OnConnectToNetworkRequested() OVERRIDE;
52
53  // Starts network check. Made virtual to simplify mocking.
54  virtual void StartNetworkCheck();
55
56  // Reboot check delay get/set, in seconds.
57  int reboot_check_delay() const { return reboot_check_delay_; }
58  void SetRebootCheckDelay(int seconds);
59
60  // Returns true if this instance is still active (i.e. has not been deleted).
61  static bool HasInstance(UpdateScreen* inst);
62
63  void SetIgnoreIdleStatus(bool ignore_idle_status);
64
65  enum ExitReason {
66     REASON_UPDATE_CANCELED = 0,
67     REASON_UPDATE_INIT_FAILED,
68     REASON_UPDATE_NON_CRITICAL,
69     REASON_UPDATE_ENDED
70  };
71  // Reports update results to the ScreenObserver.
72  virtual void ExitUpdate(ExitReason reason);
73
74  // UpdateEngineClient::Observer implementation:
75  virtual void UpdateStatusChanged(
76      const UpdateEngineClient::Status& status) OVERRIDE;
77
78  // NetworkPortalDetector::Observer implementation:
79  virtual void OnPortalDetectionCompleted(
80      const NetworkState* network,
81      const NetworkPortalDetector::CaptivePortalState& state) OVERRIDE;
82
83 private:
84  FRIEND_TEST_ALL_PREFIXES(UpdateScreenTest, TestBasic);
85  FRIEND_TEST_ALL_PREFIXES(UpdateScreenTest, TestUpdateAvailable);
86  FRIEND_TEST_ALL_PREFIXES(UpdateScreenTest, TestAPReselection);
87
88  enum State {
89    STATE_IDLE = 0,
90    STATE_FIRST_PORTAL_CHECK,
91    STATE_UPDATE,
92    STATE_ERROR
93  };
94
95  // Updates downloading stats (remaining time and downloading
96  // progress) on the AU screen.
97  void UpdateDownloadingStats(const UpdateEngineClient::Status& status);
98
99  // Returns true if there is critical system update that requires installation
100  // and immediate reboot.
101  bool HasCriticalUpdate();
102
103  // Timer notification handlers.
104  void OnWaitForRebootTimeElapsed();
105
106  // Checks that screen is shown, shows if not.
107  void MakeSureScreenIsShown();
108
109  // Returns an instance of the error screen.
110  ErrorScreen* GetErrorScreen();
111
112  void StartUpdateCheck();
113  void ShowErrorMessage();
114  void HideErrorMessage();
115  void UpdateErrorMessage(
116      const NetworkState* network,
117      const NetworkPortalDetector::CaptivePortalStatus status);
118  // Timer for the interval to wait for the reboot.
119  // If reboot didn't happen - ask user to reboot manually.
120  base::OneShotTimer<UpdateScreen> reboot_timer_;
121
122  // Returns a static InstanceSet.
123  typedef std::set<UpdateScreen*> InstanceSet;
124  static InstanceSet& GetInstanceSet();
125
126  // Current state of the update screen.
127  State state_;
128
129  // Time in seconds after which we decide that the device has not rebooted
130  // automatically. If reboot didn't happen during this interval, ask user to
131  // reboot device manually.
132  int reboot_check_delay_;
133
134  // True if in the process of checking for update.
135  bool is_checking_for_update_;
136  // Flag that is used to detect when update download has just started.
137  bool is_downloading_update_;
138  // If true, update deadlines are ignored.
139  // Note, this is false by default.
140  bool is_ignore_update_deadlines_;
141  // Whether the update screen is shown.
142  bool is_shown_;
143  // Ignore fist IDLE status that is sent before update screen initiated check.
144  bool ignore_idle_status_;
145
146  // Keeps actor which is delegated with all showing operations.
147  UpdateScreenActor* actor_;
148
149  // Time of the first notification from the downloading stage.
150  base::Time download_start_time_;
151  double download_start_progress_;
152
153  // Time of the last notification from the downloading stage.
154  base::Time download_last_time_;
155  double download_last_progress_;
156
157  bool is_download_average_speed_computed_;
158  double download_average_speed_;
159
160  // True if there was no notification from NetworkPortalDetector
161  // about state for the default network.
162  bool is_first_detection_notification_;
163
164  // True if there was no notification about captive portal state for
165  // the default network.
166  bool is_first_portal_notification_;
167
168  scoped_ptr<ErrorScreensHistogramHelper> histogram_helper_;
169
170  base::WeakPtrFactory<UpdateScreen> weak_factory_;
171
172  DISALLOW_COPY_AND_ASSIGN(UpdateScreen);
173};
174
175}  // namespace chromeos
176
177#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_SCREENS_UPDATE_SCREEN_H_
178