OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/password_manager/password_generation_manager.h" | 5 #include "chrome/browser/password_manager/password_generation_manager.h" |
6 | 6 |
7 #include "chrome/browser/password_manager/password_manager.h" | 7 #include "chrome/browser/password_manager/password_manager.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/sync/profile_sync_service.h" | 9 #include "chrome/browser/sync/profile_sync_service.h" |
10 #include "chrome/browser/sync/profile_sync_service_factory.h" | 10 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 11 #include "chrome/browser/ui/autofill/password_generation_popup_controller_impl.h
" |
11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_finder.h" | 13 #include "chrome/browser/ui/browser_finder.h" |
13 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
14 #include "components/autofill/content/common/autofill_messages.h" | 15 #include "components/autofill/content/common/autofill_messages.h" |
15 #include "components/autofill/core/browser/autofill_field.h" | 16 #include "components/autofill/core/browser/autofill_field.h" |
16 #include "components/autofill/core/browser/field_types.h" | 17 #include "components/autofill/core/browser/field_types.h" |
17 #include "components/autofill/core/browser/form_structure.h" | 18 #include "components/autofill/core/browser/form_structure.h" |
18 #include "components/autofill/core/browser/password_generator.h" | 19 #include "components/autofill/core/browser/password_generator.h" |
19 #include "components/autofill/core/common/form_data.h" | 20 #include "components/autofill/core/common/form_data.h" |
20 #include "components/autofill/core/common/password_form.h" | 21 #include "components/autofill/core/common/password_form.h" |
21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
23 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/browser/web_contents_view.h" |
24 #include "ipc/ipc_message_macros.h" | 26 #include "ipc/ipc_message_macros.h" |
25 #include "ui/gfx/rect.h" | 27 #include "ui/gfx/rect.h" |
26 | 28 |
27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PasswordGenerationManager); | 29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PasswordGenerationManager); |
28 | 30 |
29 PasswordGenerationManager::PasswordGenerationManager( | 31 PasswordGenerationManager::PasswordGenerationManager( |
30 content::WebContents* contents) | 32 content::WebContents* contents) |
31 : content::WebContentsObserver(contents) {} | 33 : content::WebContentsObserver(contents), |
| 34 observer_(NULL) {} |
32 | 35 |
33 PasswordGenerationManager::~PasswordGenerationManager() {} | 36 PasswordGenerationManager::~PasswordGenerationManager() {} |
34 | 37 |
| 38 void PasswordGenerationManager::SetTestObserver( |
| 39 autofill::PasswordGenerationPopupObserver* observer) { |
| 40 observer_ = observer; |
| 41 } |
| 42 |
35 void PasswordGenerationManager::DetectAccountCreationForms( | 43 void PasswordGenerationManager::DetectAccountCreationForms( |
36 const std::vector<autofill::FormStructure*>& forms) { | 44 const std::vector<autofill::FormStructure*>& forms) { |
37 std::vector<autofill::FormData> account_creation_forms; | 45 std::vector<autofill::FormData> account_creation_forms; |
38 for (std::vector<autofill::FormStructure*>::const_iterator form_it = | 46 for (std::vector<autofill::FormStructure*>::const_iterator form_it = |
39 forms.begin(); form_it != forms.end(); ++form_it) { | 47 forms.begin(); form_it != forms.end(); ++form_it) { |
40 autofill::FormStructure* form = *form_it; | 48 autofill::FormStructure* form = *form_it; |
41 for (std::vector<autofill::AutofillField*>::const_iterator field_it = | 49 for (std::vector<autofill::AutofillField*>::const_iterator field_it = |
42 form->begin(); field_it != form->end(); ++field_it) { | 50 form->begin(); field_it != form->end(); ++field_it) { |
43 autofill::AutofillField* field = *field_it; | 51 autofill::AutofillField* field = *field_it; |
44 if (field->server_type() == autofill::ACCOUNT_CREATION_PASSWORD) { | 52 if (field->server_type() == autofill::ACCOUNT_CREATION_PASSWORD) { |
45 account_creation_forms.push_back(form->ToFormData()); | 53 account_creation_forms.push_back(form->ToFormData()); |
46 break; | 54 break; |
47 } | 55 } |
48 } | 56 } |
49 } | 57 } |
50 if (!account_creation_forms.empty() && IsGenerationEnabled()) { | 58 if (!account_creation_forms.empty() && IsGenerationEnabled()) { |
51 SendAccountCreationFormsToRenderer(web_contents()->GetRenderViewHost(), | 59 SendAccountCreationFormsToRenderer(web_contents()->GetRenderViewHost(), |
52 account_creation_forms); | 60 account_creation_forms); |
53 } | 61 } |
54 } | 62 } |
55 | 63 |
56 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) { | 64 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) { |
57 bool handled = true; | 65 bool handled = true; |
58 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message) | 66 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message) |
59 IPC_MESSAGE_HANDLER(AutofillHostMsg_ShowPasswordGenerationPopup, | 67 IPC_MESSAGE_HANDLER(AutofillHostMsg_ShowPasswordGenerationPopup, |
60 OnShowPasswordGenerationPopup) | 68 OnShowPasswordGenerationPopup) |
| 69 IPC_MESSAGE_HANDLER(AutofillHostMsg_ShowPasswordEditingPopup, |
| 70 OnShowPasswordEditingPopup) |
| 71 IPC_MESSAGE_HANDLER(AutofillHostMsg_HidePasswordGenerationPopup, |
| 72 OnHidePasswordGenerationPopup) |
61 IPC_MESSAGE_UNHANDLED(handled = false) | 73 IPC_MESSAGE_UNHANDLED(handled = false) |
62 IPC_END_MESSAGE_MAP() | 74 IPC_END_MESSAGE_MAP() |
63 | 75 |
64 return handled; | 76 return handled; |
65 } | 77 } |
66 | 78 |
67 // In order for password generation to be enabled, we need to make sure: | 79 // In order for password generation to be enabled, we need to make sure: |
68 // (1) Password sync is enabled, and | 80 // (1) Password sync is enabled, and |
69 // (2) Password saving is enabled. | 81 // (2) Password saving is enabled. |
70 bool PasswordGenerationManager::IsGenerationEnabled() const { | 82 bool PasswordGenerationManager::IsGenerationEnabled() const { |
(...skipping 24 matching lines...) Expand all Loading... |
95 return true; | 107 return true; |
96 } | 108 } |
97 | 109 |
98 void PasswordGenerationManager::SendAccountCreationFormsToRenderer( | 110 void PasswordGenerationManager::SendAccountCreationFormsToRenderer( |
99 content::RenderViewHost* host, | 111 content::RenderViewHost* host, |
100 const std::vector<autofill::FormData>& forms) { | 112 const std::vector<autofill::FormData>& forms) { |
101 host->Send(new AutofillMsg_AccountCreationFormsDetected( | 113 host->Send(new AutofillMsg_AccountCreationFormsDetected( |
102 host->GetRoutingID(), forms)); | 114 host->GetRoutingID(), forms)); |
103 } | 115 } |
104 | 116 |
| 117 gfx::RectF PasswordGenerationManager::GetBoundsInScreenSpace( |
| 118 const gfx::RectF& bounds) { |
| 119 gfx::Rect client_area; |
| 120 web_contents()->GetView()->GetContainerBounds(&client_area); |
| 121 return bounds + client_area.OffsetFromOrigin(); |
| 122 } |
| 123 |
105 void PasswordGenerationManager::OnShowPasswordGenerationPopup( | 124 void PasswordGenerationManager::OnShowPasswordGenerationPopup( |
106 const gfx::Rect& bounds, | 125 const gfx::RectF& bounds, |
107 int max_length, | 126 int max_length, |
108 const autofill::PasswordForm& form) { | 127 const autofill::PasswordForm& form) { |
109 #if defined(OS_ANDROID) | 128 // TODO(gcasto): Validate data in PasswordForm. |
110 NOTIMPLEMENTED(); | 129 |
111 #else | 130 // Only implemented for Aura right now. |
| 131 #if defined(USE_AURA) |
| 132 // Convert element_bounds to be in screen space. |
| 133 gfx::RectF element_bounds_in_screen_space = GetBoundsInScreenSpace(bounds); |
| 134 |
112 password_generator_.reset(new autofill::PasswordGenerator(max_length)); | 135 password_generator_.reset(new autofill::PasswordGenerator(max_length)); |
113 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 136 |
114 browser->window()->ShowPasswordGenerationBubble(bounds, | 137 popup_controller_ = |
115 form, | 138 autofill::PasswordGenerationPopupControllerImpl::GetOrCreate( |
116 password_generator_.get()); | 139 popup_controller_, |
117 #endif // #if defined(OS_ANDROID) | 140 element_bounds_in_screen_space, |
| 141 form, |
| 142 password_generator_.get(), |
| 143 PasswordManager::FromWebContents(web_contents()), |
| 144 observer_, |
| 145 web_contents(), |
| 146 web_contents()->GetView()->GetNativeView()); |
| 147 popup_controller_->Show(); |
| 148 #endif // #if defined(USE_AURA) |
118 } | 149 } |
| 150 |
| 151 void PasswordGenerationManager::OnShowPasswordEditingPopup( |
| 152 const gfx::RectF& bounds) { |
| 153 // TODO(gcasto): Enable this. |
| 154 } |
| 155 |
| 156 void PasswordGenerationManager::OnHidePasswordGenerationPopup() { |
| 157 HidePopup(); |
| 158 } |
| 159 |
| 160 void PasswordGenerationManager::HidePopup() { |
| 161 if (popup_controller_) |
| 162 popup_controller_->HideAndDestroy(); |
| 163 } |
OLD | NEW |