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/google/google_util.h" | 5 #include "chrome/browser/google/google_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/string16.h" | 11 #include "base/string16.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
Joao da Silva
2012/09/28 11:46:10
needed?
beaudoin
2012/10/02 17:43:29
Needed.
| |
13 #include "base/string_split.h" | 13 #include "base/string_split.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/google/google_url_tracker.h" | 17 #include "chrome/browser/google/google_url_tracker.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/net/url_util.h" | 19 #include "chrome/common/net/url_util.h" |
20 #include "chrome/installer/util/google_update_settings.h" | 20 #include "chrome/installer/util/google_update_settings.h" |
21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
22 #include "googleurl/src/url_parse.h" | 22 #include "googleurl/src/url_parse.h" |
Joao da Silva
2012/09/28 11:46:10
needed?
beaudoin
2012/10/02 17:43:29
Done.
| |
23 #include "net/base/escape.h" | |
24 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 23 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
25 | 24 |
26 #if defined(OS_MACOSX) | 25 #if defined(OS_MACOSX) |
27 #include "chrome/browser/mac/keystone_glue.h" | 26 #include "chrome/browser/mac/keystone_glue.h" |
28 #endif | 27 #endif |
29 | 28 |
30 #if defined(GOOGLE_CHROME_BUILD) | 29 #if defined(GOOGLE_CHROME_BUILD) |
31 #include "chrome/browser/google/linkdoctor_internal/linkdoctor_internal.h" | 30 #include "chrome/browser/google/linkdoctor_internal/linkdoctor_internal.h" |
32 #endif | 31 #endif |
33 | 32 |
(...skipping 16 matching lines...) Expand all Loading... | |
50 itr != parameters.end(); | 49 itr != parameters.end(); |
51 ++itr) { | 50 ++itr) { |
52 if (StartsWithASCII(*itr, "q=", false) && itr->size() > 2) | 51 if (StartsWithASCII(*itr, "q=", false) && itr->size() > 2) |
53 return true; | 52 return true; |
54 } | 53 } |
55 return false; | 54 return false; |
56 } | 55 } |
57 | 56 |
58 bool gUseMockLinkDoctorBaseURLForTesting = false; | 57 bool gUseMockLinkDoctorBaseURLForTesting = false; |
59 | 58 |
60 // Finds the first key-value pair where the key matches |query_key|. Returns | |
61 // true if a match is found and sets |search_terms| to the value. | |
62 bool ExtractSearchTermsFromComponent(const std::string& url, | |
63 url_parse::Component* component, | |
64 string16* search_terms) { | |
65 const std::string query_key = "q"; | |
66 url_parse::Component key, value; | |
67 | |
68 while (url_parse::ExtractQueryKeyValue(url.c_str(), component, | |
69 &key, &value)) { | |
70 if (url.compare(key.begin, key.len, query_key) != 0) | |
71 continue; | |
72 std::string value_str = url.substr(value.begin, value.len); | |
73 *search_terms = net::UnescapeAndDecodeUTF8URLComponent( | |
74 value_str, | |
75 net::UnescapeRule::SPACES | | |
76 net::UnescapeRule::URL_SPECIAL_CHARS | | |
77 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE, | |
78 NULL); | |
79 return true; | |
80 } | |
81 return false; | |
82 } | |
83 | |
84 } // anonymous namespace | 59 } // anonymous namespace |
85 | 60 |
86 namespace google_util { | 61 namespace google_util { |
87 | 62 |
88 GURL LinkDoctorBaseURL() { | 63 GURL LinkDoctorBaseURL() { |
89 if (gUseMockLinkDoctorBaseURLForTesting) | 64 if (gUseMockLinkDoctorBaseURLForTesting) |
90 return GURL("http://mock.linkdoctor.url/for?testing"); | 65 return GURL("http://mock.linkdoctor.url/for?testing"); |
91 return GURL(LINKDOCTOR_SERVER_REQUEST_URL); | 66 return GURL(LINKDOCTOR_SERVER_REQUEST_URL); |
92 } | 67 } |
93 | 68 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 return true; | 147 return true; |
173 } | 148 } |
174 | 149 |
175 bool GetReactivationBrand(std::string* brand) { | 150 bool GetReactivationBrand(std::string* brand) { |
176 brand->clear(); | 151 brand->clear(); |
177 return true; | 152 return true; |
178 } | 153 } |
179 | 154 |
180 #endif | 155 #endif |
181 | 156 |
182 string16 GetSearchTermsFromGoogleSearchURL(const std::string& url) { | |
183 if (!IsInstantExtendedAPIGoogleSearchUrl(url)) | |
184 return string16(); | |
185 | |
186 url_parse::Parsed parsed_url; | |
187 url_parse::ParseStandardURL(url.c_str(), url.length(), &parsed_url); | |
188 | |
189 string16 search_terms; | |
190 // The search terms can be in either the query or ref component - for | |
191 // instance, in a regular Google search they'll be in the query but in a | |
192 // Google Instant search they can be in both. The ref is the correct one to | |
193 // return in this case, so test the ref component first. | |
194 if (ExtractSearchTermsFromComponent(url, &parsed_url.ref, &search_terms) || | |
195 ExtractSearchTermsFromComponent(url, &parsed_url.query, &search_terms)) { | |
196 return search_terms; | |
197 } | |
198 return string16(); | |
199 } | |
200 | |
201 bool IsGoogleDomainUrl(const std::string& url, | 157 bool IsGoogleDomainUrl(const std::string& url, |
202 SubdomainPermission subdomain_permission, | 158 SubdomainPermission subdomain_permission, |
203 PortPermission port_permission) { | 159 PortPermission port_permission) { |
204 GURL original_url(url); | 160 GURL original_url(url); |
205 if (!original_url.is_valid() || | 161 if (!original_url.is_valid() || |
206 !(original_url.SchemeIs("http") || original_url.SchemeIs("https"))) | 162 !(original_url.SchemeIs("http") || original_url.SchemeIs("https"))) |
207 return false; | 163 return false; |
208 | 164 |
209 // If we have the Instant URL overridden with a command line flag, accept | 165 // If we have the Instant URL overridden with a command line flag, accept |
210 // its domain/port combination as well. | 166 // its domain/port combination as well. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 return false; | 232 return false; |
277 | 233 |
278 // Check for query parameter in URL parameter and hash fragment, depending on | 234 // Check for query parameter in URL parameter and hash fragment, depending on |
279 // the path type. | 235 // the path type. |
280 std::string query(original_url.query()); | 236 std::string query(original_url.query()); |
281 std::string ref(original_url.ref()); | 237 std::string ref(original_url.ref()); |
282 return HasQueryParameter(ref) || | 238 return HasQueryParameter(ref) || |
283 (!is_home_page_base && HasQueryParameter(query)); | 239 (!is_home_page_base && HasQueryParameter(query)); |
284 } | 240 } |
285 | 241 |
286 bool IsInstantExtendedAPIGoogleSearchUrl(const std::string& url) { | |
287 if (!IsGoogleSearchUrl(url)) | |
288 return false; | |
289 | |
290 const std::string embedded_search_key = kInstantExtendedAPIParam; | |
291 | |
292 url_parse::Parsed parsed_url; | |
293 url_parse::ParseStandardURL(url.c_str(), url.length(), &parsed_url); | |
294 url_parse::Component key, value; | |
295 while (url_parse::ExtractQueryKeyValue( | |
296 url.c_str(), &parsed_url.query, &key, &value)) { | |
297 // If the parameter key is |embedded_search_key| and the value is not 0 this | |
298 // is an Instant Extended API Google search URL. | |
299 if (!url.compare(key.begin, key.len, embedded_search_key)) { | |
300 int int_value = 0; | |
301 if (value.is_nonempty()) | |
302 base::StringToInt(url.substr(value.begin, value.len), &int_value); | |
303 return int_value != 0; | |
304 } | |
305 } | |
306 return false; | |
307 } | |
308 | |
309 bool IsOrganic(const std::string& brand) { | 242 bool IsOrganic(const std::string& brand) { |
310 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 243 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
311 if (command_line.HasSwitch(switches::kOrganicInstall)) | 244 if (command_line.HasSwitch(switches::kOrganicInstall)) |
312 return true; | 245 return true; |
313 | 246 |
314 #if defined(OS_MACOSX) | 247 #if defined(OS_MACOSX) |
315 if (brand.empty()) { | 248 if (brand.empty()) { |
316 // An empty brand string on Mac is used for channels other than stable, | 249 // An empty brand string on Mac is used for channels other than stable, |
317 // which are always organic. | 250 // which are always organic. |
318 return true; | 251 return true; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
361 const char* const kBrands[] = { | 294 const char* const kBrands[] = { |
362 "CHIQ", "CHSG", "HLJY", "NTMO", "OOBA", "OOBB", "OOBC", "OOBD", "OOBE", | 295 "CHIQ", "CHSG", "HLJY", "NTMO", "OOBA", "OOBB", "OOBC", "OOBD", "OOBE", |
363 "OOBF", "OOBG", "OOBH", "OOBI", "OOBJ", "IDCM", | 296 "OOBF", "OOBG", "OOBH", "OOBI", "OOBJ", "IDCM", |
364 }; | 297 }; |
365 const char* const* end = &kBrands[arraysize(kBrands)]; | 298 const char* const* end = &kBrands[arraysize(kBrands)]; |
366 const char* const* found = std::find(&kBrands[0], end, brand); | 299 const char* const* found = std::find(&kBrands[0], end, brand); |
367 return found != end; | 300 return found != end; |
368 } | 301 } |
369 | 302 |
370 } // namespace google_util | 303 } // namespace google_util |
OLD | NEW |