summaryrefslogtreecommitdiffstats
path: root/chrome/browser/template_url.cc
blob: 8a042181f809a36ef384f5434b6745c06278f34f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/template_url.h"

#include "base/logging.h"
#include "base/string_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/rlz/rlz.h"
#include "chrome/browser/google_url_tracker.h"
#include "chrome/browser/template_url_model.h"
#include "chrome/common/gfx/favicon_size.h"
#include "net/base/escape.h"

// The TemplateURLRef has any number of terms that need to be replaced. Each of
// the terms is enclosed in braces. If the character preceeding the final
// brace is a ?, it indicates the term is optional and can be replaced with
// an empty string.
static const wchar_t kStartParameter = '{';
static const wchar_t kEndParameter = '}';
static const wchar_t kOptional = '?';

// Known parameters found in the URL.
static const wchar_t kSearchTermsParameter[] = L"searchTerms";
static const wchar_t kSearchTermsParameterFull[] = L"{searchTerms}";
static const wchar_t kCountParameter[] = L"count";
static const wchar_t kStartIndexParameter[] = L"startIndex";
static const wchar_t kStartPageParameter[] = L"startPage";
static const wchar_t kLanguageParameter[] = L"language";
static const wchar_t kInputEncodingParameter[] = L"inputEncoding";
static const wchar_t kOutputEncodingParameter[] = L"outputEncoding";

static const wchar_t kGoogleAcceptedSuggestionParameter[] =
    L"google:acceptedSuggestion";
// Host/Domain Google searches are relative to.
static const wchar_t kGoogleBaseURLParameter[] = L"google:baseURL";
static const wchar_t kGoogleBaseURLParameterFull[] = L"{google:baseURL}";
// Like google:baseURL, but for the Search Suggest capability.
static const wchar_t kGoogleBaseSuggestURLParameter[] =
    L"google:baseSuggestURL";
static const wchar_t kGoogleBaseSuggestURLParameterFull[] =
    L"{google:baseSuggestURL}";
static const wchar_t kGoogleOriginalQueryForSuggestionParameter[] =
    L"google:originalQueryForSuggestion";
static const wchar_t kGoogleRLZParameter[] = L"google:RLZ";
// Same as kSearchTermsParameter, with no escaping.
static const wchar_t kGoogleUnescapedSearchTermsParameter[] =
    L"google:unescapedSearchTerms";
static const wchar_t kGoogleUnescapedSearchTermsParameterFull[] =
    L"{google:unescapedSearchTerms}";

// Display value for kSearchTermsParameter.
static const wchar_t kDisplaySearchTerms[] = L"%s";

// Display value for kGoogleUnescapedSearchTermsParameter.
static const wchar_t kDisplayUnescapedSearchTerms[] = L"%S";

// Used if the count parameter is not optional. Indicates we want 10 search
// results.
static const wchar_t kDefaultCount[] = L"10";

// Used if the parameter kOutputEncodingParameter is required.
static const wchar_t kOutputEncodingType[] = L"UTF-8";

// static
std::wstring* TemplateURLRef::google_base_url_ = NULL;

TemplateURLRef::TemplateURLRef() {
  Set(std::wstring(), 0, 0);
}

void TemplateURLRef::Set(const std::wstring& url,
                         int index_offset,
                         int page_offset) {
  url_ = url;
  index_offset_ = index_offset;
  page_offset_ = page_offset;
  InvalidateCachedValues();
}

