summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorshess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-19 00:08:22 +0000
committershess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-19 00:08:22 +0000
commit543b130ee81bc8d263d27639726ffcc68c7f74ea (patch)
treef789b657b7f0ac1bde724afac9f4e42dd4234040 /chrome
parent280d93e7786a2f10b4d25990fafd3cfe3b830ba9 (diff)
downloadchromium_src-543b130ee81bc8d263d27639726ffcc68c7f74ea.zip
chromium_src-543b130ee81bc8d263d27639726ffcc68c7f74ea.tar.gz
chromium_src-543b130ee81bc8d263d27639726ffcc68c7f74ea.tar.bz2
[Mac] Prevent entering raw control characters in Omnibox.
The code already intercepted some of the shady ways for entering raw control characters. This also handles some cases which weren't intercepted by doCommandBySelector:. BUG=38345 TEST=Try to enter Ctrl-Q Enter, C-Q TAB, Ctrl-Alt-Enter and Ctrl-Alt-TAB in Omnibox. Review URL: http://codereview.chromium.org/1008003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42036 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/cocoa/autocomplete_text_field_editor.h2
-rw-r--r--chrome/browser/cocoa/autocomplete_text_field_editor.mm34
-rw-r--r--chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm32
3 files changed, 67 insertions, 1 deletions
diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor.h b/chrome/browser/cocoa/autocomplete_text_field_editor.h
index bb3afc5..aee4e6a 100644
--- a/chrome/browser/cocoa/autocomplete_text_field_editor.h
+++ b/chrome/browser/cocoa/autocomplete_text_field_editor.h
@@ -27,6 +27,8 @@ class Profile;
// The browser profile for the editor. Weak.
Profile* profile_;
+
+ scoped_nsobject<NSCharacterSet> forbiddenCharacters_;
}
@property(nonatomic) Profile* profile;
diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor.mm b/chrome/browser/cocoa/autocomplete_text_field_editor.mm
index 7eb8b93..2a5a95e 100644
--- a/chrome/browser/cocoa/autocomplete_text_field_editor.mm
+++ b/chrome/browser/cocoa/autocomplete_text_field_editor.mm
@@ -30,8 +30,11 @@ class Extension;
@synthesize profile = profile_;
- (id)initWithFrame:(NSRect)frameRect {
- if ((self = [super initWithFrame:frameRect]))
+ if ((self = [super initWithFrame:frameRect])) {
dropHandler_.reset([[URLDropTargetHandler alloc] initWithView:self]);
+
+ forbiddenCharacters_.reset([[NSCharacterSet controlCharacterSet] retain]);
+ }
return self;
}
@@ -215,4 +218,33 @@ class Extension;
return [dropHandler_ performDragOperation:sender];
}
+// Prevent control characters from being entered into the Omnibox.
+// This is invoked for keyboard entry, not for pasting.
+- (void)insertText:(id)aString {
+ // This method is documented as received either |NSString| or
+ // |NSAttributedString|. The autocomplete code will restyle the
+ // results in any case, so simplify by always using |NSString|.
+ if ([aString isKindOfClass:[NSAttributedString class]])
+ aString = [aString string];
+
+ // Repeatedly remove control characters. The loop will only ever
+ // execute at allwhen the user enters control characters (using
+ // Ctrl-Alt- or Ctrl-Q). Making this generally efficient would
+ // probably be a loss, since the input always seems to be a single
+ // character.
+ NSRange range = [aString rangeOfCharacterFromSet:forbiddenCharacters_];
+ while (range.location != NSNotFound) {
+ aString = [aString stringByReplacingCharactersInRange:range withString:@""];
+ range = [aString rangeOfCharacterFromSet:forbiddenCharacters_];
+ }
+ DCHECK_EQ(range.length, 0U);
+
+ // TODO(shess): Beep on empty? I hate beeps, though.
+ // NOTE: If |aString| is empty, this intentionally replaces the
+ // selection with empty. This seems consistent with the case where
+ // the input contained a mixture of characters and the string ended
+ // up not empty.
+ [super insertText:aString];
+}
+
@end
diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm
index 29d87ec..39b73a4 100644
--- a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm
+++ b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm
@@ -61,6 +61,38 @@ class AutocompleteTextFieldEditorTest : public CocoaTest {
TEST_VIEW(AutocompleteTextFieldEditorTest, field_);
+// Test that control characters are stripped from insertions.
+TEST_F(AutocompleteTextFieldEditorTest, InsertStripsControlChars) {
+ // Sets a string in the field.
+ NSString* test_string = @"astring";
+ [field_ setStringValue:test_string];
+ [editor_ selectAll:nil];
+
+ [editor_ insertText:@"t"];
+ EXPECT_TRUE([[field_ stringValue] isEqualToString:@"t"]);
+
+ [editor_ insertText:@"h"];
+ EXPECT_TRUE([[field_ stringValue] isEqualToString:@"th"]);
+
+ // TAB doesn't get inserted.
+ [editor_ insertText:[NSString stringWithFormat:@"%c", 7]];
+ EXPECT_TRUE([[field_ stringValue] isEqualToString:@"th"]);
+
+ // Newline doesn't get inserted.
+ [editor_ insertText:[NSString stringWithFormat:@"%c", 12]];
+ EXPECT_TRUE([[field_ stringValue] isEqualToString:@"th"]);
+
+ // Multi-character strings get through.
+ [editor_ insertText:[NSString stringWithFormat:@"i%cs%c", 8, 127]];
+ EXPECT_TRUE([[field_ stringValue] isEqualToString:@"this"]);
+
+ // Attempting to insert newline when everything is selected clears
+ // the field.
+ [editor_ selectAll:nil];
+ [editor_ insertText:[NSString stringWithFormat:@"%c", 12]];
+ EXPECT_TRUE([[field_ stringValue] isEqualToString:@""]);
+}
+
// Base class for testing AutocompleteTextFieldObserver messages.
class AutocompleteTextFieldEditorObserverTest
: public AutocompleteTextFieldEditorTest {