| OLD | NEW |
| (Empty) |
| 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 #include "chrome/browser/chromeos/arc/arc_settings_bridge.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/json/json_writer.h" | |
| 10 #include "base/prefs/pref_service.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "chrome/browser/profiles/profile_manager.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 #include "components/arc/common/settings.mojom.h" | |
| 15 | |
| 16 using ::chromeos::system::TimezoneSettings; | |
| 17 | |
| 18 namespace arc { | |
| 19 | |
| 20 namespace fontsizes { | |
| 21 | |
| 22 double ConvertFontSizeChromeToAndroid(int default_size, | |
| 23 int default_fixed_size, | |
| 24 int minimum_size) { | |
| 25 // kWebKitDefaultFixedFontSize is automatically set to be 3 pixels smaller | |
| 26 // than kWebKitDefaultFontSize when Chrome's settings page's main font | |
| 27 // dropdown control is adjusted. If the user specifically sets a higher | |
| 28 // fixed font size we will want to take into account the adjustment. | |
| 29 default_fixed_size += 3; | |
| 30 int max_chrome_size = | |
| 31 std::max(std::max(default_fixed_size, default_size), minimum_size); | |
| 32 | |
| 33 double android_scale = kAndroidFontScaleSmall; | |
| 34 if (max_chrome_size >= kChromeFontSizeVeryLarge) { | |
| 35 android_scale = kAndroidFontScaleHuge; | |
| 36 } else if (max_chrome_size >= kChromeFontSizeLarge) { | |
| 37 android_scale = kAndroidFontScaleLarge; | |
| 38 } else if (max_chrome_size >= kChromeFontSizeNormal) { | |
| 39 android_scale = kAndroidFontScaleNormal; | |
| 40 } | |
| 41 | |
| 42 return android_scale; | |
| 43 } | |
| 44 | |
| 45 } // namespace fontsizes | |
| 46 | |
| 47 ArcSettingsBridge::ArcSettingsBridge(ArcBridgeService* bridge_service) | |
| 48 : ArcService(bridge_service) { | |
| 49 arc_bridge_service()->AddObserver(this); | |
| 50 } | |
| 51 | |
| 52 ArcSettingsBridge::~ArcSettingsBridge() { | |
| 53 arc_bridge_service()->RemoveObserver(this); | |
| 54 } | |
| 55 | |
| 56 void ArcSettingsBridge::StartObservingSettingsChanges() { | |
| 57 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 58 registrar_.Init(profile->GetPrefs()); | |
| 59 | |
| 60 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize); | |
| 61 AddPrefToObserve(prefs::kWebKitDefaultFontSize); | |
| 62 AddPrefToObserve(prefs::kWebKitMinimumFontSize); | |
| 63 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled); | |
| 64 | |
| 65 TimezoneSettings::GetInstance()->AddObserver(this); | |
| 66 } | |
| 67 | |
| 68 void ArcSettingsBridge::SyncAllPrefs() const { | |
| 69 SyncFontSize(); | |
| 70 SyncLocale(); | |
| 71 SyncSpokenFeedbackEnabled(); | |
| 72 SyncTimeZone(); | |
| 73 } | |
| 74 | |
| 75 void ArcSettingsBridge::StopObservingSettingsChanges() { | |
| 76 registrar_.RemoveAll(); | |
| 77 | |
| 78 TimezoneSettings::GetInstance()->RemoveObserver(this); | |
| 79 } | |
| 80 | |
| 81 void ArcSettingsBridge::AddPrefToObserve(const std::string& pref_name) { | |
| 82 registrar_.Add(pref_name, base::Bind(&ArcSettingsBridge::OnPrefChanged, | |
| 83 base::Unretained(this))); | |
| 84 } | |
| 85 | |
| 86 void ArcSettingsBridge::OnPrefChanged(const std::string& pref_name) const { | |
| 87 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) { | |
| 88 SyncSpokenFeedbackEnabled(); | |
| 89 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize || | |
| 90 pref_name == prefs::kWebKitDefaultFontSize || | |
| 91 pref_name == prefs::kWebKitMinimumFontSize) { | |
| 92 SyncFontSize(); | |
| 93 } else { | |
| 94 LOG(ERROR) << "Unknown pref changed."; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void ArcSettingsBridge::OnStateChanged(ArcBridgeService::State state) { | |
| 99 // ArcBridgeService::State::READY is emitted before ArcSettings app is ready | |
| 100 // to send broadcasts. Instead we wait for the SettingsInstance to be ready. | |
| 101 if (state == ArcBridgeService::State::STOPPING) { | |
| 102 StopObservingSettingsChanges(); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void ArcSettingsBridge::OnSettingsInstanceReady() { | |
| 107 StartObservingSettingsChanges(); | |
| 108 SyncAllPrefs(); | |
| 109 } | |
| 110 | |
| 111 void ArcSettingsBridge::TimezoneChanged(const icu::TimeZone& timezone) { | |
| 112 SyncTimeZone(); | |
| 113 } | |
| 114 | |
| 115 int ArcSettingsBridge::GetIntegerPref(const std::string& pref_name) const { | |
| 116 const PrefService::Preference* pref = | |
| 117 registrar_.prefs()->FindPreference(pref_name); | |
| 118 DCHECK(pref); | |
| 119 int val = -1; | |
| 120 bool value_exists = pref->GetValue()->GetAsInteger(&val); | |
| 121 DCHECK(value_exists); | |
| 122 return val; | |
| 123 } | |
| 124 | |
| 125 void ArcSettingsBridge::SyncFontSize() const { | |
| 126 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize); | |
| 127 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize); | |
| 128 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize); | |
| 129 | |
| 130 double android_scale = fontsizes::ConvertFontSizeChromeToAndroid( | |
| 131 default_size, default_fixed_size, minimum_size); | |
| 132 | |
| 133 base::DictionaryValue extras; | |
| 134 extras.SetDouble("scale", android_scale); | |
| 135 SendSettingsBroadcast("org.chromium.arc.settings.SET_FONT_SCALE", extras); | |
| 136 } | |
| 137 | |
| 138 void ArcSettingsBridge::SyncSpokenFeedbackEnabled() const { | |
| 139 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( | |
| 140 prefs::kAccessibilitySpokenFeedbackEnabled); | |
| 141 DCHECK(pref); | |
| 142 bool enabled = false; | |
| 143 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled); | |
| 144 DCHECK(value_exists); | |
| 145 base::DictionaryValue extras; | |
| 146 extras.SetBoolean("enabled", enabled); | |
| 147 SendSettingsBroadcast("org.chromium.arc.settings.SET_SPOKEN_FEEDBACK_ENABLED", | |
| 148 extras); | |
| 149 } | |
| 150 | |
| 151 void ArcSettingsBridge::SyncLocale() const { | |
| 152 const PrefService::Preference* pref = | |
| 153 registrar_.prefs()->FindPreference(prefs::kApplicationLocale); | |
| 154 DCHECK(pref); | |
| 155 std::string locale; | |
| 156 bool value_exists = pref->GetValue()->GetAsString(&locale); | |
| 157 DCHECK(value_exists); | |
| 158 base::DictionaryValue extras; | |
| 159 extras.SetString("locale", locale); | |
| 160 SendSettingsBroadcast("org.chromium.arc.settings.SET_LOCALE", extras); | |
| 161 } | |
| 162 | |
| 163 void ArcSettingsBridge::SyncTimeZone() const { | |
| 164 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance(); | |
| 165 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID(); | |
| 166 base::DictionaryValue extras; | |
| 167 extras.SetString("olsonTimeZone", timezoneID); | |
| 168 SendSettingsBroadcast("org.chromium.arc.settings.SET_TIME_ZONE", extras); | |
| 169 } | |
| 170 | |
| 171 void ArcSettingsBridge::SendSettingsBroadcast( | |
| 172 const std::string& action, | |
| 173 const base::DictionaryValue& extras) const { | |
| 174 SettingsInstance* settings_instance = | |
| 175 arc_bridge_service()->settings_instance(); | |
| 176 if (!settings_instance) { | |
| 177 LOG(ERROR) << "Bridge service is not ready."; | |
| 178 return; | |
| 179 } | |
| 180 | |
| 181 std::string extras_json; | |
| 182 bool write_success = base::JSONWriter::Write(extras, &extras_json); | |
| 183 DCHECK(write_success); | |
| 184 settings_instance->SendBroadcast(action, "org.chromium.arc.settings", | |
| 185 "org.chromium.arc.settings.SettingsReceiver", | |
| 186 extras_json); | |
| 187 } | |
| 188 | |
| 189 } // namespace arc | |
| OLD | NEW |