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/test/histogram_tester.h" | |
| 12 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 13 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
| 13 #include "chrome/browser/net/safe_search_util.h" | 14 #include "chrome/browser/net/safe_search_util.h" |
| 14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/test/base/testing_pref_service_syncable.h" | 16 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/content_settings/core/browser/cookie_settings.h" | 18 #include "components/content_settings/core/browser/cookie_settings.h" |
| 18 #include "components/content_settings/core/common/pref_names.h" | 19 #include "components/content_settings/core/common/pref_names.h" |
| 20 #include "components/data_use_measurement/core/data_use_user_data.h" | |
| 21 #include "content/public/browser/resource_request_info.h" | |
| 19 #include "content/public/common/content_switches.h" | 22 #include "content/public/common/content_switches.h" |
| 23 #include "content/public/common/resource_type.h" | |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "net/base/request_priority.h" | 25 #include "net/base/request_priority.h" |
| 22 #include "net/url_request/url_request.h" | 26 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 29 |
| 26 #if defined(ENABLE_EXTENSIONS) | 30 #if defined(ENABLE_EXTENSIONS) |
| 27 #include "chrome/browser/extensions/event_router_forwarder.h" | 31 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 28 #endif | 32 #endif |
| 29 | 33 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 // Loop over all combinations of the two policies. | 149 // Loop over all combinations of the two policies. |
| 146 for (int i = 0; i < 4; i++) { | 150 for (int i = 0; i < 4; i++) { |
| 147 bool google_safe_search = i % 2; | 151 bool google_safe_search = i % 2; |
| 148 bool youtube_safety_mode = i / 2; | 152 bool youtube_safety_mode = i / 2; |
| 149 SetSafeSearch(google_safe_search, youtube_safety_mode); | 153 SetSafeSearch(google_safe_search, youtube_safety_mode); |
| 150 | 154 |
| 151 QueryURL(google_safe_search, youtube_safety_mode); | 155 QueryURL(google_safe_search, youtube_safety_mode); |
| 152 } | 156 } |
| 153 } | 157 } |
| 154 | 158 |
| 159 // This class tests recoding data use of user and services in histograms. | |
| 160 class ChromeNetworkDelegateDataUseMeasurementTest : public testing::Test { | |
|
Alexei Svitkine (slow)
2015/09/01 20:04:04
A lot of this class is similar to ChromeNetworkDel
amohammadkhan
2015/09/01 23:02:43
You are right. But there are some difference here
| |
| 161 public: | |
| 162 ChromeNetworkDelegateDataUseMeasurementTest() | |
| 163 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | |
| 164 #if defined(ENABLE_EXTENSIONS) | |
| 165 forwarder_ = new extensions::EventRouterForwarder(); | |
| 166 #endif | |
| 167 } | |
| 168 | |
| 169 void SetUp() override { | |
| 170 ChromeNetworkDelegate::InitializePrefsOnUIThread( | |
| 171 &enable_referrers_, NULL, NULL, NULL, profile_.GetTestingPrefService()); | |
| 172 } | |
| 173 | |
| 174 protected: | |
| 175 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() { | |
| 176 scoped_ptr<ChromeNetworkDelegate> network_delegate( | |
| 177 new ChromeNetworkDelegate(forwarder(), &enable_referrers_)); | |
| 178 return network_delegate.Pass(); | |
| 179 } | |
| 180 | |
| 181 void SetDelegate(net::NetworkDelegate* delegate) { | |
| 182 network_delegate_ = delegate; | |
| 183 context_.set_network_delegate(network_delegate_); | |
| 184 } | |
| 185 | |
| 186 // This function queries a URLRequest. If |from_user| is true, it attaches a | |
| 187 // ResourceRequestInfo to the URLRequest, because requests from users have | |
| 188 // this info. If |from_user| is false, the request is presumed to be from a | |
| 189 // service, and the service name is set in the request's user data. | |
| 190 void QueryURLDataUse(bool from_user) { | |
| 191 scoped_ptr<net::URLRequest> request(context_.CreateRequest( | |
| 192 GURL("http://example.com"), net::DEFAULT_PRIORITY, &delegate_)); | |
| 193 if (from_user) { | |
| 194 content::ResourceRequestInfo::AllocateForTesting( | |
| 195 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, NULL, -2, -2, -2, | |
| 196 true, false, true, true); | |
| 197 } else { | |
| 198 request->SetUserData( | |
| 199 data_use_measurement::DataUseUserData::kUserDataKey, | |
| 200 new data_use_measurement::DataUseUserData( | |
| 201 data_use_measurement::DataUseUserData::SUGGESTIONS)); | |
| 202 } | |
| 203 request->Start(); | |
| 204 base::MessageLoop::current()->RunUntilIdle(); | |
| 205 } | |
| 206 | |
| 207 private: | |
| 208 extensions::EventRouterForwarder* forwarder() { | |
| 209 #if defined(ENABLE_EXTENSIONS) | |
| 210 return forwarder_.get(); | |
| 211 #else | |
| 212 return NULL; | |
| 213 #endif | |
| 214 } | |
| 215 | |
| 216 content::TestBrowserThreadBundle thread_bundle_; | |
| 217 #if defined(ENABLE_EXTENSIONS) | |
| 218 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | |
| 219 #endif | |
| 220 TestingProfile profile_; | |
| 221 BooleanPrefMember enable_referrers_; | |
| 222 scoped_ptr<net::URLRequest> request_; | |
| 223 net::TestURLRequestContext context_; | |
| 224 net::NetworkDelegate* network_delegate_; | |
| 225 net::TestDelegate delegate_; | |
| 226 }; | |
| 227 | |
| 228 // This function tests data use measurement for different types of requests by | |
| 229 // making two queries. First query is similar to a query from a service, so it | |
| 230 // should affect DataUse.NotUser.Dimensions and DataUse.MessageSize.ServiceName | |
| 231 // histograms. The second query is similar to a query from a user, so it should | |
| 232 // affect DataUse.User.Dimensions. AppState and ConnectionType dimensions are | |
| 233 // always Foreground and NotCellular respectively. | |
| 234 TEST_F(ChromeNetworkDelegateDataUseMeasurementTest, MainTest) { | |
| 235 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); | |
| 236 SetDelegate(delegate.get()); | |
| 237 base::HistogramTester histogram_tester; | |
| 238 | |
| 239 QueryURLDataUse(0); // A query from a service | |
| 240 histogram_tester.ExpectTotalCount( | |
| 241 "DataUse.NotUser.Downstream.Foreground.NotCellular", 1); | |
| 242 histogram_tester.ExpectTotalCount( | |
| 243 "DataUse.NotUser.Upstream.Foreground.NotCellular", 1); | |
| 244 // One upload and one download message, so totalCount shoudl be 2. | |
| 245 histogram_tester.ExpectTotalCount("DataUse.MessageSize.Suggestions", 2); | |
| 246 | |
| 247 QueryURLDataUse(1); // A query from user | |
| 248 histogram_tester.ExpectTotalCount( | |
| 249 "DataUse.User.Downstream.Foreground.NotCellular", 1); | |
| 250 histogram_tester.ExpectTotalCount( | |
| 251 "DataUse.User.Upstream.Foreground.NotCellular", 1); | |
| 252 } | |
| 253 | |
| 155 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) | 254 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) |
| 156 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { | 255 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { |
| 157 public: | 256 public: |
| 158 ChromeNetworkDelegatePrivacyModeTest() | 257 ChromeNetworkDelegatePrivacyModeTest() |
| 159 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 258 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 160 #if defined(ENABLE_EXTENSIONS) | 259 #if defined(ENABLE_EXTENSIONS) |
| 161 forwarder_(new extensions::EventRouterForwarder()), | 260 forwarder_(new extensions::EventRouterForwarder()), |
| 162 #endif | 261 #endif |
| 163 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), | 262 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), |
| 164 kBlockedSite("http://ads.thirdparty.com"), | 263 kBlockedSite("http://ads.thirdparty.com"), |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 kBlockedFirstPartySite)); | 360 kBlockedFirstPartySite)); |
| 262 | 361 |
| 263 cookie_settings_->SetCookieSetting( | 362 cookie_settings_->SetCookieSetting( |
| 264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), | 363 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), |
| 265 ContentSettingsPattern::Wildcard(), | 364 ContentSettingsPattern::Wildcard(), |
| 266 CONTENT_SETTING_BLOCK); | 365 CONTENT_SETTING_BLOCK); |
| 267 // Privacy mode is disabled as kAllowedSite is still getting cookies | 366 // Privacy mode is disabled as kAllowedSite is still getting cookies |
| 268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, | 367 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, |
| 269 kBlockedFirstPartySite)); | 368 kBlockedFirstPartySite)); |
| 270 } | 369 } |
| OLD | NEW |