Chromium Code Reviews| 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 ComponentUpdaterServiceObserver. | |
| 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, " errorcat=\"%d\"", item->diff_error_category); | |
|
waffles
2013/07/12 20:48:23
"differrorcat"?
Sorin Jianu
2013/07/12 21:01:57
thank you!
| |
| 135 if (item->diff_error_code) | |
| 136 StringAppendF(&ping_event, " differrorcode=\"%d\"", item->diff_error_code); | |
| 137 if (item->diff_extra_code1) { | |
| 138 StringAppendF(&ping_event, | |
| 139 " diffextracode1=\"%d\"", | |
| 140 item->diff_extra_code1); | |
| 141 } | |
| 142 if (!item->previous_fp.empty()) | |
| 143 StringAppendF(&ping_event, " previousfp=\"%s\"", item->previous_fp.c_str()); | |
| 144 if (!item->next_fp.empty()) | |
| 145 StringAppendF(&ping_event, " nextfp=\"%s\"", item->next_fp.c_str()); | |
| 146 StringAppendF(&ping_event, "/>"); | |
| 147 return ping_event; | |
| 148 } | |
| 149 | |
| 150 PingManager::PingManager( | |
| 151 const GURL& ping_url, | |
| 152 net::URLRequestContextGetter* url_request_context_getter) | |
| 153 : ping_url_(ping_url), | |
| 154 url_request_context_getter_(url_request_context_getter) { | |
| 155 } | |
| 156 | |
| 157 PingManager::~PingManager() { | |
| 158 } | |
| 159 | |
| 160 // Sends a fire and forget ping when the updates are complete. The ping | |
| 161 // sender object self-deletes after sending the ping. | |
| 162 void PingManager::OnUpdateComplete(const CrxUpdateItem* item) { | |
| 163 component_updater::PingSender* ping_sender(new PingSender); | |
| 164 ping_sender->SendPing(ping_url_, url_request_context_getter_, item); | |
| 165 } | |
| 166 | |
| 167 } // namespace component_updater | |
| 168 | |
| OLD | NEW |