bool TemplateURLRef::ParseParameter(size_t start,
                                    size_t end,
                                    std::wstring* url,
                                    Replacements* replacements) const {
  DCHECK(start != std::string::npos &&
         end != std::string::npos && end > start);
  size_t length = end - start - 1;
  bool optional = false;
  if ((*url)[end - 1] == kOptional) {
    optional = true;
    length--;
  }
  std::wstring parameter(url->substr(start + 1, length));
  // Remove the parameter from the string.
  url->erase(start, end - start + 1);
  if (parameter == kSearchTermsParameter) {
    replacements->push_back(Replacement(SEARCH_TERMS, static_cast<int>(start)));
  } else if (parameter == kCountParameter) {
    if (!optional)
      url->insert(start, kDefaultCount);
  } else if (parameter == kStartIndexParameter) {
    if (!optional) {
      wchar_t start_string[12];
      if (swprintf_s(start_string, L"%d", index_offset_) <= 0)
        return false;
      url->insert(start, start_string);
    }
  } else if (parameter == kStartPageParameter) {
    if (!optional) {
      wchar_t start_string[12];
      if (swprintf_s(start_string, L"%d", page_offset_) <= 0)
        return false;
      url->insert(start, start_string);
    }
  } else if (parameter == kLanguageParameter) {
    replacements->push_back(Replacement(LANGUAGE, static_cast<int>(start)));
  } else if (parameter == kInputEncodingParameter) {
    replacements->push_back(Replacement(ENCODING, static_cast<int>(start)));
  } else if (parameter == kOutputEncodingParameter) {
    if (!optional)
      url->insert(start, kOutputEncodingType);
  } else if (parameter == kGoogleAcceptedSuggestionParameter) {
    replacements->push_back(Replacement(GOOGLE_ACCEPTED_SUGGESTION,
                                        static_cast<int>(start)));
  } else if (parameter == kGoogleBaseURLParameter) {
    replacements->push_back(Replacement(GOOGLE_BASE_URL,
                                        static_cast<int>(start)));
  } else if (parameter == kGoogleBaseSuggestURLParameter) {
    replacements->push_back(Replacement(GOOGLE_BASE_SUGGEST_URL,
                                        static_cast<int>(start)));
  } else if (parameter == kGoogleOriginalQueryForSuggestionParameter) {
    replacements->push_back(Replacement(GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION,
                                        static_cast<int>(start)));
  } else if (parameter == kGoogleRLZParameter) {
    replacements->push_back(Replacement(GOOGLE_RLZ, static_cast<int>(start)));
  } else if (parameter == kGoogleUnescapedSearchTermsParameter) {
    replacements->push_back(Replacement(GOOGLE_UNESCAPED_SEARCH_TERMS,
                                        static_cast<int>(start)));
  } else if (!optional) {
    // Unknown required parameter. No idea what to replace this with,
    // so fail.
    return false;
  }
  return true;
}

std::wstring TemplateURLRef::ParseURL(const std::wstring& url,
                                      Replacements* replacements,
                                      bool* valid) const {
  *valid = false;
  std::wstring parsed_url = url;
  for (size_t last = 0; last != std::string::npos; ) {
    last = parsed_url.find(kStartParameter, last);
    if (last != std::string::npos) {
      size_t endTemplate = parsed_url.find(kEndParameter, last);
      if (endTemplate != std::string::npos) {
        if (!ParseParameter(last, endTemplate, &parsed_url, replacements)) {
          // Not a valid parameter, return.
          return std::wstring();
        }
        // ParseParamter erases from the string, as such we don't need
        // to update last.
      } else {
        // Open brace without a closing brace, return.
        return std::wstring();
      }
    }
  }
  *valid = true;
  return parsed_url;
}

void TemplateURLRef::ParseIfNecessary() const {
  if (!parsed_) {
    parsed_ = true;
    parsed_url_ = ParseURL(url_, &replacements_, &valid_);
    supports_replacements_ = false;
    if (valid_) {
      bool has_only_one_search_term = false;
      for (Replacements::const_iterator i = replacements_.begin();
           i != replacements_.end(); ++i) {
        if ((i->type == SEARCH_TERMS) ||
            (i->type == GOOGLE_UNESCAPED_SEARCH_TERMS)) {
          if (has_only_one_search_term) {
            has_only_one_search_term = false;
            break;
          }
          has_only_one_search_term = true;
          supports_replacements_ = true;
        }
      }
      // Only parse the host/key if there is one search term. Technically there
      // could be more than one term, but it's uncommon; so we punt.
      if (has_only_one_search_term)
        ParseHostAndSearchTermKey();
    }
  }
}

