OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/renderer/autofill/password_generation_manager.h" | 5 #include "chrome/renderer/autofill/password_generation_manager.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/common/autofill_messages.h" | 8 #include "chrome/common/autofill_messages.h" |
| 9 #include "content/public/renderer/render_view.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | |
16 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
17 | 20 |
18 namespace autofill { | 21 namespace autofill { |
19 | 22 |
20 PasswordGenerationManager::PasswordGenerationManager( | 23 PasswordGenerationManager::PasswordGenerationManager( |
21 content::RenderView* render_view) | 24 content::RenderView* render_view) |
22 : content::RenderViewObserver(render_view), | 25 : content::RenderViewObserver(render_view), |
23 enabled_(false) {} | 26 enabled_(false) { |
| 27 render_view->GetWebView()->addTextFieldDecoratorClient(this); |
| 28 } |
24 PasswordGenerationManager::~PasswordGenerationManager() {} | 29 PasswordGenerationManager::~PasswordGenerationManager() {} |
25 | 30 |
26 void PasswordGenerationManager::DidFinishDocumentLoad(WebKit::WebFrame* frame) { | 31 void PasswordGenerationManager::DidFinishDocumentLoad(WebKit::WebFrame* frame) { |
27 // We don't want to generate passwords if the browser won't store or sync | 32 // We don't want to generate passwords if the browser won't store or sync |
28 // them. | 33 // them. |
29 if (!enabled_) | 34 if (!enabled_) |
30 return; | 35 return; |
31 | 36 |
32 if (!ShouldAnalyzeFrame(*frame)) | 37 if (!ShouldAnalyzeDocument(frame->document())) |
33 return; | 38 return; |
34 | 39 |
35 WebKit::WebVector<WebKit::WebFormElement> forms; | 40 WebKit::WebVector<WebKit::WebFormElement> forms; |
36 frame->document().forms(forms); | 41 frame->document().forms(forms); |
37 for (size_t i = 0; i < forms.size(); ++i) { | 42 for (size_t i = 0; i < forms.size(); ++i) { |
38 const WebKit::WebFormElement& web_form = forms[i]; | 43 const WebKit::WebFormElement& web_form = forms[i]; |
39 if (!web_form.autoComplete()) | 44 if (web_form.isNull() || !web_form.autoComplete()) |
40 continue; | 45 continue; |
41 | 46 |
42 // Grab all of the passwords for each form. | 47 // Grab all of the passwords for each form. |
43 WebKit::WebVector<WebKit::WebFormControlElement> control_elements; | 48 WebKit::WebVector<WebKit::WebFormControlElement> control_elements; |
44 web_form.getFormControlElements(control_elements); | 49 web_form.getFormControlElements(control_elements); |
45 | 50 |
46 std::vector<WebKit::WebInputElement> passwords; | 51 std::vector<WebKit::WebInputElement> passwords; |
47 for (size_t i = 0; i < control_elements.size(); i++) { | 52 for (size_t i = 0; i < control_elements.size(); i++) { |
48 WebKit::WebInputElement* input_element = | 53 WebKit::WebInputElement* input_element = |
49 toWebInputElement(&control_elements[i]); | 54 toWebInputElement(&control_elements[i]); |
50 if (input_element && input_element->isPasswordField()) | 55 if (input_element && input_element->isPasswordField()) |
51 passwords.push_back(*input_element); | 56 passwords.push_back(*input_element); |
52 } | 57 } |
| 58 |
53 // For now, just assume that if there are two password fields in the | 59 // For now, just assume that if there are two password fields in the |
54 // form that this is meant for account creation. | 60 // form that this is meant for account creation. Also, we assume that there |
| 61 // is only one account creation field per URL. |
55 // TODO(gcasto): Determine better heauristics for this. | 62 // TODO(gcasto): Determine better heauristics for this. |
56 if (passwords.size() == 2) { | 63 if (passwords.size() == 2) { |
57 account_creation_elements_ = make_pair(passwords[0], passwords); | 64 passwords_ = passwords; |
58 break; | 65 // Make the decoration visible for this element. |
| 66 passwords[0].decorationElementFor(this).setAttribute("style", |
| 67 "display:block"); |
| 68 return; |
59 } | 69 } |
60 } | 70 } |
61 } | 71 } |
62 | 72 |
63 bool PasswordGenerationManager::ShouldAnalyzeFrame( | 73 bool PasswordGenerationManager::ShouldAnalyzeDocument( |
64 const WebKit::WebFrame& frame) const { | 74 const WebKit::WebDocument& document) const { |
65 // Make sure that this security origin is allowed to use password manager. | 75 // Make sure that this security origin is allowed to use password manager. |
66 // Generating a password that can't be saved is a bad idea. | 76 // Generating a password that can't be saved is a bad idea. |
67 WebKit::WebSecurityOrigin origin = frame.document().securityOrigin(); | 77 WebKit::WebSecurityOrigin origin = document.securityOrigin(); |
68 if (!origin.canAccessPasswordManager()) { | 78 if (!origin.canAccessPasswordManager()) { |
69 DVLOG(1) << "No PasswordManager access"; | 79 DVLOG(1) << "No PasswordManager access"; |
70 return false; | 80 return false; |
71 } | 81 } |
72 | 82 |
73 return true; | 83 return true; |
74 } | 84 } |
75 | 85 |
76 void PasswordGenerationManager::FocusedNodeChanged( | 86 bool PasswordGenerationManager::shouldAddDecorationTo( |
77 const WebKit::WebNode& node) { | 87 const WebKit::WebInputElement& element) { |
78 WebKit::WebInputElement input_element = | 88 return element.isPasswordField(); |
79 node.toConst<WebKit::WebInputElement>(); | 89 } |
80 if (!input_element.isNull() && | |
81 account_creation_elements_.first == input_element) { | |
82 gfx::Rect rect(input_element.boundsInViewportSpace()); | |
83 webkit::forms::PasswordForm* password_form( | |
84 webkit::forms::PasswordFormDomManager::CreatePasswordForm( | |
85 input_element.form())); | |
86 | 90 |
87 if (password_form) { | 91 bool PasswordGenerationManager::visibleByDefault() { |
88 Send(new AutofillHostMsg_ShowPasswordGenerationPopup(routing_id(), | 92 return false; |
89 rect, | 93 } |
90 *password_form)); | 94 |
91 } | 95 WebKit::WebCString PasswordGenerationManager::imageNameForNormalState() { |
| 96 return WebKit::WebCString("generatePassword"); |
| 97 } |
| 98 |
| 99 WebKit::WebCString PasswordGenerationManager::imageNameForDisabledState() { |
| 100 return imageNameForNormalState(); |
| 101 } |
| 102 |
| 103 WebKit::WebCString PasswordGenerationManager::imageNameForReadOnlyState() { |
| 104 return imageNameForNormalState(); |
| 105 } |
| 106 |
| 107 void PasswordGenerationManager::handleClick(WebKit::WebInputElement& element) { |
| 108 gfx::Rect rect(element.decorationElementFor(this).boundsInViewportSpace()); |
| 109 webkit::forms::PasswordForm* password_form( |
| 110 webkit::forms::PasswordFormDomManager::CreatePasswordForm( |
| 111 element.form())); |
| 112 if (password_form) { |
| 113 Send(new AutofillHostMsg_ShowPasswordGenerationPopup(routing_id(), |
| 114 rect, |
| 115 *password_form)); |
92 } | 116 } |
93 } | 117 } |
94 | 118 |
| 119 void PasswordGenerationManager::willDetach( |
| 120 const WebKit::WebInputElement& element) { |
| 121 // No implementation |
| 122 } |
| 123 |
95 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) { | 124 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) { |
96 bool handled = true; | 125 bool handled = true; |
97 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message) | 126 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message) |
98 IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted, | 127 IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted, |
99 OnPasswordAccepted) | 128 OnPasswordAccepted) |
100 IPC_MESSAGE_HANDLER(AutofillMsg_PasswordGenerationEnabled, | 129 IPC_MESSAGE_HANDLER(AutofillMsg_PasswordGenerationEnabled, |
101 OnPasswordGenerationEnabled) | 130 OnPasswordGenerationEnabled) |
102 IPC_MESSAGE_UNHANDLED(handled = false) | 131 IPC_MESSAGE_UNHANDLED(handled = false) |
103 IPC_END_MESSAGE_MAP() | 132 IPC_END_MESSAGE_MAP() |
104 return handled; | 133 return handled; |
105 } | 134 } |
106 | 135 |
107 void PasswordGenerationManager::OnPasswordAccepted(const string16& password) { | 136 void PasswordGenerationManager::OnPasswordAccepted(const string16& password) { |
108 for (std::vector<WebKit::WebInputElement>::iterator it = | 137 for (std::vector<WebKit::WebInputElement>::iterator it = passwords_.begin(); |
109 account_creation_elements_.second.begin(); | 138 it != passwords_.end(); ++it) { |
110 it != account_creation_elements_.second.end(); ++it) { | |
111 it->setValue(password); | 139 it->setValue(password); |
112 it->setAutofilled(true); | 140 it->setAutofilled(true); |
113 } | 141 } |
114 } | 142 } |
115 | 143 |
116 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) { | 144 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) { |
117 enabled_ = enabled; | 145 enabled_ = enabled; |
118 } | 146 } |
119 | 147 |
120 } // namespace autofill | 148 } // namespace autofill |
OLD | NEW |