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_factory.h"
6 
7 #include "base/command_line.h"
8 #include "base/memory/singleton.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/chromeos/device_sync/device_sync_client_factory.h"
11 #include "chrome/browser/chromeos/multidevice_setup/multidevice_setup_client_factory.h"
12 #include "chrome/browser/chromeos/secure_channel/secure_channel_client_provider.h"
13 #include "chrome/browser/chromeos/tether/fake_tether_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/pref_names.h"
16 #include "chromeos/constants/chromeos_switches.h"
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/network/network_handler.h"
19 #include "chromeos/network/network_state_handler.h"
20 #include "chromeos/services/multidevice_setup/public/cpp/prefs.h"
21 #include "chromeos/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
22 #include "components/keyed_service/content/browser_context_dependency_manager.h"
23 #include "components/pref_registry/pref_registry_syncable.h"
24 #include "components/session_manager/core/session_manager.h"
25 
26 namespace {
27 
IsFeatureAllowed(content::BrowserContext * context)28 bool IsFeatureAllowed(content::BrowserContext* context) {
29   return chromeos::multidevice_setup::IsFeatureAllowed(
30       chromeos::multidevice_setup::mojom::Feature::kInstantTethering,
31       Profile::FromBrowserContext(context)->GetPrefs());
32 }
33 
34 }  // namespace
35 
36 // static
GetInstance()37 TetherServiceFactory* TetherServiceFactory::GetInstance() {
38   return base::Singleton<TetherServiceFactory>::get();
39 }
40 
41 // static
GetForBrowserContext(content::BrowserContext * browser_context)42 TetherService* TetherServiceFactory::GetForBrowserContext(
43     content::BrowserContext* browser_context) {
44   return static_cast<TetherService*>(
45       TetherServiceFactory::GetInstance()->GetServiceForBrowserContext(
46           browser_context, true));
47 }
48 
TetherServiceFactory()49 TetherServiceFactory::TetherServiceFactory()
50     : BrowserContextKeyedServiceFactory(
51           "TetherService",
52           BrowserContextDependencyManager::GetInstance()) {
53   DependsOn(chromeos::device_sync::DeviceSyncClientFactory::GetInstance());
54   DependsOn(chromeos::multidevice_setup::MultiDeviceSetupClientFactory::
55                 GetInstance());
56 }
57 
~TetherServiceFactory()58 TetherServiceFactory::~TetherServiceFactory() {}
59 
BuildServiceInstanceFor(content::BrowserContext * context) const60 KeyedService* TetherServiceFactory::BuildServiceInstanceFor(
61     content::BrowserContext* context) const {
62   DCHECK(chromeos::NetworkHandler::IsInitialized());
63 
64   if (!IsFeatureAllowed(context))
65     return nullptr;
66 
67   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
68   if (command_line->HasSwitch(chromeos::switches::kTetherStub)) {
69     FakeTetherService* fake_tether_service = new FakeTetherService(
70         Profile::FromBrowserContext(context),
71         chromeos::PowerManagerClient::Get(),
72         chromeos::device_sync::DeviceSyncClientFactory::GetForProfile(
73             Profile::FromBrowserContext(context)),
74         chromeos::secure_channel::SecureChannelClientProvider::GetInstance()
75             ->GetClient(),
76         chromeos::multidevice_setup::MultiDeviceSetupClientFactory::
77             GetForProfile(Profile::FromBrowserContext(context)),
78         chromeos::NetworkHandler::Get()->network_state_handler(),
79         session_manager::SessionManager::Get());
80 
81     int num_tether_networks = 0;
82     base::StringToInt(
83         command_line->GetSwitchValueASCII(chromeos::switches::kTetherStub),
84         &num_tether_networks);
85     fake_tether_service->set_num_tether_networks(num_tether_networks);
86 
87     return fake_tether_service;
88   }
89 
90   return new TetherService(
91       Profile::FromBrowserContext(context), chromeos::PowerManagerClient::Get(),
92       chromeos::device_sync::DeviceSyncClientFactory::GetForProfile(
93           Profile::FromBrowserContext(context)),
94       chromeos::secure_channel::SecureChannelClientProvider::GetInstance()
95           ->GetClient(),
96       chromeos::multidevice_setup::MultiDeviceSetupClientFactory::GetForProfile(
97           Profile::FromBrowserContext(context)),
98       chromeos::NetworkHandler::Get()->network_state_handler(),
99       session_manager::SessionManager::Get());
100 }
101 
RegisterProfilePrefs(user_prefs::PrefRegistrySyncable * registry)102 void TetherServiceFactory::RegisterProfilePrefs(
103     user_prefs::PrefRegistrySyncable* registry) {
104   TetherService::RegisterProfilePrefs(registry);
105 }
106 
ServiceIsNULLWhileTesting() const107 bool TetherServiceFactory::ServiceIsNULLWhileTesting() const {
108   return true;
109 }
110