OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/arc/arc_settings_bridge_impl.h" | 5 #include "chrome/browser/chromeos/arc/settings_bridge.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/json/json_writer.h" | |
10 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
11 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
12 #include "chrome/browser/profiles/profile_manager.h" | 11 #include "chrome/browser/profiles/profile_manager.h" |
13 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
14 #include "components/arc/common/settings.mojom.h" | |
15 | 13 |
16 using ::chromeos::system::TimezoneSettings; | 14 using ::chromeos::system::TimezoneSettings; |
17 | 15 |
18 namespace arc { | 16 namespace arc { |
19 | 17 |
20 namespace fontsizes { | 18 namespace fontsizes { |
21 | 19 |
22 double ConvertFontSizeChromeToAndroid(int default_size, | 20 double ConvertFontSizeChromeToAndroid(int default_size, |
23 int default_fixed_size, | 21 int default_fixed_size, |
24 int minimum_size) { | 22 int minimum_size) { |
(...skipping 12 matching lines...) Expand all Loading... | |
37 android_scale = kAndroidFontScaleLarge; | 35 android_scale = kAndroidFontScaleLarge; |
38 } else if (max_chrome_size >= kChromeFontSizeNormal) { | 36 } else if (max_chrome_size >= kChromeFontSizeNormal) { |
39 android_scale = kAndroidFontScaleNormal; | 37 android_scale = kAndroidFontScaleNormal; |
40 } | 38 } |
41 | 39 |
42 return android_scale; | 40 return android_scale; |
43 } | 41 } |
44 | 42 |
45 } // namespace fontsizes | 43 } // namespace fontsizes |
46 | 44 |
47 ArcSettingsBridgeImpl::~ArcSettingsBridgeImpl() { | 45 SettingsBridge::SettingsBridge(SettingsBridge::Delegate* delegate) |
48 ArcBridgeService* bridge_service = ArcBridgeService::Get(); | 46 : delegate_(delegate) { |
49 DCHECK(bridge_service); | 47 StartObservingSettingsChanges(); |
50 bridge_service->RemoveObserver(this); | 48 SyncAllPrefs(); |
51 } | 49 } |
52 | 50 |
53 void ArcSettingsBridgeImpl::StartObservingBridgeServiceChanges() { | 51 SettingsBridge::~SettingsBridge() { |
54 ArcBridgeService* bridge_service = ArcBridgeService::Get(); | 52 StopObservingSettingsChanges(); |
55 DCHECK(bridge_service); | |
56 bridge_service->AddObserver(this); | |
57 } | 53 } |
58 | 54 |
59 void ArcSettingsBridgeImpl::StartObservingSettingsChanges() { | 55 void SettingsBridge::StartObservingSettingsChanges() { |
60 Profile* profile = ProfileManager::GetActiveUserProfile(); | 56 Profile* profile = ProfileManager::GetActiveUserProfile(); |
61 registrar_.Init(profile->GetPrefs()); | 57 registrar_.Init(profile->GetPrefs()); |
62 | 58 |
63 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize); | 59 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize); |
64 AddPrefToObserve(prefs::kWebKitDefaultFontSize); | 60 AddPrefToObserve(prefs::kWebKitDefaultFontSize); |
65 AddPrefToObserve(prefs::kWebKitMinimumFontSize); | 61 AddPrefToObserve(prefs::kWebKitMinimumFontSize); |
66 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled); | 62 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled); |
67 | 63 |
68 TimezoneSettings::GetInstance()->AddObserver(this); | 64 TimezoneSettings::GetInstance()->AddObserver(this); |
69 } | 65 } |
70 | 66 |
71 void ArcSettingsBridgeImpl::SyncAllPrefs() const { | 67 void SettingsBridge::SyncAllPrefs() const { |
72 SyncFontSize(); | 68 SyncFontSize(); |
73 SyncLocale(); | 69 SyncLocale(); |
74 SyncSpokenFeedbackEnabled(); | 70 SyncSpokenFeedbackEnabled(); |
75 SyncTimeZone(); | 71 SyncTimeZone(); |
76 } | 72 } |
77 | 73 |
78 void ArcSettingsBridgeImpl::StopObservingSettingsChanges() { | 74 void SettingsBridge::StopObservingSettingsChanges() { |
79 registrar_.RemoveAll(); | 75 registrar_.RemoveAll(); |
80 | 76 |
81 TimezoneSettings::GetInstance()->RemoveObserver(this); | 77 TimezoneSettings::GetInstance()->RemoveObserver(this); |
82 } | 78 } |
83 | 79 |
84 void ArcSettingsBridgeImpl::AddPrefToObserve(const std::string& pref_name) { | 80 void SettingsBridge::AddPrefToObserve(const std::string& pref_name) { |
85 registrar_.Add(pref_name, base::Bind(&ArcSettingsBridgeImpl::OnPrefChanged, | 81 registrar_.Add(pref_name, base::Bind(&SettingsBridge::OnPrefChanged, |
86 base::Unretained(this))); | 82 base::Unretained(this))); |
87 } | 83 } |
88 | 84 |
89 void ArcSettingsBridgeImpl::OnPrefChanged(const std::string& pref_name) const { | 85 void SettingsBridge::OnPrefChanged(const std::string& pref_name) const { |
90 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) { | 86 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) { |
91 SyncSpokenFeedbackEnabled(); | 87 SyncSpokenFeedbackEnabled(); |
92 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize || | 88 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize || |
93 pref_name == prefs::kWebKitDefaultFontSize || | 89 pref_name == prefs::kWebKitDefaultFontSize || |
94 pref_name == prefs::kWebKitMinimumFontSize) { | 90 pref_name == prefs::kWebKitMinimumFontSize) { |
95 SyncFontSize(); | 91 SyncFontSize(); |
96 } else { | 92 } else { |
97 LOG(ERROR) << "Unknown pref changed."; | 93 LOG(ERROR) << "Unknown pref changed."; |
98 } | 94 } |
99 } | 95 } |
100 | 96 |
101 void ArcSettingsBridgeImpl::OnStateChanged(ArcBridgeService::State state) { | 97 void SettingsBridge::TimezoneChanged(const icu::TimeZone& timezone) { |
102 // ArcBridgeService::State::READY is emitted before ArcSettings app is ready | |
103 // to send broadcasts. Instead we wait for the SettingsInstance to be ready. | |
104 if (state == ArcBridgeService::State::STOPPING) { | |
105 StopObservingSettingsChanges(); | |
106 } | |
107 } | |
108 | |
109 void ArcSettingsBridgeImpl::OnSettingsInstanceReady() { | |
110 StartObservingSettingsChanges(); | |
111 SyncAllPrefs(); | |
112 } | |
113 | |
114 void ArcSettingsBridgeImpl::TimezoneChanged(const icu::TimeZone& timezone) { | |
115 SyncTimeZone(); | 98 SyncTimeZone(); |
116 } | 99 } |
117 | 100 |
118 int ArcSettingsBridgeImpl::GetIntegerPref(const std::string& pref_name) const { | 101 int SettingsBridge::GetIntegerPref(const std::string& pref_name) const { |
119 const PrefService::Preference* pref = | 102 const PrefService::Preference* pref = |
120 registrar_.prefs()->FindPreference(pref_name); | 103 registrar_.prefs()->FindPreference(pref_name); |
121 DCHECK(pref); | 104 DCHECK(pref); |
122 int val = -1; | 105 int val = -1; |
123 bool value_exists = pref->GetValue()->GetAsInteger(&val); | 106 bool value_exists = pref->GetValue()->GetAsInteger(&val); |
124 DCHECK(value_exists); | 107 DCHECK(value_exists); |
125 return val; | 108 return val; |
126 } | 109 } |
127 | 110 |
128 void ArcSettingsBridgeImpl::SyncFontSize() const { | 111 void SettingsBridge::SyncFontSize() const { |
129 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize); | 112 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize); |
130 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize); | 113 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize); |
131 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize); | 114 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize); |
132 | 115 |
133 double android_scale = fontsizes::ConvertFontSizeChromeToAndroid( | 116 double android_scale = fontsizes::ConvertFontSizeChromeToAndroid( |
134 default_size, default_fixed_size, minimum_size); | 117 default_size, default_fixed_size, minimum_size); |
135 | 118 |
136 base::DictionaryValue extras; | 119 base::DictionaryValue extras; |
137 extras.SetDouble("scale", android_scale); | 120 extras.SetDouble("scale", android_scale); |
138 SendSettingsBroadcast("org.chromium.arc.settings.SET_FONT_SCALE", extras); | 121 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_FONT_SCALE", |
122 extras); | |
139 } | 123 } |
140 | 124 |
141 void ArcSettingsBridgeImpl::SyncSpokenFeedbackEnabled() const { | 125 void SettingsBridge::SyncSpokenFeedbackEnabled() const { |
142 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( | 126 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( |
143 prefs::kAccessibilitySpokenFeedbackEnabled); | 127 prefs::kAccessibilitySpokenFeedbackEnabled); |
144 DCHECK(pref); | 128 DCHECK(pref); |
145 bool enabled = false; | 129 bool enabled = false; |
146 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled); | 130 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled); |
147 DCHECK(value_exists); | 131 DCHECK(value_exists); |
148 base::DictionaryValue extras; | 132 base::DictionaryValue extras; |
149 extras.SetBoolean("enabled", enabled); | 133 extras.SetBoolean("enabled", enabled); |
150 SendSettingsBroadcast("org.chromium.arc.settings.SET_SPOKEN_FEEDBACK_ENABLED", | 134 SendSettingsBroadcast( |
151 extras); | 135 "org.chromium.arc.intent_helper.SET_SPOKEN_FEEDBACK_ENABLED", extras); |
152 } | 136 } |
153 | 137 |
154 void ArcSettingsBridgeImpl::SyncLocale() const { | 138 void SettingsBridge::SyncLocale() const { |
155 const PrefService::Preference* pref = | 139 const PrefService::Preference* pref = |
156 registrar_.prefs()->FindPreference(prefs::kApplicationLocale); | 140 registrar_.prefs()->FindPreference(prefs::kApplicationLocale); |
157 DCHECK(pref); | 141 DCHECK(pref); |
158 std::string locale; | 142 std::string locale; |
159 bool value_exists = pref->GetValue()->GetAsString(&locale); | 143 bool value_exists = pref->GetValue()->GetAsString(&locale); |
160 DCHECK(value_exists); | 144 DCHECK(value_exists); |
161 base::DictionaryValue extras; | 145 base::DictionaryValue extras; |
162 extras.SetString("locale", locale); | 146 extras.SetString("locale", locale); |
163 SendSettingsBroadcast("org.chromium.arc.settings.SET_LOCALE", extras); | 147 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_LOCALE", extras); |
164 } | 148 } |
165 | 149 |
166 void ArcSettingsBridgeImpl::SyncTimeZone() const { | 150 void SettingsBridge::SyncTimeZone() const { |
167 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance(); | 151 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance(); |
168 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID(); | 152 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID(); |
169 base::DictionaryValue extras; | 153 base::DictionaryValue extras; |
170 extras.SetString("olsonTimeZone", timezoneID); | 154 extras.SetString("olsonTimeZone", timezoneID); |
171 SendSettingsBroadcast("org.chromium.arc.settings.SET_TIME_ZONE", extras); | 155 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_TIME_ZONE", extras); |
172 } | 156 } |
173 | 157 |
174 void ArcSettingsBridgeImpl::SendSettingsBroadcast( | 158 void SettingsBridge::SendSettingsBroadcast( |
175 const std::string& action, | 159 const std::string& action, |
176 const base::DictionaryValue& extras) const { | 160 const base::DictionaryValue& extras) const { |
177 ArcBridgeService* bridge_service = ArcBridgeService::Get(); | 161 if (delegate_) |
benhansen1
2016/01/21 02:41:05
Should we DCHECK delegate instead since it should
Yusuke Sato
2016/01/21 22:42:57
Done.
| |
178 if (!bridge_service || | 162 delegate_->OnBroadcastNeeded(action, extras); |
179 bridge_service->state() != ArcBridgeService::State::READY) { | |
180 LOG(ERROR) << "Bridge service is not ready."; | |
181 return; | |
182 } | |
183 | |
184 std::string extras_json; | |
185 bool write_success = base::JSONWriter::Write(extras, &extras_json); | |
186 DCHECK(write_success); | |
187 bridge_service->settings_instance()->SendBroadcast( | |
188 action, "org.chromium.arc.settings", | |
189 "org.chromium.arc.settings.SettingsReceiver", extras_json); | |
190 } | 163 } |
191 | 164 |
192 } // namespace arc | 165 } // namespace arc |
OLD | NEW |