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_LOCALE_CHANGE_GUARD_H_ 6 #define CHROME_BROWSER_CHROMEOS_LOCALE_CHANGE_GUARD_H_ 7 8 #include <string> 9 10 #include "ash/system/locale/locale_observer.h" 11 #include "base/compiler_specific.h" 12 #include "base/gtest_prod_util.h" 13 #include "base/lazy_instance.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/strings/string16.h" 17 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/browser/notification_types.h" 20 21 class Profile; 22 23 namespace base { 24 class ListValue; 25 } 26 27 namespace chromeos { 28 29 // Performs check whether locale has been changed automatically recently 30 // (based on synchronized user preference). If so: shows notification that 31 // allows user to revert change. 32 class LocaleChangeGuard : public content::NotificationObserver, 33 public ash::LocaleObserver::Delegate, 34 public base::SupportsWeakPtr<LocaleChangeGuard> { 35 public: 36 explicit LocaleChangeGuard(Profile* profile); 37 virtual ~LocaleChangeGuard(); 38 39 // ash::LocaleChangeDelegate implementation. 40 virtual void AcceptLocaleChange() OVERRIDE; 41 virtual void RevertLocaleChange() OVERRIDE; 42 43 // Called just before changing locale. 44 void PrepareChangingLocale( 45 const std::string& from_locale, const std::string& to_locale); 46 47 // Called after login. 48 void OnLogin(); 49 50 private: 51 FRIEND_TEST_ALL_PREFIXES(LocaleChangeGuardTest, 52 ShowNotificationLocaleChanged); 53 FRIEND_TEST_ALL_PREFIXES(LocaleChangeGuardTest, 54 ShowNotificationLocaleChangedList); 55 56 void RevertLocaleChangeCallback(const base::ListValue* list); 57 void Check(); 58 59 // content::NotificationObserver implementation. 60 virtual void Observe(int type, 61 const content::NotificationSource& source, 62 const content::NotificationDetails& details) OVERRIDE; 63 64 // Returns true if we should notify user about automatic locale change. 65 static bool ShouldShowLocaleChangeNotification(const std::string& from_locale, 66 const std::string& to_locale); 67 68 static const char* const* GetSkipShowNotificationLanguagesForTesting(); 69 static size_t GetSkipShowNotificationLanguagesSizeForTesting(); 70 71 std::string from_locale_; 72 std::string to_locale_; 73 Profile* profile_; 74 bool reverted_; 75 bool session_started_; 76 bool main_frame_loaded_; 77 content::NotificationRegistrar registrar_; 78 79 // We want to show locale change notification in previous language however 80 // we cannot directly load strings for non-current locale. So we cache 81 // messages before locale change. 82 base::string16 title_text_; 83 base::string16 message_text_; 84 base::string16 revert_link_text_; 85 }; 86 87 } // namespace chromeos 88 89 #endif // CHROME_BROWSER_CHROMEOS_LOCALE_CHANGE_GUARD_H_ 90