Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/net/chrome_network_delegate.h" | 5 #include "chrome/browser/net/chrome_network_delegate.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/prefs/pref_member.h" | 11 #include "base/prefs/pref_member.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 15 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
| 16 #include "chrome/browser/net/safe_search_util.h" | 16 #include "chrome/browser/net/safe_search_util.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/test/base/testing_browser_process.h" | 18 #include "chrome/test/base/testing_browser_process.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 20 #include "chrome/test/base/testing_profile_manager.h" | 20 #include "chrome/test/base/testing_profile_manager.h" |
| 21 #include "components/content_settings/core/browser/cookie_settings.h" | 21 #include "components/content_settings/core/browser/cookie_settings.h" |
| 22 #include "components/content_settings/core/common/pref_names.h" | 22 #include "components/content_settings/core/common/pref_names.h" |
| 23 #include "components/data_usage/core/data_use_aggregator.h" | |
| 23 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 24 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 24 #include "content/public/browser/resource_request_info.h" | 25 #include "content/public/browser/resource_request_info.h" |
| 25 #include "content/public/common/content_switches.h" | 26 #include "content/public/common/content_switches.h" |
| 26 #include "content/public/common/resource_type.h" | 27 #include "content/public/common/resource_type.h" |
| 27 #include "content/public/test/test_browser_thread_bundle.h" | 28 #include "content/public/test/test_browser_thread_bundle.h" |
| 28 #include "net/base/request_priority.h" | 29 #include "net/base/request_priority.h" |
| 29 #include "net/socket/socket_test_util.h" | 30 #include "net/socket/socket_test_util.h" |
| 30 #include "net/url_request/url_request.h" | 31 #include "net/url_request/url_request.h" |
| 31 #include "net/url_request/url_request_test_util.h" | 32 #include "net/url_request/url_request_test_util.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 } else { | 83 } else { |
| 83 request->SetUserData( | 84 request->SetUserData( |
| 84 data_use_measurement::DataUseUserData::kUserDataKey, | 85 data_use_measurement::DataUseUserData::kUserDataKey, |
| 85 new data_use_measurement::DataUseUserData( | 86 new data_use_measurement::DataUseUserData( |
| 86 data_use_measurement::DataUseUserData::SUGGESTIONS)); | 87 data_use_measurement::DataUseUserData::SUGGESTIONS)); |
| 87 } | 88 } |
| 88 request->Start(); | 89 request->Start(); |
| 89 base::RunLoop().RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
| 90 } | 91 } |
| 91 | 92 |
| 93 // A fake DataUseAggregator for testing that only counts how many times its | |
| 94 // respective methods have been called. | |
| 95 class FakeDataUseAggregator : public data_usage::DataUseAggregator { | |
| 96 public: | |
| 97 FakeDataUseAggregator() | |
| 98 : num_report_data_use_calls_(0), | |
| 99 num_report_off_the_record_data_use_calls_(0) {} | |
| 100 | |
| 101 ~FakeDataUseAggregator() override {} | |
| 102 | |
| 103 void ReportDataUse(const net::URLRequest& request, | |
| 104 int tab_id, | |
| 105 int64_t tx_bytes, | |
| 106 int64_t rx_bytes) override { | |
| 107 ++num_report_data_use_calls_; | |
|
bengr
2015/10/06 19:30:28
Is this sufficient for testing? You could add the
sclittle
2015/10/07 01:07:55
Changed to keep track of the bytes. The main thing
| |
| 108 } | |
| 109 | |
| 110 void ReportOffTheRecordDataUse(int64_t tx_bytes, int64_t rx_bytes) override { | |
| 111 ++num_report_off_the_record_data_use_calls_; | |
|
bengr
2015/10/06 19:30:28
Here too.
sclittle
2015/10/07 01:07:55
Changed this to keep track of the bytes too.
| |
| 112 } | |
| 113 | |
| 114 int num_report_data_use_calls() const { return num_report_data_use_calls_; } | |
| 115 | |
| 116 int num_report_off_the_record_data_use_calls() const { | |
| 117 return num_report_off_the_record_data_use_calls_; | |
| 118 } | |
| 119 | |
| 120 private: | |
| 121 int num_report_data_use_calls_; | |
| 122 int num_report_off_the_record_data_use_calls_; | |
| 123 }; | |
| 124 | |
| 92 } // namespace | 125 } // namespace |
| 93 | 126 |
| 94 class ChromeNetworkDelegateTest : public testing::Test { | 127 class ChromeNetworkDelegateTest : public testing::Test { |
| 95 public: | 128 public: |
| 96 ChromeNetworkDelegateTest() | 129 ChromeNetworkDelegateTest() |
| 97 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 130 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 98 context_(new net::TestURLRequestContext(true)) { | 131 context_(new net::TestURLRequestContext(true)) { |
| 99 #if defined(ENABLE_EXTENSIONS) | 132 #if defined(ENABLE_EXTENSIONS) |
| 100 forwarder_ = new extensions::EventRouterForwarder(); | 133 forwarder_ = new extensions::EventRouterForwarder(); |
| 101 #endif | 134 #endif |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 115 new ChromeNetworkDelegate(forwarder(), &enable_referrers_)); | 148 new ChromeNetworkDelegate(forwarder(), &enable_referrers_)); |
| 116 context_->set_client_socket_factory(&socket_factory_); | 149 context_->set_client_socket_factory(&socket_factory_); |
| 117 context_->set_network_delegate(network_delegate_.get()); | 150 context_->set_network_delegate(network_delegate_.get()); |
| 118 context_->Init(); | 151 context_->Init(); |
| 119 } | 152 } |
| 120 | 153 |
| 121 net::TestURLRequestContext* context() { return context_.get(); } | 154 net::TestURLRequestContext* context() { return context_.get(); } |
| 122 net::NetworkDelegate* network_delegate() { return network_delegate_.get(); } | 155 net::NetworkDelegate* network_delegate() { return network_delegate_.get(); } |
| 123 net::MockClientSocketFactory* socket_factory() { return &socket_factory_; } | 156 net::MockClientSocketFactory* socket_factory() { return &socket_factory_; } |
| 124 | 157 |
| 158 ChromeNetworkDelegate* chrome_network_delegate() { | |
| 159 return network_delegate_.get(); | |
| 160 } | |
| 161 | |
| 125 extensions::EventRouterForwarder* forwarder() { | 162 extensions::EventRouterForwarder* forwarder() { |
| 126 #if defined(ENABLE_EXTENSIONS) | 163 #if defined(ENABLE_EXTENSIONS) |
| 127 return forwarder_.get(); | 164 return forwarder_.get(); |
| 128 #else | 165 #else |
| 129 return nullptr; | 166 return nullptr; |
| 130 #endif | 167 #endif |
| 131 } | 168 } |
| 132 | 169 |
| 133 private: | 170 private: |
| 134 scoped_ptr<TestingProfileManager> profile_manager_; | 171 scoped_ptr<TestingProfileManager> profile_manager_; |
| 135 content::TestBrowserThreadBundle thread_bundle_; | 172 content::TestBrowserThreadBundle thread_bundle_; |
| 136 #if defined(ENABLE_EXTENSIONS) | 173 #if defined(ENABLE_EXTENSIONS) |
| 137 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | 174 scoped_refptr<extensions::EventRouterForwarder> forwarder_; |
| 138 #endif | 175 #endif |
| 139 TestingProfile profile_; | 176 TestingProfile profile_; |
| 140 BooleanPrefMember enable_referrers_; | 177 BooleanPrefMember enable_referrers_; |
| 141 scoped_ptr<net::NetworkDelegate> network_delegate_; | 178 scoped_ptr<ChromeNetworkDelegate> network_delegate_; |
| 142 net::MockClientSocketFactory socket_factory_; | 179 net::MockClientSocketFactory socket_factory_; |
| 143 scoped_ptr<net::TestURLRequestContext> context_; | 180 scoped_ptr<net::TestURLRequestContext> context_; |
| 144 }; | 181 }; |
| 145 | 182 |
| 146 // This function tests data use measurement for requests by services. it makes a | 183 // This function tests data use measurement for requests by services. it makes a |
| 147 // query which is similar to a query of a service, so it should affect | 184 // query which is similar to a query of a service, so it should affect |
| 148 // DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName | 185 // DataUse.TrafficSize.System.Dimensions and DataUse.MessageSize.ServiceName |
| 149 // histograms. AppState and ConnectionType dimensions are always Foreground and | 186 // histograms. AppState and ConnectionType dimensions are always Foreground and |
| 150 // NotCellular respectively. | 187 // NotCellular respectively. |
| 151 #if !defined(OS_IOS) | 188 #if !defined(OS_IOS) |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); | 277 EXPECT_FALSE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); |
| 241 } | 278 } |
| 242 | 279 |
| 243 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { | 280 TEST_F(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { |
| 244 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 281 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 245 switches::kEnableExperimentalWebPlatformFeatures); | 282 switches::kEnableExperimentalWebPlatformFeatures); |
| 246 Initialize(); | 283 Initialize(); |
| 247 EXPECT_TRUE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); | 284 EXPECT_TRUE(network_delegate()->FirstPartyOnlyCookieExperimentEnabled()); |
| 248 } | 285 } |
| 249 | 286 |
| 287 TEST_F(ChromeNetworkDelegateTest, ReportDataUseToAggregator) { | |
| 288 FakeDataUseAggregator fake_aggregator; | |
| 289 Initialize(); | |
| 290 | |
| 291 chrome_network_delegate()->set_data_use_aggregator( | |
| 292 &fake_aggregator, false /* is_data_usage_off_the_record */); | |
| 293 RequestURL(context(), socket_factory(), true, false); | |
| 294 EXPECT_GT(fake_aggregator.num_report_data_use_calls(), 0); | |
| 295 EXPECT_EQ(0, fake_aggregator.num_report_off_the_record_data_use_calls()); | |
| 296 } | |
| 297 | |
| 298 TEST_F(ChromeNetworkDelegateTest, ReportOffTheRecordDataUseToAggregator) { | |
| 299 FakeDataUseAggregator fake_aggregator; | |
| 300 Initialize(); | |
| 301 | |
| 302 chrome_network_delegate()->set_data_use_aggregator( | |
| 303 &fake_aggregator, true /* is_data_usage_off_the_record */); | |
| 304 RequestURL(context(), socket_factory(), true, false); | |
| 305 EXPECT_EQ(0, fake_aggregator.num_report_data_use_calls()); | |
| 306 EXPECT_GT(fake_aggregator.num_report_off_the_record_data_use_calls(), 0); | |
| 307 } | |
| 308 | |
| 250 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { | 309 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { |
| 251 public: | 310 public: |
| 252 ChromeNetworkDelegateSafeSearchTest() | 311 ChromeNetworkDelegateSafeSearchTest() |
| 253 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 312 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 254 #if defined(ENABLE_EXTENSIONS) | 313 #if defined(ENABLE_EXTENSIONS) |
| 255 forwarder_ = new extensions::EventRouterForwarder(); | 314 forwarder_ = new extensions::EventRouterForwarder(); |
| 256 #endif | 315 #endif |
| 257 } | 316 } |
| 258 | 317 |
| 259 void SetUp() override { | 318 void SetUp() override { |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 451 kBlockedFirstPartySite)); | 510 kBlockedFirstPartySite)); |
| 452 | 511 |
| 453 cookie_settings_->SetCookieSetting( | 512 cookie_settings_->SetCookieSetting( |
| 454 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), | 513 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), |
| 455 ContentSettingsPattern::Wildcard(), | 514 ContentSettingsPattern::Wildcard(), |
| 456 CONTENT_SETTING_BLOCK); | 515 CONTENT_SETTING_BLOCK); |
| 457 // Privacy mode is disabled as kAllowedSite is still getting cookies | 516 // Privacy mode is disabled as kAllowedSite is still getting cookies |
| 458 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, | 517 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, |
| 459 kBlockedFirstPartySite)); | 518 kBlockedFirstPartySite)); |
| 460 } | 519 } |
| OLD | NEW |