void TemplateURLRef::ParseHostAndSearchTermKey() const {
  std::wstring url_string = url_;
  ReplaceSubstringsAfterOffset(&url_string, 0, kGoogleBaseURLParameterFull,
                               GoogleBaseURLValue());
  ReplaceSubstringsAfterOffset(&url_string, 0,
                               kGoogleBaseSuggestURLParameterFull,
                               GoogleBaseSuggestURLValue());

  GURL url(url_string);
  if (!url.is_valid())
    return;

  std::string query_string = url.query();
  if (query_string.empty())
    return;

  url_parse::Component query, key, value;
  query.len = static_cast<int>(query_string.size());
  while (url_parse::ExtractQueryKeyValue(query_string.c_str(), &query, &key,
                                         &value)) {
    if (key.is_nonempty() && value.is_nonempty()) {
      std::string value_string = query_string.substr(value.begin, value.len);
      if (value_string.find(WideToASCII(kSearchTermsParameterFull), 0) !=
          std::string::npos ||
          value_string.find(
              WideToASCII(kGoogleUnescapedSearchTermsParameterFull), 0) !=
          std::string::npos) {
        search_term_key_ = query_string.substr(key.begin, key.len);
        host_ = url.host();
        path_ = url.path();
        break;
      }
    }
  }
}

std::wstring TemplateURLRef::ReplaceSearchTerms(
    const TemplateURL& host,
    const std::wstring& terms,
    int accepted_suggestion,
    const std::wstring& original_query_for_suggestion) const {
  ParseIfNecessary();
  if (!valid_)
    return std::wstring();

  if (replacements_.empty())
    return parsed_url_;

  // Encode the search terms so that we know the encoding.
  const std::vector<std::string>& encodings = host.input_encodings();
  std::wstring encoded_terms;
  std::wstring encoded_original_query;
  std::wstring input_encoding;
  for (size_t i = 0; i < encodings.size(); ++i) {
    if (EscapeQueryParamValue(terms, encodings[i].c_str(), &encoded_terms)) {
      if (!original_query_for_suggestion.empty()) {
        EscapeQueryParamValue(original_query_for_suggestion,
                              encodings[i].c_str(), &encoded_original_query);
      }
      input_encoding = ASCIIToWide(encodings[i]);
      break;
    }
  }
  if (input_encoding.empty()) {
    encoded_terms = EscapeQueryParamValueUTF8(terms);
    if (!original_query_for_suggestion.empty()) {
      encoded_original_query =
          EscapeQueryParamValueUTF8(original_query_for_suggestion);
    }
    input_encoding = L"UTF-8";
  }

  std::wstring url = parsed_url_;

  // replacements_ is ordered in ascending order, as such we need to iterate
  // from the back.
  for (Replacements::const_reverse_iterator i = replacements_.rbegin();
       i != replacements_.rend(); ++i) {
    switch (i->type) {
      case ENCODING:
        url.insert(i->index, input_encoding);
        break;

      case GOOGLE_ACCEPTED_SUGGESTION:
        if (accepted_suggestion == NO_SUGGESTION_CHOSEN)
          url.insert(i->index, L"aq=f&");
        else if (accepted_suggestion != NO_SUGGESTIONS_AVAILABLE)
          url.insert(i->index, StringPrintf(L"aq=%d&", accepted_suggestion));
        break;

      case GOOGLE_BASE_URL:
        url.insert(i->index, GoogleBaseURLValue());
        break;

      case GOOGLE_BASE_SUGGEST_URL:
        url.insert(i->index, GoogleBaseSuggestURLValue());
        break;

      case GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION:
        if (accepted_suggestion >= 0)
          url.insert(i->index, L"oq=" + encoded_original_query + L"&");
        break;

      case GOOGLE_RLZ: {
        std::wstring rlz_string;
        RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
        if (!rlz_string.empty()) {
          rlz_string = L"rlz=" + rlz_string + L"&";
          url.insert(i->index, rlz_string);
        }
        break;
      }

      case GOOGLE_UNESCAPED_SEARCH_TERMS: {
        std::string unescaped_terms;
        WideToCodepage(terms, WideToASCII(input_encoding).c_str(),
                       OnStringUtilConversionError::SKIP, &unescaped_terms);
        url.insert(i->index, std::wstring(unescaped_terms.begin(),
                                          unescaped_terms.end()));
        break;
      }

      case LANGUAGE:
        url.insert(i->index, g_browser_process->GetApplicationLocale());
        break;

      case SEARCH_TERMS:
        url.insert(i->index, encoded_terms);
        break;

      default:
        NOTREACHED();
        break;
    }
  }

  return url;
}

