Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: chrome/renderer/searchbox/searchbox.cc

Issue 13375003: Fixing iframe jank in the local omnibox popup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: virtual Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/searchbox/searchbox.h ('k') | chrome/renderer/searchbox/searchbox_extension.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 std::vector<InstantMostVisitedItemIDPair>* items) const { 385 std::vector<InstantMostVisitedItemIDPair>* items) const {
418 return most_visited_items_cache_.GetCurrentItems(items); 386 return most_visited_items_cache_.GetCurrentItems(items);
419 } 387 }
420 388
421 bool SearchBox::GetMostVisitedItemWithID( 389 bool SearchBox::GetMostVisitedItemWithID(
422 InstantRestrictedID most_visited_item_id, 390 InstantRestrictedID most_visited_item_id,
423 InstantMostVisitedItem* item) const { 391 InstantMostVisitedItem* item) const {
424 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id, 392 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id,
425 item); 393 item);
426 } 394 }
427
428 bool SearchBox::GenerateDataURLForSuggestionRequest(const GURL& request_url,
429 GURL* data_url) const {
430 DCHECK(data_url);
431
432 if (!ShouldUseIframes())
433 return false;
434
435 // The origin URL is required so that the iframe knows what origin to post
436 // messages to.
437 WebKit::WebView* webview = render_view()->GetWebView();
438 if (!webview)
439 return false;
440 GURL embedder_url(webview->mainFrame()->document().url());
441 GURL embedder_origin = embedder_url.GetOrigin();
442 if (!embedder_origin.is_valid())
443 return false;
444
445 DCHECK(StartsWithASCII(request_url.path(), "/", true));
446 std::string restricted_id_str = request_url.path().substr(1);
447
448 InstantRestrictedID restricted_id = 0;
449 DCHECK_EQ(sizeof(InstantRestrictedID), sizeof(int));
450 if (!base::StringToInt(restricted_id_str, &restricted_id))
451 return false;
452
453 std::string response_html;
454 InstantAutocompleteResult result;
455 if (autocomplete_results_cache_.GetItemWithRestrictedID(
456 restricted_id, &result)) {
457 std::string template_html =
458 ResourceBundle::GetSharedInstance().GetRawDataResource(
459 IDR_OMNIBOX_RESULT).as_string();
460
461 DCHECK(IsColorValid(autocomplete_results_style_.url_color));
462 DCHECK(IsColorValid(autocomplete_results_style_.title_color));
463
464 string16 contents;
465 if (result.search_query.empty()) {
466 contents = result.destination_url;
467 FormatURLForDisplay(&contents);
468 } else {
469 contents = result.search_query;
470 }
471
472 // First, HTML-encode the text so that '&', '<' and such lose their special
473 // meaning. Next, URL-encode the text because it will be inserted into
474 // "data:" URIs; thus '%' and such lose their special meaning.
475 std::string encoded_contents = net::EscapeQueryParamValue(
476 net::EscapeForHTML(UTF16ToUTF8(contents)), false);
477 std::string encoded_description = net::EscapeQueryParamValue(
478 net::EscapeForHTML(UTF16ToUTF8(result.description)), false);
479
480 response_html = base::StringPrintf(
481 template_html.c_str(),
482 embedder_origin.spec().c_str(),
483 UTF16ToUTF8(omnibox_font_).c_str(),
484 omnibox_font_size_,
485 autocomplete_results_style_.url_color,
486 autocomplete_results_style_.title_color,
487 encoded_contents.c_str(),
488 encoded_description.c_str());
489 } else {
490 response_html = base::StringPrintf(kInvalidSuggestionHtml, restricted_id);
491 }
492
493 *data_url = GURL("data:text/html;charset=utf-8," + response_html);
494 return true;
495 }
496
497 void SearchBox::SetInstantAutocompleteResultStyle(
498 const InstantAutocompleteResultStyle& style) {
499 if (IsColorValid(style.url_color) && IsColorValid(style.title_color))
500 autocomplete_results_style_ = style;
501 }
502
503 void SearchBox::FormatURLForDisplay(string16* url) const {
504 StripURLPrefixes(url, query());
505
506 string16 trimmedUserInput;
507 TrimWhitespace(query(), TRIM_LEADING, &trimmedUserInput);
508 if (EndsWith(*url, trimmedUserInput, true))
509 return;
510
511 // Strip a lone trailing slash.
512 if (EndsWith(*url, ASCIIToUTF16("/"), true))
513 url->erase(url->length() - 1, 1);
514 }
515
516 // static
517 bool SearchBox::ShouldUseIframes() {
518 // TODO(shishir): All the code below is just temporary and needs to be removed
519 // once support for ShadowDom is removed.
520
521 // The following is hacky. But given the short lifespan of this code
522 // and the amount of code that would need to be moved/copied for this change,
523 // it's probably worth it.
524 static const char kInstantExtendedFieldTrialName[] = "InstantExtended";
525 static const char kIframesEnabledFlagWithValue[] = "iframe:1";
526 std::string trial_flags =
527 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName);
528 std::vector<std::string> flags;
529 Tokenize(trial_flags, " ", &flags);
530 for (size_t i = 0; i < flags.size(); ++i) {
531 if (flags[i] == kIframesEnabledFlagWithValue)
532 return true;
533 }
534 return false;
535 }
OLDNEW
« no previous file with comments | « chrome/renderer/searchbox/searchbox.h ('k') | chrome/renderer/searchbox/searchbox_extension.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698