| 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 "chrome/browser/search/suggestions/suggestions_service.h" | 5 #include "chrome/browser/search/suggestions/suggestions_service.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 UMA_HISTOGRAM_ENUMERATION("Suggestions.ResponseState", state, | 41 UMA_HISTOGRAM_ENUMERATION("Suggestions.ResponseState", state, |
| 42 RESPONSE_STATE_SIZE); | 42 RESPONSE_STATE_SIZE); |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Obtains the experiment parameter under the supplied |key|. | 45 // Obtains the experiment parameter under the supplied |key|. |
| 46 std::string GetExperimentParam(const std::string& key) { | 46 std::string GetExperimentParam(const std::string& key) { |
| 47 return chrome_variations::GetVariationParamValue(kSuggestionsFieldTrialName, | 47 return chrome_variations::GetVariationParamValue(kSuggestionsFieldTrialName, |
| 48 key); | 48 key); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Runs each callback in |requestors| on |suggestions|, then deallocates | |
| 52 // |requestors|. | |
| 53 void DispatchRequestsAndClear( | |
| 54 const SuggestionsProfile& suggestions, | |
| 55 std::vector<SuggestionsService::ResponseCallback>* requestors) { | |
| 56 std::vector<SuggestionsService::ResponseCallback>::iterator it; | |
| 57 for (it = requestors->begin(); it != requestors->end(); ++it) { | |
| 58 it->Run(suggestions); | |
| 59 } | |
| 60 std::vector<SuggestionsService::ResponseCallback>().swap(*requestors); | |
| 61 } | |
| 62 | |
| 63 } // namespace | 51 } // namespace |
| 64 | 52 |
| 65 const char kSuggestionsFieldTrialName[] = "ChromeSuggestions"; | 53 const char kSuggestionsFieldTrialName[] = "ChromeSuggestions"; |
| 66 const char kSuggestionsFieldTrialURLParam[] = "url"; | 54 const char kSuggestionsFieldTrialURLParam[] = "url"; |
| 67 const char kSuggestionsFieldTrialStateParam[] = "state"; | 55 const char kSuggestionsFieldTrialStateParam[] = "state"; |
| 68 const char kSuggestionsFieldTrialStateEnabled[] = "enabled"; | 56 const char kSuggestionsFieldTrialStateEnabled[] = "enabled"; |
| 69 | 57 |
| 70 SuggestionsService::SuggestionsService(Profile* profile) | 58 SuggestionsService::SuggestionsService(Profile* profile) |
| 71 : profile_(profile) { | 59 : profile_(profile) { |
| 72 // Obtain the URL to use to fetch suggestions data from the Variations param. | 60 // Obtain the URL to use to fetch suggestions data from the Variations param. |
| 73 suggestions_url_ = GURL(GetExperimentParam(kSuggestionsFieldTrialURLParam)); | 61 suggestions_url_ = GURL(GetExperimentParam(kSuggestionsFieldTrialURLParam)); |
| 74 } | 62 } |
| 75 | 63 |
| 76 SuggestionsService::~SuggestionsService() { | 64 SuggestionsService::~SuggestionsService() { |
| 77 } | 65 } |
| 78 | 66 |
| 79 // static | 67 // static |
| 80 bool SuggestionsService::IsEnabled() { | 68 bool SuggestionsService::IsEnabled() { |
| 81 return GetExperimentParam(kSuggestionsFieldTrialStateParam) == | 69 return GetExperimentParam(kSuggestionsFieldTrialStateParam) == |
| 82 kSuggestionsFieldTrialStateEnabled; | 70 kSuggestionsFieldTrialStateEnabled; |
| 83 } | 71 } |
| 84 | 72 |
| 85 void SuggestionsService::FetchSuggestionsData( | 73 void SuggestionsService::FetchSuggestionsData() { |
| 86 SuggestionsService::ResponseCallback callback) { | |
| 87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 74 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 88 | 75 |
| 89 if (pending_request_.get()) { | |
| 90 // Request already exists, so just add requestor to queue. | |
| 91 waiting_requestors_.push_back(callback); | |
| 92 return; | |
| 93 } | |
| 94 | |
| 95 // Form new request. | |
| 96 DCHECK(waiting_requestors_.empty()); | |
| 97 waiting_requestors_.push_back(callback); | |
| 98 | |
| 99 pending_request_.reset(net::URLFetcher::Create( | 76 pending_request_.reset(net::URLFetcher::Create( |
| 100 0, suggestions_url_, net::URLFetcher::GET, this)); | 77 0, suggestions_url_, net::URLFetcher::GET, this)); |
| 101 pending_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 78 pending_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 102 pending_request_->SetRequestContext(profile_->GetRequestContext()); | 79 pending_request_->SetRequestContext(profile_->GetRequestContext()); |
| 103 // Add Chrome experiment state to the request headers. | 80 // Add Chrome experiment state to the request headers. |
| 104 net::HttpRequestHeaders headers; | 81 net::HttpRequestHeaders headers; |
| 105 chrome_variations::VariationsHttpHeaderProvider::GetInstance()-> | 82 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( |
| 106 AppendHeaders(pending_request_->GetOriginalURL(), | 83 pending_request_->GetOriginalURL(), profile_->IsOffTheRecord(), false, |
| 107 profile_->IsOffTheRecord(), false, &headers); | 84 &headers); |
| 108 pending_request_->SetExtraRequestHeaders(headers.ToString()); | 85 pending_request_->SetExtraRequestHeaders(headers.ToString()); |
| 109 pending_request_->Start(); | 86 pending_request_->Start(); |
| 110 | 87 |
| 111 last_request_started_time_ = base::TimeTicks::Now(); | 88 last_request_started_time_ = base::TimeTicks::Now(); |
| 112 } | 89 } |
| 113 | 90 |
| 114 void SuggestionsService::OnURLFetchComplete(const net::URLFetcher* source) { | 91 void SuggestionsService::OnURLFetchComplete(const net::URLFetcher* source) { |
| 115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 116 DCHECK_EQ(pending_request_.get(), source); | 92 DCHECK_EQ(pending_request_.get(), source); |
| 117 | 93 |
| 118 // The fetcher will be deleted when the request is handled. | 94 // The fetcher will be deleted when the request is handled. |
| 119 scoped_ptr<const net::URLFetcher> request(pending_request_.release()); | 95 scoped_ptr<const net::URLFetcher> request(pending_request_.release()); |
| 120 const net::URLRequestStatus& request_status = request->GetStatus(); | 96 const net::URLRequestStatus& request_status = request->GetStatus(); |
| 121 if (request_status.status() != net::URLRequestStatus::SUCCESS) { | 97 if (request_status.status() != net::URLRequestStatus::SUCCESS) { |
| 122 UMA_HISTOGRAM_SPARSE_SLOWLY("Suggestions.FailedRequestErrorCode", | 98 UMA_HISTOGRAM_SPARSE_SLOWLY("Suggestions.FailedRequestErrorCode", |
| 123 -request_status.error()); | 99 -request_status.error()); |
| 124 DVLOG(1) << "Suggestions server request failed with error: " | 100 DVLOG(1) << "Suggestions server request failed with error: " |
| 125 << request_status.error() << ": " | 101 << request_status.error() << ": " |
| 126 << net::ErrorToString(request_status.error()); | 102 << net::ErrorToString(request_status.error()); |
| 127 return; | 103 return; |
| 128 } | 104 } |
| 129 | 105 |
| 130 // Log the response code. | 106 // Log the response code. |
| 131 const int response_code = request->GetResponseCode(); | 107 const int response_code = request->GetResponseCode(); |
| 132 UMA_HISTOGRAM_SPARSE_SLOWLY("Suggestions.FetchResponseCode", | 108 UMA_HISTOGRAM_SPARSE_SLOWLY("Suggestions.FetchResponseCode", |
| 133 response_code); | 109 response_code); |
| 134 | 110 |
| 135 const base::TimeDelta latency = | 111 const base::TimeDelta latency = |
| 136 base::TimeTicks::Now() - last_request_started_time_; | 112 base::TimeTicks::Now() - last_request_started_time_; |
| 137 UMA_HISTOGRAM_MEDIUM_TIMES("Suggestions.FetchSuccessLatency", latency); | 113 UMA_HISTOGRAM_MEDIUM_TIMES("Suggestions.FetchSuccessLatency", latency); |
| 138 | 114 |
| 139 std::string suggestions_data; | 115 std::string suggestions_data; |
| 140 bool success = request->GetResponseAsString(&suggestions_data); | 116 bool success = request->GetResponseAsString(&suggestions_data); |
| 117 |
| 141 DCHECK(success); | 118 DCHECK(success); |
| 142 | |
| 143 // Compute suggestions, and dispatch then to requestors. On error still | |
| 144 // dispatch empty suggestions. | |
| 145 SuggestionsProfile suggestions; | |
| 146 if (suggestions_data.empty()) { | 119 if (suggestions_data.empty()) { |
| 147 LogResponseState(RESPONSE_EMPTY); | 120 LogResponseState(RESPONSE_EMPTY); |
| 148 } else if (suggestions.ParseFromString(suggestions_data)) { | 121 } else if (suggestions_.ParseFromString(suggestions_data)) { |
| 149 LogResponseState(RESPONSE_VALID); | 122 LogResponseState(RESPONSE_VALID); |
| 150 } else { | 123 } else { |
| 151 LogResponseState(RESPONSE_INVALID); | 124 LogResponseState(RESPONSE_INVALID); |
| 152 } | 125 } |
| 153 | |
| 154 DispatchRequestsAndClear(suggestions, &waiting_requestors_); | |
| 155 } | |
| 156 | |
| 157 void SuggestionsService::Shutdown() { | |
| 158 // Cancel pending request. | |
| 159 pending_request_.reset(NULL); | |
| 160 | |
| 161 // Dispatch empty suggestions to requestors. | |
| 162 DispatchRequestsAndClear(SuggestionsProfile(), &waiting_requestors_); | |
| 163 } | 126 } |
| 164 | 127 |
| 165 } // namespace suggestions | 128 } // namespace suggestions |
| OLD | NEW |