| OLD | NEW |
| 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 "base/threading/thread_task_runner_handle.h" |
| 11 #include "chrome/browser/chromeos/net/tether_notification_presenter.h" | 12 #include "chrome/browser/chromeos/net/tether_notification_presenter.h" |
| 12 #include "chrome/browser/chromeos/tether/tether_service_factory.h" | 13 #include "chrome/browser/chromeos/tether/tether_service_factory.h" |
| 13 #include "chrome/browser/cryptauth/chrome_cryptauth_service_factory.h" | 14 #include "chrome/browser/cryptauth/chrome_cryptauth_service_factory.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 17 #include "chromeos/chromeos_switches.h" | 18 #include "chromeos/chromeos_switches.h" |
| 18 #include "chromeos/components/tether/initializer.h" | 19 #include "chromeos/components/tether/initializer.h" |
| 19 #include "chromeos/network/network_connect.h" | 20 #include "chromeos/network/network_connect.h" |
| 20 #include "chromeos/network/network_type_pattern.h" | 21 #include "chromeos/network/network_type_pattern.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 | 63 |
| 63 cryptauth_service_->GetCryptAuthDeviceManager()->AddObserver(this); | 64 cryptauth_service_->GetCryptAuthDeviceManager()->AddObserver(this); |
| 64 | 65 |
| 65 network_state_handler_->AddObserver(this, FROM_HERE); | 66 network_state_handler_->AddObserver(this, FROM_HERE); |
| 66 | 67 |
| 67 registrar_.Init(profile_->GetPrefs()); | 68 registrar_.Init(profile_->GetPrefs()); |
| 68 registrar_.Add(prefs::kInstantTetheringAllowed, | 69 registrar_.Add(prefs::kInstantTetheringAllowed, |
| 69 base::Bind(&TetherService::OnPrefsChanged, | 70 base::Bind(&TetherService::OnPrefsChanged, |
| 70 weak_ptr_factory_.GetWeakPtr())); | 71 weak_ptr_factory_.GetWeakPtr())); |
| 71 | 72 |
| 72 device::BluetoothAdapterFactory::GetAdapter( | 73 // GetAdapter may call OnBluetoothAdapterFetched immediately which can cause |
| 73 base::Bind(&TetherService::OnBluetoothAdapterFetched, | 74 // problems with the Fake implementation since the class is not fully |
| 74 weak_ptr_factory_.GetWeakPtr())); | 75 // constructed yet. Post the GetAdapter call to avoid this. |
| 76 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 77 FROM_HERE, |
| 78 base::Bind(device::BluetoothAdapterFactory::GetAdapter, |
| 79 base::Bind(&TetherService::OnBluetoothAdapterFetched, |
| 80 weak_ptr_factory_.GetWeakPtr()))); |
| 75 } | 81 } |
| 76 | 82 |
| 77 TetherService::~TetherService() {} | 83 TetherService::~TetherService() {} |
| 78 | 84 |
| 79 void TetherService::StartTetherIfEnabled() { | 85 void TetherService::StartTetherIfEnabled() { |
| 80 if (GetTetherTechnologyState() != | 86 if (GetTetherTechnologyState() != |
| 81 chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED) { | 87 chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED) { |
| 82 return; | 88 return; |
| 83 } | 89 } |
| 84 | 90 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 TECHNOLOGY_UNINITIALIZED; | 229 TECHNOLOGY_UNINITIALIZED; |
| 224 } else if (!IsEnabledbyPreference()) { | 230 } else if (!IsEnabledbyPreference()) { |
| 225 return chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_AVAILABLE; | 231 return chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_AVAILABLE; |
| 226 } | 232 } |
| 227 | 233 |
| 228 return chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED; | 234 return chromeos::NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED; |
| 229 } | 235 } |
| 230 | 236 |
| 231 void TetherService::OnBluetoothAdapterFetched( | 237 void TetherService::OnBluetoothAdapterFetched( |
| 232 scoped_refptr<device::BluetoothAdapter> adapter) { | 238 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 239 if (shut_down_) |
| 240 return; |
| 233 adapter_ = adapter; | 241 adapter_ = adapter; |
| 234 adapter_->AddObserver(this); | 242 adapter_->AddObserver(this); |
| 235 UpdateTetherTechnologyState(); | 243 UpdateTetherTechnologyState(); |
| 236 } | 244 } |
| 237 | 245 |
| 238 bool TetherService::IsBluetoothAvailable() const { | 246 bool TetherService::IsBluetoothAvailable() const { |
| 239 return adapter_.get() && adapter_->IsPresent() && adapter_->IsPowered(); | 247 return adapter_.get() && adapter_->IsPresent() && adapter_->IsPowered(); |
| 240 } | 248 } |
| 241 | 249 |
| 242 bool TetherService::IsAllowedByPolicy() const { | 250 bool TetherService::IsAllowedByPolicy() const { |
| 243 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringAllowed); | 251 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringAllowed); |
| 244 } | 252 } |
| 245 | 253 |
| 246 bool TetherService::IsEnabledbyPreference() const { | 254 bool TetherService::IsEnabledbyPreference() const { |
| 247 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringEnabled); | 255 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringEnabled); |
| 248 } | 256 } |
| OLD | NEW |