Skip to content

Commit

Permalink
Updating PageClickTracker to Notify listeners When Text Input Loses F…
Browse files Browse the repository at this point in the history
…ocus.

Changing PageClickTracker and PageClickListener to give listeners the option to be informed when a text input element loses focus after gaining it. This will be used by the new autofill UI code once it is moved out of WebKit.


BUG=51644
TEST=


Review URL: http://codereview.chromium.org/8351009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@108649 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
csharp@chromium.org committed Nov 4, 2011
1 parent d03e378 commit 65ec8db
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 70 deletions.
4 changes: 4 additions & 0 deletions chrome/renderer/autofill/autofill_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ bool AutofillAgent::InputElementClicked(const WebInputElement& element,
return false;
}

bool AutofillAgent::InputElementLostFocus() {
return false;
}

void AutofillAgent::didAcceptAutofillSuggestion(const WebNode& node,
const WebString& value,
const WebString& label,
Expand Down
1 change: 1 addition & 0 deletions chrome/renderer/autofill/autofill_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class AutofillAgent : public content::RenderViewObserver,
virtual bool InputElementClicked(const WebKit::WebInputElement& element,
bool was_focused,
bool is_focused) OVERRIDE;
virtual bool InputElementLostFocus() OVERRIDE;

// WebKit::WebAutofillClient:
virtual void didAcceptAutofillSuggestion(const WebKit::WebNode& node,
Expand Down
4 changes: 4 additions & 0 deletions chrome/renderer/autofill/password_autofill_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ bool PasswordAutofillManager::InputElementClicked(
return false;
}

bool PasswordAutofillManager::InputElementLostFocus() {
return false;
}

void PasswordAutofillManager::OnFillPasswordForm(
const webkit_glue::PasswordFormFillData& form_data) {
FormElementsList forms;
Expand Down
3 changes: 2 additions & 1 deletion chrome/renderer/autofill/password_autofill_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class PasswordAutofillManager : public content::RenderViewObserver,
// PageClickListener:
virtual bool InputElementClicked(const WebKit::WebInputElement& element,
bool was_focused,
bool is_focused);
bool is_focused) OVERRIDE;
virtual bool InputElementLostFocus() OVERRIDE;

// RenderView IPC handlers:
void OnFillPasswordForm(const webkit_glue::PasswordFormFillData& form_data);
Expand Down
8 changes: 7 additions & 1 deletion chrome/renderer/page_click_listener.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand All @@ -23,6 +23,12 @@ class PageClickListener {
bool was_focused,
bool is_focused) = 0;

// If the previously focused element was an input field, listeners are
// informed that the text field has lost its focus.
// If this method returns true, the notification will not be propagated to
// other listeners.
virtual bool InputElementLostFocus() = 0;

protected:
virtual ~PageClickListener() {}
};
Expand Down
31 changes: 28 additions & 3 deletions chrome/renderer/page_click_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ const WebInputElement GetTextWebInputElement(const WebNode& node) {
return element.toConst<WebInputElement>();
}

// Checks to see if a text field was the previously selected node and is now
// losing its focus.
bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) {
WebKit::WebNode focused_node = newly_clicked_node.document().focusedNode();

if (focused_node.isNull() || GetTextWebInputElement(focused_node).isNull())
return false;

return focused_node != newly_clicked_node;
}

} // namespace

PageClickTracker::PageClickTracker(content::RenderView* render_view)
Expand Down Expand Up @@ -79,8 +90,6 @@ void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) {
if (listener->InputElementClicked(input_element, was_focused_, is_focused))
break;
}

last_node_clicked_.reset();
}

void PageClickTracker::AddListener(PageClickListener* listener) {
Expand Down Expand Up @@ -123,10 +132,26 @@ void PageClickTracker::handleEvent(const WebDOMEvent& event) {
// We'll get a notification once the mouse event has been processed
// (DidHandleMouseEvent), we'll notify the listener at that point.
WebNode node = mouse_event.target();

HandleTextFieldMaybeLosingFocus(node);

// We are only interested in text field clicks.
if (GetTextWebInputElement(node).isNull())
return;

last_node_clicked_ = node;
was_focused_ = (render_view()->GetFocusedNode() == last_node_clicked_);
was_focused_ = (node.document().focusedNode() == last_node_clicked_);
}

void PageClickTracker::HandleTextFieldMaybeLosingFocus(
const WebNode& newly_clicked_node) {
if (!DidSelectedTextFieldLoseFocus(newly_clicked_node))
return;

ObserverListBase<PageClickListener>::Iterator it(listeners_);
PageClickListener* listener;
while ((listener = it.GetNext()) != NULL) {
if (listener->InputElementLostFocus())
break;
}
}
7 changes: 6 additions & 1 deletion chrome/renderer/page_click_tracker.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand Down Expand Up @@ -50,6 +50,11 @@ class PageClickTracker : public content::RenderViewObserver,
// WebKit::WebDOMEventListener implementation.
virtual void handleEvent(const WebKit::WebDOMEvent& event);

// Checks to see if a text field is losing focus and inform listeners if
// it is.
void HandleTextFieldMaybeLosingFocus(
const WebKit::WebNode& newly_clicked_node);

// The last node that was clicked and had focus.
WebKit::WebNode last_node_clicked_;

Expand Down
Loading

0 comments on commit 65ec8db

Please sign in to comment.