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/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 10 #include "base/prefs/pref_member.h" | 11 #include "base/prefs/pref_member.h" |
| 11 #include "chrome/browser/content_settings/cookie_settings.h" | 12 #include "chrome/browser/content_settings/cookie_settings.h" |
| 12 #include "chrome/browser/net/safe_search_util.h" | 13 #include "chrome/browser/net/safe_search_util.h" |
| 13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/testing_pref_service_syncable.h" | 15 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/common/content_switches.h" | |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
| 18 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 21 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 23 |
| 22 #if defined(ENABLE_EXTENSIONS) | 24 #if defined(ENABLE_EXTENSIONS) |
| 23 #include "chrome/browser/extensions/event_router_forwarder.h" | 25 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 24 #endif | 26 #endif |
| 25 | 27 |
| 28 TEST(ChromeNetworkDelegateTest, DisableFirstPartyOnlyCookiesIffFlagDisabled) { | |
| 29 BooleanPrefMember pref_member_; | |
| 30 scoped_ptr<ChromeNetworkDelegate> delegate; | |
| 31 | |
| 26 #if defined(ENABLE_EXTENSIONS) | 32 #if defined(ENABLE_EXTENSIONS) |
| 27 class ChromeNetworkDelegateTest : public testing::Test { | 33 scoped_refptr<extensions::EventRouterForwarder> forwarder = |
| 34 new extensions::EventRouterForwarder(); | |
| 35 delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_)); | |
| 36 #else | |
| 37 delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_)); | |
| 38 #endif | |
| 39 EXPECT_FALSE(delegate->FirstPartyOnlyCookieExperimentEnabled()); | |
| 40 } | |
| 41 | |
| 42 TEST(ChromeNetworkDelegateTest, EnableFirstPartyOnlyCookiesIffFlagEnabled) { | |
| 43 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 44 switches::kEnableExperimentalWebPlatformFeatures); | |
| 45 BooleanPrefMember pref_member_; | |
| 46 scoped_ptr<ChromeNetworkDelegate> delegate; | |
| 47 | |
| 48 #if defined(ENABLE_EXTENSIONS) | |
| 49 scoped_refptr<extensions::EventRouterForwarder> forwarder = | |
| 50 new extensions::EventRouterForwarder(); | |
| 51 delegate.reset(new ChromeNetworkDelegate(forwarder.get(), &pref_member_)); | |
| 52 #else | |
| 53 delegate.reset(new ChromeNetworkDelegate(nullptr, &pref_member_)); | |
| 54 #endif | |
|
mmenke
2015/02/26 16:24:58
Hadn't realized you'd need to do this, but I still
| |
| 55 EXPECT_TRUE(delegate->FirstPartyOnlyCookieExperimentEnabled()); | |
| 56 } | |
| 57 | |
| 58 #if defined(ENABLE_EXTENSIONS) | |
| 59 class ChromeNetworkDelegateThrottlingTest : public testing::Test { | |
| 28 protected: | 60 protected: |
| 29 ChromeNetworkDelegateTest() | 61 ChromeNetworkDelegateThrottlingTest() |
| 30 : forwarder_(new extensions::EventRouterForwarder()) { | 62 : forwarder_(new extensions::EventRouterForwarder()) {} |
| 31 } | |
| 32 | 63 |
| 33 void SetUp() override { | 64 void SetUp() override { |
| 34 never_throttle_requests_original_value_ = | 65 never_throttle_requests_original_value_ = |
| 35 ChromeNetworkDelegate::g_never_throttle_requests_; | 66 ChromeNetworkDelegate::g_never_throttle_requests_; |
| 36 ChromeNetworkDelegate::g_never_throttle_requests_ = false; | 67 ChromeNetworkDelegate::g_never_throttle_requests_ = false; |
| 37 } | 68 } |
| 38 | 69 |
| 39 void TearDown() override { | 70 void TearDown() override { |
| 40 ChromeNetworkDelegate::g_never_throttle_requests_ = | 71 ChromeNetworkDelegate::g_never_throttle_requests_ = |
| 41 never_throttle_requests_original_value_; | 72 never_throttle_requests_original_value_; |
| 42 } | 73 } |
| 43 | 74 |
| 44 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() { | 75 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() { |
| 45 return make_scoped_ptr( | 76 return make_scoped_ptr( |
| 46 new ChromeNetworkDelegate(forwarder_.get(), &pref_member_)); | 77 new ChromeNetworkDelegate(forwarder(), &pref_member_)); |
| 47 } | 78 } |
| 48 | 79 |
| 49 // Implementation moved here for access to private bits. | 80 // Implementation moved here for access to private bits. |
| 50 void NeverThrottleLogicImpl() { | 81 void NeverThrottleLogicImpl() { |
| 51 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); | 82 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); |
| 52 | 83 |
| 53 net::TestURLRequestContext context; | 84 net::TestURLRequestContext context; |
| 54 scoped_ptr<net::URLRequest> extension_request(context.CreateRequest( | 85 scoped_ptr<net::URLRequest> extension_request(context.CreateRequest( |
| 55 GURL("http://example.com/"), net::DEFAULT_PRIORITY, NULL, NULL)); | 86 GURL("http://example.com/"), net::DEFAULT_PRIORITY, NULL, NULL)); |
| 56 extension_request->set_first_party_for_cookies( | 87 extension_request->set_first_party_for_cookies( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 74 // We test the side effects of the flag rather than just the flag | 105 // We test the side effects of the flag rather than just the flag |
| 75 // itself (which we did above) to help ensure that a changed | 106 // itself (which we did above) to help ensure that a changed |
| 76 // implementation would show the same behavior, i.e. all instances | 107 // implementation would show the same behavior, i.e. all instances |
| 77 // of ChromeNetworkDelegate after the flag is set obey the flag. | 108 // of ChromeNetworkDelegate after the flag is set obey the flag. |
| 78 scoped_ptr<ChromeNetworkDelegate> second_delegate(CreateNetworkDelegate()); | 109 scoped_ptr<ChromeNetworkDelegate> second_delegate(CreateNetworkDelegate()); |
| 79 ASSERT_FALSE(delegate->OnCanThrottleRequest(*extension_request)); | 110 ASSERT_FALSE(delegate->OnCanThrottleRequest(*extension_request)); |
| 80 ASSERT_FALSE(delegate->OnCanThrottleRequest(*web_page_request)); | 111 ASSERT_FALSE(delegate->OnCanThrottleRequest(*web_page_request)); |
| 81 } | 112 } |
| 82 | 113 |
| 83 private: | 114 private: |
| 115 extensions::EventRouterForwarder* forwarder() { return forwarder_.get(); } | |
| 116 | |
| 84 bool never_throttle_requests_original_value_; | 117 bool never_throttle_requests_original_value_; |
| 85 base::MessageLoopForIO message_loop_; | 118 base::MessageLoopForIO message_loop_; |
| 86 | 119 |
| 87 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | 120 scoped_refptr<extensions::EventRouterForwarder> forwarder_; |
| 88 BooleanPrefMember pref_member_; | 121 BooleanPrefMember pref_member_; |
| 89 }; | 122 }; |
| 90 | 123 |
| 91 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) { | 124 TEST_F(ChromeNetworkDelegateThrottlingTest, NeverThrottleLogic) { |
| 92 NeverThrottleLogicImpl(); | 125 NeverThrottleLogicImpl(); |
| 93 } | 126 } |
| 94 #endif // defined(ENABLE_EXTENSIONS) | 127 #endif // defined(ENABLE_EXTENSIONS) |
| 95 | 128 |
| 96 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { | 129 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { |
| 97 public: | 130 public: |
| 98 ChromeNetworkDelegateSafeSearchTest() | 131 ChromeNetworkDelegateSafeSearchTest() |
| 99 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 132 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 100 #if defined(ENABLE_EXTENSIONS) | 133 #if defined(ENABLE_EXTENSIONS) |
| 101 forwarder_ = new extensions::EventRouterForwarder(); | 134 forwarder_ = new extensions::EventRouterForwarder(); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 306 kBlockedFirstPartySite)); | 339 kBlockedFirstPartySite)); |
| 307 | 340 |
| 308 cookie_settings_->SetCookieSetting( | 341 cookie_settings_->SetCookieSetting( |
| 309 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), | 342 ContentSettingsPattern::FromURL(kBlockedFirstPartySite), |
| 310 ContentSettingsPattern::Wildcard(), | 343 ContentSettingsPattern::Wildcard(), |
| 311 CONTENT_SETTING_BLOCK); | 344 CONTENT_SETTING_BLOCK); |
| 312 // Privacy mode is disabled as kAllowedSite is still getting cookies | 345 // Privacy mode is disabled as kAllowedSite is still getting cookies |
| 313 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, | 346 EXPECT_FALSE(network_delegate_->CanEnablePrivacyMode(kAllowedSite, |
| 314 kBlockedFirstPartySite)); | 347 kBlockedFirstPartySite)); |
| 315 } | 348 } |
| 316 | |
| OLD | NEW |