OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/autofill/autofill_download.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/rand_util.h" |
| 9 #include "base/stl_util-inl.h" |
| 10 #include "chrome/browser/profile.h" |
| 11 |
| 12 #if defined(GOOGLE_CHROME_BUILD) |
| 13 #include "chrome/browser/autofill/internal/autofill_download_internal.h" |
| 14 #else |
| 15 #define AUTO_FILL_QUERY_SERVER_REQUEST_URL "http://disabled" |
| 16 #define AUTO_FILL_UPLOAD_SERVER_REQUEST_URL "http://disabled" |
| 17 #endif |
| 18 |
| 19 namespace { |
| 20 // We only send a fraction of the forms to upload server. |
| 21 // The rate for positive/negative matches potentially could be different. |
| 22 const double kAutoFillPositiveUploadRate = 0.01; |
| 23 const double kAutoFillNegativeUploadRate = 0.01; |
| 24 } // namespace |
| 25 |
| 26 AutoFillDownloadManager::AutoFillDownloadManager() |
| 27 : observer_(NULL) { |
| 28 } |
| 29 |
| 30 AutoFillDownloadManager::~AutoFillDownloadManager() { |
| 31 STLDeleteContainerPairFirstPointers(url_fetchers_.begin(), |
| 32 url_fetchers_.end()); |
| 33 } |
| 34 |
| 35 void AutoFillDownloadManager::SetObserver( |
| 36 AutoFillDownloadManager::Observer *observer) { |
| 37 if (observer) { |
| 38 DCHECK(!observer_); |
| 39 observer_ = observer; |
| 40 } else { |
| 41 observer_ = NULL; |
| 42 } |
| 43 } |
| 44 |
| 45 bool AutoFillDownloadManager::StartRequest(const std::string& form_xml, |
| 46 const std::string& form_signature, |
| 47 bool query_data, |
| 48 bool form_was_matched) { |
| 49 if (!query_data) { |
| 50 // Check if we need to upload form. |
| 51 double upload_rate = form_was_matched ? kAutoFillPositiveUploadRate : |
| 52 kAutoFillNegativeUploadRate; |
| 53 if (base::RandDouble() > upload_rate) { |
| 54 LOG(INFO) << "AutoFillDownloadManager: Upload request is ignored"; |
| 55 if (observer_) |
| 56 observer_->OnUploadedAutoFillHeuristics(form_signature); |
| 57 return true; |
| 58 } |
| 59 } |
| 60 |
| 61 std::string request_url; |
| 62 if (query_data) |
| 63 request_url = AUTO_FILL_QUERY_SERVER_REQUEST_URL; |
| 64 else |
| 65 request_url = AUTO_FILL_UPLOAD_SERVER_REQUEST_URL; |
| 66 |
| 67 URLFetcher *fetcher = |
| 68 URLFetcher::Create(0, GURL(request_url), URLFetcher::POST, this); |
| 69 FormRequestData data; |
| 70 data.form_signature = form_signature; |
| 71 data.query = query_data; |
| 72 url_fetchers_[fetcher] = data; |
| 73 fetcher->set_request_context(Profile::GetDefaultRequestContext()); |
| 74 fetcher->set_upload_data("text/plain", form_xml); |
| 75 fetcher->Start(); |
| 76 return true; |
| 77 } |
| 78 |
| 79 bool AutoFillDownloadManager::CancelRequest(const std::string& form_signature, |
| 80 bool query_data) { |
| 81 for (std::map<URLFetcher *, FormRequestData>::iterator it = |
| 82 url_fetchers_.begin(); |
| 83 it != url_fetchers_.end(); |
| 84 ++it) { |
| 85 if (it->second.form_signature == form_signature && |
| 86 it->second.query == query_data) { |
| 87 delete it->first; |
| 88 url_fetchers_.erase(it); |
| 89 return true; |
| 90 } |
| 91 } |
| 92 return false; |
| 93 } |
| 94 |
| 95 void AutoFillDownloadManager::OnURLFetchComplete(const URLFetcher* source, |
| 96 const GURL& url, |
| 97 const URLRequestStatus& status, |
| 98 int response_code, |
| 99 const ResponseCookies& cookies, |
| 100 const std::string& data) { |
| 101 std::map<URLFetcher *, FormRequestData>::iterator it = |
| 102 url_fetchers_.find(const_cast<URLFetcher*>(source)); |
| 103 DCHECK(it != url_fetchers_.end()); |
| 104 std::string type_of_request(it->second.query ? "query" : "upload"); |
| 105 const int kHttpResponseOk = 200; |
| 106 if (response_code != kHttpResponseOk) { |
| 107 LOG(INFO) << "AutoFillDownloadManager: " << type_of_request << |
| 108 " request has failed with response" << response_code; |
| 109 if (observer_) { |
| 110 observer_->OnHeuristicsRequestError(it->second.form_signature, |
| 111 response_code); |
| 112 } |
| 113 } else { |
| 114 LOG(INFO) << "AutoFillDownloadManager: " << type_of_request << |
| 115 " request has succeeded"; |
| 116 if (it->second.query) { |
| 117 if (observer_) |
| 118 observer_->OnLoadedAutoFillHeuristics(it->second.form_signature, data); |
| 119 } else { |
| 120 if (observer_) |
| 121 observer_->OnUploadedAutoFillHeuristics(it->second.form_signature); |
| 122 } |
| 123 } |
| 124 } |
| 125 |
| 126 |
OLD | NEW |