File: nearby_share_contact_downloader_impl.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 (232 lines) | stat: -rw-r--r-- 9,002 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
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
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/nearby_sharing/contacts/nearby_share_contact_downloader_impl.h"

#include <utility>
#include <vector>

#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_client.h"
#include "components/cross_device/logging/logging.h"

namespace {

void RecordListContactPeopleResultMetrics(
    ash::nearby::NearbyHttpResult result) {
  base::UmaHistogramEnumeration("Nearby.Share.Contacts.HttpResult", result);
}

void RecordContactDownloadResultMetrics(bool success,
                                        size_t current_page_number,
                                        base::TimeTicks start_timestamp_) {
  base::UmaHistogramBoolean("Nearby.Share.Contacts.DownloadResult", success);
  if (success) {
    base::UmaHistogramCounts100(
        "Nearby.Share.Contacts.DownloadPageCount.Success", current_page_number);
    base::UmaHistogramTimes("Nearby.Share.Contacts.TimeToDownload.Success",
                            base::TimeTicks::Now() - start_timestamp_);
  } else {
    base::UmaHistogramCounts100(
        "Nearby.Share.Contacts.DownloadPageCount.Failure", current_page_number);
    base::UmaHistogramTimes("Nearby.Share.Contacts.TimeToDownload.Failure",
                            base::TimeTicks::Now() - start_timestamp_);
  }
}

void RecordContactDistributionMetrics(
    const std::vector<nearby::sharing::proto::ContactRecord>&
        unfiltered_contacts) {
  size_t num_reachable = 0;
  size_t num_unknown_type = 0;
  size_t num_google_type = 0;
  size_t num_device_type = 0;
  for (const auto& contact : unfiltered_contacts) {
    if (contact.is_reachable())
      ++num_reachable;

    switch (contact.type()) {
      case nearby::sharing::proto::ContactRecord::UNKNOWN:
        ++num_unknown_type;
        break;
      case nearby::sharing::proto::ContactRecord::GOOGLE_CONTACT:
        ++num_google_type;
        break;
      case nearby::sharing::proto::ContactRecord::DEVICE_CONTACT:
        ++num_device_type;
        break;
      case nearby::sharing::proto::
          ContactRecord_Type_ContactRecord_Type_INT_MIN_SENTINEL_DO_NOT_USE_:
      case nearby::sharing::proto::
          ContactRecord_Type_ContactRecord_Type_INT_MAX_SENTINEL_DO_NOT_USE_:
        NOTREACHED();
    }
  }
  base::UmaHistogramCounts10000("Nearby.Share.Contacts.NumContacts.Unfiltered",
                                unfiltered_contacts.size());
  base::UmaHistogramCounts10000("Nearby.Share.Contacts.NumContacts.Reachable",
                                num_reachable);
  base::UmaHistogramCounts10000("Nearby.Share.Contacts.NumContacts.Unreachable",
                                unfiltered_contacts.size() - num_reachable);
  base::UmaHistogramCounts10000(
      "Nearby.Share.Contacts.NumContacts.UnknownContactType", num_unknown_type);
  base::UmaHistogramCounts10000(
      "Nearby.Share.Contacts.NumContacts.GoogleContactType", num_google_type);
  base::UmaHistogramCounts10000(
      "Nearby.Share.Contacts.NumContacts.DeviceContactType", num_device_type);
  if (!unfiltered_contacts.empty()) {
    base::UmaHistogramPercentage(
        "Nearby.Share.Contacts.PercentReachable",
        std::lround(100.0f * num_reachable / unfiltered_contacts.size()));
    base::UmaHistogramPercentage(
        "Nearby.Share.Contacts.PercentDeviceContactType",
        std::lround(100.0f * num_device_type / unfiltered_contacts.size()));
  }
}

}  // namespace

// static
NearbyShareContactDownloaderImpl::Factory*
    NearbyShareContactDownloaderImpl::Factory::test_factory_ = nullptr;

// static
std::unique_ptr<NearbyShareContactDownloader>
NearbyShareContactDownloaderImpl::Factory::Create(
    const std::string& device_id,
    base::TimeDelta timeout,
    NearbyShareClientFactory* client_factory,
    SuccessCallback success_callback,
    FailureCallback failure_callback) {
  if (test_factory_) {
    return test_factory_->CreateInstance(device_id, timeout, client_factory,
                                         std::move(success_callback),
                                         std::move(failure_callback));
  }

  return base::WrapUnique(new NearbyShareContactDownloaderImpl(
      device_id, timeout, client_factory, std::move(success_callback),
      std::move(failure_callback)));
}

