1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/component_updater/component_updater_ping_manager.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/compiler_specific.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/sequenced_task_runner.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/threading/thread_checker.h"
22 #include "components/component_updater/component_updater_configurator.h"
23 #include "components/component_updater/component_updater_utils.h"
24 #include "components/component_updater/crx_update_item.h"
25 #include "components/component_updater/request_sender.h"
32 namespace component_updater
{
36 // Returns a string literal corresponding to the value of the downloader |d|.
37 const char* DownloaderToString(CrxDownloader::DownloadMetrics::Downloader d
) {
39 case CrxDownloader::DownloadMetrics::kUrlFetcher
:
41 case CrxDownloader::DownloadMetrics::kBits
:
48 // Returns a string representing a sequence of download complete events
49 // corresponding to each download metrics in |item|.
50 std::string
BuildDownloadCompleteEventElements(const CrxUpdateItem
* item
) {
51 using base::StringAppendF
;
52 std::string download_events
;
53 for (size_t i
= 0; i
!= item
->download_metrics
.size(); ++i
) {
54 const CrxDownloader::DownloadMetrics
& metrics
= item
->download_metrics
[i
];
55 std::string
event("<event eventtype=\"14\"");
56 StringAppendF(&event
, " eventresult=\"%d\"", metrics
.error
== 0);
59 DownloaderToString(metrics
.downloader
));
61 StringAppendF(&event
, " errorcode=\"%d\"", metrics
.error
);
63 StringAppendF(&event
, " url=\"%s\"", metrics
.url
.spec().c_str());
65 // -1 means that the byte counts are not known.
66 if (metrics
.downloaded_bytes
!= -1) {
69 base::Int64ToString(metrics
.downloaded_bytes
).c_str());
71 if (metrics
.total_bytes
!= -1) {
74 base::Int64ToString(metrics
.total_bytes
).c_str());
77 if (metrics
.download_time_ms
) {
79 " download_time_ms=\"%s\"",
80 base::Uint64ToString(metrics
.download_time_ms
).c_str());
82 StringAppendF(&event
, "/>");
84 download_events
+= event
;
86 return download_events
;
89 // Returns a string representing one ping event xml element for an update item.
90 std::string
BuildUpdateCompleteEventElement(const CrxUpdateItem
* item
) {
91 DCHECK(item
->status
== CrxUpdateItem::kNoUpdate
||
92 item
->status
== CrxUpdateItem::kUpdated
);
94 using base::StringAppendF
;
96 std::string
ping_event("<event eventtype=\"3\"");
97 const int event_result
= item
->status
== CrxUpdateItem::kUpdated
;
98 StringAppendF(&ping_event
, " eventresult=\"%d\"", event_result
);
99 if (item
->error_category
)
100 StringAppendF(&ping_event
, " errorcat=\"%d\"", item
->error_category
);
101 if (item
->error_code
)
102 StringAppendF(&ping_event
, " errorcode=\"%d\"", item
->error_code
);
103 if (item
->extra_code1
)
104 StringAppendF(&ping_event
, " extracode1=\"%d\"", item
->extra_code1
);
105 if (HasDiffUpdate(item
))
106 StringAppendF(&ping_event
, " diffresult=\"%d\"", !item
->diff_update_failed
);
107 if (item
->diff_error_category
) {
109 &ping_event
, " differrorcat=\"%d\"", item
->diff_error_category
);
111 if (item
->diff_error_code
)
112 StringAppendF(&ping_event
, " differrorcode=\"%d\"", item
->diff_error_code
);
113 if (item
->diff_extra_code1
) {
115 &ping_event
, " diffextracode1=\"%d\"", item
->diff_extra_code1
);
117 if (!item
->previous_fp
.empty())
118 StringAppendF(&ping_event
, " previousfp=\"%s\"", item
->previous_fp
.c_str());
119 if (!item
->next_fp
.empty())
120 StringAppendF(&ping_event
, " nextfp=\"%s\"", item
->next_fp
.c_str());
121 StringAppendF(&ping_event
, "/>");
125 // Builds a ping message for the specified update item.
126 std::string
BuildPing(const Configurator
& config
, const CrxUpdateItem
* item
) {
127 const char app_element_format
[] =
128 "<app appid=\"%s\" version=\"%s\" nextversion=\"%s\">"
132 const std::string
app_element(base::StringPrintf(
134 item
->id
.c_str(), // "appid"
135 item
->previous_version
.GetString().c_str(), // "version"
136 item
->next_version
.GetString().c_str(), // "nextversion"
137 BuildUpdateCompleteEventElement(item
).c_str(), // update event
138 BuildDownloadCompleteEventElements(item
).c_str())); // download events
140 return BuildProtocolRequest(config
.GetBrowserVersion().GetString(),
143 config
.GetOSLongName(),
148 // Sends a fire and forget ping. The instances of this class have no
149 // ownership and they self-delete upon completion. One instance of this class
150 // can send only one ping.
153 explicit PingSender(const Configurator
& config
);
156 bool SendPing(const CrxUpdateItem
* item
);
159 void OnRequestSenderComplete(const net::URLFetcher
* source
);
161 const Configurator
& config_
;
162 scoped_ptr
<RequestSender
> request_sender_
;
163 base::ThreadChecker thread_checker_
;
165 DISALLOW_COPY_AND_ASSIGN(PingSender
);
168 PingSender::PingSender(const Configurator
& config
) : config_(config
) {
171 PingSender::~PingSender() {
172 DCHECK(thread_checker_
.CalledOnValidThread());
175 void PingSender::OnRequestSenderComplete(const net::URLFetcher
* source
) {
176 DCHECK(thread_checker_
.CalledOnValidThread());
180 bool PingSender::SendPing(const CrxUpdateItem
* item
) {
182 DCHECK(thread_checker_
.CalledOnValidThread());
184 std::vector
<GURL
> urls(config_
.PingUrl());
189 request_sender_
.reset(new RequestSender(config_
));
190 request_sender_
->Send(
191 BuildPing(config_
, item
),
193 base::Bind(&PingSender::OnRequestSenderComplete
, base::Unretained(this)));
199 PingManager::PingManager(const Configurator
& config
) : config_(config
) {
202 PingManager::~PingManager() {
205 // Sends a fire and forget ping when the updates are complete. The ping
206 // sender object self-deletes after sending the ping has completed asynchrously.
207 void PingManager::OnUpdateComplete(const CrxUpdateItem
* item
) {
208 PingSender
* ping_sender(new PingSender(config_
));
209 if (!ping_sender
->SendPing(item
))
213 } // namespace component_updater