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/content/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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 112 |
109 request->Start(); | 113 request->Start(); |
110 base::MessageLoop::current()->RunUntilIdle(); | 114 base::MessageLoop::current()->RunUntilIdle(); |
111 | 115 |
112 EXPECT_EQ(expect_google_safe_search ? 1 : 0, | 116 EXPECT_EQ(expect_google_safe_search ? 1 : 0, |
113 safe_search_util::GetForceGoogleSafeSearchCountForTesting()); | 117 safe_search_util::GetForceGoogleSafeSearchCountForTesting()); |
114 EXPECT_EQ(expect_youtube_safety_mode ? 1 : 0, | 118 EXPECT_EQ(expect_youtube_safety_mode ? 1 : 0, |
115 safe_search_util::GetForceYouTubeSafetyModeCountForTesting()); | 119 safe_search_util::GetForceYouTubeSafetyModeCountForTesting()); |
116 } | 120 } |
117 | 121 |
| 122 // This function queries a URLRequest. If |from_user| is true, so it attaches |
| 123 // a ResourceRequestInfo to the URLRequest, because requests from users have |
| 124 // this info. If |from_user| is false, the request assumed to be from one of |
| 125 // the services so it attaches a DataUseUserData. ServiceName is used as the |
| 126 // name of the service owning this request. |
| 127 void QueryURLDataUse(bool from_user) { |
| 128 scoped_ptr<net::URLRequest> request(context_.CreateRequest( |
| 129 GURL("http://example.com"), net::DEFAULT_PRIORITY, &delegate_)); |
| 130 // The render_process_id should be negative, all the other parameters are |
| 131 // arbitrary. |
| 132 if (from_user) { |
| 133 content::ResourceRequestInfo::AllocateForTesting( |
| 134 request.get(), |
| 135 content::RESOURCE_TYPE_MAIN_FRAME, |
| 136 NULL, |
| 137 -2, // render_process_id |
| 138 -2, |
| 139 -2, |
| 140 true, |
| 141 false, |
| 142 true, |
| 143 true); |
| 144 } else { |
| 145 request->SetUserData( |
| 146 measurement_component::DataUseUserData::kUserDataKey, |
| 147 new measurement_component::DataUseUserData("ServiceName")); |
| 148 } |
| 149 request->Start(); |
| 150 base::MessageLoop::current()->RunUntilIdle(); |
| 151 } |
| 152 |
118 private: | 153 private: |
119 extensions::EventRouterForwarder* forwarder() { | 154 extensions::EventRouterForwarder* forwarder() { |
120 #if defined(ENABLE_EXTENSIONS) | 155 #if defined(ENABLE_EXTENSIONS) |
121 return forwarder_.get(); | 156 return forwarder_.get(); |
122 #else | 157 #else |
123 return NULL; | 158 return NULL; |
124 #endif | 159 #endif |
125 } | 160 } |
126 | 161 |
127 content::TestBrowserThreadBundle thread_bundle_; | 162 content::TestBrowserThreadBundle thread_bundle_; |
(...skipping 17 matching lines...) Expand all Loading... |
145 // Loop over all combinations of the two policies. | 180 // Loop over all combinations of the two policies. |
146 for (int i = 0; i < 4; i++) { | 181 for (int i = 0; i < 4; i++) { |
147 bool google_safe_search = i % 2; | 182 bool google_safe_search = i % 2; |
148 bool youtube_safety_mode = i / 2; | 183 bool youtube_safety_mode = i / 2; |
149 SetSafeSearch(google_safe_search, youtube_safety_mode); | 184 SetSafeSearch(google_safe_search, youtube_safety_mode); |
150 | 185 |
151 QueryURL(google_safe_search, youtube_safety_mode); | 186 QueryURL(google_safe_search, youtube_safety_mode); |
152 } | 187 } |
153 } | 188 } |
154 | 189 |
| 190 // This function tests data use measurement for different types of requests by |
| 191 // making two queries. First query is similar to a query from a service, so it |
| 192 // should affect DataUse.NotUser.Dimensions and DataUse.service.ServiceName |
| 193 // histograms. The second query is similar to a query from a user, so it should |
| 194 // affect DataUse.User.Dimensions. AppState and ConnectionType dimensions are |
| 195 // always Foreground and NotCellular respectively. Because Background dimension |
| 196 // is only defined for android platform and when Chrome is in background and |
| 197 // Cellular ConnectionType is only exist on mobile devices. |
| 198 TEST_F(ChromeNetworkDelegateSafeSearchTest, DataUseMeasurement) { |
| 199 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); |
| 200 SetDelegate(delegate.get()); |
| 201 |
| 202 base::HistogramTester histogram_tester; |
| 203 |
| 204 QueryURLDataUse(0); // A query from a service |
| 205 histogram_tester.ExpectTotalCount( |
| 206 "DataUse.NotUser.Download.Foreground.NotCellular", 1); |
| 207 histogram_tester.ExpectTotalCount( |
| 208 "DataUse.NotUser.Upload.Foreground.NotCellular", 1); |
| 209 // One upload and one download message, so totalCount shoudl be 2. |
| 210 histogram_tester.ExpectTotalCount("DataUse.Service.ServiceName", 2); |
| 211 // The upload nad download messages are empty, so their size should be 0. |
| 212 histogram_tester.ExpectUniqueSample("DataUse.Service.ServiceName", 0, 2); |
| 213 |
| 214 QueryURLDataUse(1); // A query from user |
| 215 histogram_tester.ExpectTotalCount( |
| 216 "DataUse.User.Download.Foreground.NotCellular", 1); |
| 217 histogram_tester.ExpectTotalCount( |
| 218 "DataUse.User.Upload.Foreground.NotCellular", 1); |
| 219 } |
| 220 |
155 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) | 221 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) |
156 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { | 222 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { |
157 public: | 223 public: |
158 ChromeNetworkDelegatePrivacyModeTest() | 224 ChromeNetworkDelegatePrivacyModeTest() |
159 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 225 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
160 #if defined(ENABLE_EXTENSIONS) | 226 #if defined(ENABLE_EXTENSIONS) |
161 forwarder_(new extensions::EventRouterForwarder()), | 227 forwarder_(new extensions::EventRouterForwarder()), |
162 #endif | 228 #endif |
163 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), | 229 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), |
164 kBlockedSite("http://ads.thirdparty.com"), | 230 kBlockedSite("http://ads.thirdparty.com"), |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 kBlockedFirstPartySite)); | 327 kBlockedFirstPartySite)); |
262 | 328 |
263 cookie_settings_->SetCookieSetting( | 329 cookie_settings_->SetCookieSetting( |
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), | 330 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), |
265 ContentSettingsPattern::Wildcard(), | 331 ContentSettingsPattern::Wildcard(), |
266 CONTENT_SETTING_BLOCK); | 332 CONTENT_SETTING_BLOCK); |
267 // Privacy mode is disabled as kAllowedSite is still getting cookies | 333 // Privacy mode is disabled as kAllowedSite is still getting cookies |
268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, | 334 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, |
269 kBlockedFirstPartySite)); | 335 kBlockedFirstPartySite)); |
270 } | 336 } |
OLD | NEW |