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