1 // Copyright 2015 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_SYSTEM_SYSTEM_CLOCK_H_ 6 #define CHROME_BROWSER_CHROMEOS_SYSTEM_SYSTEM_CLOCK_H_ 7 8 #include <memory> 9 10 #include "base/callback_list.h" 11 #include "base/i18n/time_formatting.h" 12 #include "base/macros.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/scoped_observer.h" 15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile_observer.h" 17 #include "chromeos/login/login_state/login_state.h" 18 #include "components/user_manager/user_manager.h" 19 20 class PrefChangeRegistrar; 21 22 namespace user_manager { 23 class User; 24 } 25 26 namespace chromeos { 27 namespace system { 28 29 class SystemClockObserver; 30 31 // This is a global singleton (actually a member of BrowserProcessPlatformPart) 32 // that is responsible for correct time formatting. It listens to events that 33 // modify on-screen time representation (like ActiveUserChanged) and notifies 34 // observers. 35 class SystemClock : public chromeos::LoginState::Observer, 36 public ProfileObserver, 37 public user_manager::UserManager::UserSessionStateObserver { 38 public: 39 SystemClock(); 40 ~SystemClock() override; 41 42 // Could be used to temporary set the required clock type. At most one should 43 // exist at the time. 44 class ScopedHourClockType { 45 public: 46 explicit ScopedHourClockType(base::WeakPtr<SystemClock> system_clock); 47 ~ScopedHourClockType(); 48 49 ScopedHourClockType(const ScopedHourClockType&) = delete; 50 ScopedHourClockType& operator=(const ScopedHourClockType&) = delete; 51 52 ScopedHourClockType(ScopedHourClockType&&); 53 ScopedHourClockType& operator=(ScopedHourClockType&&); 54 55 void UpdateClockType(base::HourClockType clock_type); 56 57 private: 58 base::WeakPtr<SystemClock> system_clock_; 59 }; 60 61 ScopedHourClockType CreateScopedHourClockType( 62 base::HourClockType hour_clock_type); 63 64 void AddObserver(SystemClockObserver* observer); 65 void RemoveObserver(SystemClockObserver* observer); 66 67 bool ShouldUse24HourClock() const; 68 69 // ProfileObserver: 70 void OnProfileWillBeDestroyed(Profile* profile) override; 71 72 // user_manager::UserManager::UserSessionStateObserver: 73 void ActiveUserChanged(user_manager::User* active_user) override; 74 75 private: 76 // Should be the same as CrosSettings::ObserverSubscription. 77 typedef base::CallbackList<void(void)>::Subscription 78 CrosSettingsObserverSubscription; 79 80 void SetProfileByUser(const user_manager::User* user); 81 void SetProfile(Profile* profile); 82 83 // LoginState::Observer overrides. 84 void LoggedInStateChanged() override; 85 86 void OnSystemPrefChanged(); 87 88 void UpdateClockType(); 89 90 base::Optional<base::HourClockType> scoped_hour_clock_type_; 91 92 Profile* user_profile_ = nullptr; 93 ScopedObserver<Profile, ProfileObserver> profile_observer_{this}; 94 std::unique_ptr<PrefChangeRegistrar> user_pref_registrar_; 95 96 base::ObserverList<SystemClockObserver>::Unchecked observer_list_; 97 98 std::unique_ptr<CrosSettingsObserverSubscription> device_settings_observer_; 99 100 base::WeakPtrFactory<SystemClock> weak_ptr_factory_{this}; 101 102 DISALLOW_COPY_AND_ASSIGN(SystemClock); 103 }; 104 105 } // namespace system 106 } // namespace chromeos 107 108 #endif // CHROME_BROWSER_CHROMEOS_SYSTEM_SYSTEM_CLOCK_H_ 109