File: tether_availability_operation.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (221 lines) | stat: -rw-r--r-- 8,594 bytes parent folder | download | duplicates (8)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2024 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_availability_operation.h"

#include <memory>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/default_clock.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/tether/connection_preserver.h"
#include "chromeos/ash/components/tether/message_wrapper.h"
#include "chromeos/ash/components/tether/proto/tether.pb.h"
#include "chromeos/ash/components/tether/tether_host_response_recorder.h"

namespace ash::tether {

namespace {

bool IsTetheringAvailableWithValidDeviceStatus(
    const TetherAvailabilityResponse* response) {
  if (!response) {
    return false;
  }

  if (!response->has_device_status()) {
    return false;
  }

  if (!response->has_response_code()) {
    return false;
  }

  const TetherAvailabilityResponse_ResponseCode response_code =
      response->response_code();
  if (response_code ==
          TetherAvailabilityResponse_ResponseCode::
              TetherAvailabilityResponse_ResponseCode_SETUP_NEEDED ||
      response_code ==
          TetherAvailabilityResponse_ResponseCode::
              TetherAvailabilityResponse_ResponseCode_TETHER_AVAILABLE ||
      response_code ==
          TetherAvailabilityResponse_ResponseCode::
              TetherAvailabilityResponse_ResponseCode_LAST_PROVISIONING_FAILED) {
    return true;
  }

  return false;
}

bool AreGmsCoreNotificationsDisabled(
    const TetherAvailabilityResponse* response) {
  if (!response) {
    return false;
  }

  if (!response->has_response_code()) {
    return false;
  }

  return response->response_code() ==
             TetherAvailabilityResponse_ResponseCode::
                 TetherAvailabilityResponse_ResponseCode_NOTIFICATIONS_DISABLED_LEGACY ||
         response->response_code() ==
             TetherAvailabilityResponse_ResponseCode::
                 TetherAvailabilityResponse_ResponseCode_NOTIFICATIONS_DISABLED_WITH_NOTIFICATION_CHANNEL;
}

}  // namespace

TetherAvailabilityOperation::Initializer::Initializer(
    raw_ptr<HostConnection::Factory> host_connection_factory,
    raw_ptr<TetherHostResponseRecorder> tether_host_response_recorder,
    raw_ptr<ConnectionPreserver> connection_preserver)
    : host_connection_factory_(host_connection_factory),
      tether_host_response_recorder_(tether_host_response_recorder),
      connection_preserver_(connection_preserver) {}

TetherAvailabilityOperation::Initializer::~Initializer() = default;

std::unique_ptr<TetherAvailabilityOperation>
TetherAvailabilityOperation::Initializer::Initialize(
    const TetherHost& tether_host,
    TetherAvailabilityOperation::OnTetherAvailabilityOperationFinishedCallback
        callback) {
  auto operation = std::make_unique<TetherAvailabilityOperation>(
      tether_host, std::move(callback), host_connection_factory_,
      tether_host_response_recorder_, connection_preserver_);
  operation->Initialize();
  return operation;
}

TetherAvailabilityOperation::TetherAvailabilityOperation(
    const TetherHost& tether_host,
    TetherAvailabilityOperation::OnTetherAvailabilityOperationFinishedCallback
        callback,
    raw_ptr<HostConnection::Factory> host_connection_factory,
    TetherHostResponseRecorder* tether_host_response_recorder,
    ConnectionPreserver* connection_preserver)
    : MessageTransferOperation(
          tether_host,
          HostConnection::Factory::ConnectionPriority::kLow,
          host_connection_factory),
      tether_host_(tether_host),
      tether_host_response_recorder_(tether_host_response_recorder),
      connection_preserver_(connection_preserver),
      clock_(base::DefaultClock::GetInstance()),
      task_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()),
      on_operation_finished_(std::move(callback)) {}

TetherAvailabilityOperation::~TetherAvailabilityOperation() = default;

void TetherAvailabilityOperation::OnDeviceAuthenticated() {
  CHECK(!tether_availability_request_start_time_.has_value());
  tether_availability_request_start_time_ = clock_->Now();
  PA_LOG(VERBOSE) << "Sending TetherAvailabilityRequest message to "
                  << GetDeviceId(/*truncate_for_logs=*/true) << ".";
  SendMessage(std::make_unique<MessageWrapper>(TetherAvailabilityRequest()),
              /*on_message_sent=*/base::DoNothing());
}

