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

Side by Side Diff: chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc

Issue 8702002: Strip invalid characters (line breaks, tabs), javascript:schemes from the copied text while pasti... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years 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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/browser/ui/gtk/omnibox/omnibox_view_gtk.h" 5 #include "chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h"
6 6
7 #include <gdk/gdkkeysyms.h> 7 #include <gdk/gdkkeysyms.h>
8 #include <gtk/gtk.h> 8 #include <gtk/gtk.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/utf_string_conversion_utils.h"
14 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
15 #include "chrome/app/chrome_command_ids.h" 16 #include "chrome/app/chrome_command_ids.h"
16 #include "chrome/browser/autocomplete/autocomplete_edit.h" 17 #include "chrome/browser/autocomplete/autocomplete_edit.h"
17 #include "chrome/browser/autocomplete/autocomplete_match.h" 18 #include "chrome/browser/autocomplete/autocomplete_match.h"
18 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" 19 #include "chrome/browser/autocomplete/autocomplete_popup_model.h"
19 #include "chrome/browser/bookmarks/bookmark_node_data.h" 20 #include "chrome/browser/bookmarks/bookmark_node_data.h"
20 #include "chrome/browser/command_updater.h" 21 #include "chrome/browser/command_updater.h"
21 #include "chrome/browser/defaults.h" 22 #include "chrome/browser/defaults.h"
22 #include "chrome/browser/instant/instant_controller.h" 23 #include "chrome/browser/instant/instant_controller.h"
23 #include "chrome/browser/platform_util.h" 24 #include "chrome/browser/platform_util.h"
(...skipping 847 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 int OmniboxViewGtk::OnPerformDrop( 872 int OmniboxViewGtk::OnPerformDrop(
872 const views::DropTargetEvent& event) { 873 const views::DropTargetEvent& event) {
873 string16 text; 874 string16 text;
874 const ui::OSExchangeData& data = event.data(); 875 const ui::OSExchangeData& data = event.data();
875 if (data.HasURL()) { 876 if (data.HasURL()) {
876 GURL url; 877 GURL url;
877 string16 title; 878 string16 title;
878 if (data.GetURLAndTitle(&url, &title)) 879 if (data.GetURLAndTitle(&url, &title))
879 text = UTF8ToUTF16(url.spec()); 880 text = UTF8ToUTF16(url.spec());
880 } else { 881 } else {
881 string16 data_string; 882 data.GetString(&text);
882 if (data.GetString(&data_string))
883 text = CollapseWhitespace(data_string, true);
884 } 883 }
885 884
886 if (!text.empty() && OnPerformDropImpl(text)) 885 if (!text.empty() && OnPerformDropImpl(text))
887 return CopyOrLinkDragOperation(event.source_operations()); 886 return CopyOrLinkDragOperation(event.source_operations());
888 887
889 return ui::DragDropTypes::DRAG_NONE; 888 return ui::DragDropTypes::DRAG_NONE;
890 } 889 }
891 #endif // defined(TOOLKIT_VIEWS) 890 #endif // defined(TOOLKIT_VIEWS)
892 891
893 void OmniboxViewGtk::Observe(int type, 892 void OmniboxViewGtk::Observe(int type,
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after
1437 gtk_widget_set_sensitive(search_engine_menuitem, 1436 gtk_widget_set_sensitive(search_engine_menuitem,
1438 command_updater_->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES)); 1437 command_updater_->IsCommandEnabled(IDC_EDIT_SEARCH_ENGINES));
1439 gtk_widget_show(search_engine_menuitem); 1438 gtk_widget_show(search_engine_menuitem);
1440 1439
1441 // We need to update the paste and go controller before we know what text 1440 // We need to update the paste and go controller before we know what text
1442 // to show. We could do this all asynchronously, but it would be elaborate 1441 // to show. We could do this all asynchronously, but it would be elaborate
1443 // because we'd have to account for multiple menus showing, getting called 1442 // because we'd have to account for multiple menus showing, getting called
1444 // back after shutdown, and similar issues. 1443 // back after shutdown, and similar issues.
1445 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); 1444 GtkClipboard* x_clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1446 gchar* text = gtk_clipboard_wait_for_text(x_clipboard); 1445 gchar* text = gtk_clipboard_wait_for_text(x_clipboard);
1447 string16 text_wstr = UTF8ToUTF16(text ? text : ""); 1446 string16 sanitized_text(text ?
1447 StripJavascriptSchemas(CollapseWhitespace(UTF8ToUTF16(text), true)) :
1448 string16());
1448 g_free(text); 1449 g_free(text);
1449 1450
1450 // Paste and Go menu item. 1451 // Paste and Go menu item. Note that CanPasteAndGo() needs to be called
1452 // before is_paste_and_search() in order to set up the paste-and-go state.
1453 bool can_paste_and_go = model_->CanPasteAndGo(sanitized_text);
1451 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic( 1454 GtkWidget* paste_go_menuitem = gtk_menu_item_new_with_mnemonic(
1452 gfx::ConvertAcceleratorsFromWindowsStyle( 1455 gfx::ConvertAcceleratorsFromWindowsStyle(
1453 l10n_util::GetStringUTF8(model_->is_paste_and_search() ? 1456 l10n_util::GetStringUTF8(model_->is_paste_and_search() ?
1454 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str()); 1457 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO)).c_str());
1455 gtk_menu_shell_append(GTK_MENU_SHELL(menu), paste_go_menuitem); 1458 gtk_menu_shell_append(GTK_MENU_SHELL(menu), paste_go_menuitem);
1456 g_signal_connect(paste_go_menuitem, "activate", 1459 g_signal_connect(paste_go_menuitem, "activate",
1457 G_CALLBACK(HandlePasteAndGoThunk), this); 1460 G_CALLBACK(HandlePasteAndGoThunk), this);
1458 gtk_widget_set_sensitive(paste_go_menuitem, 1461 gtk_widget_set_sensitive(paste_go_menuitem, can_paste_and_go);
1459 model_->CanPasteAndGo(text_wstr));
1460 gtk_widget_show(paste_go_menuitem); 1462 gtk_widget_show(paste_go_menuitem);
1461 1463
1462 g_signal_connect(menu, "deactivate", 1464 g_signal_connect(menu, "deactivate",
1463 G_CALLBACK(HandlePopupMenuDeactivateThunk), this); 1465 G_CALLBACK(HandlePopupMenuDeactivateThunk), this);
1464 } 1466 }
1465 1467
1466 void OmniboxViewGtk::HandleEditSearchEngines(GtkWidget* sender) { 1468 void OmniboxViewGtk::HandleEditSearchEngines(GtkWidget* sender) {
1467 command_updater_->ExecuteCommand(IDC_EDIT_SEARCH_ENGINES); 1469 command_updater_->ExecuteCommand(IDC_EDIT_SEARCH_ENGINES);
1468 } 1470 }
1469 1471
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1625 GtkTargetList* copy_targets = 1627 GtkTargetList* copy_targets =
1626 gtk_text_buffer_get_copy_target_list(text_buffer_); 1628 gtk_text_buffer_get_copy_target_list(text_buffer_);
1627 gtk_target_list_remove(copy_targets, atom); 1629 gtk_target_list_remove(copy_targets, atom);
1628 dragged_text_.clear(); 1630 dragged_text_.clear();
1629 } 1631 }
1630 1632
1631 void OmniboxViewGtk::HandleInsertText(GtkTextBuffer* buffer, 1633 void OmniboxViewGtk::HandleInsertText(GtkTextBuffer* buffer,
1632 GtkTextIter* location, 1634 GtkTextIter* location,
1633 const gchar* text, 1635 const gchar* text,
1634 gint len) { 1636 gint len) {
1635 std::string filtered_text; 1637 string16 filtered_text;
1636 filtered_text.reserve(len); 1638 filtered_text.reserve(len);
1637 1639
1638 // Filter out new line and tab characters. 1640 // Filter out new line and tab characters.
1639 // |text| is guaranteed to be a valid UTF-8 string, so we don't need to 1641 // |text| is guaranteed to be a valid UTF-8 string, so we don't need to
1640 // validate it here. 1642 // validate it here.
1641 // 1643 //
1642 // If there was only a single character, then it might be generated by a key 1644 // If there was only a single character, then it might be generated by a key
1643 // event. In this case, we save the single character to help our 1645 // event. In this case, we save the single character to help our
1644 // "key-press-event" signal handler distinguish if an Enter key event is 1646 // "key-press-event" signal handler distinguish if an Enter key event is
1645 // handled by IME or not. 1647 // handled by IME or not.
1646 if (len == 1 && (text[0] == '\n' || text[0] == '\r')) 1648 if (len == 1 && (text[0] == '\n' || text[0] == '\r'))
1647 enter_was_inserted_ = true; 1649 enter_was_inserted_ = true;
1648 1650
1649 const gchar* p = text; 1651 for (const gchar* p = text; *p && (p - text) < len;
1650 while (*p && (p - text) < len) { 1652 p = g_utf8_next_char(p)) {
1651 gunichar c = g_utf8_get_char(p); 1653 gunichar c = g_utf8_get_char(p);
1652 const gchar* next = g_utf8_next_char(p);
1653 1654
1654 // 0x200B is Zero Width Space, which is inserted just before the instant 1655 // 0x200B is Zero Width Space, which is inserted just before the instant
1655 // anchor for working around the GtkTextView's misalignment bug. 1656 // anchor for working around the GtkTextView's misalignment bug.
1656 // This character might be captured and inserted into the content by undo 1657 // This character might be captured and inserted into the content by undo
1657 // manager, so we need to filter it out here. 1658 // manager, so we need to filter it out here.
1658 if (c != L'\n' && c != L'\r' && c != L'\t' && c != 0x200B) 1659 if (c != 0x200B)
1659 filtered_text.append(p, next); 1660 base::WriteUnicodeCharacter(c, &filtered_text);
1660
1661 p = next;
1662 } 1661 }
1663 1662
1664 if (filtered_text.length()) { 1663 if (model_->is_pasting())
1664 filtered_text = StripJavascriptSchemas(
1665 CollapseWhitespace(filtered_text, true));
1666
1667 if (!filtered_text.empty()) {
1665 // Avoid inserting the text after the instant anchor. 1668 // Avoid inserting the text after the instant anchor.
1666 ValidateTextBufferIter(location); 1669 ValidateTextBufferIter(location);
1667 1670
1668 // Call the default handler to insert filtered text. 1671 // Call the default handler to insert filtered text.
1669 GtkTextBufferClass* klass = GTK_TEXT_BUFFER_GET_CLASS(buffer); 1672 GtkTextBufferClass* klass = GTK_TEXT_BUFFER_GET_CLASS(buffer);
1670 klass->insert_text(buffer, location, filtered_text.data(), 1673 std::string utf8_text = UTF16ToUTF8(filtered_text);
1671 static_cast<gint>(filtered_text.length())); 1674 klass->insert_text(buffer, location, utf8_text.data(),
1675 static_cast<gint>(utf8_text.length()));
1672 } 1676 }
1673 1677
1674 // Stop propagating the signal emission to prevent the default handler from 1678 // Stop propagating the signal emission to prevent the default handler from
1675 // being called again. 1679 // being called again.
1676 static guint signal_id = g_signal_lookup("insert-text", GTK_TYPE_TEXT_BUFFER); 1680 static guint signal_id = g_signal_lookup("insert-text", GTK_TYPE_TEXT_BUFFER);
1677 g_signal_stop_emission(buffer, signal_id, 0); 1681 g_signal_stop_emission(buffer, signal_id, 0);
1678 } 1682 }
1679 1683
1680 void OmniboxViewGtk::HandleBackSpace(GtkWidget* sender) { 1684 void OmniboxViewGtk::HandleBackSpace(GtkWidget* sender) {
1681 // Checks if it's currently in keyword search mode. 1685 // Checks if it's currently in keyword search mode.
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1788 0); 1792 0);
1789 1793
1790 if (!copy && gtk_text_view_get_editable(GTK_TEXT_VIEW(text_view_))) 1794 if (!copy && gtk_text_view_get_editable(GTK_TEXT_VIEW(text_view_)))
1791 gtk_text_buffer_delete_selection(text_buffer_, true, true); 1795 gtk_text_buffer_delete_selection(text_buffer_, true, true);
1792 } 1796 }
1793 1797
1794 OwnPrimarySelection(UTF16ToUTF8(text)); 1798 OwnPrimarySelection(UTF16ToUTF8(text));
1795 } 1799 }
1796 1800
1797 bool OmniboxViewGtk::OnPerformDropImpl(const string16& text) { 1801 bool OmniboxViewGtk::OnPerformDropImpl(const string16& text) {
1798 if (model_->CanPasteAndGo(CollapseWhitespace(text, true))) { 1802 if (model_->CanPasteAndGo(StripJavascriptSchemas(
1803 CollapseWhitespace(text, true)))) {
1799 model_->PasteAndGo(); 1804 model_->PasteAndGo();
1800 return true; 1805 return true;
1801 } 1806 }
1802 1807
1803 return false; 1808 return false;
1804 } 1809 }
1805 1810
1806 gfx::Font OmniboxViewGtk::GetFont() { 1811 gfx::Font OmniboxViewGtk::GetFont() {
1807 #if defined(TOOLKIT_VIEWS) 1812 #if defined(TOOLKIT_VIEWS)
1808 bool use_gtk = false; 1813 bool use_gtk = false;
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after
2394 2399
2395 // Make all the children of the widget visible. NOTE: this won't display 2400 // Make all the children of the widget visible. NOTE: this won't display
2396 // anything, it just toggles the visible flag. 2401 // anything, it just toggles the visible flag.
2397 gtk_widget_show_all(omnibox_view->GetNativeView()); 2402 gtk_widget_show_all(omnibox_view->GetNativeView());
2398 // Hide the widget. NativeViewHostGtk will make it visible again as necessary. 2403 // Hide the widget. NativeViewHostGtk will make it visible again as necessary.
2399 gtk_widget_hide(omnibox_view->GetNativeView()); 2404 gtk_widget_hide(omnibox_view->GetNativeView());
2400 2405
2401 return omnibox_view; 2406 return omnibox_view;
2402 } 2407 }
2403 #endif 2408 #endif
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/autocomplete_edit.h ('k') | chrome/browser/ui/omnibox/omnibox_view_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698