Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / update_client / ping_manager.cc
blob24afde6958c0375c464f6aaed26b8efaae5ec318
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/update_client/ping_manager.h"
7 #include <string>
8 #include <vector>
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/update_client/configurator.h"
23 #include "components/update_client/crx_update_item.h"
24 #include "components/update_client/request_sender.h"
25 #include "components/update_client/utils.h"
26 #include "net/url_request/url_fetcher.h"
27 #include "url/gurl.h"
29 namespace update_client {
31 namespace {
33 // Returns a string literal corresponding to the value of the downloader |d|.
34 const char* DownloaderToString(CrxDownloader::DownloadMetrics::Downloader d) {
35 switch (d) {
36 case CrxDownloader::DownloadMetrics::kUrlFetcher:
37 return "direct";
38 case CrxDownloader::DownloadMetrics::kBits:
39 return "bits";
40 default:
41 return "unknown";
45 // Returns a string representing a sequence of download complete events
46 // corresponding to each download metrics in |item|.
47 std::string BuildDownloadCompleteEventElements(const CrxUpdateItem* item) {
48 using base::StringAppendF;
49 std::string download_events;
50 for (size_t i = 0; i != item->download_metrics.size(); ++i) {
51 const CrxDownloader::DownloadMetrics& metrics = item->download_metrics[i];
52 std::string event("<event eventtype=\"14\"");
53 StringAppendF(&event, " eventresult=\"%d\"", metrics.error == 0);
54 StringAppendF(&event, " downloader=\"%s\"",
55 DownloaderToString(metrics.downloader));
56 if (metrics.error) {
57 StringAppendF(&event, " errorcode=\"%d\"", metrics.error);
59 StringAppendF(&event, " url=\"%s\"", metrics.url.spec().c_str());
61 // -1 means that the byte counts are not known.
62 if (metrics.downloaded_bytes != -1) {
63 StringAppendF(&event, " downloaded=\"%s\"",
64 base::Int64ToString(metrics.downloaded_bytes).c_str());
66 if (metrics.total_bytes != -1) {
67 StringAppendF(&event, " total=\"%s\"",
68 base::Int64ToString(metrics.total_bytes).c_str());
71 if (metrics.download_time_ms) {
72 StringAppendF(&event, " download_time_ms=\"%s\"",
73 base::Uint64ToString(metrics.download_time_ms).c_str());
75 StringAppendF(&event, "/>");
77 download_events += event;
79 return download_events;
82 // Returns a string representing one ping event xml element for an update item.
83 std::string BuildUpdateCompleteEventElement(const CrxUpdateItem* item) {
84 DCHECK(item->state == CrxUpdateItem::State::kNoUpdate ||
85 item->state == CrxUpdateItem::State::kUpdated);
87 using base::StringAppendF;
89 std::string ping_event("<event eventtype=\"3\"");
90 const int event_result = item->state == CrxUpdateItem::State::kUpdated;
91 StringAppendF(&ping_event, " eventresult=\"%d\"", event_result);
92 if (item->error_category)
93 StringAppendF(&ping_event, " errorcat=\"%d\"", item->error_category);
94 if (item->error_code)
95 StringAppendF(&ping_event, " errorcode=\"%d\"", item->error_code);
96 if (item->extra_code1)
97 StringAppendF(&ping_event, " extracode1=\"%d\"", item->extra_code1);
98 if (HasDiffUpdate(item))
99 StringAppendF(&ping_event, " diffresult=\"%d\"", !item->diff_update_failed);
100 if (item->diff_error_category) {
101 StringAppendF(&ping_event, " differrorcat=\"%d\"",
102 item->diff_error_category);
104 if (item->diff_error_code)
105 StringAppendF(&ping_event, " differrorcode=\"%d\"", item->diff_error_code);
106 if (item->diff_extra_code1) {
107 StringAppendF(&ping_event, " diffextracode1=\"%d\"",
108 item->diff_extra_code1);
110 if (!item->previous_fp.empty())
111 StringAppendF(&ping_event, " previousfp=\"%s\"", item->previous_fp.c_str());
112 if (!item->next_fp.empty())
113 StringAppendF(&ping_event, " nextfp=\"%s\"", item->next_fp.c_str());
114 StringAppendF(&ping_event, "/>");
115 return ping_event;
118 // Builds a ping message for the specified update item.
119 std::string BuildPing(const Configurator& config, const CrxUpdateItem* item) {
120 const char app_element_format[] =
121 "<app appid=\"%s\" version=\"%s\" nextversion=\"%s\">"
122 "%s"
123 "%s"
124 "</app>";
125 const std::string app_element(base::StringPrintf(
126 app_element_format,
127 item->id.c_str(), // "appid"
128 item->previous_version.GetString().c_str(), // "version"
129 item->next_version.GetString().c_str(), // "nextversion"
130 BuildUpdateCompleteEventElement(item).c_str(), // update event
131 BuildDownloadCompleteEventElements(item).c_str())); // download events
133 return BuildProtocolRequest(config.GetBrowserVersion().GetString(),
134 config.GetChannel(), config.GetLang(),
135 config.GetOSLongName(), app_element, "");
138 // Sends a fire and forget ping. The instances of this class have no
139 // ownership and they self-delete upon completion. One instance of this class
140 // can send only one ping.
141 class PingSender {
142 public:
143 explicit PingSender(const Configurator& config);
144 ~PingSender();
146 bool SendPing(const CrxUpdateItem* item);
148 private:
149 void OnRequestSenderComplete(const net::URLFetcher* source);
151 const Configurator& config_;
152 scoped_ptr<RequestSender> request_sender_;
153 base::ThreadChecker thread_checker_;
155 DISALLOW_COPY_AND_ASSIGN(PingSender);
158 PingSender::PingSender(const Configurator& config) : config_(config) {
161 PingSender::~PingSender() {
162 DCHECK(thread_checker_.CalledOnValidThread());
165 void PingSender::OnRequestSenderComplete(const net::URLFetcher* source) {
166 DCHECK(thread_checker_.CalledOnValidThread());
167 delete this;
170 bool PingSender::SendPing(const CrxUpdateItem* item) {
171 DCHECK(item);
172 DCHECK(thread_checker_.CalledOnValidThread());
174 std::vector<GURL> urls(config_.PingUrl());
176 if (urls.empty())
177 return false;
179 request_sender_.reset(new RequestSender(config_));
180 request_sender_->Send(
181 BuildPing(config_, item), urls,
182 base::Bind(&PingSender::OnRequestSenderComplete, base::Unretained(this)));
183 return true;
186 } // namespace
188 PingManager::PingManager(const Configurator& config) : config_(config) {
191 PingManager::~PingManager() {
194 // Sends a fire and forget ping when the updates are complete. The ping
195 // sender object self-deletes after sending the ping has completed asynchrously.
196 void PingManager::OnUpdateComplete(const CrxUpdateItem* item) {
197 PingSender* ping_sender(new PingSender(config_));
198 if (!ping_sender->SendPing(item))
199 delete ping_sender;
202 } // namespace update_client