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

Side by Side Diff: chrome/browser/chromeos/tether/tether_service.cc

Issue 2900903002: Create a FakeTetherService, which stubs out TetherService for development. (Closed)
Patch Set: Fix TetherService tests. Created 3 years, 7 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/tether/tether_service.h" 5 #include "chrome/browser/chromeos/tether/tether_service.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "chrome/browser/chromeos/net/tether_notification_presenter.h" 11 #include "chrome/browser/chromeos/net/tether_notification_presenter.h"
12 #include "chrome/browser/chromeos/tether/tether_service_factory.h" 12 #include "chrome/browser/chromeos/tether/tether_service_factory.h"
13 #include "chrome/browser/cryptauth/chrome_cryptauth_service_factory.h" 13 #include "chrome/browser/cryptauth/chrome_cryptauth_service_factory.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
17 #include "chromeos/chromeos_switches.h" 17 #include "chromeos/chromeos_switches.h"
18 #include "chromeos/components/tether/initializer.h" 18 #include "chromeos/components/tether/initializer.h"
19 #include "chromeos/network/network_connect.h" 19 #include "chromeos/network/network_connect.h"
20 #include "chromeos/network/network_type_pattern.h" 20 #include "chromeos/network/network_type_pattern.h"
21 #include "components/cryptauth/cryptauth_service.h" 21 #include "components/cryptauth/cryptauth_service.h"
22 #include "components/prefs/pref_registry_simple.h" 22 #include "components/prefs/pref_registry_simple.h"
23 #include "components/prefs/pref_service.h" 23 #include "components/prefs/pref_service.h"
24 #include "device/bluetooth/bluetooth_adapter_factory.h" 24 #include "device/bluetooth/bluetooth_adapter_factory.h"
25 #include "ui/message_center/message_center.h" 25 #include "ui/message_center/message_center.h"
26 26
27 // static 27 // static
28 TetherService* TetherService::Get(Profile* profile) { 28 TetherService* TetherService::Get(Profile* profile) {
29 return TetherServiceFactory::GetForBrowserContext(profile); 29 if (IsFeatureFlagEnabled())
30 return TetherServiceFactory::GetForBrowserContext(profile);
31
32 return nullptr;
30 } 33 }
31 34
32 // static 35 // static
33 void TetherService::RegisterProfilePrefs(PrefRegistrySimple* registry) { 36 void TetherService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
34 registry->RegisterBooleanPref(prefs::kInstantTetheringAllowed, true); 37 registry->RegisterBooleanPref(prefs::kInstantTetheringAllowed, true);
35 registry->RegisterBooleanPref(prefs::kInstantTetheringEnabled, true); 38 registry->RegisterBooleanPref(prefs::kInstantTetheringEnabled, true);
36 chromeos::tether::Initializer::RegisterProfilePrefs(registry); 39 chromeos::tether::Initializer::RegisterProfilePrefs(registry);
37 } 40 }
38 41
42 // static
43 bool TetherService::IsFeatureFlagEnabled() {
44 return base::CommandLine::ForCurrentProcess()->HasSwitch(
45 chromeos::switches::kEnableTether);
46 }
47
39 TetherService::TetherService( 48 TetherService::TetherService(
40 Profile* profile, 49 Profile* profile,
41 chromeos::PowerManagerClient* power_manager_client, 50 chromeos::PowerManagerClient* power_manager_client,
42 chromeos::SessionManagerClient* session_manager_client, 51 chromeos::SessionManagerClient* session_manager_client,
43 cryptauth::CryptAuthService* cryptauth_service, 52 cryptauth::CryptAuthService* cryptauth_service,
44 chromeos::NetworkStateHandler* network_state_handler) 53 chromeos::NetworkStateHandler* network_state_handler)
45 : profile_(profile), 54 : profile_(profile),
46 power_manager_client_(power_manager_client), 55 power_manager_client_(power_manager_client),
47 session_manager_client_(session_manager_client), 56 session_manager_client_(session_manager_client),
48 cryptauth_service_(cryptauth_service), 57 cryptauth_service_(cryptauth_service),
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 profile_->GetPrefs()->SetBoolean(prefs::kInstantTetheringEnabled, 178 profile_->GetPrefs()->SetBoolean(prefs::kInstantTetheringEnabled,
170 is_enabled); 179 is_enabled);
171 UpdateTetherTechnologyState(); 180 UpdateTetherTechnologyState();
172 } 181 }
173 } 182 }
174 183
175 void TetherService::OnPrefsChanged() { 184 void TetherService::OnPrefsChanged() {
176 UpdateTetherTechnologyState(); 185 UpdateTetherTechnologyState();
177 } 186 }
178 187
188 bool TetherService::HasSyncedTetherHosts() const {
189 return !cryptauth_service_->GetCryptAuthDeviceManager()
190 ->GetTetherHosts()
191 .empty();
192 }
193
179 void TetherService::UpdateTetherTechnologyState() { 194 void TetherService::UpdateTetherTechnologyState() {
180 chromeos::NetworkStateHandler::TechnologyState tether_technology_state = 195 chromeos::NetworkStateHandler::TechnologyState tether_technology_state =
181 GetTetherTechnologyState(); 196 GetTetherTechnologyState();
182 197
183 network_state_handler_->SetTetherTechnologyState(tether_technology_state); 198 network_state_handler_->SetTetherTechnologyState(tether_technology_state);
184 199
185 if (tether_technology_state == 200 if (tether_technology_state ==
186 chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED) { 201 chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED) {
187 StartTetherIfEnabled(); 202 StartTetherIfEnabled();
188 } else { 203 } else {
189 StopTether(); 204 StopTether();
190 } 205 }
191 } 206 }
192 207
193 chromeos::NetworkStateHandler::TechnologyState 208 chromeos::NetworkStateHandler::TechnologyState
194 TetherService::GetTetherTechnologyState() { 209 TetherService::GetTetherTechnologyState() {
195 if (shut_down_ || suspended_ || session_manager_client_->IsScreenLocked() || 210 if (shut_down_ || suspended_ || session_manager_client_->IsScreenLocked() ||
196 !IsFeatureFlagEnabled() || !HasSyncedTetherHosts()) { 211 !HasSyncedTetherHosts()) {
197 return chromeos::NetworkStateHandler::TechnologyState:: 212 return chromeos::NetworkStateHandler::TechnologyState::
198 TECHNOLOGY_UNAVAILABLE; 213 TECHNOLOGY_UNAVAILABLE;
199 } else if (!IsAllowedByPolicy()) { 214 } else if (!IsAllowedByPolicy()) {
200 return chromeos::NetworkStateHandler::TechnologyState:: 215 return chromeos::NetworkStateHandler::TechnologyState::
201 TECHNOLOGY_PROHIBITED; 216 TECHNOLOGY_PROHIBITED;
202 } else if (!IsBluetoothAvailable()) { 217 } else if (!IsBluetoothAvailable()) {
203 // TODO (hansberry): This unfortunately results in a weird UI state for 218 // TODO (hansberry): This unfortunately results in a weird UI state for
204 // Settings where the toggle is clickable but immediately becomes disabled 219 // Settings where the toggle is clickable but immediately becomes disabled
205 // after enabling it. Possible solution: grey out the toggle and tell the 220 // after enabling it. Possible solution: grey out the toggle and tell the
206 // user to turn Bluetooth on? 221 // user to turn Bluetooth on?
207 return chromeos::NetworkStateHandler::TechnologyState:: 222 return chromeos::NetworkStateHandler::TechnologyState::
208 TECHNOLOGY_UNINITIALIZED; 223 TECHNOLOGY_UNINITIALIZED;
209 } else if (!IsEnabledbyPreference()) { 224 } else if (!IsEnabledbyPreference()) {
210 return chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_AVAILABLE; 225 return chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_AVAILABLE;
211 } 226 }
212 227
213 return chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED; 228 return chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED;
214 } 229 }
215 230
216 void TetherService::OnBluetoothAdapterFetched( 231 void TetherService::OnBluetoothAdapterFetched(
217 scoped_refptr<device::BluetoothAdapter> adapter) { 232 scoped_refptr<device::BluetoothAdapter> adapter) {
218 adapter_ = adapter; 233 adapter_ = adapter;
219 adapter_->AddObserver(this); 234 adapter_->AddObserver(this);
220 UpdateTetherTechnologyState(); 235 UpdateTetherTechnologyState();
221 } 236 }
222 237
223 bool TetherService::HasSyncedTetherHosts() const {
224 return !cryptauth_service_->GetCryptAuthDeviceManager()
225 ->GetTetherHosts()
226 .empty();
227 }
228
229 bool TetherService::IsFeatureFlagEnabled() const {
230 return base::CommandLine::ForCurrentProcess()->HasSwitch(
231 chromeos::switches::kEnableTether);
232 }
233
234 bool TetherService::IsBluetoothAvailable() const { 238 bool TetherService::IsBluetoothAvailable() const {
235 return adapter_.get() && adapter_->IsPresent() && adapter_->IsPowered(); 239 return adapter_.get() && adapter_->IsPresent() && adapter_->IsPowered();
236 } 240 }
237 241
238 bool TetherService::IsAllowedByPolicy() const { 242 bool TetherService::IsAllowedByPolicy() const {
239 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringAllowed); 243 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringAllowed);
240 } 244 }
241 245
242 bool TetherService::IsEnabledbyPreference() const { 246 bool TetherService::IsEnabledbyPreference() const {
243 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringEnabled); 247 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringEnabled);
244 } 248 }
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/tether/tether_service.h ('k') | chrome/browser/chromeos/tether/tether_service_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698