diff options
author | blundell@chromium.org <blundell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-02 23:04:05 +0000 |
---|---|---|
committer | blundell@chromium.org <blundell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-02 23:04:05 +0000 |
commit | 8580772c15a5c81eea0fb456a8738ff3277f9908 (patch) | |
tree | 717cde02ca0f916b8fdb7aa50886fb278b8ba39d /components | |
parent | f0e2a92949258398ada7e5d7ecf4dfe475125c70 (diff) | |
download | chromium_src-8580772c15a5c81eea0fb456a8738ff3277f9908.zip chromium_src-8580772c15a5c81eea0fb456a8738ff3277f9908.tar.gz chromium_src-8580772c15a5c81eea0fb456a8738ff3277f9908.tar.bz2 |
Move SendAutofillTypePredictions() into AutofillDriver.
This CL abstracts the implementation of sending autofill type predictions to
the renderer out of autofill core code and into the driver implementations.
BUG=247015
Review URL: https://chromiumcodereview.appspot.com/18563002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@209807 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
12 files changed, 127 insertions, 25 deletions
diff --git a/components/autofill/content/browser/autofill_driver_impl.cc b/components/autofill/content/browser/autofill_driver_impl.cc index 2cc9c25..64b3662 100644 --- a/components/autofill/content/browser/autofill_driver_impl.cc +++ b/components/autofill/content/browser/autofill_driver_impl.cc @@ -4,10 +4,13 @@ #include "components/autofill/content/browser/autofill_driver_impl.h" +#include "base/command_line.h" #include "components/autofill/core/browser/autofill_external_delegate.h" #include "components/autofill/core/browser/autofill_manager.h" #include "components/autofill/core/browser/autofill_manager_delegate.h" #include "components/autofill/core/common/autofill_messages.h" +#include "components/autofill/core/browser/form_structure.h" +#include "components/autofill/core/common/autofill_switches.h" #include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_details.h" #include "content/public/browser/notification_service.h" @@ -95,6 +98,23 @@ void AutofillDriverImpl::SendFormDataToRenderer(int query_id, new AutofillMsg_FormDataFilled(host->GetRoutingID(), query_id, data)); } +void AutofillDriverImpl::SendAutofillTypePredictionsToRenderer( + const std::vector<FormStructure*>& forms) { + if (!CommandLine::ForCurrentProcess()->HasSwitch( + switches::kShowAutofillTypePredictions)) + return; + + content::RenderViewHost* host = GetWebContents()->GetRenderViewHost(); + if (!host) + return; + + std::vector<FormDataPredictions> type_predictions; + FormStructure::GetFieldTypePredictions(forms, &type_predictions); + host->Send( + new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(), + type_predictions)); +} + bool AutofillDriverImpl::OnMessageReceived(const IPC::Message& message) { bool handled = true; IPC_BEGIN_MESSAGE_MAP(AutofillDriverImpl, message) diff --git a/components/autofill/content/browser/autofill_driver_impl.h b/components/autofill/content/browser/autofill_driver_impl.h index da931e5..0296198 100644 --- a/components/autofill/content/browser/autofill_driver_impl.h +++ b/components/autofill/content/browser/autofill_driver_impl.h @@ -49,6 +49,8 @@ class AutofillDriverImpl : public AutofillDriver, virtual bool RendererIsAvailable() OVERRIDE; virtual void SendFormDataToRenderer(int query_id, const FormData& data) OVERRIDE; + virtual void SendAutofillTypePredictionsToRenderer( + const std::vector<FormStructure*>& forms) OVERRIDE; AutofillExternalDelegate* autofill_external_delegate() { return autofill_external_delegate_.get(); diff --git a/components/autofill/content/browser/autofill_driver_impl_unittest.cc b/components/autofill/content/browser/autofill_driver_impl_unittest.cc index f8e3ed7..ae053fb 100644 --- a/components/autofill/content/browser/autofill_driver_impl_unittest.cc +++ b/components/autofill/content/browser/autofill_driver_impl_unittest.cc @@ -5,6 +5,7 @@ #include <algorithm> #include <vector> +#include "base/command_line.h" #include "base/memory/scoped_ptr.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h" #include "components/autofill/content/browser/autofill_driver_impl.h" @@ -13,6 +14,8 @@ #include "components/autofill/core/browser/autofill_manager.h" #include "components/autofill/core/browser/test_autofill_manager_delegate.h" #include "components/autofill/core/common/autofill_messages.h" +#include "components/autofill/core/common/autofill_switches.h" +#include "components/autofill/core/common/form_data_predictions.h" #include "content/public/browser/navigation_details.h" #include "content/public/browser/web_contents.h" #include "content/public/common/frame_navigate_params.h" @@ -100,6 +103,28 @@ class AutofillDriverImplTest : public ChromeRenderViewHostTestHarness { return true; } + // Searches for an |AutofillMsg_FieldTypePredictionsAvailable| message in the + // queue of sent IPC messages. If none is present, returns false. Otherwise, + // extracts the first |AutofillMsg_FieldTypePredictionsAvailable| message, + // fills the output parameter with the values of the message's parameter, and + // clears the queue of sent messages. + bool GetFieldTypePredictionsAvailable( + std::vector<FormDataPredictions>* predictions) { + const uint32 kMsgID = AutofillMsg_FieldTypePredictionsAvailable::ID; + const IPC::Message* message = + process()->sink().GetFirstMessageMatching(kMsgID); + if (!message) + return false; + Tuple1<std::vector<FormDataPredictions> > autofill_param; + AutofillMsg_FieldTypePredictionsAvailable::Read(message, &autofill_param); + if (predictions) + *predictions = autofill_param.a; + + process()->sink().ClearMessages(); + return true; + } + + scoped_ptr<TestAutofillManagerDelegate> test_manager_delegate_; scoped_ptr<TestAutofillDriverImpl> driver_; }; @@ -137,4 +162,30 @@ TEST_F(AutofillDriverImplTest, FormDataSentToRenderer) { EXPECT_EQ(input_form_data, output_form_data); } +TEST_F(AutofillDriverImplTest, TypePredictionsNotSentToRendererWhenDisabled) { + FormData form; + test::CreateTestAddressFormData(&form); + FormStructure form_structure(form, std::string()); + std::vector<FormStructure*> forms(1, &form_structure); + driver_->SendAutofillTypePredictionsToRenderer(forms); + EXPECT_FALSE(GetFieldTypePredictionsAvailable(NULL)); +} + +TEST_F(AutofillDriverImplTest, TypePredictionsSentToRendererWhenEnabled) { + CommandLine::ForCurrentProcess()->AppendSwitch( + switches::kShowAutofillTypePredictions); + + FormData form; + test::CreateTestAddressFormData(&form); + FormStructure form_structure(form, std::string()); + std::vector<FormStructure*> forms(1, &form_structure); + std::vector<FormDataPredictions> expected_type_predictions; + FormStructure::GetFieldTypePredictions(forms, &expected_type_predictions); + driver_->SendAutofillTypePredictionsToRenderer(forms); + + std::vector<FormDataPredictions> output_type_predictions; + EXPECT_TRUE(GetFieldTypePredictionsAvailable(&output_type_predictions)); + EXPECT_EQ(expected_type_predictions, output_type_predictions); +} + } // namespace autofill diff --git a/components/autofill/core/browser/autofill_driver.h b/components/autofill/core/browser/autofill_driver.h index 88ea7a8..14620ed 100644 --- a/components/autofill/core/browser/autofill_driver.h +++ b/components/autofill/core/browser/autofill_driver.h @@ -5,6 +5,8 @@ #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DRIVER_H_ #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DRIVER_H_ +#include <vector> + #include "components/autofill/core/common/form_data.h" namespace content { @@ -13,6 +15,8 @@ class WebContents; namespace autofill { +class FormStructure; + // Interface that allows Autofill core code to interact with its driver (i.e., // obtain information from it and give information to it). A concrete // implementation must be provided by the driver. @@ -31,6 +35,12 @@ class AutofillDriver { // original request for the data. This method is a no-op if the renderer is // not currently available. virtual void SendFormDataToRenderer(int query_id, const FormData& data) = 0; + + // Sends the field type predictions specified in |forms| to the renderer. This + // method is a no-op if the renderer is not available or the appropriate + // command-line flag is not set. + virtual void SendAutofillTypePredictionsToRenderer( + const std::vector<FormStructure*>& forms) = 0; }; } // namespace autofill diff --git a/components/autofill/core/browser/autofill_manager.cc b/components/autofill/core/browser/autofill_manager.cc index 7acb75f..7912bb8 100644 --- a/components/autofill/core/browser/autofill_manager.cc +++ b/components/autofill/core/browser/autofill_manager.cc @@ -776,7 +776,7 @@ void AutofillManager::OnLoadedServerPredictions( #endif // #if defined(TOOLKIT_VIEWS) // If the corresponding flag is set, annotate forms with the predicted types. - SendAutofillTypePredictions(form_structures_.get()); + driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get()); } void AutofillManager::OnDidEndTextFieldEditing() { @@ -803,23 +803,6 @@ bool AutofillManager::IsAutofillEnabled() const { return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled); } -void AutofillManager::SendAutofillTypePredictions( - const std::vector<FormStructure*>& forms) const { - if (!CommandLine::ForCurrentProcess()->HasSwitch( - switches::kShowAutofillTypePredictions)) - return; - - RenderViewHost* host = driver_->GetWebContents()->GetRenderViewHost(); - if (!host) - return; - - std::vector<FormDataPredictions> type_predictions; - FormStructure::GetFieldTypePredictions(forms, &type_predictions); - host->Send( - new AutofillMsg_FieldTypePredictionsAvailable(host->GetRoutingID(), - type_predictions)); -} - void AutofillManager::ImportFormData(const FormStructure& submitted_form) { const CreditCard* imported_credit_card; if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card)) @@ -1100,7 +1083,7 @@ bool AutofillManager::UpdateCachedForm(const FormData& live_form, // Annotate the updated form with its predicted types. std::vector<FormStructure*> forms(1, *updated_form); - SendAutofillTypePredictions(forms); + driver_->SendAutofillTypePredictionsToRenderer(forms); return true; } @@ -1189,7 +1172,7 @@ void AutofillManager::ParseForms(const std::vector<FormData>& forms) { // For the |non_queryable_forms|, we have all the field type info we're ever // going to get about them. For the other forms, we'll wait until we get a // response from the server. - SendAutofillTypePredictions(non_queryable_forms); + driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms); } int AutofillManager::GUIDToID(const GUIDPair& guid) const { diff --git a/components/autofill/core/browser/autofill_manager.h b/components/autofill/core/browser/autofill_manager.h index c46593f..a4f0194 100644 --- a/components/autofill/core/browser/autofill_manager.h +++ b/components/autofill/core/browser/autofill_manager.h @@ -318,11 +318,6 @@ class AutofillManager : public AutofillDownloadManager::Observer { void UpdateInitialInteractionTimestamp( const base::TimeTicks& interaction_timestamp); - // Send our current field type predictions to the renderer. This is a no-op if - // the appropriate command-line flag is not set. - void SendAutofillTypePredictions( - const std::vector<FormStructure*>& forms) const; - // Provides driver-level context to the shared code of the component. Must // outlive this object. AutofillDriver* driver_; diff --git a/components/autofill/core/browser/test_autofill_driver.cc b/components/autofill/core/browser/test_autofill_driver.cc index eabd42e..e3a3d70 100644 --- a/components/autofill/core/browser/test_autofill_driver.cc +++ b/components/autofill/core/browser/test_autofill_driver.cc @@ -23,4 +23,8 @@ void TestAutofillDriver::SendFormDataToRenderer(int query_id, const FormData& form_data) { } +void TestAutofillDriver::SendAutofillTypePredictionsToRenderer( + const std::vector<FormStructure*>& forms) { +} + } // namespace autofill diff --git a/components/autofill/core/browser/test_autofill_driver.h b/components/autofill/core/browser/test_autofill_driver.h index b451dd0..3bf20e6 100644 --- a/components/autofill/core/browser/test_autofill_driver.h +++ b/components/autofill/core/browser/test_autofill_driver.h @@ -27,6 +27,8 @@ class TestAutofillDriver : public AutofillDriver, virtual bool RendererIsAvailable() OVERRIDE; virtual void SendFormDataToRenderer(int query_id, const FormData& data) OVERRIDE; + virtual void SendAutofillTypePredictionsToRenderer( + const std::vector<FormStructure*>& forms) OVERRIDE; private: DISALLOW_COPY_AND_ASSIGN(TestAutofillDriver); diff --git a/components/autofill/core/common/form_data_predictions.cc b/components/autofill/core/common/form_data_predictions.cc index 15e30c0..8375c30 100644 --- a/components/autofill/core/common/form_data_predictions.cc +++ b/components/autofill/core/common/form_data_predictions.cc @@ -19,4 +19,17 @@ FormDataPredictions::FormDataPredictions(const FormDataPredictions& other) FormDataPredictions::~FormDataPredictions() { } +bool FormDataPredictions::operator==( + const FormDataPredictions& predictions) const { + return (data == predictions.data && + signature == predictions.signature && + experiment_id == predictions.experiment_id && + fields == predictions.fields); +} + +bool FormDataPredictions::operator!=( + const FormDataPredictions& predictions) const { + return !operator==(predictions); +} + } // namespace autofill diff --git a/components/autofill/core/common/form_data_predictions.h b/components/autofill/core/common/form_data_predictions.h index 6b6e90a..80b04be 100644 --- a/components/autofill/core/common/form_data_predictions.h +++ b/components/autofill/core/common/form_data_predictions.h @@ -27,6 +27,10 @@ struct FormDataPredictions { FormDataPredictions(); FormDataPredictions(const FormDataPredictions& other); ~FormDataPredictions(); + + // Added for the sake of testing. + bool operator==(const FormDataPredictions& predictions) const; + bool operator!=(const FormDataPredictions& predictions) const; }; } // namespace autofill diff --git a/components/autofill/core/common/form_field_data_predictions.cc b/components/autofill/core/common/form_field_data_predictions.cc index e5bdaf9..6e06559 100644 --- a/components/autofill/core/common/form_field_data_predictions.cc +++ b/components/autofill/core/common/form_field_data_predictions.cc @@ -21,4 +21,18 @@ FormFieldDataPredictions::FormFieldDataPredictions( FormFieldDataPredictions::~FormFieldDataPredictions() { } +bool FormFieldDataPredictions::operator==( + const FormFieldDataPredictions& predictions) const { + return (field == predictions.field && + signature == predictions.signature && + heuristic_type == predictions.heuristic_type && + server_type == predictions.server_type && + overall_type == predictions.overall_type); +} + +bool FormFieldDataPredictions::operator!=( + const FormFieldDataPredictions& predictions) const { + return !operator==(predictions); +} + } // namespace autofill diff --git a/components/autofill/core/common/form_field_data_predictions.h b/components/autofill/core/common/form_field_data_predictions.h index f830092..86f8bda 100644 --- a/components/autofill/core/common/form_field_data_predictions.h +++ b/components/autofill/core/common/form_field_data_predictions.h @@ -23,6 +23,10 @@ struct FormFieldDataPredictions { std::string heuristic_type; std::string server_type; std::string overall_type; + + // Added for the sake of testing. + bool operator==(const FormFieldDataPredictions& predictions) const; + bool operator!=(const FormFieldDataPredictions& predictions) const; }; } // namespace autofill |