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

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: Removing sites, too. 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
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 327 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 std::vector<InstantMostVisitedItemIDPair>* items) const { 364 std::vector<InstantMostVisitedItemIDPair>* items) const {
397 return most_visited_items_cache_.GetCurrentItems(items); 365 return most_visited_items_cache_.GetCurrentItems(items);
398 } 366 }
399 367
400 bool SearchBox::GetMostVisitedItemWithID( 368 bool SearchBox::GetMostVisitedItemWithID(
401 InstantRestrictedID most_visited_item_id, 369 InstantRestrictedID most_visited_item_id,
402 InstantMostVisitedItem* item) const { 370 InstantMostVisitedItem* item) const {
403 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id, 371 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id,
404 item); 372 item);
405 } 373 }
406
407 bool SearchBox::GenerateDataURLForSuggestionRequest(const GURL& request_url,
408 GURL* data_url) const {
409 DCHECK(data_url);
410
411 if (!ShouldUseIframes())
412 return false;
413
414 // The origin URL is required so that the iframe knows what origin to post
415 // messages to.
416 WebKit::WebView* webview = render_view()->GetWebView();
417 if (!webview)
418 return false;
419 GURL embedder_url(webview->mainFrame()->document().url());
420 GURL embedder_origin = embedder_url.GetOrigin();
421 if (!embedder_origin.is_valid())
422 return false;
423
424 DCHECK(StartsWithASCII(request_url.path(), "/", true));
425 std::string restricted_id_str = request_url.path().substr(1);
426
427 InstantRestrictedID restricted_id = 0;
428 DCHECK_EQ(sizeof(InstantRestrictedID), sizeof(int));
429 if (!base::StringToInt(restricted_id_str, &restricted_id))
430 return false;
431
432 std::string response_html;
433 InstantAutocompleteResult result;
434 if (autocomplete_results_cache_.GetItemWithRestrictedID(
435 restricted_id, &result)) {
436 std::string template_html =
437 ResourceBundle::GetSharedInstance().GetRawDataResource(
438 IDR_OMNIBOX_RESULT).as_string();
439
440 DCHECK(IsColorValid(autocomplete_results_style_.url_color));
441 DCHECK(IsColorValid(autocomplete_results_style_.title_color));
442
443 string16 contents;
444 if (result.search_query.empty()) {
445 contents = result.destination_url;
446 FormatURLForDisplay(&contents);
447 } else {
448 contents = result.search_query;
449 }
450
451 // First, HTML-encode the text so that '&', '<' and such lose their special
452 // meaning. Next, URL-encode the text because it will be inserted into
453 // "data:" URIs; thus '%' and such lose their special meaning.
454 std::string encoded_contents = net::EscapeQueryParamValue(
455 net::EscapeForHTML(UTF16ToUTF8(contents)), false);
456 std::string encoded_description = net::EscapeQueryParamValue(
457 net::EscapeForHTML(UTF16ToUTF8(result.description)), false);
458
459 response_html = base::StringPrintf(
460 template_html.c_str(),
461 embedder_origin.spec().c_str(),
462 UTF16ToUTF8(omnibox_font_).c_str(),
463 omnibox_font_size_,
464 autocomplete_results_style_.url_color,
465 autocomplete_results_style_.title_color,
466 encoded_contents.c_str(),
467 encoded_description.c_str());
468 } else {
469 response_html = base::StringPrintf(kInvalidSuggestionHtml, restricted_id);
470 }
471
472 *data_url = GURL("data:text/html;charset=utf-8," + response_html);
473 return true;
474 }
475
476 void SearchBox::SetInstantAutocompleteResultStyle(
477 const InstantAutocompleteResultStyle& style) {
478 if (IsColorValid(style.url_color) && IsColorValid(style.title_color))
479 autocomplete_results_style_ = style;
480 }
481
482 void SearchBox::FormatURLForDisplay(string16* url) const {
483 StripURLPrefixes(url, query());
484
485 string16 trimmedUserInput;
486 TrimWhitespace(query(), TRIM_LEADING, &trimmedUserInput);
487 if (EndsWith(*url, trimmedUserInput, true))
488 return;
489
490 // Strip a lone trailing slash.
491 if (EndsWith(*url, ASCIIToUTF16("/"), true))
492 url->erase(url->length() - 1, 1);
493 }
494
495 // static
496 bool SearchBox::ShouldUseIframes() {
497 // TODO(shishir): All the code below is just temporary and needs to be removed
498 // once support for ShadowDom is removed.
499
500 // The following is hacky. But given the short lifespan of this code
501 // and the amount of code that would need to be moved/copied for this change,
502 // it's probably worth it.
503 static const char kInstantExtendedFieldTrialName[] = "InstantExtended";
504 static const char kIframesEnabledFlagWithValue[] = "iframe:1";
505 std::string trial_flags =
506 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName);
507 std::vector<std::string> flags;
508 Tokenize(trial_flags, " ", &flags);
509 for (size_t i = 0; i < flags.size(); ++i) {
510 if (flags[i] == kIframesEnabledFlagWithValue)
511 return true;
512 }
513 return false;
514 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698