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" |
| 14 #include "chrome/browser/net/data_use_user_data.h" |
13 #include "chrome/browser/net/safe_search_util.h" | 15 #include "chrome/browser/net/safe_search_util.h" |
14 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/testing_pref_service_syncable.h" | 17 #include "chrome/test/base/testing_pref_service_syncable.h" |
16 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
17 #include "components/content_settings/core/browser/cookie_settings.h" | 19 #include "components/content_settings/core/browser/cookie_settings.h" |
18 #include "components/content_settings/core/common/pref_names.h" | 20 #include "components/content_settings/core/common/pref_names.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(DataUseUserData::kUserDataKey, |
| 146 new DataUseUserData("ServiceName")); |
| 147 } |
| 148 request->Start(); |
| 149 base::MessageLoop::current()->RunUntilIdle(); |
| 150 } |
| 151 |
118 private: | 152 private: |
119 extensions::EventRouterForwarder* forwarder() { | 153 extensions::EventRouterForwarder* forwarder() { |
120 #if defined(ENABLE_EXTENSIONS) | 154 #if defined(ENABLE_EXTENSIONS) |
121 return forwarder_.get(); | 155 return forwarder_.get(); |
122 #else | 156 #else |
123 return NULL; | 157 return NULL; |
124 #endif | 158 #endif |
125 } | 159 } |
126 | 160 |
127 content::TestBrowserThreadBundle thread_bundle_; | 161 content::TestBrowserThreadBundle thread_bundle_; |
(...skipping 17 matching lines...) Expand all Loading... |
145 // Loop over all combinations of the two policies. | 179 // Loop over all combinations of the two policies. |
146 for (int i = 0; i < 4; i++) { | 180 for (int i = 0; i < 4; i++) { |
147 bool google_safe_search = i % 2; | 181 bool google_safe_search = i % 2; |
148 bool youtube_safety_mode = i / 2; | 182 bool youtube_safety_mode = i / 2; |
149 SetSafeSearch(google_safe_search, youtube_safety_mode); | 183 SetSafeSearch(google_safe_search, youtube_safety_mode); |
150 | 184 |
151 QueryURL(google_safe_search, youtube_safety_mode); | 185 QueryURL(google_safe_search, youtube_safety_mode); |
152 } | 186 } |
153 } | 187 } |
154 | 188 |
| 189 // This function tests data use measurement for different types of requests by |
| 190 // making two queries. First query is similar to a query from a service, so it |
| 191 // should affect DataUse.NotUser.Dimensions and DataUse.service.ServiceName |
| 192 // histograms. The second query is similar to a query from a user, so it should |
| 193 // affect DataUse.User.Dimensions. AppState and ConnectionType dimensions are |
| 194 // always Foreground and NotCellular respectively. Because Background dimension |
| 195 // is only defined for android platform and when Chrome is in background and |
| 196 // Cellular ConnectionType is only exist on mobile devices. |
| 197 TEST_F(ChromeNetworkDelegateSafeSearchTest, DataUseMeasurement) { |
| 198 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); |
| 199 SetDelegate(delegate.get()); |
| 200 |
| 201 base::HistogramTester histogram_tester; |
| 202 |
| 203 QueryURLDataUse(0); // A query from a service |
| 204 histogram_tester.ExpectTotalCount( |
| 205 "DataUse.NotUser.Download.Foreground.NotCellular", 1); |
| 206 histogram_tester.ExpectTotalCount( |
| 207 "DataUse.NotUser.Upload.Foreground.NotCellular", 1); |
| 208 // One upload and one download message, so totalCount shoudl be 2. |
| 209 histogram_tester.ExpectTotalCount("DataUse.Service.ServiceName", 2); |
| 210 // The upload nad download messages are empty, so their size should be 0. |
| 211 histogram_tester.ExpectUniqueSample("DataUse.Service.ServiceName", 0, 2); |
| 212 |
| 213 QueryURLDataUse(1); // A query from user |
| 214 histogram_tester.ExpectTotalCount( |
| 215 "DataUse.User.Download.Foreground.NotCellular", 1); |
| 216 histogram_tester.ExpectTotalCount( |
| 217 "DataUse.User.Upload.Foreground.NotCellular", 1); |
| 218 } |
| 219 |
155 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) | 220 // Privacy Mode disables Channel Id if cookies are blocked (cr223191) |
156 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { | 221 class ChromeNetworkDelegatePrivacyModeTest : public testing::Test { |
157 public: | 222 public: |
158 ChromeNetworkDelegatePrivacyModeTest() | 223 ChromeNetworkDelegatePrivacyModeTest() |
159 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 224 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
160 #if defined(ENABLE_EXTENSIONS) | 225 #if defined(ENABLE_EXTENSIONS) |
161 forwarder_(new extensions::EventRouterForwarder()), | 226 forwarder_(new extensions::EventRouterForwarder()), |
162 #endif | 227 #endif |
163 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), | 228 cookie_settings_(CookieSettingsFactory::GetForProfile(&profile_).get()), |
164 kBlockedSite("http://ads.thirdparty.com"), | 229 kBlockedSite("http://ads.thirdparty.com"), |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 kBlockedFirstPartySite)); | 326 kBlockedFirstPartySite)); |
262 | 327 |
263 cookie_settings_->SetCookieSetting( | 328 cookie_settings_->SetCookieSetting( |
264 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), | 329 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), |
265 ContentSettingsPattern::Wildcard(), | 330 ContentSettingsPattern::Wildcard(), |
266 CONTENT_SETTING_BLOCK); | 331 CONTENT_SETTING_BLOCK); |
267 // Privacy mode is disabled as kAllowedSite is still getting cookies | 332 // Privacy mode is disabled as kAllowedSite is still getting cookies |
268 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, | 333 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, |
269 kBlockedFirstPartySite)); | 334 kBlockedFirstPartySite)); |
270 } | 335 } |
OLD | NEW |