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/search_engines/template_url_prepopulate_data.h" | 5 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
6 | 6 |
7 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
8 #include <locale.h> | 8 #include <locale.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
14 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
18 #include "chrome/browser/google/google_util.h" | 18 #include "chrome/browser/google/google_util.h" |
19 #include "chrome/browser/prefs/pref_service.h" | 19 #include "chrome/browser/prefs/pref_service.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/search_engines/search_engine_type.h" | 21 #include "chrome/browser/search_engines/search_engine_type.h" |
22 #include "chrome/browser/search_engines/search_terms_data.h" | 22 #include "chrome/browser/search_engines/search_terms_data.h" |
23 #include "chrome/browser/search_engines/template_url.h" | 23 #include "chrome/browser/search_engines/template_url.h" |
24 #include "chrome/browser/search_engines/template_url_service.h" | 24 #include "chrome/browser/search_engines/template_url_service.h" |
25 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 27 #include "content/public/browser/browser_thread.h" |
27 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
28 #include "grit/generated_resources.h" | 29 #include "grit/generated_resources.h" |
29 #include "grit/theme_resources.h" | 30 #include "grit/theme_resources.h" |
30 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
31 | 32 |
32 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
33 #undef IN // On Windows, windef.h defines this, which screws up "India" cases. | 34 #undef IN // On Windows, windef.h defines this, which screws up "India" cases. |
34 #elif defined(OS_MACOSX) | 35 #elif defined(OS_MACOSX) |
35 #include "base/mac/scoped_cftyperef.h" | 36 #include "base/mac/scoped_cftyperef.h" |
36 #endif | 37 #endif |
(...skipping 1044 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1081 NULL, | 1082 NULL, |
1082 NULL, | 1083 NULL, |
1083 SEARCH_ENGINE_GOO, | 1084 SEARCH_ENGINE_GOO, |
1084 23, | 1085 23, |
1085 }; | 1086 }; |
1086 | 1087 |
1087 const PrepopulatedEngine google = { | 1088 const PrepopulatedEngine google = { |
1088 L"Google", | 1089 L"Google", |
1089 L"google.com", // This will be dynamically updated by the TemplateURL system. | 1090 L"google.com", // This will be dynamically updated by the TemplateURL system. |
1090 "http://www.google.com/favicon.ico", | 1091 "http://www.google.com/favicon.ico", |
1091 "{google:baseURL}search?{google:RLZ}{google:acceptedSuggestion}" | 1092 "{google:baseURL}search?q={searchTerms}&{google:RLZ}" |
1092 "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}" | 1093 "{google:acceptedSuggestion}{google:originalQueryForSuggestion}" |
1093 "sourceid=chrome&ie={inputEncoding}&q={searchTerms}", | 1094 "{google:searchFieldtrialParameter}sourceid=chrome&ie={inputEncoding}", |
1094 "UTF-8", | 1095 "UTF-8", |
1095 "{google:baseSuggestURL}search?{google:searchFieldtrialParameter}" | 1096 "{google:baseSuggestURL}search?{google:searchFieldtrialParameter}" |
1096 "client=chrome&hl={language}&q={searchTerms}", | 1097 "client=chrome&hl={language}&q={searchTerms}", |
1097 "{google:baseURL}webhp?{google:RLZ}sourceid=chrome-instant&" | 1098 "{google:baseURL}webhp?{google:RLZ}sourceid=chrome-instant&" |
1098 "ie={inputEncoding}{google:instantEnabledParameter}{searchTerms}", | 1099 "ie={inputEncoding}{google:instantEnabledParameter}{searchTerms}", |
1099 SEARCH_ENGINE_GOOGLE, | 1100 SEARCH_ENGINE_GOOGLE, |
1100 1, | 1101 1, |
1101 }; | 1102 }; |
1102 | 1103 |
1103 const PrepopulatedEngine guruji = { | 1104 const PrepopulatedEngine guruji = { |
(...skipping 2289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3393 // This could be more efficient. We are loading all the URLs to only keep | 3394 // This could be more efficient. We are loading all the URLs to only keep |
3394 // the first one. | 3395 // the first one. |
3395 GetPrepopulatedEngines(profile, &loaded_urls.get(), &default_search_index); | 3396 GetPrepopulatedEngines(profile, &loaded_urls.get(), &default_search_index); |
3396 if (default_search_index < loaded_urls.size()) { | 3397 if (default_search_index < loaded_urls.size()) { |
3397 default_search_provider = loaded_urls[default_search_index]; | 3398 default_search_provider = loaded_urls[default_search_index]; |
3398 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); | 3399 loaded_urls.weak_erase(loaded_urls.begin() + default_search_index); |
3399 } | 3400 } |
3400 return default_search_provider; | 3401 return default_search_provider; |
3401 } | 3402 } |
3402 | 3403 |
3403 // Finds a prepopulated engine whose origin is the same as |url|'s origin. | 3404 SearchEngineType GetEngineType(const std::string& url) { |
3404 static const PrepopulatedEngine* GetEngineForURL(const std::string& url) { | 3405 // Restricted to UI thread because ReplaceSearchTerms() is so restricted. |
| 3406 using content::BrowserThread; |
| 3407 DCHECK(!BrowserThread::IsWellKnownThread(BrowserThread::UI) || |
| 3408 BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 3409 |
3405 // We may get a valid URL, or we may get the Google prepopulate URL which | 3410 // We may get a valid URL, or we may get the Google prepopulate URL which |
3406 // can't be converted to a GURL. Instead of forcing callers to substitute to | 3411 // can't be converted directly to a GURL. To handle the latter, we first |
3407 // ensure the provided URL is valid, just detect the second case directly | 3412 // construct a TemplateURL from the provided |url|, then call |
3408 // here. | 3413 // ReplaceSearchTerms(). This should return a valid URL even when the input |
3409 GURL as_gurl(url); | 3414 // has Google base URLs. |
3410 if (!as_gurl.is_valid()) { | 3415 TemplateURLData data; |
3411 // We only need to check Google, the other engines should all be valid URLs | 3416 data.SetURL(url); |
3412 // and thus won't ever string-compare successfully against |url|. | 3417 TemplateURL turl(NULL, data); |
3413 return (url == google.search_url) ? &google : NULL; | 3418 GURL as_gurl(turl.url_ref().ReplaceSearchTerms(ASCIIToUTF16("x"), |
3414 } | 3419 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
| 3420 if (!as_gurl.is_valid()) |
| 3421 return SEARCH_ENGINE_OTHER; |
3415 | 3422 |
3416 // For all other cases, check using origins, in order to more aggressively | 3423 // Check using origins, in order to more aggressively match search engine |
3417 // match search engine types for data imported from other browsers. | 3424 // types for data imported from other browsers. |
3418 // | 3425 // |
3419 // First special-case Google, because the prepopulate URL for it will not | 3426 // First special-case Google, because the prepopulate URL for it will not |
3420 // convert to a GURL and thus won't have an origin. Instead see if the | 3427 // convert to a GURL and thus won't have an origin. Instead see if the |
3421 // incoming URL's host is "[*.]google.<TLD>". | 3428 // incoming URL's host is "[*.]google.<TLD>". |
3422 if (google_util::IsGoogleHostname(as_gurl.host(), | 3429 if (google_util::IsGoogleHostname(as_gurl.host(), |
3423 google_util::DISALLOW_SUBDOMAIN)) | 3430 google_util::DISALLOW_SUBDOMAIN)) |
3424 return &google; | 3431 return google.type; |
3425 | 3432 |
3426 // Now check the rest of the prepopulate data. | 3433 // Now check the rest of the prepopulate data. |
3427 GURL origin(as_gurl.GetOrigin()); | 3434 GURL origin(as_gurl.GetOrigin()); |
3428 for (size_t i = 0; i < arraysize(kAllEngines); ++i) { | 3435 for (size_t i = 0; i < arraysize(kAllEngines); ++i) { |
3429 GURL engine_url(kAllEngines[i]->search_url); | 3436 GURL engine_url(kAllEngines[i]->search_url); |
3430 if (engine_url.is_valid() && (origin == engine_url.GetOrigin())) | 3437 if (engine_url.is_valid() && (origin == engine_url.GetOrigin())) |
3431 return kAllEngines[i]; | 3438 return kAllEngines[i]->type; |
3432 } | 3439 } |
3433 | 3440 |
3434 return NULL; | 3441 return SEARCH_ENGINE_OTHER; |
3435 } | |
3436 | |
3437 string16 GetEngineName(const std::string& url) { | |
3438 const PrepopulatedEngine* engine = GetEngineForURL(url); | |
3439 if (engine) | |
3440 return WideToUTF16(engine->name); | |
3441 GURL as_gurl(url); | |
3442 return (as_gurl.is_valid() && !as_gurl.host().empty()) ? | |
3443 UTF8ToUTF16(as_gurl.host()) : | |
3444 l10n_util::GetStringUTF16(IDS_UNKNOWN_SEARCH_ENGINE_NAME); | |
3445 } | |
3446 | |
3447 SearchEngineType GetEngineType(const std::string& url) { | |
3448 const PrepopulatedEngine* engine = GetEngineForURL(url); | |
3449 return engine ? engine->type : SEARCH_ENGINE_OTHER; | |
3450 } | 3442 } |
3451 | 3443 |
3452 } // namespace TemplateURLPrepopulateData | 3444 } // namespace TemplateURLPrepopulateData |
OLD | NEW |