1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
8 #include "base/stl_util.h"
9 #include "components/autofill/content/browser/content_autofill_driver.h"
10 #include "components/autofill/core/browser/autofill_client.h"
11 #include "components/autofill/core/browser/autofill_manager.h"
12 #include "components/autofill/core/browser/form_structure.h"
13 #include "components/autofill/core/common/autofill_switches.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "ipc/ipc_message_macros.h"
20 const char ContentAutofillDriverFactory::
21 kContentAutofillDriverFactoryWebContentsUserDataKey
[] =
22 "web_contents_autofill_driver_factory";
25 void ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
26 content::WebContents
* contents
,
27 AutofillClient
* client
,
28 const std::string
& app_locale
,
29 AutofillManager::AutofillDownloadManagerState enable_download_manager
) {
30 if (FromWebContents(contents
))
33 contents
->SetUserData(
34 kContentAutofillDriverFactoryWebContentsUserDataKey
,
35 new ContentAutofillDriverFactory(contents
, client
, app_locale
,
36 enable_download_manager
));
40 ContentAutofillDriverFactory
* ContentAutofillDriverFactory::FromWebContents(
41 content::WebContents
* contents
) {
42 return static_cast<ContentAutofillDriverFactory
*>(contents
->GetUserData(
43 kContentAutofillDriverFactoryWebContentsUserDataKey
));
46 ContentAutofillDriverFactory::ContentAutofillDriverFactory(
47 content::WebContents
* web_contents
,
48 AutofillClient
* client
,
49 const std::string
& app_locale
,
50 AutofillManager::AutofillDownloadManagerState enable_download_manager
)
51 : content::WebContentsObserver(web_contents
),
53 app_locale_(app_locale
),
54 enable_download_manager_(enable_download_manager
) {
55 web_contents
->ForEachFrame(
56 base::Bind(&ContentAutofillDriverFactory::CreateDriverForFrame
,
57 base::Unretained(this)));
60 ContentAutofillDriverFactory::~ContentAutofillDriverFactory() {
61 STLDeleteContainerPairSecondPointers(frame_driver_map_
.begin(),
62 frame_driver_map_
.end());
63 frame_driver_map_
.clear();
66 ContentAutofillDriver
* ContentAutofillDriverFactory::DriverForFrame(
67 content::RenderFrameHost
* render_frame_host
) {
68 auto mapping
= frame_driver_map_
.find(render_frame_host
);
69 return mapping
== frame_driver_map_
.end() ? nullptr : mapping
->second
;
72 bool ContentAutofillDriverFactory::OnMessageReceived(
73 const IPC::Message
& message
,
74 content::RenderFrameHost
* render_frame_host
) {
75 return frame_driver_map_
[render_frame_host
]->HandleMessage(message
);
78 void ContentAutofillDriverFactory::RenderFrameCreated(
79 content::RenderFrameHost
* render_frame_host
) {
80 // RenderFrameCreated is called more than once for the main frame.
81 if (!frame_driver_map_
[render_frame_host
])
82 CreateDriverForFrame(render_frame_host
);
85 void ContentAutofillDriverFactory::RenderFrameDeleted(
86 content::RenderFrameHost
* render_frame_host
) {
87 delete frame_driver_map_
[render_frame_host
];
88 frame_driver_map_
.erase(render_frame_host
);
91 void ContentAutofillDriverFactory::DidNavigateAnyFrame(
92 content::RenderFrameHost
* rfh
,
93 const content::LoadCommittedDetails
& details
,
94 const content::FrameNavigateParams
& params
) {
95 frame_driver_map_
[rfh
]->DidNavigateFrame(details
, params
);
98 void ContentAutofillDriverFactory::NavigationEntryCommitted(
99 const content::LoadCommittedDetails
& load_details
) {
100 client_
->HideAutofillPopup();
103 void ContentAutofillDriverFactory::WasHidden() {
104 client_
->HideAutofillPopup();
107 void ContentAutofillDriverFactory::CreateDriverForFrame(
108 content::RenderFrameHost
* render_frame_host
) {
109 DCHECK(!frame_driver_map_
[render_frame_host
]);
110 frame_driver_map_
[render_frame_host
] = new ContentAutofillDriver(
111 render_frame_host
, client_
, app_locale_
, enable_download_manager_
);
114 } // namespace autofill