bool TemplateURLRef::SupportsReplacement() const {
  ParseIfNecessary();
  return valid_ && supports_replacements_;
}

bool TemplateURLRef::IsValid() const {
  ParseIfNecessary();
  return valid_;
}

std::wstring TemplateURLRef::DisplayURL() const {
  ParseIfNecessary();
  if (!valid_)
    return url_;  // If we're not valid, don't escape anything.

  if (replacements_.empty())
    return url_;  // Nothing to replace, return the url.

  std::wstring result = url_;
  ReplaceSubstringsAfterOffset(&result, 0, kSearchTermsParameterFull,
                               kDisplaySearchTerms);

  ReplaceSubstringsAfterOffset(&result, 0,
                               kGoogleUnescapedSearchTermsParameterFull,
                               kDisplayUnescapedSearchTerms);

  return result;
}

// static
std::wstring TemplateURLRef::DisplayURLToURLRef(
    const std::wstring& display_url) {
  std::wstring result = display_url;
  ReplaceSubstringsAfterOffset(&result, 0, kDisplaySearchTerms,
                               kSearchTermsParameterFull);
  ReplaceSubstringsAfterOffset(&result, 0, kDisplayUnescapedSearchTerms,
                               kGoogleUnescapedSearchTermsParameterFull);
  return result;
}

const std::string& TemplateURLRef::GetHost() const {
  ParseIfNecessary();
  return host_;
}

const std::string& TemplateURLRef::GetPath() const {
  ParseIfNecessary();
  return path_;
}

const std::string& TemplateURLRef::GetSearchTermKey() const {
  ParseIfNecessary();
  return search_term_key_;
}

std::wstring TemplateURLRef::SearchTermToWide(const TemplateURL& host,
                                              const std::string& term) const {
  const std::vector<std::string>& encodings = host.input_encodings();
  std::wstring result;

  std::string unescaped =
      UnescapeURLComponent(term, UnescapeRule::REPLACE_PLUS_WITH_SPACE);
  for (size_t i = 0; i < encodings.size(); ++i) {
    if (CodepageToWide(unescaped, encodings[i].c_str(),
                       OnStringUtilConversionError::FAIL, &result))
      return result;
  }

  // Always fall back on UTF-8 if it works.
  if (CodepageToWide(unescaped, "UTF-8",
                     OnStringUtilConversionError::FAIL, &result))
    return result;

  // When nothing worked, just use the escaped text. We have no idea what the
  // encoding is. We need to substitute spaces for pluses ourselves since we're
  // not sending it through an unescaper.
  result = UTF8ToWide(term);
  std::replace(result.begin(), result.end(), '+', ' ');
  return result;
}

bool TemplateURLRef::HasGoogleBaseURLs() const {
  ParseIfNecessary();
  for (size_t i = 0; i < replacements_.size(); ++i) {
    if ((replacements_[i].type == GOOGLE_BASE_URL) ||
        (replacements_[i].type == GOOGLE_BASE_SUGGEST_URL))
      return true;
  }
  return false;
}

void TemplateURLRef::InvalidateCachedValues() const {
  supports_replacements_ = valid_ = parsed_ = false;
  host_.clear();
  path_.clear();
  search_term_key_.clear();
  replacements_.clear();
}

