Commit 8fd04b5f authored by Tessa Nijssen's avatar Tessa Nijssen Committed by Commit Bot

[Mac] Moved TextSuggestionsTouchBarController's Range Validity Check

Previously, -updateTextSelection:range:offset: could receive an invalid
range from WebContentsTextObserver::DidChangeTextSelection(). This
invalid range would then cause a crash in -requestSuggestions.

A check has been added to -updateTextSelection:range:offset: to make
sure that a selection with an invalid range is not passed to
-requestSuggestions. This should prevent future crashes.

TextSuggestionsTouchBarControllerTest.InvalidRange tests that an
invalid range does not crash the controller and is handled
properly.

Bug: 717553
Change-Id: I69f49cb1f1819e1d528837767dfb894d8652e84d
Reviewed-on: https://chromium-review.googlesource.com/1178467Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarSarah Chan <spqchan@chromium.org>
Commit-Queue: Tessa Nijssen <tnijssen@google.com>
Cr-Commit-Position: refs/heads/master@{#583815}
parent cdbb74a6
......@@ -173,6 +173,11 @@ class WebContentsTextObserver : public content::WebContentsObserver {
return;
}
if (!range.IsValid()) {
[self updateTextSelection:base::string16() range:gfx::Range() offset:0];
return;
}
text_.reset([base::SysUTF16ToNSString(text) retain]);
selectionRange_ =
NSMakeRange(range.start() - offset, range.end() - range.start());
......@@ -271,10 +276,8 @@ class WebContentsTextObserver : public content::WebContentsObserver {
webContents_->GetTopLevelRenderWidgetHostView()->GetSelectedRange();
const size_t offset = webContents_->GetTopLevelRenderWidgetHostView()
->GetOffsetForSurroundingText();
if (range.IsValid())
[self updateTextSelection:text range:range offset:offset];
else
[self updateTextSelection:base::string16() range:gfx::Range() offset:0];
}
- (content::WebContents*)webContents {
......
......@@ -191,6 +191,17 @@ IN_PROC_BROWSER_TEST_F(TextSuggestionsTouchBarControllerTest,
}
}
// Tests that an invalid range does not crash the controller.
IN_PROC_BROWSER_TEST_F(TextSuggestionsTouchBarControllerTest, InvalidRange) {
FocusTextfield();
[touch_bar_controller_
updateTextSelection:base::string16(base::ASCIIToUTF16("text"))
range:gfx::Range::InvalidRange()
offset:0];
EXPECT_STREQ("", [touch_bar_controller_ text].UTF8String);
EXPECT_EQ(gfx::Range(), [touch_bar_controller_ selectionRange]);
}
// Tests that a change in WebContents is handled properly.
IN_PROC_BROWSER_TEST_F(TextSuggestionsTouchBarControllerTest, SetWebContents) {
NSString* const kText = @"text";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment