| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/component_updater/component_updater_ping_manager.h" | 5 #include "components/update_client/ping_manager.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
| 22 #include "components/component_updater/component_updater_configurator.h" | 22 #include "components/update_client/configurator.h" |
| 23 #include "components/component_updater/component_updater_utils.h" | 23 #include "components/update_client/crx_update_item.h" |
| 24 #include "components/component_updater/crx_update_item.h" | 24 #include "components/update_client/request_sender.h" |
| 25 #include "components/component_updater/request_sender.h" | 25 #include "components/update_client/utils.h" |
| 26 #include "net/url_request/url_fetcher.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 27 | 28 |
| 28 namespace net { | 29 namespace update_client { |
| 29 class URLFetcher; | |
| 30 } // namespace net | |
| 31 | |
| 32 namespace component_updater { | |
| 33 | 30 |
| 34 namespace { | 31 namespace { |
| 35 | 32 |
| 36 // Returns a string literal corresponding to the value of the downloader |d|. | 33 // Returns a string literal corresponding to the value of the downloader |d|. |
| 37 const char* DownloaderToString(CrxDownloader::DownloadMetrics::Downloader d) { | 34 const char* DownloaderToString(CrxDownloader::DownloadMetrics::Downloader d) { |
| 38 switch (d) { | 35 switch (d) { |
| 39 case CrxDownloader::DownloadMetrics::kUrlFetcher: | 36 case CrxDownloader::DownloadMetrics::kUrlFetcher: |
| 40 return "direct"; | 37 return "direct"; |
| 41 case CrxDownloader::DownloadMetrics::kBits: | 38 case CrxDownloader::DownloadMetrics::kBits: |
| 42 return "bits"; | 39 return "bits"; |
| 43 default: | 40 default: |
| 44 return "unknown"; | 41 return "unknown"; |
| 45 } | 42 } |
| 46 } | 43 } |
| 47 | 44 |
| 48 // Returns a string representing a sequence of download complete events | 45 // Returns a string representing a sequence of download complete events |
| 49 // corresponding to each download metrics in |item|. | 46 // corresponding to each download metrics in |item|. |
| 50 std::string BuildDownloadCompleteEventElements(const CrxUpdateItem* item) { | 47 std::string BuildDownloadCompleteEventElements(const CrxUpdateItem* item) { |
| 51 using base::StringAppendF; | 48 using base::StringAppendF; |
| 52 std::string download_events; | 49 std::string download_events; |
| 53 for (size_t i = 0; i != item->download_metrics.size(); ++i) { | 50 for (size_t i = 0; i != item->download_metrics.size(); ++i) { |
| 54 const CrxDownloader::DownloadMetrics& metrics = item->download_metrics[i]; | 51 const CrxDownloader::DownloadMetrics& metrics = item->download_metrics[i]; |
| 55 std::string event("<event eventtype=\"14\""); | 52 std::string event("<event eventtype=\"14\""); |
| 56 StringAppendF(&event, " eventresult=\"%d\"", metrics.error == 0); | 53 StringAppendF(&event, " eventresult=\"%d\"", metrics.error == 0); |
| 57 StringAppendF(&event, | 54 StringAppendF(&event, " downloader=\"%s\"", |
| 58 " downloader=\"%s\"", | |
| 59 DownloaderToString(metrics.downloader)); | 55 DownloaderToString(metrics.downloader)); |
| 60 if (metrics.error) { | 56 if (metrics.error) { |
| 61 StringAppendF(&event, " errorcode=\"%d\"", metrics.error); | 57 StringAppendF(&event, " errorcode=\"%d\"", metrics.error); |
| 62 } | 58 } |
| 63 StringAppendF(&event, " url=\"%s\"", metrics.url.spec().c_str()); | 59 StringAppendF(&event, " url=\"%s\"", metrics.url.spec().c_str()); |
| 64 | 60 |
| 65 // -1 means that the byte counts are not known. | 61 // -1 means that the byte counts are not known. |
| 66 if (metrics.downloaded_bytes != -1) { | 62 if (metrics.downloaded_bytes != -1) { |
| 67 StringAppendF(&event, | 63 StringAppendF(&event, " downloaded=\"%s\"", |
| 68 " downloaded=\"%s\"", | |
| 69 base::Int64ToString(metrics.downloaded_bytes).c_str()); | 64 base::Int64ToString(metrics.downloaded_bytes).c_str()); |
| 70 } | 65 } |
| 71 if (metrics.total_bytes != -1) { | 66 if (metrics.total_bytes != -1) { |
| 72 StringAppendF(&event, | 67 StringAppendF(&event, " total=\"%s\"", |
| 73 " total=\"%s\"", | |
| 74 base::Int64ToString(metrics.total_bytes).c_str()); | 68 base::Int64ToString(metrics.total_bytes).c_str()); |
| 75 } | 69 } |
| 76 | 70 |
| 77 if (metrics.download_time_ms) { | 71 if (metrics.download_time_ms) { |
| 78 StringAppendF(&event, | 72 StringAppendF(&event, " download_time_ms=\"%s\"", |
| 79 " download_time_ms=\"%s\"", | |
| 80 base::Uint64ToString(metrics.download_time_ms).c_str()); | 73 base::Uint64ToString(metrics.download_time_ms).c_str()); |
| 81 } | 74 } |
| 82 StringAppendF(&event, "/>"); | 75 StringAppendF(&event, "/>"); |
| 83 | 76 |
| 84 download_events += event; | 77 download_events += event; |
| 85 } | 78 } |
| 86 return download_events; | 79 return download_events; |
| 87 } | 80 } |
| 88 | 81 |
| 89 // Returns a string representing one ping event xml element for an update item. | 82 // Returns a string representing one ping event xml element for an update item. |
| 90 std::string BuildUpdateCompleteEventElement(const CrxUpdateItem* item) { | 83 std::string BuildUpdateCompleteEventElement(const CrxUpdateItem* item) { |
| 91 DCHECK(item->status == CrxUpdateItem::kNoUpdate || | 84 DCHECK(item->status == CrxUpdateItem::kNoUpdate || |
| 92 item->status == CrxUpdateItem::kUpdated); | 85 item->status == CrxUpdateItem::kUpdated); |
| 93 | 86 |
| 94 using base::StringAppendF; | 87 using base::StringAppendF; |
| 95 | 88 |
| 96 std::string ping_event("<event eventtype=\"3\""); | 89 std::string ping_event("<event eventtype=\"3\""); |
| 97 const int event_result = item->status == CrxUpdateItem::kUpdated; | 90 const int event_result = item->status == CrxUpdateItem::kUpdated; |
| 98 StringAppendF(&ping_event, " eventresult=\"%d\"", event_result); | 91 StringAppendF(&ping_event, " eventresult=\"%d\"", event_result); |
| 99 if (item->error_category) | 92 if (item->error_category) |
| 100 StringAppendF(&ping_event, " errorcat=\"%d\"", item->error_category); | 93 StringAppendF(&ping_event, " errorcat=\"%d\"", item->error_category); |
| 101 if (item->error_code) | 94 if (item->error_code) |
| 102 StringAppendF(&ping_event, " errorcode=\"%d\"", item->error_code); | 95 StringAppendF(&ping_event, " errorcode=\"%d\"", item->error_code); |
| 103 if (item->extra_code1) | 96 if (item->extra_code1) |
| 104 StringAppendF(&ping_event, " extracode1=\"%d\"", item->extra_code1); | 97 StringAppendF(&ping_event, " extracode1=\"%d\"", item->extra_code1); |
| 105 if (HasDiffUpdate(item)) | 98 if (HasDiffUpdate(item)) |
| 106 StringAppendF(&ping_event, " diffresult=\"%d\"", !item->diff_update_failed); | 99 StringAppendF(&ping_event, " diffresult=\"%d\"", !item->diff_update_failed); |
| 107 if (item->diff_error_category) { | 100 if (item->diff_error_category) { |
| 108 StringAppendF( | 101 StringAppendF(&ping_event, " differrorcat=\"%d\"", |
| 109 &ping_event, " differrorcat=\"%d\"", item->diff_error_category); | 102 item->diff_error_category); |
| 110 } | 103 } |
| 111 if (item->diff_error_code) | 104 if (item->diff_error_code) |
| 112 StringAppendF(&ping_event, " differrorcode=\"%d\"", item->diff_error_code); | 105 StringAppendF(&ping_event, " differrorcode=\"%d\"", item->diff_error_code); |
| 113 if (item->diff_extra_code1) { | 106 if (item->diff_extra_code1) { |
| 114 StringAppendF( | 107 StringAppendF(&ping_event, " diffextracode1=\"%d\"", |
| 115 &ping_event, " diffextracode1=\"%d\"", item->diff_extra_code1); | 108 item->diff_extra_code1); |
| 116 } | 109 } |
| 117 if (!item->previous_fp.empty()) | 110 if (!item->previous_fp.empty()) |
| 118 StringAppendF(&ping_event, " previousfp=\"%s\"", item->previous_fp.c_str()); | 111 StringAppendF(&ping_event, " previousfp=\"%s\"", item->previous_fp.c_str()); |
| 119 if (!item->next_fp.empty()) | 112 if (!item->next_fp.empty()) |
| 120 StringAppendF(&ping_event, " nextfp=\"%s\"", item->next_fp.c_str()); | 113 StringAppendF(&ping_event, " nextfp=\"%s\"", item->next_fp.c_str()); |
| 121 StringAppendF(&ping_event, "/>"); | 114 StringAppendF(&ping_event, "/>"); |
| 122 return ping_event; | 115 return ping_event; |
| 123 } | 116 } |
| 124 | 117 |
| 125 // Builds a ping message for the specified update item. | 118 // Builds a ping message for the specified update item. |
| 126 std::string BuildPing(const Configurator& config, const CrxUpdateItem* item) { | 119 std::string BuildPing(const Configurator& config, const CrxUpdateItem* item) { |
| 127 const char app_element_format[] = | 120 const char app_element_format[] = |
| 128 "<app appid=\"%s\" version=\"%s\" nextversion=\"%s\">" | 121 "<app appid=\"%s\" version=\"%s\" nextversion=\"%s\">" |
| 129 "%s" | 122 "%s" |
| 130 "%s" | 123 "%s" |
| 131 "</app>"; | 124 "</app>"; |
| 132 const std::string app_element(base::StringPrintf( | 125 const std::string app_element(base::StringPrintf( |
| 133 app_element_format, | 126 app_element_format, |
| 134 item->id.c_str(), // "appid" | 127 item->id.c_str(), // "appid" |
| 135 item->previous_version.GetString().c_str(), // "version" | 128 item->previous_version.GetString().c_str(), // "version" |
| 136 item->next_version.GetString().c_str(), // "nextversion" | 129 item->next_version.GetString().c_str(), // "nextversion" |
| 137 BuildUpdateCompleteEventElement(item).c_str(), // update event | 130 BuildUpdateCompleteEventElement(item).c_str(), // update event |
| 138 BuildDownloadCompleteEventElements(item).c_str())); // download events | 131 BuildDownloadCompleteEventElements(item).c_str())); // download events |
| 139 | 132 |
| 140 return BuildProtocolRequest(config.GetBrowserVersion().GetString(), | 133 return BuildProtocolRequest(config.GetBrowserVersion().GetString(), |
| 141 config.GetChannel(), | 134 config.GetChannel(), config.GetLang(), |
| 142 config.GetLang(), | 135 config.GetOSLongName(), app_element, ""); |
| 143 config.GetOSLongName(), | |
| 144 app_element, | |
| 145 ""); | |
| 146 } | 136 } |
| 147 | 137 |
| 148 // Sends a fire and forget ping. The instances of this class have no | 138 // 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 | 139 // ownership and they self-delete upon completion. One instance of this class |
| 150 // can send only one ping. | 140 // can send only one ping. |
| 151 class PingSender { | 141 class PingSender { |
| 152 public: | 142 public: |
| 153 explicit PingSender(const Configurator& config); | 143 explicit PingSender(const Configurator& config); |
| 154 ~PingSender(); | 144 ~PingSender(); |
| 155 | 145 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 181 DCHECK(item); | 171 DCHECK(item); |
| 182 DCHECK(thread_checker_.CalledOnValidThread()); | 172 DCHECK(thread_checker_.CalledOnValidThread()); |
| 183 | 173 |
| 184 std::vector<GURL> urls(config_.PingUrl()); | 174 std::vector<GURL> urls(config_.PingUrl()); |
| 185 | 175 |
| 186 if (urls.empty()) | 176 if (urls.empty()) |
| 187 return false; | 177 return false; |
| 188 | 178 |
| 189 request_sender_.reset(new RequestSender(config_)); | 179 request_sender_.reset(new RequestSender(config_)); |
| 190 request_sender_->Send( | 180 request_sender_->Send( |
| 191 BuildPing(config_, item), | 181 BuildPing(config_, item), urls, |
| 192 urls, | |
| 193 base::Bind(&PingSender::OnRequestSenderComplete, base::Unretained(this))); | 182 base::Bind(&PingSender::OnRequestSenderComplete, base::Unretained(this))); |
| 194 return true; | 183 return true; |
| 195 } | 184 } |
| 196 | 185 |
| 197 } // namespace | 186 } // namespace |
| 198 | 187 |
| 199 PingManager::PingManager(const Configurator& config) : config_(config) { | 188 PingManager::PingManager(const Configurator& config) : config_(config) { |
| 200 } | 189 } |
| 201 | 190 |
| 202 PingManager::~PingManager() { | 191 PingManager::~PingManager() { |
| 203 } | 192 } |
| 204 | 193 |
| 205 // Sends a fire and forget ping when the updates are complete. The ping | 194 // 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. | 195 // sender object self-deletes after sending the ping has completed asynchrously. |
| 207 void PingManager::OnUpdateComplete(const CrxUpdateItem* item) { | 196 void PingManager::OnUpdateComplete(const CrxUpdateItem* item) { |
| 208 PingSender* ping_sender(new PingSender(config_)); | 197 PingSender* ping_sender(new PingSender(config_)); |
| 209 if (!ping_sender->SendPing(item)) | 198 if (!ping_sender->SendPing(item)) |
| 210 delete ping_sender; | 199 delete ping_sender; |
| 211 } | 200 } |
| 212 | 201 |
| 213 } // namespace component_updater | 202 } // namespace update_client |
| OLD | NEW |