OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_RENDERER_AUTOFILL_HELPER_H_ | 5 #ifndef CHROME_RENDERER_AUTOFILL_HELPER_H_ |
6 #define CHROME_RENDERER_AUTOFILL_HELPER_H_ | 6 #define CHROME_RENDERER_AUTOFILL_HELPER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/task.h" |
13 #include "chrome/renderer/form_manager.h" | 14 #include "chrome/renderer/form_manager.h" |
14 #include "chrome/renderer/page_click_listener.h" | 15 #include "chrome/renderer/page_click_listener.h" |
| 16 #include "chrome/renderer/render_view_observer.h" |
| 17 #include "third_party/WebKit/WebKit/chromium/public/WebAutoFillClient.h" |
15 #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" | 18 #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" |
16 | 19 |
17 class RenderView; | 20 class PasswordAutocompleteManager; |
18 | |
19 namespace WebKit { | |
20 class WebInputElement; | |
21 class WebKeyboardEvent; | |
22 class WebString; | |
23 } | |
24 | 21 |
25 // AutoFillHelper deals with AutoFill related communications between WebKit and | 22 // AutoFillHelper deals with AutoFill related communications between WebKit and |
26 // the browser. There is one AutofillHelper per RenderView. | 23 // the browser. There is one AutofillHelper per RenderView. |
27 // This code was originally part of RenderView. | 24 // This code was originally part of RenderView. |
28 // Note that AutoFill encompasses: | 25 // Note that AutoFill encompasses: |
29 // - single text field suggestions, that we usually refer to as Autocomplete | 26 // - single text field suggestions, that we usually refer to as Autocomplete |
30 // - entire form fill based on one field entry, referred to as form AutoFill. | 27 // - entire form fill based on one field entry, referred to as form AutoFill. |
31 | 28 |
32 class AutoFillHelper : public PageClickListener { | 29 class AutoFillHelper : public RenderViewObserver, |
| 30 public PageClickListener, |
| 31 public WebKit::WebAutoFillClient { |
33 public: | 32 public: |
34 explicit AutoFillHelper(RenderView* render_view); | 33 // PasswordAutocompleteManager is guaranteed to outlive AutoFillHelper. |
| 34 AutoFillHelper(RenderView* render_view, |
| 35 PasswordAutocompleteManager* password_autocomplete_manager); |
35 | 36 |
36 // Removes the Autocomplete suggestion |value| for the field named |name|. | 37 // WebKit::WebAutoFillClient implementation. Public for tests. |
37 void RemoveAutocompleteSuggestion(const WebKit::WebString& name, | 38 virtual void didAcceptAutoFillSuggestion(const WebKit::WebNode& node, |
38 const WebKit::WebString& value); | 39 const WebKit::WebString& value, |
39 | 40 const WebKit::WebString& label, |
40 // Called when we have received AutoFill suggestions from the browser. | 41 int unique_id, |
41 void SuggestionsReceived(int query_id, | 42 unsigned index); |
42 const std::vector<string16>& values, | 43 virtual void didSelectAutoFillSuggestion(const WebKit::WebNode& node, |
43 const std::vector<string16>& labels, | 44 const WebKit::WebString& value, |
44 const std::vector<string16>& icons, | 45 const WebKit::WebString& label, |
45 const std::vector<int>& unique_ids); | 46 int unique_id); |
46 | 47 virtual void didClearAutoFillSelection(const WebKit::WebNode& node); |
47 // Called when we have received suggestions for an entire form from the | 48 virtual void didAcceptAutocompleteSuggestion( |
48 // browser. | 49 const WebKit::WebInputElement& element); |
49 void FormDataFilled(int query_id, const webkit_glue::FormData& form); | 50 virtual void removeAutocompleteSuggestion(const WebKit::WebString& name, |
50 | 51 const WebKit::WebString& value); |
51 // Called by Webkit when the user has selected a suggestion in the popup (this | 52 virtual void textFieldDidEndEditing(const WebKit::WebInputElement& element); |
52 // happens when the user hovers over an suggestion or navigates the popup with | 53 virtual void textFieldDidChange(const WebKit::WebInputElement& element); |
53 // the arrow keys). | 54 virtual void textFieldDidReceiveKeyDown( |
54 void DidSelectAutoFillSuggestion(const WebKit::WebNode& node, | 55 const WebKit::WebInputElement& element, |
55 int unique_id); | 56 const WebKit::WebKeyboardEvent& event); |
56 | |
57 // Called by Webkit when the user has accepted a suggestion in the popup. | |
58 void DidAcceptAutoFillSuggestion(const WebKit::WebNode& node, | |
59 const WebKit::WebString& value, | |
60 int unique_id, | |
61 unsigned index); | |
62 | |
63 // Called by WebKit when the user has cleared the selection from the AutoFill | |
64 // suggestions popup. This happens when a user uses the arrow keys to | |
65 // navigate outside the range of possible selections, or when the popup | |
66 // closes. | |
67 void DidClearAutoFillSelection(const WebKit::WebNode& node); | |
68 | |
69 // Called when the frame contents are available. Extracts the forms from that | |
70 // frame and sends them to the browser for parsing. | |
71 void FrameContentsAvailable(WebKit::WebFrame* frame); | |
72 | |
73 // Called before a frame is closed. Gives us an opportunity to clean up. | |
74 // DEPRECATED. | |
75 void FrameWillClose(WebKit::WebFrame* frame); | |
76 | |
77 // Called when |frame| is detached from the view. Gives us an opportunity to | |
78 // clean up. | |
79 void FrameDetached(WebKit::WebFrame* frame); | |
80 | |
81 // WebViewClient editor call forwarded by the RenderView. | |
82 void TextDidChangeInTextField(const WebKit::WebInputElement& element); | |
83 | |
84 // WebViewClient editor call forwarded by the RenderView. For lower level | |
85 // event translation. Specifically, for down/up key presses in an input | |
86 // element. | |
87 void KeyDownInTextField(const WebKit::WebInputElement& element, | |
88 const WebKit::WebKeyboardEvent& event); | |
89 | 57 |
90 private: | 58 private: |
91 enum AutoFillAction { | 59 enum AutoFillAction { |
92 AUTOFILL_NONE, // No state set. | 60 AUTOFILL_NONE, // No state set. |
93 AUTOFILL_FILL, // Fill the AutoFill form data. | 61 AUTOFILL_FILL, // Fill the AutoFill form data. |
94 AUTOFILL_PREVIEW, // Preview the AutoFill form data. | 62 AUTOFILL_PREVIEW, // Preview the AutoFill form data. |
95 }; | 63 }; |
96 | 64 |
| 65 // RenderView::Observer implementation. |
| 66 virtual bool OnMessageReceived(const IPC::Message& message); |
| 67 virtual void DidFinishDocumentLoad(WebKit::WebFrame* frame); |
| 68 virtual void FrameDetached(WebKit::WebFrame* frame); |
| 69 virtual void FrameWillClose(WebKit::WebFrame* frame); |
| 70 virtual void FrameTranslated(WebKit::WebFrame* frame); |
| 71 |
97 // PageClickListener implementation: | 72 // PageClickListener implementation: |
98 virtual bool InputElementClicked(const WebKit::WebInputElement& element, | 73 virtual bool InputElementClicked(const WebKit::WebInputElement& element, |
99 bool was_focused, | 74 bool was_focused, |
100 bool is_focused); | 75 bool is_focused); |
101 | 76 |
| 77 void OnSuggestionsReturned(int query_id, |
| 78 const std::vector<string16>& values, |
| 79 const std::vector<string16>& labels, |
| 80 const std::vector<string16>& icons, |
| 81 const std::vector<int>& unique_ids); |
| 82 void OnFormDataFilled(int query_id, const webkit_glue::FormData& form); |
| 83 |
| 84 // Called in a posted task by textFieldDidChange() to work-around a WebKit bug |
| 85 // http://bugs.webkit.org/show_bug.cgi?id=16976 |
| 86 void TextFieldDidChangeImpl(const WebKit::WebInputElement& element); |
| 87 |
102 // Shows the autocomplete suggestions for |element|. | 88 // Shows the autocomplete suggestions for |element|. |
103 // This call is asynchronous and may or may not lead to the showing of a | 89 // This call is asynchronous and may or may not lead to the showing of a |
104 // suggestion popup (no popup is shown if there are no available suggestions). | 90 // suggestion popup (no popup is shown if there are no available suggestions). |
105 // |autofill_on_empty_values| specifies whether suggestions should be shown | 91 // |autofill_on_empty_values| specifies whether suggestions should be shown |
106 // when |element| contains no text. | 92 // when |element| contains no text. |
107 // |requires_caret_at_end| specifies whether suggestions should be shown when | 93 // |requires_caret_at_end| specifies whether suggestions should be shown when |
108 // the caret is not after the last character in |element|. | 94 // the caret is not after the last character in |element|. |
109 // |display_warning_if_disabled| specifies whether a warning should be | 95 // |display_warning_if_disabled| specifies whether a warning should be |
110 // displayed to the user if AutoFill has suggestions available, but cannot | 96 // displayed to the user if AutoFill has suggestions available, but cannot |
111 // fill them because it is disabled (e.g. when trying to fill a credit card | 97 // fill them because it is disabled (e.g. when trying to fill a credit card |
(...skipping 19 matching lines...) Expand all Loading... |
131 // Scans the given frame for forms and sends them up to the browser. | 117 // Scans the given frame for forms and sends them up to the browser. |
132 void SendForms(WebKit::WebFrame* frame); | 118 void SendForms(WebKit::WebFrame* frame); |
133 | 119 |
134 // Fills |form| and |field| with the FormData and FormField corresponding to | 120 // Fills |form| and |field| with the FormData and FormField corresponding to |
135 // |node|. Returns true if the data was found; and false otherwise. | 121 // |node|. Returns true if the data was found; and false otherwise. |
136 bool FindFormAndFieldForNode( | 122 bool FindFormAndFieldForNode( |
137 const WebKit::WebNode& node, | 123 const WebKit::WebNode& node, |
138 webkit_glue::FormData* form, | 124 webkit_glue::FormData* form, |
139 webkit_glue::FormField* field) WARN_UNUSED_RESULT; | 125 webkit_glue::FormField* field) WARN_UNUSED_RESULT; |
140 | 126 |
141 // Weak reference. | 127 FormManager form_manager_; |
142 RenderView* render_view_; | |
143 | 128 |
144 FormManager form_manager_; | 129 PasswordAutocompleteManager* password_autocomplete_manager_; |
145 | 130 |
146 // The ID of the last request sent for form field AutoFill. Used to ignore | 131 // The ID of the last request sent for form field AutoFill. Used to ignore |
147 // out of date responses. | 132 // out of date responses. |
148 int autofill_query_id_; | 133 int autofill_query_id_; |
149 | 134 |
150 // The node corresponding to the last request sent for form field AutoFill. | 135 // The node corresponding to the last request sent for form field AutoFill. |
151 WebKit::WebNode autofill_query_node_; | 136 WebKit::WebNode autofill_query_node_; |
152 | 137 |
153 // The action to take when receiving AutoFill data from the AutoFillManager. | 138 // The action to take when receiving AutoFill data from the AutoFillManager. |
154 AutoFillAction autofill_action_; | 139 AutoFillAction autofill_action_; |
155 | 140 |
156 // Should we display a warning if autofill is disabled? | 141 // Should we display a warning if autofill is disabled? |
157 bool display_warning_if_disabled_; | 142 bool display_warning_if_disabled_; |
158 | 143 |
159 // Was the query node autofilled prior to previewing the form? | 144 // Was the query node autofilled prior to previewing the form? |
160 bool was_query_node_autofilled_; | 145 bool was_query_node_autofilled_; |
161 | 146 |
162 // The menu index of the "Clear" menu item. | 147 // The menu index of the "Clear" menu item. |
163 int suggestions_clear_index_; | 148 int suggestions_clear_index_; |
164 | 149 |
165 // The menu index of the "AutoFill options..." menu item. | 150 // The menu index of the "AutoFill options..." menu item. |
166 int suggestions_options_index_; | 151 int suggestions_options_index_; |
167 | 152 |
| 153 ScopedRunnableMethodFactory<AutoFillHelper> method_factory_; |
| 154 |
168 DISALLOW_COPY_AND_ASSIGN(AutoFillHelper); | 155 DISALLOW_COPY_AND_ASSIGN(AutoFillHelper); |
169 }; | 156 }; |
170 | 157 |
171 #endif // CHROME_RENDERER_AUTOFILL_HELPER_H_ | 158 #endif // CHROME_RENDERER_AUTOFILL_HELPER_H_ |
OLD | NEW |