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 3 // found in the LICENSE file. 4 5 #include "components/autofill/content/browser/request_autocomplete_manager.h" 6 7 #include "components/autofill/content/browser/autofill_driver_impl.h" 8 #include "components/autofill/content/common/autofill_messages.h" 9 #include "components/autofill/core/browser/form_structure.h" 10 #include "components/autofill/core/common/form_data.h" 11 #include "content/public/browser/render_view_host.h" 12 #include "content/public/browser/web_contents.h" 13 #include "url/gurl.h" 14 15 namespace autofill { 16 17 RequestAutocompleteManager::RequestAutocompleteManager( 18 AutofillDriverImpl* autofill_driver) 19 : autofill_driver_(autofill_driver), 20 weak_ptr_factory_(this) { 21 DCHECK(autofill_driver_); 22 } 23 24 RequestAutocompleteManager::~RequestAutocompleteManager() {} 25 26 void RequestAutocompleteManager::OnRequestAutocomplete( 27 const FormData& form, 28 const GURL& frame_url) { 29 if (!autofill_driver_->autofill_manager()->IsAutofillEnabled()) { 30 ReturnAutocompleteResult( 31 blink::WebFormElement::AutocompleteResultErrorDisabled, 32 FormData()); 33 return; 34 } 35 36 base::Callback<void(const FormStructure*)> callback = 37 base::Bind(&RequestAutocompleteManager::ReturnAutocompleteData, 38 weak_ptr_factory_.GetWeakPtr()); 39 ShowRequestAutocompleteDialog(form, frame_url, callback); 40 } 41 42 void RequestAutocompleteManager::ReturnAutocompleteResult( 43 blink::WebFormElement::AutocompleteResult result, 44 const FormData& form_data) { 45 // autofill_driver_->GetWebContents() will be NULL when the interactive 46 // autocomplete is closed due to a tab or browser window closing. 47 if (!autofill_driver_->GetWebContents()) 48 return; 49 50 content::RenderViewHost* host = 51 autofill_driver_->GetWebContents()->GetRenderViewHost(); 52 if (!host) 53 return; 54 55 host->Send(new AutofillMsg_RequestAutocompleteResult(host->GetRoutingID(), 56 result, 57 form_data)); 58 } 59 60 void RequestAutocompleteManager::ReturnAutocompleteData( 61 const FormStructure* result) { 62 if (!result) { 63 ReturnAutocompleteResult( 64 blink::WebFormElement::AutocompleteResultErrorCancel, FormData()); 65 } else { 66 ReturnAutocompleteResult(blink::WebFormElement::AutocompleteResultSuccess, 67 result->ToFormData()); 68 } 69 } 70 71 void RequestAutocompleteManager::ShowRequestAutocompleteDialog( 72 const FormData& form, 73 const GURL& source_url, 74 const base::Callback<void(const FormStructure*)>& callback) { 75 AutofillManagerDelegate* delegate = 76 autofill_driver_->autofill_manager()->delegate(); 77 delegate->ShowRequestAutocompleteDialog(form, source_url, callback); 78 } 79 80 } // namespace autofill 81