Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: chrome/browser/chromeos/arc/arc_settings_bridge.cc

Issue 1596663002: arc-bridge: Introduce the ArcService class (Closed) Base URL: https://chromium.googlesource.com/a/chromium/src.git@master
Patch Set: Rebased + protected ArcService direct instantiation Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/arc_settings_bridge.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/profiles/profile_manager.h" 12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/common/pref_names.h" 13 #include "chrome/common/pref_names.h"
14 #include "components/arc/common/settings.mojom.h" 14 #include "components/arc/common/settings.mojom.h"
15 15
(...skipping 21 matching lines...) Expand all
37 android_scale = kAndroidFontScaleLarge; 37 android_scale = kAndroidFontScaleLarge;
38 } else if (max_chrome_size >= kChromeFontSizeNormal) { 38 } else if (max_chrome_size >= kChromeFontSizeNormal) {
39 android_scale = kAndroidFontScaleNormal; 39 android_scale = kAndroidFontScaleNormal;
40 } 40 }
41 41
42 return android_scale; 42 return android_scale;
43 } 43 }
44 44
45 } // namespace fontsizes 45 } // namespace fontsizes
46 46
47 ArcSettingsBridgeImpl::~ArcSettingsBridgeImpl() { 47 ArcSettingsBridge::ArcSettingsBridge(ArcBridgeService* bridge_service)
48 ArcBridgeService* bridge_service = ArcBridgeService::Get(); 48 : ArcService(bridge_service) {
49 DCHECK(bridge_service); 49 arc_bridge_service()->AddObserver(this);
50 bridge_service->RemoveObserver(this);
51 } 50 }
52 51
53 void ArcSettingsBridgeImpl::StartObservingBridgeServiceChanges() { 52 ArcSettingsBridge::~ArcSettingsBridge() {
54 ArcBridgeService* bridge_service = ArcBridgeService::Get(); 53 arc_bridge_service()->RemoveObserver(this);
55 DCHECK(bridge_service);
56 bridge_service->AddObserver(this);
57 } 54 }
58 55
59 void ArcSettingsBridgeImpl::StartObservingSettingsChanges() { 56 void ArcSettingsBridge::StartObservingSettingsChanges() {
60 Profile* profile = ProfileManager::GetActiveUserProfile(); 57 Profile* profile = ProfileManager::GetActiveUserProfile();
61 registrar_.Init(profile->GetPrefs()); 58 registrar_.Init(profile->GetPrefs());
62 59
63 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize); 60 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize);
64 AddPrefToObserve(prefs::kWebKitDefaultFontSize); 61 AddPrefToObserve(prefs::kWebKitDefaultFontSize);
65 AddPrefToObserve(prefs::kWebKitMinimumFontSize); 62 AddPrefToObserve(prefs::kWebKitMinimumFontSize);
66 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled); 63 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled);
67 64
68 TimezoneSettings::GetInstance()->AddObserver(this); 65 TimezoneSettings::GetInstance()->AddObserver(this);
69 } 66 }
70 67
71 void ArcSettingsBridgeImpl::SyncAllPrefs() const { 68 void ArcSettingsBridge::SyncAllPrefs() const {
72 SyncFontSize(); 69 SyncFontSize();
73 SyncLocale(); 70 SyncLocale();
74 SyncSpokenFeedbackEnabled(); 71 SyncSpokenFeedbackEnabled();
75 SyncTimeZone(); 72 SyncTimeZone();
76 } 73 }
77 74
78 void ArcSettingsBridgeImpl::StopObservingSettingsChanges() { 75 void ArcSettingsBridge::StopObservingSettingsChanges() {
79 registrar_.RemoveAll(); 76 registrar_.RemoveAll();
80 77
81 TimezoneSettings::GetInstance()->RemoveObserver(this); 78 TimezoneSettings::GetInstance()->RemoveObserver(this);
82 } 79 }
83 80
84 void ArcSettingsBridgeImpl::AddPrefToObserve(const std::string& pref_name) { 81 void ArcSettingsBridge::AddPrefToObserve(const std::string& pref_name) {
85 registrar_.Add(pref_name, base::Bind(&ArcSettingsBridgeImpl::OnPrefChanged, 82 registrar_.Add(pref_name, base::Bind(&ArcSettingsBridge::OnPrefChanged,
86 base::Unretained(this))); 83 base::Unretained(this)));
87 } 84 }
88 85
89 void ArcSettingsBridgeImpl::OnPrefChanged(const std::string& pref_name) const { 86 void ArcSettingsBridge::OnPrefChanged(const std::string& pref_name) const {
90 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) { 87 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) {
91 SyncSpokenFeedbackEnabled(); 88 SyncSpokenFeedbackEnabled();
92 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize || 89 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize ||
93 pref_name == prefs::kWebKitDefaultFontSize || 90 pref_name == prefs::kWebKitDefaultFontSize ||
94 pref_name == prefs::kWebKitMinimumFontSize) { 91 pref_name == prefs::kWebKitMinimumFontSize) {
95 SyncFontSize(); 92 SyncFontSize();
96 } else { 93 } else {
97 LOG(ERROR) << "Unknown pref changed."; 94 LOG(ERROR) << "Unknown pref changed.";
98 } 95 }
99 } 96 }
100 97
101 void ArcSettingsBridgeImpl::OnStateChanged(ArcBridgeService::State state) { 98 void ArcSettingsBridge::OnStateChanged(ArcBridgeService::State state) {
102 // ArcBridgeService::State::READY is emitted before ArcSettings app is ready 99 // ArcBridgeService::State::READY is emitted before ArcSettings app is ready
103 // to send broadcasts. Instead we wait for the SettingsInstance to be ready. 100 // to send broadcasts. Instead we wait for the SettingsInstance to be ready.
104 if (state == ArcBridgeService::State::STOPPING) { 101 if (state == ArcBridgeService::State::STOPPING) {
105 StopObservingSettingsChanges(); 102 StopObservingSettingsChanges();
106 } 103 }
107 } 104 }
108 105
109 void ArcSettingsBridgeImpl::OnSettingsInstanceReady() { 106 void ArcSettingsBridge::OnSettingsInstanceReady() {
110 StartObservingSettingsChanges(); 107 StartObservingSettingsChanges();
111 SyncAllPrefs(); 108 SyncAllPrefs();
112 } 109 }
113 110
114 void ArcSettingsBridgeImpl::TimezoneChanged(const icu::TimeZone& timezone) { 111 void ArcSettingsBridge::TimezoneChanged(const icu::TimeZone& timezone) {
115 SyncTimeZone(); 112 SyncTimeZone();
116 } 113 }
117 114
118 int ArcSettingsBridgeImpl::GetIntegerPref(const std::string& pref_name) const { 115 int ArcSettingsBridge::GetIntegerPref(const std::string& pref_name) const {
119 const PrefService::Preference* pref = 116 const PrefService::Preference* pref =
120 registrar_.prefs()->FindPreference(pref_name); 117 registrar_.prefs()->FindPreference(pref_name);
121 DCHECK(pref); 118 DCHECK(pref);
122 int val = -1; 119 int val = -1;
123 bool value_exists = pref->GetValue()->GetAsInteger(&val); 120 bool value_exists = pref->GetValue()->GetAsInteger(&val);
124 DCHECK(value_exists); 121 DCHECK(value_exists);
125 return val; 122 return val;
126 } 123 }
127 124
128 void ArcSettingsBridgeImpl::SyncFontSize() const { 125 void ArcSettingsBridge::SyncFontSize() const {
129 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize); 126 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize);
130 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize); 127 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize);
131 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize); 128 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize);
132 129
133 double android_scale = fontsizes::ConvertFontSizeChromeToAndroid( 130 double android_scale = fontsizes::ConvertFontSizeChromeToAndroid(
134 default_size, default_fixed_size, minimum_size); 131 default_size, default_fixed_size, minimum_size);
135 132
136 base::DictionaryValue extras; 133 base::DictionaryValue extras;
137 extras.SetDouble("scale", android_scale); 134 extras.SetDouble("scale", android_scale);
138 SendSettingsBroadcast("org.chromium.arc.settings.SET_FONT_SCALE", extras); 135 SendSettingsBroadcast("org.chromium.arc.settings.SET_FONT_SCALE", extras);
139 } 136 }
140 137
141 void ArcSettingsBridgeImpl::SyncSpokenFeedbackEnabled() const { 138 void ArcSettingsBridge::SyncSpokenFeedbackEnabled() const {
142 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( 139 const PrefService::Preference* pref = registrar_.prefs()->FindPreference(
143 prefs::kAccessibilitySpokenFeedbackEnabled); 140 prefs::kAccessibilitySpokenFeedbackEnabled);
144 DCHECK(pref); 141 DCHECK(pref);
145 bool enabled = false; 142 bool enabled = false;
146 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled); 143 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled);
147 DCHECK(value_exists); 144 DCHECK(value_exists);
148 base::DictionaryValue extras; 145 base::DictionaryValue extras;
149 extras.SetBoolean("enabled", enabled); 146 extras.SetBoolean("enabled", enabled);
150 SendSettingsBroadcast("org.chromium.arc.settings.SET_SPOKEN_FEEDBACK_ENABLED", 147 SendSettingsBroadcast("org.chromium.arc.settings.SET_SPOKEN_FEEDBACK_ENABLED",
151 extras); 148 extras);
152 } 149 }
153 150
154 void ArcSettingsBridgeImpl::SyncLocale() const { 151 void ArcSettingsBridge::SyncLocale() const {
155 const PrefService::Preference* pref = 152 const PrefService::Preference* pref =
156 registrar_.prefs()->FindPreference(prefs::kApplicationLocale); 153 registrar_.prefs()->FindPreference(prefs::kApplicationLocale);
157 DCHECK(pref); 154 DCHECK(pref);
158 std::string locale; 155 std::string locale;
159 bool value_exists = pref->GetValue()->GetAsString(&locale); 156 bool value_exists = pref->GetValue()->GetAsString(&locale);
160 DCHECK(value_exists); 157 DCHECK(value_exists);
161 base::DictionaryValue extras; 158 base::DictionaryValue extras;
162 extras.SetString("locale", locale); 159 extras.SetString("locale", locale);
163 SendSettingsBroadcast("org.chromium.arc.settings.SET_LOCALE", extras); 160 SendSettingsBroadcast("org.chromium.arc.settings.SET_LOCALE", extras);
164 } 161 }
165 162
166 void ArcSettingsBridgeImpl::SyncTimeZone() const { 163 void ArcSettingsBridge::SyncTimeZone() const {
167 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance(); 164 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance();
168 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID(); 165 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID();
169 base::DictionaryValue extras; 166 base::DictionaryValue extras;
170 extras.SetString("olsonTimeZone", timezoneID); 167 extras.SetString("olsonTimeZone", timezoneID);
171 SendSettingsBroadcast("org.chromium.arc.settings.SET_TIME_ZONE", extras); 168 SendSettingsBroadcast("org.chromium.arc.settings.SET_TIME_ZONE", extras);
172 } 169 }
173 170
174 void ArcSettingsBridgeImpl::SendSettingsBroadcast( 171 void ArcSettingsBridge::SendSettingsBroadcast(
175 const std::string& action, 172 const std::string& action,
176 const base::DictionaryValue& extras) const { 173 const base::DictionaryValue& extras) const {
177 ArcBridgeService* bridge_service = ArcBridgeService::Get(); 174 SettingsInstance* settings_instance =
178 if (!bridge_service || 175 arc_bridge_service()->settings_instance();
179 bridge_service->state() != ArcBridgeService::State::READY) { 176 if (!settings_instance) {
180 LOG(ERROR) << "Bridge service is not ready."; 177 LOG(ERROR) << "Bridge service is not ready.";
181 return; 178 return;
182 } 179 }
183 180
184 std::string extras_json; 181 std::string extras_json;
185 bool write_success = base::JSONWriter::Write(extras, &extras_json); 182 bool write_success = base::JSONWriter::Write(extras, &extras_json);
186 DCHECK(write_success); 183 DCHECK(write_success);
187 bridge_service->settings_instance()->SendBroadcast( 184 settings_instance->SendBroadcast(action, "org.chromium.arc.settings",
188 action, "org.chromium.arc.settings", 185 "org.chromium.arc.settings.SettingsReceiver",
189 "org.chromium.arc.settings.SettingsReceiver", extras_json); 186 extras_json);
190 } 187 }
191 188
192 } // namespace arc 189 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_settings_bridge.h ('k') | chrome/browser/chromeos/arc/arc_settings_bridge_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698