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 "components/suggestions/suggestions_service_impl.h" | 5 #include "components/suggestions/suggestions_service_impl.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/feature_list.h" | 10 #include "base/feature_list.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/field_trial_params.h" |
13 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
14 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
20 #include "components/data_use_measurement/core/data_use_user_data.h" | 21 #include "components/data_use_measurement/core/data_use_user_data.h" |
21 #include "components/google/core/browser/google_util.h" | 22 #include "components/google/core/browser/google_util.h" |
22 #include "components/pref_registry/pref_registry_syncable.h" | 23 #include "components/pref_registry/pref_registry_syncable.h" |
23 #include "components/signin/core/browser/signin_manager_base.h" | 24 #include "components/signin/core/browser/signin_manager_base.h" |
24 #include "components/suggestions/blacklist_store.h" | 25 #include "components/suggestions/blacklist_store.h" |
| 26 #include "components/suggestions/features.h" |
25 #include "components/suggestions/image_manager.h" | 27 #include "components/suggestions/image_manager.h" |
26 #include "components/suggestions/suggestions_store.h" | 28 #include "components/suggestions/suggestions_store.h" |
27 #include "components/sync/driver/sync_service.h" | 29 #include "components/sync/driver/sync_service.h" |
28 #include "components/variations/net/variations_http_headers.h" | 30 #include "components/variations/net/variations_http_headers.h" |
29 #include "google_apis/gaia/gaia_constants.h" | 31 #include "google_apis/gaia/gaia_constants.h" |
30 #include "google_apis/gaia/oauth2_token_service.h" | 32 #include "google_apis/gaia/oauth2_token_service.h" |
31 #include "net/base/escape.h" | 33 #include "net/base/escape.h" |
32 #include "net/base/load_flags.h" | 34 #include "net/base/load_flags.h" |
33 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
34 #include "net/base/url_util.h" | 36 #include "net/base/url_util.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 GURL GetGoogleBaseURL() { | 77 GURL GetGoogleBaseURL() { |
76 GURL url(google_util::CommandLineGoogleBaseURL()); | 78 GURL url(google_util::CommandLineGoogleBaseURL()); |
77 if (url.is_valid()) | 79 if (url.is_valid()) |
78 return url; | 80 return url; |
79 return GURL(kDefaultGoogleBaseURL); | 81 return GURL(kDefaultGoogleBaseURL); |
80 } | 82 } |
81 | 83 |
82 // Format strings for the various suggestions URLs. They all have two string | 84 // Format strings for the various suggestions URLs. They all have two string |
83 // params: The Google base URL and the device type. | 85 // params: The Google base URL and the device type. |
84 // TODO(mathp): Put this in TemplateURL. | 86 // TODO(mathp): Put this in TemplateURL. |
85 const char kSuggestionsURLFormat[] = "%schromesuggestions?t=%s"; | 87 const char kSuggestionsURLFormat[] = "%schromesuggestions?%s"; |
86 const char kSuggestionsBlacklistURLPrefixFormat[] = | 88 const char kSuggestionsBlacklistURLPrefixFormat[] = |
87 "%schromesuggestions/blacklist?t=%s&url="; | 89 "%schromesuggestions/blacklist?t=%s&url="; |
88 const char kSuggestionsBlacklistClearURLFormat[] = | 90 const char kSuggestionsBlacklistClearURLFormat[] = |
89 "%schromesuggestions/blacklist/clear?t=%s"; | 91 "%schromesuggestions/blacklist/clear?t=%s"; |
90 | 92 |
91 const char kSuggestionsBlacklistURLParam[] = "url"; | 93 const char kSuggestionsBlacklistURLParam[] = "url"; |
| 94 const char kSuggestionsDeviceParam[] = "t=%s"; |
| 95 const char kSuggestionsMinParam[] = "min=%i"; |
| 96 |
| 97 const char kSuggestionsMinVariationName[] = "min_suggestions"; |
| 98 const int kSuggestionsMinVariationDefault = 0; |
92 | 99 |
93 #if defined(OS_ANDROID) || defined(OS_IOS) | 100 #if defined(OS_ANDROID) || defined(OS_IOS) |
94 const char kDeviceType[] = "2"; | 101 const char kDeviceType[] = "2"; |
95 #else | 102 #else |
96 const char kDeviceType[] = "1"; | 103 const char kDeviceType[] = "1"; |
97 #endif | 104 #endif |
98 | 105 |
99 // Format string for OAuth2 authentication headers. | 106 // Format string for OAuth2 authentication headers. |
100 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | 107 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; |
101 | 108 |
102 const char kFaviconURL[] = | 109 const char kFaviconURL[] = |
103 "https://s2.googleusercontent.com/s2/favicons?domain_url=%s&alt=s&sz=32"; | 110 "https://s2.googleusercontent.com/s2/favicons?domain_url=%s&alt=s&sz=32"; |
104 | 111 |
105 // The default expiry timeout is 168 hours. | 112 // The default expiry timeout is 168 hours. |
106 const int64_t kDefaultExpiryUsec = 168 * base::Time::kMicrosecondsPerHour; | 113 const int64_t kDefaultExpiryUsec = 168 * base::Time::kMicrosecondsPerHour; |
107 | 114 |
| 115 int GetMinimumSuggestionsCount() { |
| 116 return base::GetFieldTrialParamByFeatureAsInt( |
| 117 kUseSuggestionsEvenIfFewFeature, kSuggestionsMinVariationName, |
| 118 kSuggestionsMinVariationDefault); |
| 119 } |
| 120 |
108 } // namespace | 121 } // namespace |
109 | 122 |
110 SuggestionsServiceImpl::SuggestionsServiceImpl( | 123 SuggestionsServiceImpl::SuggestionsServiceImpl( |
111 SigninManagerBase* signin_manager, | 124 SigninManagerBase* signin_manager, |
112 OAuth2TokenService* token_service, | 125 OAuth2TokenService* token_service, |
113 syncer::SyncService* sync_service, | 126 syncer::SyncService* sync_service, |
114 net::URLRequestContextGetter* url_request_context, | 127 net::URLRequestContextGetter* url_request_context, |
115 std::unique_ptr<SuggestionsStore> suggestions_store, | 128 std::unique_ptr<SuggestionsStore> suggestions_store, |
116 std::unique_ptr<ImageManager> thumbnail_manager, | 129 std::unique_ptr<ImageManager> thumbnail_manager, |
117 std::unique_ptr<BlacklistStore> blacklist_store) | 130 std::unique_ptr<BlacklistStore> blacklist_store) |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 | 262 |
250 // static | 263 // static |
251 void SuggestionsServiceImpl::RegisterProfilePrefs( | 264 void SuggestionsServiceImpl::RegisterProfilePrefs( |
252 user_prefs::PrefRegistrySyncable* registry) { | 265 user_prefs::PrefRegistrySyncable* registry) { |
253 SuggestionsStore::RegisterProfilePrefs(registry); | 266 SuggestionsStore::RegisterProfilePrefs(registry); |
254 BlacklistStore::RegisterProfilePrefs(registry); | 267 BlacklistStore::RegisterProfilePrefs(registry); |
255 } | 268 } |
256 | 269 |
257 // static | 270 // static |
258 GURL SuggestionsServiceImpl::BuildSuggestionsURL() { | 271 GURL SuggestionsServiceImpl::BuildSuggestionsURL() { |
| 272 std::string device = base::StringPrintf(kSuggestionsDeviceParam, kDeviceType); |
| 273 std::string query = device; |
| 274 if (base::FeatureList::IsEnabled(kUseSuggestionsEvenIfFewFeature)) { |
| 275 std::string min_suggestions = |
| 276 base::StringPrintf(kSuggestionsMinParam, GetMinimumSuggestionsCount()); |
| 277 query = |
| 278 base::StringPrintf("%s&%s", device.c_str(), min_suggestions.c_str()); |
| 279 } |
259 return GURL(base::StringPrintf( | 280 return GURL(base::StringPrintf( |
260 kSuggestionsURLFormat, GetGoogleBaseURL().spec().c_str(), kDeviceType)); | 281 kSuggestionsURLFormat, GetGoogleBaseURL().spec().c_str(), query.c_str())); |
261 } | 282 } |
262 | 283 |
263 // static | 284 // static |
264 std::string SuggestionsServiceImpl::BuildSuggestionsBlacklistURLPrefix() { | 285 std::string SuggestionsServiceImpl::BuildSuggestionsBlacklistURLPrefix() { |
265 return base::StringPrintf(kSuggestionsBlacklistURLPrefixFormat, | 286 return base::StringPrintf(kSuggestionsBlacklistURLPrefixFormat, |
266 GetGoogleBaseURL().spec().c_str(), kDeviceType); | 287 GetGoogleBaseURL().spec().c_str(), kDeviceType); |
267 } | 288 } |
268 | 289 |
269 // static | 290 // static |
270 GURL SuggestionsServiceImpl::BuildSuggestionsBlacklistURL( | 291 GURL SuggestionsServiceImpl::BuildSuggestionsBlacklistURL( |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); | 606 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); |
586 } else { | 607 } else { |
587 TimeDelta candidate_delay = | 608 TimeDelta candidate_delay = |
588 scheduling_delay_ * kSchedulingBackoffMultiplier; | 609 scheduling_delay_ * kSchedulingBackoffMultiplier; |
589 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) | 610 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) |
590 scheduling_delay_ = candidate_delay; | 611 scheduling_delay_ = candidate_delay; |
591 } | 612 } |
592 } | 613 } |
593 | 614 |
594 } // namespace suggestions | 615 } // namespace suggestions |
OLD | NEW |