| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013 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. |
| 4 |
| 5 #include "chrome/browser/component_updater/component_updater_ping_manager.h" |
| 6 #include "base/guid.h" |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/sys_info.h" |
| 11 #include "base/win/windows_version.h" |
| 12 #include "chrome/browser/component_updater/crx_update_item.h" |
| 13 #include "chrome/common/chrome_version_info.h" |
| 14 #include "chrome/common/omaha_query_params/omaha_query_params.h" |
| 15 #include "net/base/load_flags.h" |
| 16 #include "net/base/net_errors.h" |
| 17 #include "net/url_request/url_fetcher.h" |
| 18 #include "net/url_request/url_fetcher_delegate.h" |
| 19 #include "net/url_request/url_request_status.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Returns true if the |update_item| contains a valid differential update url. |
| 24 bool HasDiffUpdate(const CrxUpdateItem* update_item) { |
| 25 return update_item->diff_crx_url.is_valid(); |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 namespace component_updater { |
| 31 |
| 32 // Sends a fire and forget ping. The instances of this class have no |
| 33 // ownership and they self-delete upon completion. |
| 34 class PingSender : public net::URLFetcherDelegate { |
| 35 public: |
| 36 explicit PingSender(ComponentUpdateService::Configurator* configurator); |
| 37 |
| 38 void SendPing(const CrxUpdateItem* item); |
| 39 |
| 40 private: |
| 41 virtual ~PingSender(); |
| 42 |
| 43 // Overrides for ComponentUpdaterServiceObserver. |
| 44 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 45 |
| 46 std::string BuildPing(const CrxUpdateItem* item) const; |
| 47 static std::string BuildPingEvent(const CrxUpdateItem* item); |
| 48 |
| 49 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 50 ComponentUpdateService::Configurator* configurator_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(PingSender); |
| 53 }; |
| 54 |
| 55 PingSender::PingSender(ComponentUpdateService::Configurator* configurator) |
| 56 : configurator_(configurator) {} |
| 57 |
| 58 PingSender::~PingSender() {} |
| 59 |
| 60 void PingSender::OnURLFetchComplete(const net::URLFetcher* source) { |
| 61 delete this; |
| 62 } |
| 63 |
| 64 void PingSender::SendPing(const CrxUpdateItem* item) { |
| 65 DCHECK(item); |
| 66 |
| 67 const GURL ping_url(configurator_->PingUrl()); |
| 68 if (!ping_url.is_valid()) |
| 69 return; |
| 70 |
| 71 url_fetcher_.reset(net::URLFetcher::Create(0, |
| 72 ping_url, |
| 73 net::URLFetcher::POST, |
| 74 this)); |
| 75 |
| 76 url_fetcher_->SetUploadData(std::string("application/xml"), BuildPing(item)); |
| 77 url_fetcher_->SetRequestContext(configurator_->RequestContext()); |
| 78 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 79 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 80 net::LOAD_DISABLE_CACHE); |
| 81 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 82 url_fetcher_->Start(); |
| 83 } |
| 84 |
| 85 // Builds a ping message for the specified update item. |
| 86 std::string PingSender::BuildPing(const CrxUpdateItem* item) const { |
| 87 const std::string prod_id(chrome::OmahaQueryParams::GetProdIdString( |
| 88 chrome::OmahaQueryParams::CHROME)); |
| 89 |
| 90 const char response_format[] = |
| 91 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" " |
| 92 "protocol=\"2.0\" version=\"%s-%s\" requestid=\"{%s}\"> " |
| 93 "<o:os platform=\"%s\" version=\"%s\"/> " |
| 94 "<o:app appid=\"%s\" version=\"%s\">" |
| 95 "%s" |
| 96 "</o:app></o:gupdate>"; |
| 97 const std::string response( |
| 98 base::StringPrintf(response_format, |
| 99 prod_id.c_str(), |
| 100 chrome::VersionInfo().Version().c_str(), |
| 101 base::GenerateGUID().c_str(), |
| 102 chrome::VersionInfo().OSType().c_str(), |
| 103 base::SysInfo().OperatingSystemVersion().c_str(), |
| 104 item->id.c_str(), |
| 105 item->component.version.GetString().c_str(), |
| 106 BuildPingEvent(item).c_str())); |
| 107 return response; |
| 108 } |
| 109 |
| 110 // Returns a string representing one ping event for an update item. |
| 111 std::string PingSender::BuildPingEvent(const CrxUpdateItem* item) { |
| 112 DCHECK(item->status == CrxUpdateItem::kNoUpdate || |
| 113 item->status == CrxUpdateItem::kUpdated); |
| 114 |
| 115 using base::StringAppendF; |
| 116 |
| 117 std::string ping_event("<o:event eventtype=\"3\""); |
| 118 const int event_result = item->status == CrxUpdateItem::kUpdated; |
| 119 StringAppendF(&ping_event, " eventresult=\"%d\"", event_result); |
| 120 StringAppendF(&ping_event, " previousversion=\"%s\"", |
| 121 item->previous_version.GetString().c_str()); |
| 122 StringAppendF(&ping_event, " nextversion=\"%s\"", |
| 123 item->next_version.GetString().c_str()); |
| 124 if (item->error_category) |
| 125 StringAppendF(&ping_event, " errorcat=\"%d\"", item->error_category); |
| 126 if (item->error_code) |
| 127 StringAppendF(&ping_event, " errorcode=\"%d\"", item->error_code); |
| 128 if (item->extra_code1) |
| 129 StringAppendF(&ping_event, " extracode1=\"%d\"", item->extra_code1); |
| 130 if (HasDiffUpdate(item)) |
| 131 StringAppendF(&ping_event, " diffresult=\"%d\"", !item->diff_update_failed); |
| 132 if (item->diff_error_category) |
| 133 StringAppendF(&ping_event, " errorcat=\"%d\"", item->diff_error_category); |
| 134 if (item->diff_error_code) |
| 135 StringAppendF(&ping_event, " differrorcode=\"%d\"", item->diff_error_code); |
| 136 if (item->diff_extra_code1) { |
| 137 StringAppendF(&ping_event, |
| 138 " diffextracode1=\"%d\"", |
| 139 item->diff_extra_code1); |
| 140 } |
| 141 if (!item->previous_fp.empty()) |
| 142 StringAppendF(&ping_event, " previousfp=\"%s\"", item->previous_fp.c_str()); |
| 143 if (!item->next_fp.empty()) |
| 144 StringAppendF(&ping_event, " nextfp=\"%s\"", item->next_fp.c_str()); |
| 145 StringAppendF(&ping_event, "/>"); |
| 146 return ping_event; |
| 147 } |
| 148 |
| 149 PingManager::PingManager(ComponentUpdateService::Configurator* config) |
| 150 : config_(config) { |
| 151 } |
| 152 |
| 153 PingManager::~PingManager() { |
| 154 } |
| 155 |
| 156 // Sends a fire and forget ping when the updates are complete. The ping |
| 157 // sender object self-deletes after sending the ping. |
| 158 void PingManager::OnUpdateComplete(const CrxUpdateItem* item) { |
| 159 component_updater::PingSender* ping_sender(new PingSender(config_)); |
| 160 ping_sender->SendPing(item); |
| 161 } |
| 162 |
| 163 } // namespace component_updater |
| 164 |
| OLD | NEW |