void TetherAvailabilityOperation::OnMessageReceived(
    std::unique_ptr<MessageWrapper> message_wrapper) {
  if (message_wrapper->GetMessageType() !=
      MessageType::TETHER_AVAILABILITY_RESPONSE) {
    // If another type of message has been received, ignore it.
    return;
  }

  TetherAvailabilityResponse* response =
      static_cast<TetherAvailabilityResponse*>(message_wrapper->GetProto());
  if (AreGmsCoreNotificationsDisabled(response)) {
    PA_LOG(WARNING)
        << "Received TetherAvailabilityResponse from device with ID "
        << GetDeviceId(/*truncate_for_logs=*/true) << " which "
        << "indicates that Google Play Services notifications are "
        << "disabled. Response code: " << response->response_code();

    scanned_device_info_result_ = ScannedDeviceInfo(
        tether_host_.GetDeviceId(), tether_host_.GetName(), std::nullopt,
        /*setup_required=*/false, /*notifications_enabled=*/false);
  } else if (!IsTetheringAvailableWithValidDeviceStatus(response)) {
    // If the received message is invalid or if it states that tethering is
    // unavailable, ignore it.
    PA_LOG(WARNING)
        << "Received TetherAvailabilityResponse from device with ID "
        << GetDeviceId(/*truncate_for_logs=*/true) << " which "
        << "indicates that tethering is not available. Response code: "
        << response->response_code();
  } else {
    bool setup_required =
        response->response_code() ==
        TetherAvailabilityResponse_ResponseCode::
            TetherAvailabilityResponse_ResponseCode_SETUP_NEEDED;

    PA_LOG(VERBOSE)
        << "Received TetherAvailabilityResponse from device with ID "
        << GetDeviceId(/*truncate_for_logs=*/true) << " which "
        << "indicates that tethering is available. setup_required = "
        << setup_required;

    tether_host_response_recorder_->RecordSuccessfulTetherAvailabilityResponse(
        GetDeviceId(/*truncate_for_logs=*/false));

    // Only attempt to preserve the BLE connection to this device if the
    // response indicated that the device can serve as a host.
    connection_preserver_->HandleSuccessfulTetherAvailabilityResponse(
        GetDeviceId(/*truncate_for_logs=*/false));

    scanned_device_info_result_ =
        ScannedDeviceInfo(tether_host_.GetDeviceId(), tether_host_.GetName(),
                          response->device_status(), setup_required,
                          /*notifications_enabled=*/true);
  }

  RecordTetherAvailabilityResponseDuration(
      GetDeviceId(/*truncate_for_logs=*/false));

  // Unregister the device after a TetherAvailabilityResponse has been received.
  // Delay this in order to let |connection_preserver_| fully preserve the
  // connection, if necessary, before attempting to tear down the connection.
  task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&TetherAvailabilityOperation::StopOperation,
                                weak_ptr_factory_.GetWeakPtr()));
}

void TetherAvailabilityOperation::OnOperationFinished() {
  std::move(on_operation_finished_).Run(scanned_device_info_result_);
}

MessageType TetherAvailabilityOperation::GetMessageTypeForConnection() {
  return MessageType::TETHER_AVAILABILITY_REQUEST;
}

void TetherAvailabilityOperation::SetTestDoubles(
    base::Clock* clock_for_test,
    scoped_refptr<base::TaskRunner> test_task_runner) {
  clock_ = clock_for_test;
  task_runner_ = test_task_runner;
}

void TetherAvailabilityOperation::RecordTetherAvailabilityResponseDuration(
    const std::string device_id) {
  if (!tether_availability_request_start_time_.has_value()) {
    LOG(ERROR) << "Failed to record TetherAvailabilityResponse duration: "
               << "start time is invalid";
    return;
  }

  UMA_HISTOGRAM_TIMES(
      "InstantTethering.Performance.TetherAvailabilityResponseDuration",
      clock_->Now() - *tether_availability_request_start_time_);
  tether_availability_request_start_time_.reset();
}

}  // namespace ash::tether