| 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 PingSender(); |
| 37 |
| 38 void SendPing(const GURL& ping_url, |
| 39 net::URLRequestContextGetter* url_request_context_getter, |
| 40 const CrxUpdateItem* item); |
| 41 private: |
| 42 virtual ~PingSender(); |
| 43 |
| 44 // Overrides for URLFetcherDelegate. |
| 45 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 46 |
| 47 static std::string BuildPing(const CrxUpdateItem* item); |
| 48 static std::string BuildPingEventElement(const CrxUpdateItem* item); |
| 49 |
| 50 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(PingSender); |
| 53 }; |
| 54 |
| 55 PingSender::PingSender() {} |
| 56 |
| 57 PingSender::~PingSender() {} |
| 58 |
| 59 void PingSender::OnURLFetchComplete(const net::URLFetcher* source) { |
| 60 delete this; |
| 61 } |
| 62 |
| 63 void PingSender::SendPing( |
| 64 const GURL& ping_url, |
| 65 net::URLRequestContextGetter* url_request_context_getter, |
| 66 const CrxUpdateItem* item) { |
| 67 DCHECK(item); |
| 68 |
| 69 if (!ping_url.is_valid()) |
| 70 return; |
| 71 |
| 72 url_fetcher_.reset(net::URLFetcher::Create(0, |
| 73 ping_url, |
| 74 net::URLFetcher::POST, |
| 75 this)); |
| 76 |
| 77 url_fetcher_->SetUploadData("application/xml", BuildPing(item)); |
| 78 url_fetcher_->SetRequestContext(url_request_context_getter); |
| 79 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 80 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 81 net::LOAD_DISABLE_CACHE); |
| 82 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 83 url_fetcher_->Start(); |
| 84 } |
| 85 |
| 86 // Builds a ping message for the specified update item. |
| 87 std::string PingSender::BuildPing(const CrxUpdateItem* item) { |
| 88 const std::string prod_id(chrome::OmahaQueryParams::GetProdIdString( |
| 89 chrome::OmahaQueryParams::CHROME)); |
| 90 |
| 91 const char response_format[] = |
| 92 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" " |
| 93 "protocol=\"2.0\" version=\"%s-%s\" requestid=\"{%s}\"> " |
| 94 "<o:os platform=\"%s\" version=\"%s\"/> " |
| 95 "<o:app appid=\"%s\" version=\"%s\">" |
| 96 "%s" |
| 97 "</o:app></o:gupdate>"; |
| 98 const std::string response( |
| 99 base::StringPrintf(response_format, |
| 100 prod_id.c_str(), |
| 101 chrome::VersionInfo().Version().c_str(), |
| 102 base::GenerateGUID().c_str(), |
| 103 chrome::VersionInfo().OSType().c_str(), |
| 104 base::SysInfo().OperatingSystemVersion().c_str(), |
| 105 item->id.c_str(), |
| 106 item->component.version.GetString().c_str(), |
| 107 BuildPingEventElement(item).c_str())); |
| 108 return response; |
| 109 } |
| 110 |
| 111 // Returns a string representing one ping event xml element for an update item. |
| 112 std::string PingSender::BuildPingEventElement(const CrxUpdateItem* item) { |
| 113 DCHECK(item->status == CrxUpdateItem::kNoUpdate || |
| 114 item->status == CrxUpdateItem::kUpdated); |
| 115 |
| 116 using base::StringAppendF; |
| 117 |
| 118 std::string ping_event("<o:event eventtype=\"3\""); |
| 119 const int event_result = item->status == CrxUpdateItem::kUpdated; |
| 120 StringAppendF(&ping_event, " eventresult=\"%d\"", event_result); |
| 121 StringAppendF(&ping_event, " previousversion=\"%s\"", |
| 122 item->previous_version.GetString().c_str()); |
| 123 StringAppendF(&ping_event, " nextversion=\"%s\"", |
| 124 item->next_version.GetString().c_str()); |
| 125 if (item->error_category) |
| 126 StringAppendF(&ping_event, " errorcat=\"%d\"", item->error_category); |
| 127 if (item->error_code) |
| 128 StringAppendF(&ping_event, " errorcode=\"%d\"", item->error_code); |
| 129 if (item->extra_code1) |
| 130 StringAppendF(&ping_event, " extracode1=\"%d\"", item->extra_code1); |
| 131 if (HasDiffUpdate(item)) |
| 132 StringAppendF(&ping_event, " diffresult=\"%d\"", !item->diff_update_failed); |
| 133 if (item->diff_error_category) |
| 134 StringAppendF(&ping_event, |
| 135 " differrorcat=\"%d\"", |
| 136 item->diff_error_category); |
| 137 if (item->diff_error_code) |
| 138 StringAppendF(&ping_event, " differrorcode=\"%d\"", item->diff_error_code); |
| 139 if (item->diff_extra_code1) { |
| 140 StringAppendF(&ping_event, |
| 141 " diffextracode1=\"%d\"", |
| 142 item->diff_extra_code1); |
| 143 } |
| 144 if (!item->previous_fp.empty()) |
| 145 StringAppendF(&ping_event, " previousfp=\"%s\"", item->previous_fp.c_str()); |
| 146 if (!item->next_fp.empty()) |
| 147 StringAppendF(&ping_event, " nextfp=\"%s\"", item->next_fp.c_str()); |
| 148 StringAppendF(&ping_event, "/>"); |
| 149 return ping_event; |
| 150 } |
| 151 |
| 152 PingManager::PingManager( |
| 153 const GURL& ping_url, |
| 154 net::URLRequestContextGetter* url_request_context_getter) |
| 155 : ping_url_(ping_url), |
| 156 url_request_context_getter_(url_request_context_getter) { |
| 157 } |
| 158 |
| 159 PingManager::~PingManager() { |
| 160 } |
| 161 |
| 162 // Sends a fire and forget ping when the updates are complete. The ping |
| 163 // sender object self-deletes after sending the ping. |
| 164 void PingManager::OnUpdateComplete(const CrxUpdateItem* item) { |
| 165 component_updater::PingSender* ping_sender(new PingSender); |
| 166 ping_sender->SendPing(ping_url_, url_request_context_getter_, item); |
| 167 } |
| 168 |
| 169 } // namespace component_updater |
| 170 |
| OLD | NEW |