// Returns the value to use for replacements of type GOOGLE_BASE_URL.
// static
std::wstring TemplateURLRef::GoogleBaseURLValue() {
  return google_base_url_ ?
    (*google_base_url_) : UTF8ToWide(GoogleURLTracker::GoogleURL().spec());
}

// Returns the value to use for replacements of type GOOGLE_BASE_SUGGEST_URL.
// static
std::wstring TemplateURLRef::GoogleBaseSuggestURLValue() {
  // The suggest base URL we want at the end is something like
  // "http://clients1.google.TLD/complete/".  The key bit we want from the
  // original Google base URL is the TLD.

  // Start with the Google base URL.
  const GURL base_url(google_base_url_ ?
      GURL(*google_base_url_) : GoogleURLTracker::GoogleURL());
  DCHECK(base_url.is_valid());

  // Change "www." to "clients1." in the hostname.  If no "www." was found, just
  // prepend "clients1.".
  const std::string base_host(base_url.host());
  GURL::Replacements repl;
  const std::string suggest_host("clients1." +
      (base_host.compare(0, 4, "www.") ? base_host : base_host.substr(4)));
  repl.SetHostStr(suggest_host);

  // Replace any existing path with "/complete/".
  static const std::string suggest_path("/complete/");
  repl.SetPathStr(suggest_path);

  // Clear the query and ref.
  repl.ClearQuery();
  repl.ClearRef();
  return UTF8ToWide(base_url.ReplaceComponents(repl).spec());
}

// TemplateURL ----------------------------------------------------------------

// static
GURL TemplateURL::GenerateFaviconURL(const GURL& url) {
  DCHECK(url.is_valid());
  GURL::Replacements rep;

  const char favicon_path[] = "/favicon.ico";
  int favicon_path_len = arraysize(favicon_path) - 1;

  rep.SetPath(favicon_path, url_parse::Component(0, favicon_path_len));
  rep.ClearUsername();
  rep.ClearPassword();
  rep.ClearQuery();
  rep.ClearRef();
  return url.ReplaceComponents(rep);
}

void TemplateURL::SetSuggestionsURL(const std::wstring& suggestions_url,
                                    int index_offset,
                                    int page_offset) {
  suggestions_url_.Set(suggestions_url, index_offset, page_offset);
}

void TemplateURL::SetURL(const std::wstring& url,
                         int index_offset,
                         int page_offset) {
  url_.Set(url, index_offset, page_offset);
}

const std::wstring& TemplateURL::keyword() const {
  if (autogenerate_keyword_ && keyword_.empty()) {
    // Generate a keyword and cache it.
    keyword_ = TemplateURLModel::GenerateKeyword(
        TemplateURLModel::GenerateSearchURL(this).GetWithEmptyPath(), true);
  }
  return keyword_;
}

bool TemplateURL::ShowInDefaultList() const {
  return show_in_default_list() && url() && url()->SupportsReplacement();
}

void TemplateURL::SetFavIconURL(const GURL& url) {
  for (std::vector<ImageRef>::iterator i = image_refs_.begin();
       i != image_refs_.end(); ++i) {
    if (i->type == L"image/x-icon" &&
        i->width == kFavIconSize && i->height == kFavIconSize) {
      if (!url.is_valid())
        image_refs_.erase(i);
      else
        i->url = url;
      return;
    }
  }
  // Don't have one yet, add it.
  if (url.is_valid()) {
    add_image_ref(
        TemplateURL::ImageRef(L"image/x-icon", kFavIconSize, kFavIconSize,
                              url));
  }
}

GURL TemplateURL::GetFavIconURL() const {
  for (std::vector<ImageRef>::const_iterator i = image_refs_.begin();
       i != image_refs_.end(); ++i) {
    if ((i->type == L"image/x-icon" || i->type == L"image/vnd.microsoft.icon")
        && i->width == kFavIconSize && i->height == kFavIconSize) {
      return i->url;
    }
  }
  return GURL();
}

void TemplateURL::InvalidateCachedValues() const {
  url_.InvalidateCachedValues();
  suggestions_url_.InvalidateCachedValues();
  if (autogenerate_keyword_)
    keyword_.clear();
}