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() || !base::CommandLine::ForCurrentProcess()->HasSwitch( | |
49 chromeos::switches::kEnableTether)) { | |
50 return; | |
51 } | |
52 | |
53 auto notification_presenter = | |
54 base::MakeUnique<chromeos::tether::TetherNotificationPresenter>( | |
55 message_center::MessageCenter::Get(), | |
56 chromeos::NetworkConnect::Get()); | |
57 chromeos::tether::Initializer::Init( | |
58 ChromeCryptAuthServiceFactory::GetForBrowserContext(profile_), | |
59 std::move(notification_presenter), profile_->GetPrefs(), | |
60 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), | |
61 chromeos::NetworkHandler::Get()->network_state_handler(), | |
62 chromeos::NetworkConnect::Get()); | |
63 } | |
64 | |
65 bool TetherService::IsAllowed() const { | |
66 if (shut_down_) | |
67 return false; | |
68 | |
69 return profile_->GetPrefs()->GetBoolean(prefs::kInstantTetheringAllowed); | |
70 } | |
71 | |
72 void TetherService::Shutdown() { | |
73 if (shut_down_) | |
74 return; | |
75 | |
76 shut_down_ = true; | |
77 registrar_.RemoveAll(); | |
78 chromeos::tether::Initializer::Shutdown(); | |
79 } | |
80 | |
81 void TetherService::OnPrefsChanged() { | |
82 // TODO (hansberry): Switch to using an IsEnabled() method. | |
83 if (IsAllowed() && base::CommandLine::ForCurrentProcess()->HasSwitch( | |
84 chromeos::switches::kEnableTether)) { | |
stevenjb
2017/04/21 20:09:17
We should just add IsEnabled in this CL to avoid d
Ryan Hansberry
2017/04/24 17:53:09
Done.
| |
85 StartTether(); | |
86 return; | |
87 } | |
88 | |
89 chromeos::tether::Initializer::Shutdown(); | |
90 } | |
OLD | NEW |