OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/tether/tether_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "chrome/browser/chromeos/net/tether_notification_presenter.h" |
| 11 #include "chrome/browser/chromeos/tether/tether_service_factory.h" |
| 12 #include "chrome/browser/cryptauth/chrome_cryptauth_service_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 15 #include "chrome/common/pref_names.h" |
| 16 #include "chromeos/chromeos_switches.h" |
| 17 #include "chromeos/components/tether/initializer.h" |
| 18 #include "chromeos/network/network_connect.h" |
| 19 #include "chromeos/network/network_state_handler.h" |
| 20 #include "components/pref_registry/pref_registry_syncable.h" |
| 21 #include "components/prefs/pref_service.h" |
| 22 #include "ui/message_center/message_center.h" |
| 23 |
| 24 // static |
| 25 TetherService* TetherService::Get(Profile* profile) { |
| 26 return TetherServiceFactory::GetForBrowserContext(profile); |
| 27 } |
| 28 |
| 29 // static |
| 30 void TetherService::RegisterProfilePrefs( |
| 31 user_prefs::PrefRegistrySyncable* registry) { |
| 32 registry->RegisterBooleanPref(prefs::kInstantTetheringAllowed, true); |
| 33 chromeos::tether::Initializer::RegisterProfilePrefs(registry); |
| 34 } |
| 35 |
| 36 TetherService::TetherService(Profile* profile) |
| 37 : profile_(profile), shut_down_(false) { |
| 38 registrar_.Init(profile_->GetPrefs()); |
| 39 registrar_.Add( |
| 40 prefs::kInstantTetheringAllowed, |
| 41 base::Bind(&TetherService::OnPrefsChanged, base::Unretained(this))); |
| 42 } |
| 43 |
| 44 TetherService::~TetherService() {} |
| 45 |
| 46 void TetherService::StartTether() { |
| 47 // TODO(hansberry): Switch to using an IsEnabled() method. |
| 48 if (!IsAllowed() || !IsEnabled()) { |
| 49 return; |
| 50 } |
| 51 |
| 52 auto notification_presenter = |
| 53 base::MakeUnique<chromeos::tether::TetherNotificationPresenter>( |
| 54 message_center::MessageCenter::Get(), |
| 55 chromeos::NetworkConnect::Get()); |
| 56 chromeos::tether::Initializer::Init( |
| 57 ChromeCryptAuthServiceFactory::GetForBrowserContext(profile_), |
| 58 std::move(notification_presenter), profile_->GetPrefs(), |
| 59 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), |
| 60 chromeos::NetworkHandler::Get()->network_state_handler(), |
| 61 chromeos::NetworkConnect::Get()); |
| 62 } |
| 63 |
| 64 bool TetherService::IsAllowed() const { |
| 65 if (shut_down_) |
| 66 return false; |
| 67 |
| 68 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringAllowed); |
| 69 } |
| 70 |
| 71 bool TetherService::IsEnabled() const { |
| 72 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 73 chromeos::switches::kEnableTether); |
| 74 } |
| 75 |
| 76 void TetherService::Shutdown() { |
| 77 if (shut_down_) |
| 78 return; |
| 79 |
| 80 shut_down_ = true; |
| 81 registrar_.RemoveAll(); |
| 82 chromeos::tether::Initializer::Shutdown(); |
| 83 } |
| 84 |
| 85 void TetherService::OnPrefsChanged() { |
| 86 if (IsAllowed() && IsEnabled()) { |
| 87 StartTether(); |
| 88 return; |
| 89 } |
| 90 |
| 91 chromeos::tether::Initializer::Shutdown(); |
| 92 } |
OLD | NEW |