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 "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/common/autofill_messages.h" | 21 #include "components/autofill/content/public/interfaces/autofill_agent.mojom.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 "content/public/test/mock_render_process_host.h" | 39 #include "mojo/public/cpp/bindings/binding.h" |
| 40 #include "services/shell/public/cpp/interface_provider.h" |
40 #include "testing/gmock/include/gmock/gmock.h" | 41 #include "testing/gmock/include/gmock/gmock.h" |
41 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
42 | 43 |
43 using content::BrowserContext; | 44 using content::BrowserContext; |
44 using content::WebContents; | 45 using content::WebContents; |
45 using testing::Return; | 46 using testing::Return; |
46 using testing::_; | 47 using testing::_; |
47 | 48 |
48 namespace { | 49 namespace { |
49 | 50 |
(...skipping 24 matching lines...) Expand all Loading... |
74 class DummyLogReceiver : public password_manager::LogReceiver { | 75 class DummyLogReceiver : public password_manager::LogReceiver { |
75 public: | 76 public: |
76 DummyLogReceiver() = default; | 77 DummyLogReceiver() = default; |
77 | 78 |
78 void LogSavePasswordProgress(const std::string& text) override {} | 79 void LogSavePasswordProgress(const std::string& text) override {} |
79 | 80 |
80 private: | 81 private: |
81 DISALLOW_COPY_AND_ASSIGN(DummyLogReceiver); | 82 DISALLOW_COPY_AND_ASSIGN(DummyLogReceiver); |
82 }; | 83 }; |
83 | 84 |
| 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 |
84 } // namespace | 134 } // namespace |
85 | 135 |
86 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { | 136 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { |
87 public: | 137 public: |
88 ChromePasswordManagerClientTest() : field_trial_list_(nullptr) {} | 138 ChromePasswordManagerClientTest() : field_trial_list_(nullptr) {} |
89 void SetUp() override; | 139 void SetUp() override; |
90 | 140 |
91 syncable_prefs::TestingPrefServiceSyncable* prefs() { | 141 syncable_prefs::TestingPrefServiceSyncable* prefs() { |
92 return profile()->GetTestingPrefService(); | 142 return profile()->GetTestingPrefService(); |
93 } | 143 } |
94 | 144 |
95 void EnforcePasswordManagerSettingsBehaviourChangeExperimentGroup( | 145 void EnforcePasswordManagerSettingsBehaviourChangeExperimentGroup( |
96 const char* name) { | 146 const char* name) { |
97 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 147 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
98 kPasswordManagerSettingsBehaviourChangeFieldTrialName, name)); | 148 kPasswordManagerSettingsBehaviourChangeFieldTrialName, name)); |
99 } | 149 } |
100 | 150 |
101 protected: | 151 protected: |
102 ChromePasswordManagerClient* GetClient(); | 152 ChromePasswordManagerClient* GetClient(); |
103 | 153 |
104 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then | 154 // If autofill::mojom::PasswordAutofillAgent::SetLoggingState() got called, |
105 // copies its argument into |activation_flag| and returns true. Otherwise | 155 // copies its argument into |activation_flag| and returns true. Otherwise |
106 // returns false. | 156 // returns false. |
107 bool WasLoggingActivationMessageSent(bool* activation_flag); | 157 bool WasLoggingActivationMessageSent(bool* activation_flag); |
108 | 158 |
| 159 FakePasswordAutofillAgent fake_agent_; |
| 160 |
109 TestingPrefServiceSimple prefs_; | 161 TestingPrefServiceSimple prefs_; |
110 base::FieldTrialList field_trial_list_; | 162 base::FieldTrialList field_trial_list_; |
111 }; | 163 }; |
112 | 164 |
113 void ChromePasswordManagerClientTest::SetUp() { | 165 void ChromePasswordManagerClientTest::SetUp() { |
114 ChromeRenderViewHostTestHarness::SetUp(); | 166 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 |
115 prefs_.registry()->RegisterBooleanPref( | 175 prefs_.registry()->RegisterBooleanPref( |
116 password_manager::prefs::kPasswordManagerSavingEnabled, true); | 176 password_manager::prefs::kPasswordManagerSavingEnabled, true); |
117 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( | 177 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( |
118 web_contents(), nullptr); | 178 web_contents(), nullptr); |
119 } | 179 } |
120 | 180 |
121 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { | 181 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { |
122 return ChromePasswordManagerClient::FromWebContents(web_contents()); | 182 return ChromePasswordManagerClient::FromWebContents(web_contents()); |
123 } | 183 } |
124 | 184 |
125 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( | 185 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( |
126 bool* activation_flag) { | 186 bool* activation_flag) { |
127 const uint32_t kMsgID = AutofillMsg_SetLoggingState::ID; | 187 base::RunLoop().RunUntilIdle(); |
128 const IPC::Message* message = | 188 if (!fake_agent_.called_set_logging_state()) |
129 process()->sink().GetFirstMessageMatching(kMsgID); | |
130 if (!message) | |
131 return false; | 189 return false; |
132 std::tuple<bool> param; | 190 |
133 AutofillMsg_SetLoggingState::Read(message, ¶m); | 191 if (activation_flag) |
134 *activation_flag = std::get<0>(param); | 192 *activation_flag = fake_agent_.logging_state_active(); |
135 process()->sink().ClearMessages(); | 193 fake_agent_.reset_data(); |
136 return true; | 194 return true; |
137 } | 195 } |
138 | 196 |
139 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { | 197 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { |
140 bool logging_active = true; | 198 bool logging_active = true; |
141 // Ensure the existence of a driver, which will send the IPCs we listen for | 199 // Ensure the existence of a driver, which will send the IPCs we listen for |
142 // below. | 200 // below. |
143 NavigateAndCommit(GURL("about:blank")); | 201 NavigateAndCommit(GURL("about:blank")); |
144 | 202 |
145 // Initially, the logging should be off, so no IPC messages. | 203 // Initially, the logging should be off, so no IPC messages. |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile()); | 406 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile()); |
349 DummyLogReceiver log_receiver; | 407 DummyLogReceiver log_receiver; |
350 EXPECT_EQ(std::string(), log_router->RegisterReceiver(&log_receiver)); | 408 EXPECT_EQ(std::string(), log_router->RegisterReceiver(&log_receiver)); |
351 | 409 |
352 // But then navigate to a WebUI, there the logging should not be active. | 410 // But then navigate to a WebUI, there the logging should not be active. |
353 NavigateAndCommit(GURL("about:password-manager-internals")); | 411 NavigateAndCommit(GURL("about:password-manager-internals")); |
354 EXPECT_FALSE(GetClient()->GetLogManager()->IsLoggingActive()); | 412 EXPECT_FALSE(GetClient()->GetLogManager()->IsLoggingActive()); |
355 | 413 |
356 log_router->UnregisterReceiver(&log_receiver); | 414 log_router->UnregisterReceiver(&log_receiver); |
357 } | 415 } |
OLD | NEW |