Home | History | Annotate | Download | only in login
      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_WIZARD_CONTROLLER_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_WIZARD_CONTROLLER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/containers/hash_tables.h"
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/linked_ptr.h"
     16 #include "base/memory/scoped_ptr.h"
     17 #include "base/memory/weak_ptr.h"
     18 #include "base/observer_list.h"
     19 #include "base/time/time.h"
     20 #include "base/timer/timer.h"
     21 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
     22 #include "chrome/browser/chromeos/login/screen_manager.h"
     23 #include "chrome/browser/chromeos/login/screens/screen_observer.h"
     24 
     25 class PrefRegistrySimple;
     26 class PrefService;
     27 
     28 namespace base {
     29 class DictionaryValue;
     30 }
     31 
     32 namespace pairing_chromeos {
     33 class SharkConnectionListener;
     34 class ControllerPairingController;
     35 class HostPairingController;
     36 }
     37 
     38 namespace chromeos {
     39 
     40 class AutoEnrollmentCheckScreen;
     41 class EnrollmentScreen;
     42 class ErrorScreen;
     43 struct Geoposition;
     44 class LoginDisplayHost;
     45 class LoginScreenContext;
     46 class NetworkScreen;
     47 class OobeDisplay;
     48 class SimpleGeolocationProvider;
     49 class SupervisedUserCreationScreen;
     50 class TimeZoneProvider;
     51 struct TimeZoneResponseData;
     52 class UpdateScreen;
     53 class UserImageScreen;
     54 
     55 // Class that manages control flow between wizard screens. Wizard controller
     56 // interacts with screen controllers to move the user between screens.
     57 class WizardController : public ScreenObserver, public ScreenManager {
     58  public:
     59   // Observes screen changes.
     60   class Observer {
     61    public:
     62     // Called before a screen change happens.
     63     virtual void OnScreenChanged(WizardScreen* next_screen) = 0;
     64 
     65     // Called after the browser session has started.
     66     virtual void OnSessionStart() = 0;
     67   };
     68 
     69   WizardController(LoginDisplayHost* host, OobeDisplay* oobe_display);
     70   virtual ~WizardController();
     71 
     72   // Returns the default wizard controller if it has been created.
     73   static WizardController* default_controller() {
     74     return default_controller_;
     75   }
     76 
     77   // Whether to skip any screens that may normally be shown after login
     78   // (registration, Terms of Service, user image selection).
     79   static bool skip_post_login_screens() {
     80     return skip_post_login_screens_;
     81   }
     82 
     83   // Sets delays to zero. MUST be used only for tests.
     84   static void SetZeroDelays();
     85 
     86   // If true zero delays have been enabled (for browser tests).
     87   static bool IsZeroDelayEnabled();
     88 
     89   // Checks whether screen show time should be tracked with UMA.
     90   static bool IsOOBEStepToTrack(const std::string& screen_id);
     91 
     92   // Skips any screens that may normally be shown after login (registration,
     93   // Terms of Service, user image selection).
     94   static void SkipPostLoginScreensForTesting();
     95 
     96   // Checks whether OOBE should start enrollment automatically.
     97   static bool ShouldAutoStartEnrollment();
     98 
     99   // Checks whether OOBE should recover enrollment.  Note that this flips to
    100   // false once device policy has been restored as a part of recovery.
    101   static bool ShouldRecoverEnrollment();
    102 
    103   // Obtains domain the device used to be enrolled to from install attributes.
    104   static std::string GetEnrollmentRecoveryDomain();
    105 
    106   // Shows the first screen defined by |first_screen_name| or by default
    107   // if the parameter is empty. Takes ownership of |screen_parameters|.
    108   void Init(const std::string& first_screen_name,
    109             scoped_ptr<base::DictionaryValue> screen_parameters);
    110 
    111   // Advances to screen defined by |screen_name| and shows it.
    112   void AdvanceToScreen(const std::string& screen_name);
    113 
    114   // Advances to login screen. Should be used in for testing only.
    115   void SkipToLoginForTesting(const LoginScreenContext& context);
    116 
    117   // Adds and removes an observer.
    118   void AddObserver(Observer* observer);
    119   void RemoveObserver(Observer* observer);
    120 
    121   // Called right after the browser session has started.
    122   void OnSessionStart();
    123 
    124   // Skip update, go straight to enrollment after EULA is accepted.
    125   void SkipUpdateEnrollAfterEula();
    126 
    127   // TODO(antrim) : temporary hack. Should be removed once screen system is
    128   // reworked at hackaton.
    129   void EnableUserImageScreenReturnToPreviousHack();
    130 
    131   // Callback for enrollment auth token.
    132   void OnEnrollmentAuthTokenReceived(const std::string& auth_token);
    133 
    134   // Returns a pointer to the current screen or NULL if there's no such
    135   // screen.
    136   WizardScreen* current_screen() const { return current_screen_; }
    137 
    138   // Returns true if the current wizard instance has reached the login screen.
    139   bool login_screen_started() const { return login_screen_started_; }
    140 
    141   // ScreenManager implementation.
    142   virtual WizardScreen* CreateScreen(const std::string& screen_name) OVERRIDE;
    143 
    144   static const char kNetworkScreenName[];
    145   static const char kLoginScreenName[];
    146   static const char kUpdateScreenName[];
    147   static const char kUserImageScreenName[];
    148   static const char kOutOfBoxScreenName[];
    149   static const char kTestNoScreenName[];
    150   static const char kEulaScreenName[];
    151   static const char kEnrollmentScreenName[];
    152   static const char kResetScreenName[];
    153   static const char kKioskEnableScreenName[];
    154   static const char kKioskAutolaunchScreenName[];
    155   static const char kErrorScreenName[];
    156   static const char kTermsOfServiceScreenName[];
    157   static const char kAutoEnrollmentCheckScreenName[];
    158   static const char kWrongHWIDScreenName[];
    159   static const char kSupervisedUserCreationScreenName[];
    160   static const char kAppLaunchSplashScreenName[];
    161   static const char kHIDDetectionScreenName[];
    162   static const char kControllerPairingScreenName[];
    163   static const char kHostPairingScreenName[];
    164 
    165   // Volume percent at which spoken feedback is still audible.
    166   static const int kMinAudibleOutputVolumePercent;
    167 
    168  private:
    169   // Show specific screen.
    170   void ShowNetworkScreen();
    171   void ShowUpdateScreen();
    172   void ShowUserImageScreen();
    173   void ShowEulaScreen();
    174   void ShowEnrollmentScreen();
    175   void ShowResetScreen();
    176   void ShowKioskAutolaunchScreen();
    177   void ShowKioskEnableScreen();
    178   void ShowTermsOfServiceScreen();
    179   void ShowWrongHWIDScreen();
    180   void ShowAutoEnrollmentCheckScreen();
    181   void ShowSupervisedUserCreationScreen();
    182   void ShowHIDDetectionScreen();
    183   void ShowControllerPairingScreen();
    184   void ShowHostPairingScreen();
    185 
    186   // Shows images login screen.
    187   void ShowLoginScreen(const LoginScreenContext& context);
    188 
    189   // Resumes a pending login screen.
    190   void ResumeLoginScreen();
    191 
    192   // Invokes corresponding first OOBE screen.
    193   void OnHIDScreenNecessityCheck(bool screen_needed);
    194 
    195   // Exit handlers:
    196   void OnHIDDetectionCompleted();
    197   void OnNetworkConnected();
    198   void OnNetworkOffline();
    199   void OnConnectionFailed();
    200   void OnUpdateCompleted();
    201   void OnEulaAccepted();
    202   void OnUpdateErrorCheckingForUpdate();
    203   void OnUpdateErrorUpdating();
    204   void OnUserImageSelected();
    205   void OnUserImageSkipped();
    206   void OnEnrollmentDone();
    207   void OnAutoEnrollmentDone();
    208   void OnResetCanceled();
    209   void OnKioskAutolaunchCanceled();
    210   void OnKioskAutolaunchConfirmed();
    211   void OnKioskEnableCompleted();
    212   void OnWrongHWIDWarningSkipped();
    213   void OnAutoEnrollmentCheckCompleted();
    214   void OnTermsOfServiceDeclined();
    215   void OnTermsOfServiceAccepted();
    216   void OnControllerPairingFinished();
    217   void OnHostPairingFinished();
    218 
    219   // Callback function after setting MetricsReporting.
    220   void InitiateMetricsReportingChangeCallback(bool enabled);
    221 
    222   // Loads brand code on I/O enabled thread and stores to Local State.
    223   void LoadBrandCodeFromFile();
    224 
    225   // Called after all post-EULA blocking tasks have been completed.
    226   void OnEulaBlockingTasksDone();
    227 
    228   // Shows update screen and starts update process.
    229   void InitiateOOBEUpdate();
    230 
    231   // Actions that should be done right after EULA is accepted,
    232   // before update check.
    233   void PerformPostEulaActions();
    234 
    235   // Actions that should be done right after update stage is finished.
    236   void PerformOOBECompletedActions();
    237 
    238   // Overridden from ScreenObserver:
    239   virtual void OnExit(ExitCodes exit_code) OVERRIDE;
    240   virtual void ShowCurrentScreen() OVERRIDE;
    241   virtual void OnSetUserNamePassword(const std::string& username,
    242                                      const std::string& password) OVERRIDE;
    243   virtual void SetUsageStatisticsReporting(bool val) OVERRIDE;
    244   virtual bool GetUsageStatisticsReporting() const OVERRIDE;
    245   virtual ErrorScreen* GetErrorScreen() OVERRIDE;
    246   virtual void ShowErrorScreen() OVERRIDE;
    247   virtual void HideErrorScreen(WizardScreen* parent_screen) OVERRIDE;
    248 
    249   // Notification of a change in the state of an accessibility setting.
    250   void OnAccessibilityStatusChanged(
    251       const AccessibilityStatusEventDetails& details);
    252 
    253   // Switches from one screen to another.
    254   void SetCurrentScreen(WizardScreen* screen);
    255 
    256   // Switches from one screen to another with delay before showing. Calling
    257   // ShowCurrentScreen directly forces screen to be shown immediately.
    258   void SetCurrentScreenSmooth(WizardScreen* screen, bool use_smoothing);
    259 
    260   // Changes status area visibility.
    261   void SetStatusAreaVisible(bool visible);
    262 
    263   // Logs in the specified user via default login screen.
    264   void Login(const std::string& username, const std::string& password);
    265 
    266   // Launched kiosk app configured for auto-launch.
    267   void AutoLaunchKioskApp();
    268 
    269   // Checks whether the user is allowed to exit enrollment.
    270   static bool CanExitEnrollment();
    271 
    272   // Gets the management domain.
    273   static std::string GetForcedEnrollmentDomain();
    274 
    275   // Called when LocalState is initialized.
    276   void OnLocalStateInitialized(bool /* succeeded */);
    277 
    278   // Returns local state.
    279   PrefService* GetLocalState();
    280 
    281   static void set_local_state_for_testing(PrefService* local_state) {
    282     local_state_for_testing_ = local_state;
    283   }
    284 
    285   std::string first_screen_name() { return first_screen_name_; }
    286 
    287   // Called when network is UP.
    288   void StartTimezoneResolve();
    289 
    290   // Creates provider on demand.
    291   TimeZoneProvider* GetTimezoneProvider();
    292 
    293   // TimeZoneRequest::TimeZoneResponseCallback implementation.
    294   void OnTimezoneResolved(scoped_ptr<TimeZoneResponseData> timezone,
    295                           bool server_error);
    296 
    297   // Called from SimpleGeolocationProvider when location is resolved.
    298   void OnLocationResolved(const Geoposition& position,
    299                           bool server_error,
    300                           const base::TimeDelta elapsed);
    301 
    302   // Returns true if callback has been installed.
    303   // Returns false if timezone has already been resolved.
    304   bool SetOnTimeZoneResolvedForTesting(const base::Closure& callback);
    305 
    306   // Returns true for pairing remora OOBE.
    307   bool IsHostPairingOobe() const;
    308 
    309   // Starts listening for an incoming shark controller connection, if we are
    310   // running remora OOBE.
    311   void MaybeStartListeningForSharkConnection();
    312 
    313   // Called when a connection to controller has been established. Wizard
    314   // controller takes the ownership of |pairing_controller| after that call.
    315   void OnSharkConnected(
    316       scoped_ptr<pairing_chromeos::HostPairingController> pairing_controller);
    317 
    318   // Whether to skip any screens that may normally be shown after login
    319   // (registration, Terms of Service, user image selection).
    320   static bool skip_post_login_screens_;
    321 
    322   static bool zero_delay_enabled_;
    323 
    324   // Screen that's currently active.
    325   WizardScreen* current_screen_;
    326 
    327   // Screen that was active before, or NULL for login screen.
    328   WizardScreen* previous_screen_;
    329 
    330   std::string username_;
    331   std::string password_;
    332   std::string auth_token_;
    333 
    334   // True if running official BUILD.
    335   bool is_official_build_;
    336 
    337   // True if full OOBE flow should be shown.
    338   bool is_out_of_box_;
    339 
    340   // Value of the screen name that WizardController was started with.
    341   std::string first_screen_name_;
    342 
    343   // OOBE/login display host.
    344   LoginDisplayHost* host_;
    345 
    346   // Default WizardController.
    347   static WizardController* default_controller_;
    348 
    349   // Parameters for the first screen. May be NULL.
    350   scoped_ptr<base::DictionaryValue> screen_parameters_;
    351 
    352   base::OneShotTimer<WizardController> smooth_show_timer_;
    353 
    354   OobeDisplay* oobe_display_;
    355 
    356   // State of Usage stat/error reporting checkbox on EULA screen
    357   // during wizard lifetime.
    358   bool usage_statistics_reporting_;
    359 
    360   // If true then update check is cancelled and enrollment is started after
    361   // EULA is accepted.
    362   bool skip_update_enroll_after_eula_;
    363 
    364   // Whether enrollment will be or has been recovered in the current wizard
    365   // instance.
    366   bool enrollment_recovery_;
    367 
    368   // Time when the EULA was accepted. Used to measure the duration from the EULA
    369   // acceptance until the Sign-In screen is displayed.
    370   base::Time time_eula_accepted_;
    371 
    372   // Time when OOBE was started. Used to measure the total time from boot to
    373   // user Sign-In completed.
    374   base::Time time_oobe_started_;
    375 
    376   ObserverList<Observer> observer_list_;
    377 
    378   bool login_screen_started_;
    379 
    380   // Indicates that once image selection screen finishes we should return to
    381   // a previous screen instead of proceeding with usual flow.
    382   bool user_image_screen_return_to_previous_hack_;
    383 
    384   // Non-owning pointer to local state used for testing.
    385   static PrefService* local_state_for_testing_;
    386 
    387   FRIEND_TEST_ALL_PREFIXES(EnrollmentScreenTest, TestCancel);
    388   FRIEND_TEST_ALL_PREFIXES(WizardControllerFlowTest, Accelerators);
    389   friend class WizardControllerFlowTest;
    390   friend class WizardControllerOobeResumeTest;
    391   friend class WizardInProcessBrowserTest;
    392   friend class WizardControllerBrokenLocalStateTest;
    393 
    394   scoped_ptr<AccessibilityStatusSubscription> accessibility_subscription_;
    395 
    396   scoped_ptr<SimpleGeolocationProvider> geolocation_provider_;
    397   scoped_ptr<TimeZoneProvider> timezone_provider_;
    398 
    399   // Pairing controller for shark devices.
    400   scoped_ptr<pairing_chromeos::ControllerPairingController>
    401       controller_pairing_controller_;
    402 
    403   // Pairing controller for remora devices.
    404   scoped_ptr<pairing_chromeos::HostPairingController> host_pairing_controller_;
    405 
    406   // Maps screen ids to last time of their shows.
    407   base::hash_map<std::string, base::Time> screen_show_times_;
    408 
    409   // Tests check result of timezone resolve.
    410   bool timezone_resolved_;
    411   base::Closure on_timezone_resolved_for_testing_;
    412 
    413   // True if shark device initiated connection to this device.
    414   bool shark_controller_detected_;
    415 
    416   // Listens for incoming connection from a shark controller if a regular (not
    417   // pairing) remora OOBE is active. If connection is established, wizard
    418   // conroller swithces to a pairing OOBE.
    419   scoped_ptr<pairing_chromeos::SharkConnectionListener>
    420       shark_connection_listener_;
    421 
    422   base::WeakPtrFactory<WizardController> weak_factory_;
    423 
    424   DISALLOW_COPY_AND_ASSIGN(WizardController);
    425 };
    426 
    427 }  // namespace chromeos
    428 
    429 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_WIZARD_CONTROLLER_H_
    430