| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/renderer/searchbox/searchbox.h" | 5 #include "chrome/renderer/searchbox/searchbox.h" |
| 6 | 6 |
| 7 #include "base/metrics/field_trial.h" | |
| 8 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
| 12 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 13 #include "chrome/renderer/searchbox/searchbox_extension.h" | 12 #include "chrome/renderer/searchbox/searchbox_extension.h" |
| 14 #include "content/public/renderer/render_view.h" | 13 #include "content/public/renderer/render_view.h" |
| 15 #include "grit/renderer_resources.h" | 14 #include "grit/renderer_resources.h" |
| 16 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 // Size of the results cache. | 23 // Size of the results cache. |
| 25 const size_t kMaxInstantAutocompleteResultItemCacheSize = 100; | 24 const size_t kMaxInstantAutocompleteResultItemCacheSize = 100; |
| 26 | 25 |
| 27 // The HTML returned when an invalid or unknown restricted ID is requested. | |
| 28 const char kInvalidSuggestionHtml[] = | |
| 29 "<div style=\"background:red\">invalid rid %d</div>"; | |
| 30 | |
| 31 // Checks if the input color is in valid range. | |
| 32 bool IsColorValid(int color) { | |
| 33 return color >= 0 && color <= 0xffffff; | |
| 34 } | |
| 35 | |
| 36 // If |url| starts with |prefix|, removes |prefix|. | |
| 37 void StripPrefix(string16* url, const string16& prefix) { | |
| 38 if (StartsWith(*url, prefix, true)) | |
| 39 url->erase(0, prefix.length()); | |
| 40 } | |
| 41 | |
| 42 // Removes leading "http://" or "http://www." from |url| unless |userInput| | |
| 43 // starts with those prefixes. | |
| 44 void StripURLPrefixes(string16* url, const string16& userInput) { | |
| 45 string16 trimmedUserInput; | |
| 46 TrimWhitespace(userInput, TRIM_TRAILING, &trimmedUserInput); | |
| 47 if (StartsWith(*url, trimmedUserInput, true)) | |
| 48 return; | |
| 49 | |
| 50 StripPrefix(url, ASCIIToUTF16(chrome::kHttpScheme) + ASCIIToUTF16("://")); | |
| 51 | |
| 52 if (StartsWith(*url, trimmedUserInput, true)) | |
| 53 return; | |
| 54 | |
| 55 StripPrefix(url, ASCIIToUTF16("www.")); | |
| 56 } | |
| 57 | |
| 58 } // namespace | 26 } // namespace |
| 59 | 27 |
| 60 SearchBox::SearchBox(content::RenderView* render_view) | 28 SearchBox::SearchBox(content::RenderView* render_view) |
| 61 : content::RenderViewObserver(render_view), | 29 : content::RenderViewObserver(render_view), |
| 62 content::RenderViewObserverTracker<SearchBox>(render_view), | 30 content::RenderViewObserverTracker<SearchBox>(render_view), |
| 63 verbatim_(false), | 31 verbatim_(false), |
| 64 selection_start_(0), | 32 selection_start_(0), |
| 65 selection_end_(0), | 33 selection_end_(0), |
| 66 start_margin_(0), | 34 start_margin_(0), |
| 67 is_key_capture_enabled_(false), | 35 is_key_capture_enabled_(false), |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 std::vector<InstantMostVisitedItemIDPair>* items) const { | 357 std::vector<InstantMostVisitedItemIDPair>* items) const { |
| 390 return most_visited_items_cache_.GetCurrentItems(items); | 358 return most_visited_items_cache_.GetCurrentItems(items); |
| 391 } | 359 } |
| 392 | 360 |
| 393 bool SearchBox::GetMostVisitedItemWithID( | 361 bool SearchBox::GetMostVisitedItemWithID( |
| 394 InstantRestrictedID most_visited_item_id, | 362 InstantRestrictedID most_visited_item_id, |
| 395 InstantMostVisitedItem* item) const { | 363 InstantMostVisitedItem* item) const { |
| 396 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id, | 364 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id, |
| 397 item); | 365 item); |
| 398 } | 366 } |
| 399 | |
| 400 bool SearchBox::GenerateDataURLForSuggestionRequest(const GURL& request_url, | |
| 401 GURL* data_url) const { | |
| 402 DCHECK(data_url); | |
| 403 | |
| 404 if (!ShouldUseIframes()) | |
| 405 return false; | |
| 406 | |
| 407 // The origin URL is required so that the iframe knows what origin to post | |
| 408 // messages to. | |
| 409 WebKit::WebView* webview = render_view()->GetWebView(); | |
| 410 if (!webview) | |
| 411 return false; | |
| 412 GURL embedder_url(webview->mainFrame()->document().url()); | |
| 413 GURL embedder_origin = embedder_url.GetOrigin(); | |
| 414 if (!embedder_origin.is_valid()) | |
| 415 return false; | |
| 416 | |
| 417 DCHECK(StartsWithASCII(request_url.path(), "/", true)); | |
| 418 std::string restricted_id_str = request_url.path().substr(1); | |
| 419 | |
| 420 InstantRestrictedID restricted_id = 0; | |
| 421 DCHECK_EQ(sizeof(InstantRestrictedID), sizeof(int)); | |
| 422 if (!base::StringToInt(restricted_id_str, &restricted_id)) | |
| 423 return false; | |
| 424 | |
| 425 std::string response_html; | |
| 426 InstantAutocompleteResult result; | |
| 427 if (autocomplete_results_cache_.GetItemWithRestrictedID( | |
| 428 restricted_id, &result)) { | |
| 429 std::string template_html = | |
| 430 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 431 IDR_OMNIBOX_RESULT).as_string(); | |
| 432 | |
| 433 DCHECK(IsColorValid(autocomplete_results_style_.url_color)); | |
| 434 DCHECK(IsColorValid(autocomplete_results_style_.title_color)); | |
| 435 | |
| 436 string16 contents; | |
| 437 if (result.search_query.empty()) { | |
| 438 contents = result.destination_url; | |
| 439 FormatURLForDisplay(&contents); | |
| 440 } else { | |
| 441 contents = result.search_query; | |
| 442 } | |
| 443 | |
| 444 // First, HTML-encode the text so that '&', '<' and such lose their special | |
| 445 // meaning. Next, URL-encode the text because it will be inserted into | |
| 446 // "data:" URIs; thus '%' and such lose their special meaning. | |
| 447 std::string encoded_contents = net::EscapeQueryParamValue( | |
| 448 net::EscapeForHTML(UTF16ToUTF8(contents)), false); | |
| 449 std::string encoded_description = net::EscapeQueryParamValue( | |
| 450 net::EscapeForHTML(UTF16ToUTF8(result.description)), false); | |
| 451 | |
| 452 response_html = base::StringPrintf( | |
| 453 template_html.c_str(), | |
| 454 embedder_origin.spec().c_str(), | |
| 455 UTF16ToUTF8(omnibox_font_).c_str(), | |
| 456 omnibox_font_size_, | |
| 457 autocomplete_results_style_.url_color, | |
| 458 autocomplete_results_style_.title_color, | |
| 459 encoded_contents.c_str(), | |
| 460 encoded_description.c_str()); | |
| 461 } else { | |
| 462 response_html = base::StringPrintf(kInvalidSuggestionHtml, restricted_id); | |
| 463 } | |
| 464 | |
| 465 *data_url = GURL("data:text/html;charset=utf-8," + response_html); | |
| 466 return true; | |
| 467 } | |
| 468 | |
| 469 void SearchBox::SetInstantAutocompleteResultStyle( | |
| 470 const InstantAutocompleteResultStyle& style) { | |
| 471 if (IsColorValid(style.url_color) && IsColorValid(style.title_color)) | |
| 472 autocomplete_results_style_ = style; | |
| 473 } | |
| 474 | |
| 475 void SearchBox::FormatURLForDisplay(string16* url) const { | |
| 476 StripURLPrefixes(url, query()); | |
| 477 | |
| 478 string16 trimmedUserInput; | |
| 479 TrimWhitespace(query(), TRIM_LEADING, &trimmedUserInput); | |
| 480 if (EndsWith(*url, trimmedUserInput, true)) | |
| 481 return; | |
| 482 | |
| 483 // Strip a lone trailing slash. | |
| 484 if (EndsWith(*url, ASCIIToUTF16("/"), true)) | |
| 485 url->erase(url->length() - 1, 1); | |
| 486 } | |
| 487 | |
| 488 // static | |
| 489 bool SearchBox::ShouldUseIframes() { | |
| 490 // TODO(shishir): All the code below is just temporary and needs to be removed | |
| 491 // once support for ShadowDom is removed. | |
| 492 | |
| 493 // The following is hacky. But given the short lifespan of this code | |
| 494 // and the amount of code that would need to be moved/copied for this change, | |
| 495 // it's probably worth it. | |
| 496 static const char kInstantExtendedFieldTrialName[] = "InstantExtended"; | |
| 497 static const char kIframesEnabledFlagWithValue[] = "iframe:1"; | |
| 498 std::string trial_flags = | |
| 499 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName); | |
| 500 std::vector<std::string> flags; | |
| 501 Tokenize(trial_flags, " ", &flags); | |
| 502 for (size_t i = 0; i < flags.size(); ++i) { | |
| 503 if (flags[i] == kIframesEnabledFlagWithValue) | |
| 504 return true; | |
| 505 } | |
| 506 return false; | |
| 507 } | |
| OLD | NEW |