OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/autocomplete/zero_suggest_provider.h" | 5 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
9 #include "base/string16.h" | 9 #include "base/string16.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/autocomplete/autocomplete_input.h" | 13 #include "chrome/browser/autocomplete/autocomplete_input.h" |
14 #include "chrome/browser/autocomplete/autocomplete_match.h" | 14 #include "chrome/browser/autocomplete/autocomplete_match.h" |
15 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 15 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/search_engines/template_url_service.h" | 18 #include "chrome/browser/search_engines/template_url_service.h" |
18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 19 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 20 #include "chrome/common/pref_names.h" |
19 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
20 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
21 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
22 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
23 #include "net/url_request/url_fetcher.h" | 25 #include "net/url_request/url_fetcher.h" |
24 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
25 | 27 |
26 namespace { | 28 namespace { |
27 // The relevance of the top match from this provider. | 29 // The relevance of the top match from this provider. |
28 const int kMaxZeroSuggestRelevance = 100; | 30 const int kMaxZeroSuggestRelevance = 100; |
29 } // namespace | 31 } // namespace |
30 | 32 |
| 33 // static |
| 34 ZeroSuggestProvider* ZeroSuggestProvider::Create( |
| 35 AutocompleteProviderListener* listener, |
| 36 Profile* profile) { |
| 37 if (profile && !profile->IsOffTheRecord() && profile->GetPrefs()) { |
| 38 std::string url_prefix = profile->GetPrefs()->GetString( |
| 39 prefs::kExperimentalZeroSuggestUrlPrefix); |
| 40 if (!url_prefix.empty()) |
| 41 return new ZeroSuggestProvider(listener, profile, url_prefix); |
| 42 } |
| 43 return NULL; |
| 44 } |
| 45 |
31 ZeroSuggestProvider::ZeroSuggestProvider( | 46 ZeroSuggestProvider::ZeroSuggestProvider( |
32 AutocompleteProviderListener* listener, | 47 AutocompleteProviderListener* listener, |
33 Profile* profile, | 48 Profile* profile, |
34 const std::string& url_prefix) | 49 const std::string& url_prefix) |
35 : AutocompleteProvider(listener, profile, | 50 : AutocompleteProvider(listener, profile, |
36 AutocompleteProvider::TYPE_ZERO_SUGGEST), | 51 AutocompleteProvider::TYPE_ZERO_SUGGEST), |
37 url_prefix_(url_prefix), | 52 url_prefix_(url_prefix), |
38 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)) { | 53 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)) { |
39 } | 54 } |
40 | 55 |
| 56 // static |
| 57 void ZeroSuggestProvider::RegisterUserPrefs(PrefService* user_prefs) { |
| 58 user_prefs->RegisterStringPref(prefs::kExperimentalZeroSuggestUrlPrefix, "", |
| 59 PrefService::UNSYNCABLE_PREF); |
| 60 } |
| 61 |
41 void ZeroSuggestProvider::Start(const AutocompleteInput& input, | 62 void ZeroSuggestProvider::Start(const AutocompleteInput& input, |
42 bool /*minimal_changes*/) { | 63 bool /*minimal_changes*/) { |
43 UpdateMatches(input.text()); | 64 UpdateMatches(input.text()); |
44 } | 65 } |
45 | 66 |
46 void ZeroSuggestProvider::StartZeroSuggest(const GURL& url, | 67 void ZeroSuggestProvider::StartZeroSuggest(const GURL& url, |
47 const string16& user_text) { | 68 const string16& user_text) { |
48 DCHECK(url.is_valid()); | 69 DCHECK(url.is_valid()); |
49 // Do not query non-http URLs. There will be no useful suggestions for https | 70 // Do not query non-http URLs. There will be no useful suggestions for https |
50 // or chrome URLs. | 71 // or chrome URLs. |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 // Style to look like normal search suggestions. | 236 // Style to look like normal search suggestions. |
216 match.contents_class.push_back( | 237 match.contents_class.push_back( |
217 ACMatchClassification(0, ACMatchClassification::DIM)); | 238 ACMatchClassification(0, ACMatchClassification::DIM)); |
218 match.contents_class.push_back( | 239 match.contents_class.push_back( |
219 ACMatchClassification(user_text_.length(), ACMatchClassification::NONE)); | 240 ACMatchClassification(user_text_.length(), ACMatchClassification::NONE)); |
220 } | 241 } |
221 match.transition = content::PAGE_TRANSITION_GENERATED; | 242 match.transition = content::PAGE_TRANSITION_GENERATED; |
222 | 243 |
223 matches_.push_back(match); | 244 matches_.push_back(match); |
224 } | 245 } |
OLD | NEW |