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" |
11 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/history/history_types.h" | 15 #include "chrome/browser/history/history_types.h" |
15 #include "chrome/browser/metrics/variations/variations_http_header_provider.h" | 16 #include "chrome/browser/metrics/variations/variations_http_header_provider.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/search/suggestions/suggestions_store.h" | 18 #include "chrome/browser/search/suggestions/suggestions_store.h" |
18 #include "components/pref_registry/pref_registry_syncable.h" | 19 #include "components/pref_registry/pref_registry_syncable.h" |
19 #include "components/variations/variations_associated_data.h" | 20 #include "components/variations/variations_associated_data.h" |
20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
(...skipping 21 matching lines...) Expand all Loading... | |
42 RESPONSE_VALID, | 43 RESPONSE_VALID, |
43 RESPONSE_STATE_SIZE | 44 RESPONSE_STATE_SIZE |
44 }; | 45 }; |
45 | 46 |
46 // Will log the supplied response |state|. | 47 // Will log the supplied response |state|. |
47 void LogResponseState(SuggestionsResponseState state) { | 48 void LogResponseState(SuggestionsResponseState state) { |
48 UMA_HISTOGRAM_ENUMERATION("Suggestions.ResponseState", state, | 49 UMA_HISTOGRAM_ENUMERATION("Suggestions.ResponseState", state, |
49 RESPONSE_STATE_SIZE); | 50 RESPONSE_STATE_SIZE); |
50 } | 51 } |
51 | 52 |
52 // Obtains the experiment parameter under the supplied |key|. | 53 // Obtains the experiment parameter under the supplied |key|, or empty string |
54 // if the parameter does not exist. | |
53 std::string GetExperimentParam(const std::string& key) { | 55 std::string GetExperimentParam(const std::string& key) { |
54 return chrome_variations::GetVariationParamValue(kSuggestionsFieldTrialName, | 56 return chrome_variations::GetVariationParamValue(kSuggestionsFieldTrialName, |
55 key); | 57 key); |
56 } | 58 } |
57 | 59 |
58 // Runs each callback in |requestors| on |suggestions|, then deallocates | 60 // Runs each callback in |requestors| on |suggestions|, then deallocates |
59 // |requestors|. | 61 // |requestors|. |
60 void DispatchRequestsAndClear( | 62 void DispatchRequestsAndClear( |
61 const SuggestionsProfile& suggestions, | 63 const SuggestionsProfile& suggestions, |
62 std::vector<SuggestionsService::ResponseCallback>* requestors) { | 64 std::vector<SuggestionsService::ResponseCallback>* requestors) { |
63 std::vector<SuggestionsService::ResponseCallback>::iterator it; | 65 std::vector<SuggestionsService::ResponseCallback>::iterator it; |
64 for (it = requestors->begin(); it != requestors->end(); ++it) { | 66 for (it = requestors->begin(); it != requestors->end(); ++it) { |
65 it->Run(suggestions); | 67 it->Run(suggestions); |
66 } | 68 } |
67 std::vector<SuggestionsService::ResponseCallback>().swap(*requestors); | 69 std::vector<SuggestionsService::ResponseCallback>().swap(*requestors); |
68 } | 70 } |
69 | 71 |
70 // Timeout before serving requestors after a fetch suggestions request has been | 72 const int kDefaultRequestTimeoutMs = 200; |
71 // issued. | |
72 // TODO(manzagop): make this a Variations parameter to enable tweaking. | |
73 const unsigned int kRequestTimeoutMs = 200; | |
74 | 73 |
75 } // namespace | 74 } // namespace |
76 | 75 |
77 const char kSuggestionsFieldTrialName[] = "ChromeSuggestions"; | 76 const char kSuggestionsFieldTrialName[] = "ChromeSuggestions"; |
78 const char kSuggestionsFieldTrialURLParam[] = "url"; | 77 const char kSuggestionsFieldTrialURLParam[] = "url"; |
79 const char kSuggestionsFieldTrialSuggestionsSuffixParam[] = | 78 const char kSuggestionsFieldTrialSuggestionsSuffixParam[] = |
80 "suggestions_suffix"; | 79 "suggestions_suffix"; |
81 const char kSuggestionsFieldTrialBlacklistSuffixParam[] = "blacklist_suffix"; | 80 const char kSuggestionsFieldTrialBlacklistSuffixParam[] = "blacklist_suffix"; |
82 const char kSuggestionsFieldTrialStateParam[] = "state"; | 81 const char kSuggestionsFieldTrialStateParam[] = "state"; |
83 const char kSuggestionsFieldTrialStateEnabled[] = "enabled"; | 82 const char kSuggestionsFieldTrialStateEnabled[] = "enabled"; |
83 const char kSuggestionsFieldTrialTimeoutMs[] = "timeout_ms"; | |
84 | 84 |
85 SuggestionsService::SuggestionsService( | 85 SuggestionsService::SuggestionsService( |
86 Profile* profile, scoped_ptr<SuggestionsStore> suggestions_store) | 86 Profile* profile, scoped_ptr<SuggestionsStore> suggestions_store) |
87 : suggestions_store_(suggestions_store.Pass()), | 87 : suggestions_store_(suggestions_store.Pass()), |
88 thumbnail_manager_(new ThumbnailManager(profile)), | 88 thumbnail_manager_(new ThumbnailManager(profile)), |
89 profile_(profile), | 89 profile_(profile), |
90 weak_ptr_factory_(this) { | 90 weak_ptr_factory_(this), |
91 // Obtain the URL to use to fetch suggestions data from the Variations param. | 91 request_timeout_ms_(kDefaultRequestTimeoutMs) { |
92 // Obtain various parameters from Variations. | |
92 suggestions_url_ = | 93 suggestions_url_ = |
93 GURL(GetExperimentParam(kSuggestionsFieldTrialURLParam) + | 94 GURL(GetExperimentParam(kSuggestionsFieldTrialURLParam) + |
94 GetExperimentParam(kSuggestionsFieldTrialSuggestionsSuffixParam)); | 95 GetExperimentParam(kSuggestionsFieldTrialSuggestionsSuffixParam)); |
95 blacklist_url_prefix_ = | 96 blacklist_url_prefix_ = |
96 GetExperimentParam(kSuggestionsFieldTrialURLParam) + | 97 GetExperimentParam(kSuggestionsFieldTrialURLParam) + |
97 GetExperimentParam(kSuggestionsFieldTrialBlacklistSuffixParam); | 98 GetExperimentParam(kSuggestionsFieldTrialBlacklistSuffixParam); |
99 std::string timeout = GetExperimentParam(kSuggestionsFieldTrialTimeoutMs); | |
100 if (!timeout.empty()) { | |
101 base::StringToInt(timeout, &request_timeout_ms_); | |
manzagop (departed)
2014/06/17 18:16:45
If returns false, fallback to the default value.
Mathieu
2014/06/17 19:49:44
Done.
| |
102 } | |
98 } | 103 } |
99 | 104 |
100 SuggestionsService::~SuggestionsService() {} | 105 SuggestionsService::~SuggestionsService() {} |
101 | 106 |
102 // static | 107 // static |
103 bool SuggestionsService::IsEnabled() { | 108 bool SuggestionsService::IsEnabled() { |
104 return GetExperimentParam(kSuggestionsFieldTrialStateParam) == | 109 return GetExperimentParam(kSuggestionsFieldTrialStateParam) == |
105 kSuggestionsFieldTrialStateEnabled; | 110 kSuggestionsFieldTrialStateEnabled; |
106 } | 111 } |
107 | 112 |
108 void SuggestionsService::FetchSuggestionsData( | 113 void SuggestionsService::FetchSuggestionsData( |
109 SuggestionsService::ResponseCallback callback) { | 114 SuggestionsService::ResponseCallback callback) { |
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
111 | 116 |
112 FetchSuggestionsDataNoTimeout(callback); | 117 FetchSuggestionsDataNoTimeout(callback); |
113 | 118 |
114 // Post a task to serve the cached suggestions if the request hasn't completed | 119 // Post a task to serve the cached suggestions if the request hasn't completed |
115 // after some time. Cancels the previous such task, if one existed. | 120 // after some time. Cancels the previous such task, if one existed. |
116 pending_timeout_closure_.reset(new CancelableClosure(base::Bind( | 121 pending_timeout_closure_.reset(new CancelableClosure(base::Bind( |
117 &SuggestionsService::OnRequestTimeout, weak_ptr_factory_.GetWeakPtr()))); | 122 &SuggestionsService::OnRequestTimeout, weak_ptr_factory_.GetWeakPtr()))); |
118 BrowserThread::PostDelayedTask( | 123 BrowserThread::PostDelayedTask( |
119 BrowserThread::UI, FROM_HERE, pending_timeout_closure_->callback(), | 124 BrowserThread::UI, FROM_HERE, pending_timeout_closure_->callback(), |
120 base::TimeDelta::FromMilliseconds(kRequestTimeoutMs)); | 125 base::TimeDelta::FromMilliseconds(request_timeout_ms_)); |
121 } | 126 } |
122 | 127 |
123 void SuggestionsService::FetchSuggestionsDataNoTimeout( | 128 void SuggestionsService::FetchSuggestionsDataNoTimeout( |
124 SuggestionsService::ResponseCallback callback) { | 129 SuggestionsService::ResponseCallback callback) { |
125 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 130 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
126 if (pending_request_.get()) { | 131 if (pending_request_.get()) { |
127 // Request already exists, so just add requestor to queue. | 132 // Request already exists, so just add requestor to queue. |
128 waiting_requestors_.push_back(callback); | 133 waiting_requestors_.push_back(callback); |
129 return; | 134 return; |
130 } | 135 } |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
269 return request; | 274 return request; |
270 } | 275 } |
271 | 276 |
272 void SuggestionsService::ServeFromCache() { | 277 void SuggestionsService::ServeFromCache() { |
273 SuggestionsProfile suggestions; | 278 SuggestionsProfile suggestions; |
274 suggestions_store_->LoadSuggestions(&suggestions); | 279 suggestions_store_->LoadSuggestions(&suggestions); |
275 DispatchRequestsAndClear(suggestions, &waiting_requestors_); | 280 DispatchRequestsAndClear(suggestions, &waiting_requestors_); |
276 } | 281 } |
277 | 282 |
278 } // namespace suggestions | 283 } // namespace suggestions |
OLD | NEW |