Commit 14f7fde1 authored by Robbie Gibson's avatar Robbie Gibson Committed by Commit Bot

[iOS] Prevent "Defocus Omnibox no action" firing after context menu

Bug: 1014702
Change-Id: Ie3b0e41a11a36bddd41be0dfbeb86423ae235847
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1879236Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Commit-Queue: Robbie Gibson <rkgibson@google.com>
Cr-Commit-Position: refs/heads/master@{#709459}
parent 8ec63efc
...@@ -130,7 +130,6 @@ ...@@ -130,7 +130,6 @@
- (void)endEditing { - (void)endEditing {
[self.textField resignFirstResponder]; [self.textField resignFirstResponder];
_editView->EndEditing();
} }
- (void)insertTextToOmnibox:(NSString*)text { - (void)insertTextToOmnibox:(NSString*)text {
......
...@@ -22,6 +22,9 @@ class OmniboxTextChangeDelegate { ...@@ -22,6 +22,9 @@ class OmniboxTextChangeDelegate {
virtual void OnDidChange(bool processing_user_input) = 0; virtual void OnDidChange(bool processing_user_input) = 0;
// Called before the Omnibox text field finishes editing. // Called before the Omnibox text field finishes editing.
virtual void OnWillEndEditing() = 0; virtual void OnWillEndEditing() = 0;
// Hide keyboard and call OnDidEndEditing. This dismisses the keyboard and
// also finalizes the editing state of the omnibox.
virtual void EndEditing() = 0;
// Called when the Omnibox text field returns. (The "go" button is tapped.) // Called when the Omnibox text field returns. (The "go" button is tapped.)
virtual void OnAccept() = 0; virtual void OnAccept() = 0;
// Called when the Omnibox text field should copy. // Called when the Omnibox text field should copy.
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#import "ios/chrome/browser/ui/omnibox/omnibox_view_controller.h" #import "ios/chrome/browser/ui/omnibox/omnibox_view_controller.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "components/omnibox/browser/omnibox_field_trial.h" #include "components/omnibox/browser/omnibox_field_trial.h"
#include "components/open_from_clipboard/clipboard_recent_content.h" #include "components/open_from_clipboard/clipboard_recent_content.h"
...@@ -28,6 +30,8 @@ ...@@ -28,6 +30,8 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
using base::UserMetricsAction;
namespace { namespace {
const CGFloat kClearButtonSize = 28.0f; const CGFloat kClearButtonSize = 28.0f;
...@@ -59,6 +63,11 @@ const CGFloat kClearButtonSize = 28.0f; ...@@ -59,6 +63,11 @@ const CGFloat kClearButtonSize = 28.0f;
// typing vs by calls to setText. // typing vs by calls to setText.
@property(nonatomic, assign) BOOL processingUserEvent; @property(nonatomic, assign) BOOL processingUserEvent;
// A flag that is set whenever any input or copy/paste event happened in the
// omnibox while it was focused. Used to count event "user focuses the omnibox
// to view the complete URL and immediately defocuses it".
@property(nonatomic, assign) BOOL omniboxInteractedWhileFocused;
@end @end
@implementation OmniboxViewController @implementation OmniboxViewController
...@@ -184,6 +193,9 @@ const CGFloat kClearButtonSize = 28.0f; ...@@ -184,6 +193,9 @@ const CGFloat kClearButtonSize = 28.0f;
if (self.forwardingOnDidChange) if (self.forwardingOnDidChange)
return; return;
// Reset the changed flag.
self.omniboxInteractedWhileFocused = YES;
BOOL savedProcessingUserEvent = self.processingUserEvent; BOOL savedProcessingUserEvent = self.processingUserEvent;
self.processingUserEvent = NO; self.processingUserEvent = NO;
self.forwardingOnDidChange = YES; self.forwardingOnDidChange = YES;
...@@ -212,6 +224,7 @@ const CGFloat kClearButtonSize = 28.0f; ...@@ -212,6 +224,7 @@ const CGFloat kClearButtonSize = 28.0f;
self.semanticContentAttribute = [self.textField bestSemanticContentAttribute]; self.semanticContentAttribute = [self.textField bestSemanticContentAttribute];
self.omniboxInteractedWhileFocused = NO;
DCHECK(_textChangeDelegate); DCHECK(_textChangeDelegate);
_textChangeDelegate->OnDidBeginEditing(); _textChangeDelegate->OnDidBeginEditing();
} }
...@@ -223,6 +236,16 @@ const CGFloat kClearButtonSize = 28.0f; ...@@ -223,6 +236,16 @@ const CGFloat kClearButtonSize = 28.0f;
} }
// When editing, forward the message on to |_textChangeDelegate|. // When editing, forward the message on to |_textChangeDelegate|.
- (void)textFieldDidEndEditing:(UITextField*)textField
reason:(UITextFieldDidEndEditingReason)reason {
if (!self.omniboxInteractedWhileFocused) {
RecordAction(
UserMetricsAction("Mobile_FocusedDefocusedOmnibox_WithNoAction"));
}
DCHECK(_textChangeDelegate);
_textChangeDelegate->EndEditing();
}
- (BOOL)textFieldShouldClear:(UITextField*)textField { - (BOOL)textFieldShouldClear:(UITextField*)textField {
DCHECK(_textChangeDelegate); DCHECK(_textChangeDelegate);
_textChangeDelegate->ClearText(); _textChangeDelegate->ClearText();
...@@ -231,6 +254,7 @@ const CGFloat kClearButtonSize = 28.0f; ...@@ -231,6 +254,7 @@ const CGFloat kClearButtonSize = 28.0f;
} }
- (void)onCopy { - (void)onCopy {
self.omniboxInteractedWhileFocused = YES;
DCHECK(_textChangeDelegate); DCHECK(_textChangeDelegate);
_textChangeDelegate->OnCopy(); _textChangeDelegate->OnCopy();
} }
...@@ -398,6 +422,7 @@ const CGFloat kClearButtonSize = 28.0f; ...@@ -398,6 +422,7 @@ const CGFloat kClearButtonSize = 28.0f;
} }
- (void)searchCopiedImage:(id)sender { - (void)searchCopiedImage:(id)sender {
self.omniboxInteractedWhileFocused = YES;
if (base::Optional<gfx::Image> optionalImage = if (base::Optional<gfx::Image> optionalImage =
ClipboardRecentContent::GetInstance() ClipboardRecentContent::GetInstance()
->GetRecentImageFromClipboard()) { ->GetRecentImageFromClipboard()) {
...@@ -428,6 +453,7 @@ const CGFloat kClearButtonSize = 28.0f; ...@@ -428,6 +453,7 @@ const CGFloat kClearButtonSize = 28.0f;
clipboardRecentContent->GetRecentTextFromClipboard()) { clipboardRecentContent->GetRecentTextFromClipboard()) {
query = base::SysUTF16ToNSString(optionalText.value()); query = base::SysUTF16ToNSString(optionalText.value());
} }
self.omniboxInteractedWhileFocused = YES;
[self.dispatcher loadQuery:query immediately:YES]; [self.dispatcher loadQuery:query immediately:YES];
[self.dispatcher cancelOmniboxEdit]; [self.dispatcher cancelOmniboxEdit];
} }
......
...@@ -98,6 +98,7 @@ class OmniboxViewIOS : public OmniboxView, ...@@ -98,6 +98,7 @@ class OmniboxViewIOS : public OmniboxView,
bool OnWillChange(NSRange range, NSString* new_text) override; bool OnWillChange(NSRange range, NSString* new_text) override;
void OnDidChange(bool processing_user_input) override; void OnDidChange(bool processing_user_input) override;
void OnWillEndEditing() override; void OnWillEndEditing() override;
void EndEditing() override;
void OnAccept() override; void OnAccept() override;
void OnCopy() override; void OnCopy() override;
void ClearText() override; void ClearText() override;
...@@ -129,10 +130,6 @@ class OmniboxViewIOS : public OmniboxView, ...@@ -129,10 +130,6 @@ class OmniboxViewIOS : public OmniboxView,
void OnClear(); void OnClear();
// Hide keyboard and call OnDidEndEditing. This dismisses the keyboard and
// also finalizes the editing state of the omnibox.
void EndEditing();
// Hide keyboard only. Used when omnibox popups grab focus but editing isn't // Hide keyboard only. Used when omnibox popups grab focus but editing isn't
// complete. // complete.
void HideKeyboard(); void HideKeyboard();
...@@ -204,11 +201,6 @@ class OmniboxViewIOS : public OmniboxView, ...@@ -204,11 +201,6 @@ class OmniboxViewIOS : public OmniboxView,
NSMutableAttributedString* attributing_display_string_; NSMutableAttributedString* attributing_display_string_;
OmniboxPopupProvider* popup_provider_; // weak OmniboxPopupProvider* popup_provider_; // weak
// A flag that is set whenever any input or copy/paste event happened in the
// omnibox while it was focused. Used to count event "user focuses the omnibox
// to view the complete URL and immediately defocuses it".
BOOL omnibox_interacted_while_focused_;
}; };
#endif // IOS_CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_IOS_H_ #endif // IOS_CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_IOS_H_
...@@ -262,8 +262,6 @@ gfx::NativeView OmniboxViewIOS::GetRelativeWindowForPopup() const { ...@@ -262,8 +262,6 @@ gfx::NativeView OmniboxViewIOS::GetRelativeWindowForPopup() const {
} }
void OmniboxViewIOS::OnDidBeginEditing() { void OmniboxViewIOS::OnDidBeginEditing() {
// Reset the changed flag.
omnibox_interacted_while_focused_ = NO;
// If Open from Clipboard offers a suggestion, the popup may be opened when // If Open from Clipboard offers a suggestion, the popup may be opened when
// |OnSetFocus| is called on the model. The state of the popup is saved early // |OnSetFocus| is called on the model. The state of the popup is saved early
...@@ -406,8 +404,6 @@ bool OmniboxViewIOS::OnWillChange(NSRange range, NSString* new_text) { ...@@ -406,8 +404,6 @@ bool OmniboxViewIOS::OnWillChange(NSRange range, NSString* new_text) {
} }
void OmniboxViewIOS::OnDidChange(bool processing_user_event) { void OmniboxViewIOS::OnDidChange(bool processing_user_event) {
omnibox_interacted_while_focused_ = YES;
// Sanitize pasted text. // Sanitize pasted text.
if (model() && model()->is_pasting()) { if (model() && model()->is_pasting()) {
base::string16 pastedText = base::SysNSStringToUTF16([field_ text]); base::string16 pastedText = base::SysNSStringToUTF16([field_ text]);
...@@ -483,7 +479,6 @@ void OmniboxViewIOS::OnClear() { ...@@ -483,7 +479,6 @@ void OmniboxViewIOS::OnClear() {
} }
void OmniboxViewIOS::OnCopy() { void OmniboxViewIOS::OnCopy() {
omnibox_interacted_while_focused_ = YES;
UIPasteboard* board = [UIPasteboard generalPasteboard]; UIPasteboard* board = [UIPasteboard generalPasteboard];
NSString* selectedText = nil; NSString* selectedText = nil;
NSInteger start_location = 0; NSInteger start_location = 0;
...@@ -658,11 +653,6 @@ void OmniboxViewIOS::EndEditing() { ...@@ -658,11 +653,6 @@ void OmniboxViewIOS::EndEditing() {
// Blow away any in-progress edits. // Blow away any in-progress edits.
RevertAll(); RevertAll();
DCHECK(![field_ hasAutocompleteText]); DCHECK(![field_ hasAutocompleteText]);
if (!omnibox_interacted_while_focused_) {
RecordAction(
UserMetricsAction("Mobile_FocusedDefocusedOmnibox_WithNoAction"));
}
} }
} }
......
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