File: tether_network_disconnection_handler.cc

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 (97 lines) | stat: -rw-r--r-- 4,394 bytes parent folder | download | duplicates (7)
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
// 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.

#include "chromeos/ash/components/tether/tether_network_disconnection_handler.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/single_thread_task_runner.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/tether/disconnect_tethering_request_sender.h"
#include "chromeos/ash/components/tether/network_configuration_remover.h"
#include "chromeos/ash/components/tether/tether_disconnector.h"
#include "chromeos/ash/components/tether/tether_host_fetcher.h"
#include "chromeos/ash/components/tether/tether_session_completion_logger.h"

namespace ash::tether {

TetherNetworkDisconnectionHandler::TetherNetworkDisconnectionHandler(
    ActiveHost* active_host,
    NetworkStateHandler* network_state_handler,
    NetworkConfigurationRemover* network_configuration_remover,
    DisconnectTetheringRequestSender* disconnect_tethering_request_sender,
    TetherSessionCompletionLogger* tether_session_completion_logger)
    : active_host_(active_host),
      network_state_handler_(network_state_handler),
      network_configuration_remover_(network_configuration_remover),
      disconnect_tethering_request_sender_(disconnect_tethering_request_sender),
      tether_session_completion_logger_(tether_session_completion_logger),
      task_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()) {
  network_state_handler_observer_.Observe(network_state_handler_.get());
}

TetherNetworkDisconnectionHandler::~TetherNetworkDisconnectionHandler() =
    default;

void TetherNetworkDisconnectionHandler::NetworkConnectionStateChanged(
    const NetworkState* network) {
  // Only handle network connection state changes which indicate that the
  // underlying Wi-Fi network for a Tether connection has been disconnected.
  if (network->guid() != active_host_->GetWifiNetworkGuid() ||
      network->IsConnectingOrConnected()) {
    return;
  }

  // Handle disconnection as part of a new task. Posting a task here ensures
  // that processing the disconnection is done after other
  // NetworkStateHandlerObservers are finished running. Processing the
  // disconnection immediately can cause crashes; see https://crbug.com/800370.
  task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&TetherNetworkDisconnectionHandler::
                                    HandleActiveWifiNetworkDisconnection,
                                weak_ptr_factory_.GetWeakPtr(), network->guid(),
                                network->path()));
}

void TetherNetworkDisconnectionHandler::OnShuttingDown() {
  network_state_handler_observer_.Reset();
}

void TetherNetworkDisconnectionHandler::HandleActiveWifiNetworkDisconnection(
    const std::string& network_guid,
    const std::string& network_path) {
  PA_LOG(WARNING) << "Connection to active host (Wi-Fi network GUID "
                  << network_guid << ") has been lost.";

  // Check if Wi-Fi is enabled; if it is, this indicates that the connection
  // to the Tether host dropped. If it isn't, then the event of Wi-Fi being
  // disabled caused the connection to end.
  tether_session_completion_logger_->RecordTetherSessionCompletion(
      network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi())
          ? TetherSessionCompletionLogger::SessionCompletionReason::
                CONNECTION_DROPPED
          : TetherSessionCompletionLogger::SessionCompletionReason::
                WIFI_DISABLED);

  network_configuration_remover_->RemoveNetworkConfigurationByPath(
      network_path);

  // Send a DisconnectTetheringRequest to the tether host so that it can shut
  // down its Wi-Fi hotspot if it is no longer in use.
  disconnect_tethering_request_sender_->SendDisconnectRequestToDevice(
      active_host_->GetActiveHostDeviceId());

  active_host_->SetActiveHostDisconnected();
}

void TetherNetworkDisconnectionHandler::SetTaskRunnerForTesting(
    scoped_refptr<base::TaskRunner> test_task_runner) {
  task_runner_ = test_task_runner;
}

}  // namespace ash::tether