1 // Copyright 2016 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 #ifndef CHROMEOS_COMPONENTS_TETHER_TETHER_COMPONENT_IMPL_H_
6 #define CHROMEOS_COMPONENTS_TETHER_TETHER_COMPONENT_IMPL_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "chromeos/components/tether/tether_component.h"
14 #include "components/prefs/pref_registry_simple.h"
15 #include "device/bluetooth/bluetooth_adapter.h"
16 
17 class PrefService;
18 
19 namespace session_manager {
20 class SessionManager;
21 }  // namespace session_manager
22 
23 namespace user_prefs {
24 class PrefRegistrySyncable;
25 }  // namespace user_prefs
26 
27 namespace chromeos {
28 
29 class ManagedNetworkConfigurationHandler;
30 class NetworkConnect;
31 class NetworkConnectionHandler;
32 class NetworkStateHandler;
33 
34 namespace device_sync {
35 class DeviceSyncClient;
36 }  // namespace device_sync
37 
38 namespace secure_channel {
39 class SecureChannelClient;
40 }  // namespace secure_channel
41 
42 namespace tether {
43 
44 class AsynchronousShutdownObjectContainer;
45 class CrashRecoveryManager;
46 class GmsCoreNotificationsStateTrackerImpl;
47 class NotificationPresenter;
48 class SynchronousShutdownObjectContainer;
49 class TetherHostFetcher;
50 
51 // Initializes the Tether component.
52 class TetherComponentImpl : public TetherComponent {
53  public:
54   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
55 
56   class Factory {
57    public:
58     static std::unique_ptr<TetherComponent> Create(
59         device_sync::DeviceSyncClient* device_sync_client,
60         secure_channel::SecureChannelClient* secure_channel_client,
61         TetherHostFetcher* tether_host_fetcher,
62         NotificationPresenter* notification_presenter,
63         GmsCoreNotificationsStateTrackerImpl*
64             gms_core_notifications_state_tracker,
65         PrefService* pref_service,
66         NetworkStateHandler* network_state_handler,
67         ManagedNetworkConfigurationHandler*
68             managed_network_configuration_handler,
69         NetworkConnect* network_connect,
70         NetworkConnectionHandler* network_connection_handler,
71         scoped_refptr<device::BluetoothAdapter> adapter,
72         session_manager::SessionManager* session_manager);
73 
74     static void SetFactoryForTesting(Factory* factory);
75 
76    protected:
77     virtual std::unique_ptr<TetherComponent> CreateInstance(
78         device_sync::DeviceSyncClient* device_sync_client,
79         secure_channel::SecureChannelClient* secure_channel_client,
80         TetherHostFetcher* tether_host_fetcher,
81         NotificationPresenter* notification_presenter,
82         GmsCoreNotificationsStateTrackerImpl*
83             gms_core_notifications_state_tracker,
84         PrefService* pref_service,
85         NetworkStateHandler* network_state_handler,
86         ManagedNetworkConfigurationHandler*
87             managed_network_configuration_handler,
88         NetworkConnect* network_connect,
89         NetworkConnectionHandler* network_connection_handler,
90         scoped_refptr<device::BluetoothAdapter> adapter,
91         session_manager::SessionManager* session_manager) = 0;
92 
93    private:
94     static Factory* factory_instance_;
95   };
96 
97   ~TetherComponentImpl() override;
98 
99   // TetherComponent:
100   void RequestShutdown(const ShutdownReason& shutdown_reason) override;
101 
102  protected:
103   TetherComponentImpl(
104       device_sync::DeviceSyncClient* device_sync_client,
105       secure_channel::SecureChannelClient* secure_channel_client,
106       TetherHostFetcher* tether_host_fetcher,
107       NotificationPresenter* notification_presenter,
108       GmsCoreNotificationsStateTrackerImpl*
109           gms_core_notifications_state_tracker,
110       PrefService* pref_service,
111       NetworkStateHandler* network_state_handler,
112       ManagedNetworkConfigurationHandler* managed_network_configuration_handler,
113       NetworkConnect* network_connect,
114       NetworkConnectionHandler* network_connection_handler,
115       scoped_refptr<device::BluetoothAdapter> adapter,
116       session_manager::SessionManager* session_manager);
117 
118  private:
119   void OnPreCrashStateRestored();
120   void InitiateShutdown();
121   void OnShutdownComplete();
122 
123   std::unique_ptr<AsynchronousShutdownObjectContainer>
124       asynchronous_shutdown_object_container_;
125   std::unique_ptr<SynchronousShutdownObjectContainer>
126       synchronous_shutdown_object_container_;
127   std::unique_ptr<CrashRecoveryManager> crash_recovery_manager_;
128 
129   bool has_shutdown_been_requested_ = false;
130   ShutdownReason shutdown_reason_;
131 
132   base::WeakPtrFactory<TetherComponentImpl> weak_ptr_factory_{this};
133 
134   DISALLOW_COPY_AND_ASSIGN(TetherComponentImpl);
135 };
136 
137 }  // namespace tether
138 
139 }  // namespace chromeos
140 
141 #endif  // CHROMEOS_COMPONENTS_TETHER_TETHER_COMPONENT_IMPL_H_
142