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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 } | 61 } |
62 | 62 |
63 } // namespace | 63 } // namespace |
64 | 64 |
65 const char kSuggestionsFieldTrialName[] = "ChromeSuggestions"; | 65 const char kSuggestionsFieldTrialName[] = "ChromeSuggestions"; |
66 const char kSuggestionsFieldTrialURLParam[] = "url"; | 66 const char kSuggestionsFieldTrialURLParam[] = "url"; |
67 const char kSuggestionsFieldTrialStateParam[] = "state"; | 67 const char kSuggestionsFieldTrialStateParam[] = "state"; |
68 const char kSuggestionsFieldTrialStateEnabled[] = "enabled"; | 68 const char kSuggestionsFieldTrialStateEnabled[] = "enabled"; |
69 | 69 |
70 SuggestionsService::SuggestionsService(Profile* profile) | 70 SuggestionsService::SuggestionsService(Profile* profile) |
71 : profile_(profile) { | 71 : thumbnail_manager_(new ThumbnailManager(profile)), |
| 72 profile_(profile) { |
72 // Obtain the URL to use to fetch suggestions data from the Variations param. | 73 // Obtain the URL to use to fetch suggestions data from the Variations param. |
73 suggestions_url_ = GURL(GetExperimentParam(kSuggestionsFieldTrialURLParam)); | 74 suggestions_url_ = GURL(GetExperimentParam(kSuggestionsFieldTrialURLParam)); |
74 } | 75 } |
75 | 76 |
76 SuggestionsService::~SuggestionsService() { | 77 SuggestionsService::~SuggestionsService() { |
77 } | 78 } |
78 | 79 |
79 // static | 80 // static |
80 bool SuggestionsService::IsEnabled() { | 81 bool SuggestionsService::IsEnabled() { |
81 return GetExperimentParam(kSuggestionsFieldTrialStateParam) == | 82 return GetExperimentParam(kSuggestionsFieldTrialStateParam) == |
(...skipping 22 matching lines...) Expand all Loading... |
104 net::HttpRequestHeaders headers; | 105 net::HttpRequestHeaders headers; |
105 chrome_variations::VariationsHttpHeaderProvider::GetInstance()-> | 106 chrome_variations::VariationsHttpHeaderProvider::GetInstance()-> |
106 AppendHeaders(pending_request_->GetOriginalURL(), | 107 AppendHeaders(pending_request_->GetOriginalURL(), |
107 profile_->IsOffTheRecord(), false, &headers); | 108 profile_->IsOffTheRecord(), false, &headers); |
108 pending_request_->SetExtraRequestHeaders(headers.ToString()); | 109 pending_request_->SetExtraRequestHeaders(headers.ToString()); |
109 pending_request_->Start(); | 110 pending_request_->Start(); |
110 | 111 |
111 last_request_started_time_ = base::TimeTicks::Now(); | 112 last_request_started_time_ = base::TimeTicks::Now(); |
112 } | 113 } |
113 | 114 |
| 115 void SuggestionsService::GetPageThumbnail(const GURL& url, |
| 116 BitmapResponseCallback callback) { |
| 117 thumbnail_manager_->GetPageThumbnail(url, callback); |
| 118 } |
| 119 |
114 void SuggestionsService::OnURLFetchComplete(const net::URLFetcher* source) { | 120 void SuggestionsService::OnURLFetchComplete(const net::URLFetcher* source) { |
115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
116 DCHECK_EQ(pending_request_.get(), source); | 122 DCHECK_EQ(pending_request_.get(), source); |
117 | 123 |
118 // The fetcher will be deleted when the request is handled. | 124 // The fetcher will be deleted when the request is handled. |
119 scoped_ptr<const net::URLFetcher> request(pending_request_.release()); | 125 scoped_ptr<const net::URLFetcher> request(pending_request_.release()); |
120 const net::URLRequestStatus& request_status = request->GetStatus(); | 126 const net::URLRequestStatus& request_status = request->GetStatus(); |
121 if (request_status.status() != net::URLRequestStatus::SUCCESS) { | 127 if (request_status.status() != net::URLRequestStatus::SUCCESS) { |
122 UMA_HISTOGRAM_SPARSE_SLOWLY("Suggestions.FailedRequestErrorCode", | 128 UMA_HISTOGRAM_SPARSE_SLOWLY("Suggestions.FailedRequestErrorCode", |
123 -request_status.error()); | 129 -request_status.error()); |
(...skipping 23 matching lines...) Expand all Loading... |
147 bool success = request->GetResponseAsString(&suggestions_data); | 153 bool success = request->GetResponseAsString(&suggestions_data); |
148 DCHECK(success); | 154 DCHECK(success); |
149 | 155 |
150 // Compute suggestions, and dispatch then to requestors. On error still | 156 // Compute suggestions, and dispatch then to requestors. On error still |
151 // dispatch empty suggestions. | 157 // dispatch empty suggestions. |
152 SuggestionsProfile suggestions; | 158 SuggestionsProfile suggestions; |
153 if (suggestions_data.empty()) { | 159 if (suggestions_data.empty()) { |
154 LogResponseState(RESPONSE_EMPTY); | 160 LogResponseState(RESPONSE_EMPTY); |
155 } else if (suggestions.ParseFromString(suggestions_data)) { | 161 } else if (suggestions.ParseFromString(suggestions_data)) { |
156 LogResponseState(RESPONSE_VALID); | 162 LogResponseState(RESPONSE_VALID); |
| 163 thumbnail_manager_->InitializeThumbnailMap(suggestions); |
157 } else { | 164 } else { |
158 LogResponseState(RESPONSE_INVALID); | 165 LogResponseState(RESPONSE_INVALID); |
159 } | 166 } |
160 | 167 |
161 DispatchRequestsAndClear(suggestions, &waiting_requestors_); | 168 DispatchRequestsAndClear(suggestions, &waiting_requestors_); |
162 } | 169 } |
163 | 170 |
164 void SuggestionsService::Shutdown() { | 171 void SuggestionsService::Shutdown() { |
165 // Cancel pending request. | 172 // Cancel pending request. |
166 pending_request_.reset(NULL); | 173 pending_request_.reset(NULL); |
167 | 174 |
168 // Dispatch empty suggestions to requestors. | 175 // Dispatch empty suggestions to requestors. |
169 DispatchRequestsAndClear(SuggestionsProfile(), &waiting_requestors_); | 176 DispatchRequestsAndClear(SuggestionsProfile(), &waiting_requestors_); |
170 } | 177 } |
171 | 178 |
172 } // namespace suggestions | 179 } // namespace suggestions |
OLD | NEW |