// static
void NearbyShareContactDownloaderImpl::Factory::SetFactoryForTesting(
    Factory* test_factory) {
  test_factory_ = test_factory;
}

NearbyShareContactDownloaderImpl::Factory::~Factory() = default;

NearbyShareContactDownloaderImpl::NearbyShareContactDownloaderImpl(
    const std::string& device_id,
    base::TimeDelta timeout,
    NearbyShareClientFactory* client_factory,
    SuccessCallback success_callback,
    FailureCallback failure_callback)
    : NearbyShareContactDownloader(device_id,
                                   std::move(success_callback),
                                   std::move(failure_callback)),
      timeout_(timeout),
      client_factory_(client_factory) {}

NearbyShareContactDownloaderImpl::~NearbyShareContactDownloaderImpl() = default;

void NearbyShareContactDownloaderImpl::OnRun() {
  CD_LOG(VERBOSE, Feature::NS) << __func__ << ": Starting contacts download.";
  start_timestamp_ = base::TimeTicks::Now();
  CallListContactPeople(/*next_page_token=*/std::nullopt);
}

void NearbyShareContactDownloaderImpl::CallListContactPeople(
    const std::optional<std::string>& next_page_token) {
  ++current_page_number_;
  CD_LOG(VERBOSE, Feature::NS)
      << __func__ << ": Making ListContactPeople RPC call to fetch page number "
      << current_page_number_
      << " with page token: " << next_page_token.value_or("[null]");
  timer_.Start(
      FROM_HERE, timeout_,
      base::BindOnce(
          &NearbyShareContactDownloaderImpl::OnListContactPeopleTimeout,
          base::Unretained(this)));

  nearby::sharing::proto::ListContactPeopleRequest request;
  if (next_page_token)
    request.set_page_token(*next_page_token);

  client_ = client_factory_->CreateInstance();
  client_->ListContactPeople(
      request,
      base::BindOnce(
          &NearbyShareContactDownloaderImpl::OnListContactPeopleSuccess,
          base::Unretained(this)),
      base::BindOnce(
          &NearbyShareContactDownloaderImpl::OnListContactPeopleFailure,
          base::Unretained(this)));
}

void NearbyShareContactDownloaderImpl::OnListContactPeopleSuccess(
    const nearby::sharing::proto::ListContactPeopleResponse& response) {
  timer_.Stop();
  contacts_.insert(contacts_.end(), response.contact_records().begin(),
                   response.contact_records().end());
  std::optional<std::string> next_page_token =
      response.next_page_token().empty()
          ? std::nullopt
          : std::make_optional<std::string>(response.next_page_token());
  client_.reset();
  RecordListContactPeopleResultMetrics(ash::nearby::NearbyHttpResult::kSuccess);

  if (next_page_token) {
    CallListContactPeople(next_page_token);
    return;
  }

  CD_LOG(VERBOSE, Feature::NS) << __func__ << ": Download of "
                               << contacts_.size() << " contacts succeeded.";
  RecordContactDownloadResultMetrics(/*success=*/true, current_page_number_,
                                     start_timestamp_);
  RecordContactDistributionMetrics(contacts_);

  // Remove unreachable contacts.
  size_t initial_num_contacts = contacts_.size();
  std::erase_if(contacts_,
                [](const nearby::sharing::proto::ContactRecord& contact) {
                  return !contact.is_reachable();
                });
  uint32_t num_unreachable_contacts_filtered_out =
      initial_num_contacts - contacts_.size();
  CD_LOG(VERBOSE, Feature::NS)
      << __func__ << ": Removed " << num_unreachable_contacts_filtered_out
      << " unreachable contacts.";

  Succeed(std::move(contacts_), num_unreachable_contacts_filtered_out);
}

void NearbyShareContactDownloaderImpl::OnListContactPeopleFailure(
    ash::nearby::NearbyHttpError error) {
  timer_.Stop();
  client_.reset();
  RecordListContactPeopleResultMetrics(
      ash::nearby::NearbyHttpErrorToResult(error));
  RecordContactDownloadResultMetrics(/*success=*/false, current_page_number_,
                                     start_timestamp_);

  CD_LOG(ERROR, Feature::NS)
      << __func__ << ": Contact download RPC call failed with error " << error
      << " fetching page number " << current_page_number_;
  Fail();
}

void NearbyShareContactDownloaderImpl::OnListContactPeopleTimeout() {
  client_.reset();
  RecordListContactPeopleResultMetrics(ash::nearby::NearbyHttpResult::kTimeout);
  RecordContactDownloadResultMetrics(/*success=*/false, current_page_number_,
                                     start_timestamp_);

  CD_LOG(ERROR, Feature::NS)
      << __func__ << ": Contact download RPC call timed out.";
  Fail();
}