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