| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/chrome_password_manager_client.h" | 5 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <tuple> |
| 10 | 11 |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/sync/profile_sync_service_factory.h" | 16 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 17 #include "chrome/browser/sync/profile_sync_test_util.h" | 17 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 18 #include "chrome/common/channel_info.h" | 18 #include "chrome/common/channel_info.h" |
| 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "components/autofill/content/public/interfaces/autofill_agent.mojom.h" | 21 #include "components/autofill/content/common/autofill_messages.h" |
| 22 #include "components/password_manager/content/browser/password_manager_internals
_service_factory.h" | 22 #include "components/password_manager/content/browser/password_manager_internals
_service_factory.h" |
| 23 #include "components/password_manager/core/browser/credentials_filter.h" | 23 #include "components/password_manager/core/browser/credentials_filter.h" |
| 24 #include "components/password_manager/core/browser/log_manager.h" | 24 #include "components/password_manager/core/browser/log_manager.h" |
| 25 #include "components/password_manager/core/browser/log_receiver.h" | 25 #include "components/password_manager/core/browser/log_receiver.h" |
| 26 #include "components/password_manager/core/browser/log_router.h" | 26 #include "components/password_manager/core/browser/log_router.h" |
| 27 #include "components/password_manager/core/browser/password_manager_internals_se
rvice.h" | 27 #include "components/password_manager/core/browser/password_manager_internals_se
rvice.h" |
| 28 #include "components/password_manager/core/browser/password_manager_test_utils.h
" | 28 #include "components/password_manager/core/browser/password_manager_test_utils.h
" |
| 29 #include "components/password_manager/core/common/credential_manager_types.h" | 29 #include "components/password_manager/core/common/credential_manager_types.h" |
| 30 #include "components/password_manager/core/common/password_manager_features.h" | 30 #include "components/password_manager/core/common/password_manager_features.h" |
| 31 #include "components/password_manager/core/common/password_manager_pref_names.h" | 31 #include "components/password_manager/core/common/password_manager_pref_names.h" |
| 32 #include "components/prefs/pref_registry_simple.h" | 32 #include "components/prefs/pref_registry_simple.h" |
| 33 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
| 34 #include "components/prefs/testing_pref_service.h" | 34 #include "components/prefs/testing_pref_service.h" |
| 35 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 35 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 36 #include "components/version_info/version_info.h" | 36 #include "components/version_info/version_info.h" |
| 37 #include "content/public/browser/browser_context.h" | 37 #include "content/public/browser/browser_context.h" |
| 38 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
| 39 #include "mojo/public/cpp/bindings/binding.h" | 39 #include "content/public/test/mock_render_process_host.h" |
| 40 #include "services/shell/public/cpp/interface_provider.h" | |
| 41 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
| 42 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 43 | 42 |
| 44 using content::BrowserContext; | 43 using content::BrowserContext; |
| 45 using content::WebContents; | 44 using content::WebContents; |
| 46 using testing::Return; | 45 using testing::Return; |
| 47 using testing::_; | 46 using testing::_; |
| 48 | 47 |
| 49 namespace { | 48 namespace { |
| 50 | 49 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 75 class DummyLogReceiver : public password_manager::LogReceiver { | 74 class DummyLogReceiver : public password_manager::LogReceiver { |
| 76 public: | 75 public: |
| 77 DummyLogReceiver() = default; | 76 DummyLogReceiver() = default; |
| 78 | 77 |
| 79 void LogSavePasswordProgress(const std::string& text) override {} | 78 void LogSavePasswordProgress(const std::string& text) override {} |
| 80 | 79 |
| 81 private: | 80 private: |
| 82 DISALLOW_COPY_AND_ASSIGN(DummyLogReceiver); | 81 DISALLOW_COPY_AND_ASSIGN(DummyLogReceiver); |
| 83 }; | 82 }; |
| 84 | 83 |
| 85 class FakePasswordAutofillAgent | |
| 86 : public autofill::mojom::PasswordAutofillAgent { | |
| 87 public: | |
| 88 FakePasswordAutofillAgent() | |
| 89 : called_set_logging_state_(false), | |
| 90 logging_state_active_(false), | |
| 91 binding_(this) {} | |
| 92 | |
| 93 ~FakePasswordAutofillAgent() override {} | |
| 94 | |
| 95 void BindRequest(mojo::ScopedMessagePipeHandle handle) { | |
| 96 binding_.Bind(mojo::MakeRequest<autofill::mojom::PasswordAutofillAgent>( | |
| 97 std::move(handle))); | |
| 98 } | |
| 99 | |
| 100 bool called_set_logging_state() { return called_set_logging_state_; } | |
| 101 | |
| 102 bool logging_state_active() { return logging_state_active_; } | |
| 103 | |
| 104 void reset_data() { | |
| 105 called_set_logging_state_ = false; | |
| 106 logging_state_active_ = false; | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 // autofill::mojom::PasswordAutofillAgent: | |
| 111 void FillPasswordForm( | |
| 112 int key, | |
| 113 const autofill::PasswordFormFillData& form_data) override {} | |
| 114 | |
| 115 void SetLoggingState(bool active) override { | |
| 116 called_set_logging_state_ = true; | |
| 117 logging_state_active_ = active; | |
| 118 } | |
| 119 | |
| 120 void AutofillUsernameAndPasswordDataReceived( | |
| 121 const autofill::FormsPredictionsMap& predictions) override {} | |
| 122 | |
| 123 void FindFocusedPasswordForm( | |
| 124 const FindFocusedPasswordFormCallback& callback) override {} | |
| 125 | |
| 126 // Records whether SetLoggingState() gets called. | |
| 127 bool called_set_logging_state_; | |
| 128 // Records data received via SetLoggingState() call. | |
| 129 bool logging_state_active_; | |
| 130 | |
| 131 mojo::Binding<autofill::mojom::PasswordAutofillAgent> binding_; | |
| 132 }; | |
| 133 | |
| 134 } // namespace | 84 } // namespace |
| 135 | 85 |
| 136 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { | 86 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { |
| 137 public: | 87 public: |
| 138 ChromePasswordManagerClientTest() : field_trial_list_(nullptr) {} | 88 ChromePasswordManagerClientTest() : field_trial_list_(nullptr) {} |
| 139 void SetUp() override; | 89 void SetUp() override; |
| 140 | 90 |
| 141 syncable_prefs::TestingPrefServiceSyncable* prefs() { | 91 syncable_prefs::TestingPrefServiceSyncable* prefs() { |
| 142 return profile()->GetTestingPrefService(); | 92 return profile()->GetTestingPrefService(); |
| 143 } | 93 } |
| 144 | 94 |
| 145 void EnforcePasswordManagerSettingsBehaviourChangeExperimentGroup( | 95 void EnforcePasswordManagerSettingsBehaviourChangeExperimentGroup( |
| 146 const char* name) { | 96 const char* name) { |
| 147 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 97 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 148 kPasswordManagerSettingsBehaviourChangeFieldTrialName, name)); | 98 kPasswordManagerSettingsBehaviourChangeFieldTrialName, name)); |
| 149 } | 99 } |
| 150 | 100 |
| 151 protected: | 101 protected: |
| 152 ChromePasswordManagerClient* GetClient(); | 102 ChromePasswordManagerClient* GetClient(); |
| 153 | 103 |
| 154 // If autofill::mojom::PasswordAutofillAgent::SetLoggingState() got called, | 104 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then |
| 155 // copies its argument into |activation_flag| and returns true. Otherwise | 105 // copies its argument into |activation_flag| and returns true. Otherwise |
| 156 // returns false. | 106 // returns false. |
| 157 bool WasLoggingActivationMessageSent(bool* activation_flag); | 107 bool WasLoggingActivationMessageSent(bool* activation_flag); |
| 158 | 108 |
| 159 FakePasswordAutofillAgent fake_agent_; | |
| 160 | |
| 161 TestingPrefServiceSimple prefs_; | 109 TestingPrefServiceSimple prefs_; |
| 162 base::FieldTrialList field_trial_list_; | 110 base::FieldTrialList field_trial_list_; |
| 163 }; | 111 }; |
| 164 | 112 |
| 165 void ChromePasswordManagerClientTest::SetUp() { | 113 void ChromePasswordManagerClientTest::SetUp() { |
| 166 ChromeRenderViewHostTestHarness::SetUp(); | 114 ChromeRenderViewHostTestHarness::SetUp(); |
| 167 | |
| 168 shell::InterfaceProvider* remote_interfaces = | |
| 169 web_contents()->GetMainFrame()->GetRemoteInterfaces(); | |
| 170 shell::InterfaceProvider::TestApi test_api(remote_interfaces); | |
| 171 test_api.SetBinderForName(autofill::mojom::PasswordAutofillAgent::Name_, | |
| 172 base::Bind(&FakePasswordAutofillAgent::BindRequest, | |
| 173 base::Unretained(&fake_agent_))); | |
| 174 | |
| 175 prefs_.registry()->RegisterBooleanPref( | 115 prefs_.registry()->RegisterBooleanPref( |
| 176 password_manager::prefs::kPasswordManagerSavingEnabled, true); | 116 password_manager::prefs::kPasswordManagerSavingEnabled, true); |
| 177 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( | 117 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( |
| 178 web_contents(), nullptr); | 118 web_contents(), nullptr); |
| 179 } | 119 } |
| 180 | 120 |
| 181 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { | 121 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { |
| 182 return ChromePasswordManagerClient::FromWebContents(web_contents()); | 122 return ChromePasswordManagerClient::FromWebContents(web_contents()); |
| 183 } | 123 } |
| 184 | 124 |
| 185 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( | 125 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( |
| 186 bool* activation_flag) { | 126 bool* activation_flag) { |
| 187 base::RunLoop().RunUntilIdle(); | 127 const uint32_t kMsgID = AutofillMsg_SetLoggingState::ID; |
| 188 if (!fake_agent_.called_set_logging_state()) | 128 const IPC::Message* message = |
| 129 process()->sink().GetFirstMessageMatching(kMsgID); |
| 130 if (!message) |
| 189 return false; | 131 return false; |
| 190 | 132 std::tuple<bool> param; |
| 191 if (activation_flag) | 133 AutofillMsg_SetLoggingState::Read(message, ¶m); |
| 192 *activation_flag = fake_agent_.logging_state_active(); | 134 *activation_flag = std::get<0>(param); |
| 193 fake_agent_.reset_data(); | 135 process()->sink().ClearMessages(); |
| 194 return true; | 136 return true; |
| 195 } | 137 } |
| 196 | 138 |
| 197 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { | 139 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { |
| 198 bool logging_active = true; | 140 bool logging_active = true; |
| 199 // Ensure the existence of a driver, which will send the IPCs we listen for | 141 // Ensure the existence of a driver, which will send the IPCs we listen for |
| 200 // below. | 142 // below. |
| 201 NavigateAndCommit(GURL("about:blank")); | 143 NavigateAndCommit(GURL("about:blank")); |
| 202 | 144 |
| 203 // Initially, the logging should be off, so no IPC messages. | 145 // Initially, the logging should be off, so no IPC messages. |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile()); | 348 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile()); |
| 407 DummyLogReceiver log_receiver; | 349 DummyLogReceiver log_receiver; |
| 408 EXPECT_EQ(std::string(), log_router->RegisterReceiver(&log_receiver)); | 350 EXPECT_EQ(std::string(), log_router->RegisterReceiver(&log_receiver)); |
| 409 | 351 |
| 410 // But then navigate to a WebUI, there the logging should not be active. | 352 // But then navigate to a WebUI, there the logging should not be active. |
| 411 NavigateAndCommit(GURL("about:password-manager-internals")); | 353 NavigateAndCommit(GURL("about:password-manager-internals")); |
| 412 EXPECT_FALSE(GetClient()->GetLogManager()->IsLoggingActive()); | 354 EXPECT_FALSE(GetClient()->GetLogManager()->IsLoggingActive()); |
| 413 | 355 |
| 414 log_router->UnregisterReceiver(&log_receiver); | 356 log_router->UnregisterReceiver(&log_receiver); |
| 415 } | 357 } |
| OLD | NEW |