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