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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/update_client/ping_manager.h"
#include <stddef.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/update_client/configurator.h"
#include "components/update_client/protocol_builder.h"
#include "components/update_client/request_sender.h"
#include "components/update_client/utils.h"
#include "net/url_request/url_fetcher.h"
#include "url/gurl.h"
namespace update_client {
namespace {
const int kErrorNoEvents = -1;
const int kErrorNoUrl = -2;
// An instance of this class can send only one ping.
class PingSender : public base::RefCountedThreadSafe<PingSender> {
public:
using Callback = PingManager::Callback;
explicit PingSender(scoped_refptr<Configurator> config);
void SendPing(const Component& component, Callback callback);
protected:
virtual ~PingSender();
private:
friend class base::RefCountedThreadSafe<PingSender>;
void SendPingComplete(int error,
const std::string& response,
int retry_after_sec);
THREAD_CHECKER(thread_checker_);
const scoped_refptr<Configurator> config_;
Callback callback_;
std::unique_ptr<RequestSender> request_sender_;
DISALLOW_COPY_AND_ASSIGN(PingSender);
};
PingSender::PingSender(scoped_refptr<Configurator> config) : config_(config) {}
PingSender::~PingSender() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
void PingSender::SendPing(const Component& component, Callback callback) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (component.events().empty()) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), kErrorNoEvents, ""));
return;
}
DCHECK(component.crx_component());
auto urls(config_->PingUrl());
if (component.crx_component()->requires_network_encryption)
RemoveUnsecureUrls(&urls);
if (urls.empty()) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), kErrorNoUrl, ""));
return;
}
callback_ = std::move(callback);
request_sender_ = std::make_unique<RequestSender>(config_);
request_sender_->Send(urls, {}, BuildEventPingRequest(*config_, component),
false,
base::BindOnce(&PingSender::SendPingComplete, this));
}
void PingSender::SendPingComplete(int error,
const std::string& response,
int retry_after_sec) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
std::move(callback_).Run(error, response);
}
} // namespace
PingManager::PingManager(scoped_refptr<Configurator> config)
: config_(config) {}
PingManager::~PingManager() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
void PingManager::SendPing(const Component& component, Callback callback) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto ping_sender = base::MakeRefCounted<PingSender>(config_);
ping_sender->SendPing(component, std::move(callback));
}
} // namespace update_client
|