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

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

Powered by Google App Engine
This is Rietveld 408576698