diff options
5 files changed, 51 insertions, 18 deletions
diff --git a/components/password_manager/content/browser/content_credential_manager_dispatcher.cc b/components/password_manager/content/browser/content_credential_manager_dispatcher.cc index aae4f34..186a1e9a 100644 --- a/components/password_manager/content/browser/content_credential_manager_dispatcher.cc +++ b/components/password_manager/content/browser/content_credential_manager_dispatcher.cc @@ -26,14 +26,8 @@ ContentCredentialManagerDispatcher::ContentCredentialManagerDispatcher( PasswordManagerClient* client) : WebContentsObserver(web_contents), client_(client), - driver_(nullptr), pending_request_id_(0) { DCHECK(web_contents); - - ContentPasswordManagerDriverFactory* driver_factory = - ContentPasswordManagerDriverFactory::FromWebContents(web_contents); - if (driver_factory) - driver_ = driver_factory->GetDriverForFrame(web_contents->GetMainFrame()); } ContentCredentialManagerDispatcher::~ContentCredentialManagerDispatcher() {} @@ -76,7 +70,8 @@ void ContentCredentialManagerDispatcher::OnNotifySignedIn( // determine whether or not the credential exists, and calling UpdateLogin // accordingly. form_manager_.reset( - new CredentialManagerPasswordFormManager(client_, driver_, *form, this)); + new CredentialManagerPasswordFormManager(client_, GetDriver(), *form, + this)); web_contents()->GetRenderViewHost()->Send( new CredentialManagerMsg_AcknowledgeSignedIn( @@ -142,6 +137,16 @@ PasswordStore* ContentCredentialManagerDispatcher::GetPasswordStore() { return client_ ? client_->GetPasswordStore() : nullptr; } +base::WeakPtr<PasswordManagerDriver> +ContentCredentialManagerDispatcher::GetDriver() { + ContentPasswordManagerDriverFactory* driver_factory = + ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); + DCHECK(driver_factory); + PasswordManagerDriver* driver = + driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); + return driver->AsWeakPtr(); +} + void ContentCredentialManagerDispatcher::SendCredential( int request_id, const CredentialInfo& info) { DCHECK(pending_request_id_); diff --git a/components/password_manager/content/browser/content_credential_manager_dispatcher.h b/components/password_manager/content/browser/content_credential_manager_dispatcher.h index fc8cac8..5ed9df0 100644 --- a/components/password_manager/content/browser/content_credential_manager_dispatcher.h +++ b/components/password_manager/content/browser/content_credential_manager_dispatcher.h @@ -7,6 +7,7 @@ #include "base/callback.h" #include "base/macros.h" +#include "base/memory/weak_ptr.h" #include "components/password_manager/core/browser/credential_manager_dispatcher.h" #include "components/password_manager/core/browser/password_store_consumer.h" #include "content/public/browser/web_contents_observer.h" @@ -56,21 +57,19 @@ class ContentCredentialManagerDispatcher : public CredentialManagerDispatcher, void OnGetPasswordStoreResults( const std::vector<autofill::PasswordForm*>& results) override; - // For testing only. - void set_password_manager_driver(PasswordManagerDriver* driver) { - driver_ = driver; - } - using CredentialCallback = base::Callback<void(const autofill::PasswordForm&)>; private: PasswordStore* GetPasswordStore(); + // Returns the driver for the current main frame. + // Virtual for testing. + virtual base::WeakPtr<PasswordManagerDriver> GetDriver(); + void SendCredential(int request_id, const CredentialInfo& info); PasswordManagerClient* client_; - PasswordManagerDriver* driver_; scoped_ptr<CredentialManagerPasswordFormManager> form_manager_; // When 'OnRequestCredential' is called, it in turn calls out to the diff --git a/components/password_manager/content/browser/content_credential_manager_dispatcher_unittest.cc b/components/password_manager/content/browser/content_credential_manager_dispatcher_unittest.cc index f5950aa..1800c32 100644 --- a/components/password_manager/content/browser/content_credential_manager_dispatcher_unittest.cc +++ b/components/password_manager/content/browser/content_credential_manager_dispatcher_unittest.cc @@ -77,6 +77,33 @@ class TestPasswordManagerClient scoped_ptr<password_manager::PasswordFormManager> manager_; }; +class TestContentCredentialManagerDispatcher + : public password_manager::ContentCredentialManagerDispatcher { + public: + TestContentCredentialManagerDispatcher( + content::WebContents* web_contents, + password_manager::PasswordManagerClient* client, + password_manager::PasswordManagerDriver* driver); + + private: + base::WeakPtr<password_manager::PasswordManagerDriver> GetDriver() override; + + base::WeakPtr<password_manager::PasswordManagerDriver> driver_; +}; + +TestContentCredentialManagerDispatcher::TestContentCredentialManagerDispatcher( + content::WebContents* web_contents, + password_manager::PasswordManagerClient* client, + password_manager::PasswordManagerDriver* driver) + : ContentCredentialManagerDispatcher(web_contents, client), + driver_(driver->AsWeakPtr()) { +} + +base::WeakPtr<password_manager::PasswordManagerDriver> +TestContentCredentialManagerDispatcher::GetDriver() { + return driver_; +} + void RunAllPendingTasks() { base::RunLoop run_loop; base::MessageLoop::current()->PostTask( @@ -98,8 +125,9 @@ class ContentCredentialManagerDispatcherTest store_ = new TestPasswordStore; client_.reset(new TestPasswordManagerClient(store_.get())); dispatcher_.reset( - new ContentCredentialManagerDispatcher(web_contents(), client_.get())); - dispatcher_->set_password_manager_driver(&stub_driver_); + new TestContentCredentialManagerDispatcher(web_contents(), + client_.get(), + &stub_driver_)); NavigateAndCommit(GURL("https://example.com/test.html")); diff --git a/components/password_manager/content/browser/credential_manager_password_form_manager.cc b/components/password_manager/content/browser/credential_manager_password_form_manager.cc index 76e4533..9cd4925 100644 --- a/components/password_manager/content/browser/credential_manager_password_form_manager.cc +++ b/components/password_manager/content/browser/credential_manager_password_form_manager.cc @@ -15,12 +15,12 @@ namespace password_manager { CredentialManagerPasswordFormManager::CredentialManagerPasswordFormManager( PasswordManagerClient* client, - PasswordManagerDriver* driver, + base::WeakPtr<PasswordManagerDriver> driver, const PasswordForm& observed_form, ContentCredentialManagerDispatcher* dispatcher) : PasswordFormManager(driver->GetPasswordManager(), client, - driver->AsWeakPtr(), + driver, observed_form, true), dispatcher_(dispatcher) { diff --git a/components/password_manager/content/browser/credential_manager_password_form_manager.h b/components/password_manager/content/browser/credential_manager_password_form_manager.h index 3a17f85..ff8f6c8 100644 --- a/components/password_manager/content/browser/credential_manager_password_form_manager.h +++ b/components/password_manager/content/browser/credential_manager_password_form_manager.h @@ -5,6 +5,7 @@ #ifndef COMPONENTS_PASSWORD_MANAGER_CONTENT_BROWSER_CREDENTIAL_MANAGER_PASSWORD_FORM_MANAGER_H_ #define COMPONENTS_PASSWORD_MANAGER_CONTENT_BROWSER_CREDENTIAL_MANAGER_PASSWORD_FORM_MANAGER_H_ +#include "base/memory/weak_ptr.h" #include "components/password_manager/core/browser/password_form_manager.h" namespace autofill { @@ -29,7 +30,7 @@ class CredentialManagerPasswordFormManager : public PasswordFormManager { // This class does not take ownership of |dispatcher|. CredentialManagerPasswordFormManager( PasswordManagerClient* client, - PasswordManagerDriver* driver, + base::WeakPtr<PasswordManagerDriver> driver, const autofill::PasswordForm& observed_form, ContentCredentialManagerDispatcher* dispatcher); ~CredentialManagerPasswordFormManager() override; |