File: active_host.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (165 lines) | stat: -rw-r--r-- 6,253 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_TETHER_ACTIVE_HOST_H_
#define CHROMEOS_ASH_COMPONENTS_TETHER_ACTIVE_HOST_H_

#include <optional>
#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "chromeos/ash/components/multidevice/remote_device_ref.h"

class PrefRegistrySimple;
class PrefService;

namespace ash {

namespace tether {

class TetherHostFetcher;

// ActiveHost tracks metadata about the current connection to a tether host.
// This data is persisted to user preferences.
class ActiveHost {
 public:
  // Enumeration used to describe the state of the active connection to a tether
  // host.
  enum class ActiveHostStatus {
    DISCONNECTED = 0,
    CONNECTING = 1,
    CONNECTED = 2
  };
  static std::string StatusToString(const ActiveHostStatus& status);

  struct ActiveHostChangeInfo {
    ActiveHostChangeInfo();
    ActiveHostChangeInfo(
        ActiveHostStatus new_status,
        ActiveHostStatus old_status,
        std::optional<multidevice::RemoteDeviceRef> new_active_host,
        std::string old_active_host_id,
        std::string new_tether_network_guid,
        std::string old_tether_network_guid,
        std::string new_wifi_network_guid,
        std::string old_wifi_network_guid);
    ActiveHostChangeInfo(const ActiveHostChangeInfo& other);

    virtual ~ActiveHostChangeInfo();

    friend bool operator==(const ActiveHostChangeInfo& first,
                           const ActiveHostChangeInfo& second);

    ActiveHostStatus new_status;
    ActiveHostStatus old_status;

    // |new_active_host| will be empty if |new_status| is DISCONNECTED.
    std::optional<multidevice::RemoteDeviceRef> new_active_host;
    // |old_active_host_id| will be "" if |old_status| is DISCONNECTED.
    std::string old_active_host_id;

    // |new_tether_network_guid| will be "" if |new_status| is DISCONNECTED.
    std::string new_tether_network_guid;
    // |old_tether_network_guid| will be "" if |old_status| is DISCONNECTED.
    std::string old_tether_network_guid;

    // |new_wifi_network_guid| will be "" if |new_status| is not CONNECTED.
    std::string new_wifi_network_guid;
    // |old_wifi_network_guid| will be "" if |old_status| is not CONNECTED.
    std::string old_wifi_network_guid;
  };

  class Observer {
   public:
    virtual void OnActiveHostChanged(
        const ActiveHostChangeInfo& active_host_change_info) = 0;
  };

  ActiveHost(TetherHostFetcher* tether_host_fetcher, PrefService* pref_service);

  ActiveHost(const ActiveHost&) = delete;
  ActiveHost& operator=(const ActiveHost&) = delete;

  virtual ~ActiveHost();

  // Registers the prefs used by this class to the given |registry|.
  static void RegisterPrefs(PrefRegistrySimple* registry);

  // Sets the active host to be no host at all (i.e., the local device is not
  // connecting or connected to a tether host).
  virtual void SetActiveHostDisconnected();

  // Sets the active host to be the device with ID |active_host_device_id| and
  // the associated tether network GUID to |tether_network_guid| and records
  // that the there is an active attempt to connect to that host (i.e., the host
  // is not yet connected but it is in the process of connecting).
  virtual void SetActiveHostConnecting(const std::string& active_host_device_id,
                                       const std::string& tether_network_guid);

  // Sets the active host to be the device with ID |active_host_device_id| and
  // that the local device is connected to that device on the mobile hotspot
  // with tether network GUID |tether_network_guid| and Wi-Fi network GUID
  // |wifi_network_guid|.
  virtual void SetActiveHostConnected(const std::string& active_host_device_id,
                                      const std::string& tether_network_guid,
                                      const std::string& wifi_network_guid);

  // Gets the active host and associated metadata asynchronously. If
  // the active host status is...
  //     DISCONNECTED: The callback's |active_host| parameter will be nullptr
  //                   and |wifi_network_guid| and |tether_network_guid|
  //                   parameters will be "".
  //     CONNECTING: The callback's |wifi_network_guid| parameter will be "".
  //     CONNECTED: All four parameters  will be present.
  using ActiveHostCallback = base::OnceCallback<void(
      ActiveHostStatus active_host_status,
      std::optional<multidevice::RemoteDeviceRef> active_host,
      const std::string& tether_network_guid,
      const std::string& wifi_network_guid)>;
  virtual void GetActiveHost(ActiveHostCallback active_host_callback);

  // Synchronous getter methods which do not return a full RemoteDevice object.
  virtual ActiveHostStatus GetActiveHostStatus() const;
  virtual std::string GetActiveHostDeviceId() const;
  virtual std::string GetWifiNetworkGuid() const;
  virtual std::string GetTetherNetworkGuid() const;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 protected:
  void SendActiveHostChangedUpdate(
      ActiveHostStatus old_status,
      const std::string& old_active_host_id,
      const std::string& old_tether_network_guid,
      const std::string& old_wifi_network_guid,
      ActiveHostStatus new_status,
      std::optional<multidevice::RemoteDeviceRef> new_active_host,
      const std::string& new_tether_network_guid,
      const std::string& new_wifi_network_guid);

 private:
  friend class CrashRecoveryManagerImpl;

  void SetActiveHost(ActiveHostStatus active_host_status,
                     const std::string& active_host_device_id,
                     const std::string& tether_network_guid,
                     const std::string& wifi_network_guid);

  raw_ptr<TetherHostFetcher> tether_host_fetcher_;
  raw_ptr<PrefService> pref_service_;

  base::ObserverList<Observer>::Unchecked observer_list_;

  base::WeakPtrFactory<ActiveHost> weak_ptr_factory_{this};
};

}  // namespace tether

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_TETHER_ACTIVE_HOST_H_