| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <memory> |
| 5 #include <utility> | 6 #include <utility> |
| 6 | 7 |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "chrome/browser/extensions/extension_browsertest.h" | 11 #include "chrome/browser/extensions/extension_browsertest.h" |
| 12 #include "chrome/browser/profiles/profile_io_data.h" | 12 #include "chrome/browser/profiles/profile_io_data.h" |
| 13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 14 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "extensions/browser/extension_throttle_manager.h" | 15 #include "extensions/browser/extension_throttle_manager.h" |
| 16 #include "extensions/test/result_catcher.h" | 16 #include "extensions/test/result_catcher.h" |
| 17 #include "net/base/backoff_entry.h" | 17 #include "net/base/backoff_entry.h" |
| 18 #include "net/base/url_util.h" | 18 #include "net/base/url_util.h" |
| 19 #include "net/dns/mock_host_resolver.h" | 19 #include "net/dns/mock_host_resolver.h" |
| 20 #include "net/test/embedded_test_server/embedded_test_server.h" | 20 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 21 #include "net/test/embedded_test_server/http_request.h" | 21 #include "net/test/embedded_test_server/http_request.h" |
| 22 #include "net/test/embedded_test_server/http_response.h" | 22 #include "net/test/embedded_test_server/http_response.h" |
| 23 | 23 |
| 24 namespace extensions { | 24 namespace extensions { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 scoped_ptr<net::test_server::HttpResponse> HandleRequest( | 28 std::unique_ptr<net::test_server::HttpResponse> HandleRequest( |
| 29 bool set_cache_header_redirect_page, | 29 bool set_cache_header_redirect_page, |
| 30 bool set_cache_header_test_throttle_page, | 30 bool set_cache_header_test_throttle_page, |
| 31 const net::test_server::HttpRequest& request) { | 31 const net::test_server::HttpRequest& request) { |
| 32 if (base::StartsWith(request.relative_url, "/redirect", | 32 if (base::StartsWith(request.relative_url, "/redirect", |
| 33 base::CompareCase::SENSITIVE)) { | 33 base::CompareCase::SENSITIVE)) { |
| 34 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 34 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
| 35 new net::test_server::BasicHttpResponse()); | 35 new net::test_server::BasicHttpResponse()); |
| 36 http_response->set_code(net::HTTP_FOUND); | 36 http_response->set_code(net::HTTP_FOUND); |
| 37 http_response->set_content("Redirecting..."); | 37 http_response->set_content("Redirecting..."); |
| 38 http_response->set_content_type("text/plain"); | 38 http_response->set_content_type("text/plain"); |
| 39 http_response->AddCustomHeader("Location", "/test_throttle"); | 39 http_response->AddCustomHeader("Location", "/test_throttle"); |
| 40 if (set_cache_header_redirect_page) | 40 if (set_cache_header_redirect_page) |
| 41 http_response->AddCustomHeader("Cache-Control", "max-age=3600"); | 41 http_response->AddCustomHeader("Cache-Control", "max-age=3600"); |
| 42 return std::move(http_response); | 42 return std::move(http_response); |
| 43 } | 43 } |
| 44 | 44 |
| 45 if (base::StartsWith(request.relative_url, "/test_throttle", | 45 if (base::StartsWith(request.relative_url, "/test_throttle", |
| 46 base::CompareCase::SENSITIVE)) { | 46 base::CompareCase::SENSITIVE)) { |
| 47 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 47 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
| 48 new net::test_server::BasicHttpResponse()); | 48 new net::test_server::BasicHttpResponse()); |
| 49 http_response->set_code(net::HTTP_SERVICE_UNAVAILABLE); | 49 http_response->set_code(net::HTTP_SERVICE_UNAVAILABLE); |
| 50 http_response->set_content("The server is overloaded right now."); | 50 http_response->set_content("The server is overloaded right now."); |
| 51 http_response->set_content_type("text/plain"); | 51 http_response->set_content_type("text/plain"); |
| 52 if (set_cache_header_test_throttle_page) | 52 if (set_cache_header_test_throttle_page) |
| 53 http_response->AddCustomHeader("Cache-Control", "max-age=3600"); | 53 http_response->AddCustomHeader("Cache-Control", "max-age=3600"); |
| 54 return std::move(http_response); | 54 return std::move(http_response); |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Unhandled requests result in the Embedded test server sending a 404. | 57 // Unhandled requests result in the Embedded test server sending a 404. |
| 58 return scoped_ptr<net::test_server::BasicHttpResponse>(); | 58 return std::unique_ptr<net::test_server::BasicHttpResponse>(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 } // namespace | 61 } // namespace |
| 62 | 62 |
| 63 class ExtensionRequestLimitingThrottleBrowserTest | 63 class ExtensionRequestLimitingThrottleBrowserTest |
| 64 : public ExtensionBrowserTest { | 64 : public ExtensionBrowserTest { |
| 65 public: | 65 public: |
| 66 void SetUpOnMainThread() override { | 66 void SetUpOnMainThread() override { |
| 67 ExtensionBrowserTest::SetUpOnMainThread(); | 67 ExtensionBrowserTest::SetUpOnMainThread(); |
| 68 ProfileIOData* io_data = | 68 ProfileIOData* io_data = |
| 69 ProfileIOData::FromResourceContext(profile()->GetResourceContext()); | 69 ProfileIOData::FromResourceContext(profile()->GetResourceContext()); |
| 70 ExtensionThrottleManager* manager = io_data->GetExtensionThrottleManager(); | 70 ExtensionThrottleManager* manager = io_data->GetExtensionThrottleManager(); |
| 71 if (manager) { | 71 if (manager) { |
| 72 // Requests issued within within |kUserGestureWindowMs| of a user gesture | 72 // Requests issued within within |kUserGestureWindowMs| of a user gesture |
| 73 // are also considered as user gestures (see | 73 // are also considered as user gestures (see |
| 74 // resource_dispatcher_host_impl.cc), so these tests need to bypass the | 74 // resource_dispatcher_host_impl.cc), so these tests need to bypass the |
| 75 // checking of the net::LOAD_MAYBE_USER_GESTURE load flag in the manager | 75 // checking of the net::LOAD_MAYBE_USER_GESTURE load flag in the manager |
| 76 // in order to test the throttling logic. | 76 // in order to test the throttling logic. |
| 77 manager->SetIgnoreUserGestureLoadFlagForTests(true); | 77 manager->SetIgnoreUserGestureLoadFlagForTests(true); |
| 78 scoped_ptr< | 78 std::unique_ptr<net::BackoffEntry::Policy> policy( |
| 79 net::BackoffEntry::Policy> policy(new net::BackoffEntry::Policy{ | 79 new net::BackoffEntry::Policy{ |
| 80 // Number of initial errors (in sequence) to ignore before applying | 80 // Number of initial errors (in sequence) to ignore before |
| 81 // exponential back-off rules. | 81 // applying |
| 82 1, | 82 // exponential back-off rules. |
| 83 1, |
| 83 | 84 |
| 84 // Initial delay for exponential back-off in ms. | 85 // Initial delay for exponential back-off in ms. |
| 85 10 * 60 * 1000, | 86 10 * 60 * 1000, |
| 86 | 87 |
| 87 // Factor by which the waiting time will be multiplied. | 88 // Factor by which the waiting time will be multiplied. |
| 88 10, | 89 10, |
| 89 | 90 |
| 90 // Fuzzing percentage. ex: 10% will spread requests randomly | 91 // Fuzzing percentage. ex: 10% will spread requests randomly |
| 91 // between 90%-100% of the calculated time. | 92 // between 90%-100% of the calculated time. |
| 92 0.1, | 93 0.1, |
| 93 | 94 |
| 94 // Maximum amount of time we are willing to delay our request in ms. | 95 // Maximum amount of time we are willing to delay our request in |
| 95 15 * 60 * 1000, | 96 // ms. |
| 97 15 * 60 * 1000, |
| 96 | 98 |
| 97 // Time to keep an entry from being discarded even when it | 99 // Time to keep an entry from being discarded even when it |
| 98 // has no significant state, -1 to never discard. | 100 // has no significant state, -1 to never discard. |
| 99 -1, | 101 -1, |
| 100 | 102 |
| 101 // Don't use initial delay unless the last request was an error. | 103 // Don't use initial delay unless the last request was an error. |
| 102 false, | 104 false, |
| 103 }); | 105 }); |
| 104 manager->SetBackoffPolicyForTests(std::move(policy)); | 106 manager->SetBackoffPolicyForTests(std::move(policy)); |
| 105 } | 107 } |
| 106 // Requests to 127.0.0.1 bypass throttling, so set up a host resolver rule | 108 // Requests to 127.0.0.1 bypass throttling, so set up a host resolver rule |
| 107 // to use a fake domain. | 109 // to use a fake domain. |
| 108 host_resolver()->AddRule("www.example.com", "127.0.0.1"); | 110 host_resolver()->AddRule("www.example.com", "127.0.0.1"); |
| 109 ASSERT_TRUE(embedded_test_server()->Start()); | 111 ASSERT_TRUE(embedded_test_server()->Start()); |
| 110 extension_ = | 112 extension_ = |
| 111 LoadExtension(test_data_dir_.AppendASCII("extension_throttle")); | 113 LoadExtension(test_data_dir_.AppendASCII("extension_throttle")); |
| 112 ASSERT_TRUE(extension_); | 114 ASSERT_TRUE(extension_); |
| 113 } | 115 } |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 ThrottleRequestDisabled) { | 235 ThrottleRequestDisabled) { |
| 234 embedded_test_server()->RegisterRequestHandler( | 236 embedded_test_server()->RegisterRequestHandler( |
| 235 base::Bind(&HandleRequest, false, false)); | 237 base::Bind(&HandleRequest, false, false)); |
| 236 ASSERT_NO_FATAL_FAILURE( | 238 ASSERT_NO_FATAL_FAILURE( |
| 237 RunTest("test_request_not_throttled.html", | 239 RunTest("test_request_not_throttled.html", |
| 238 base::StringPrintf("http://www.example.com:%d/test_throttle", | 240 base::StringPrintf("http://www.example.com:%d/test_throttle", |
| 239 embedded_test_server()->port()))); | 241 embedded_test_server()->port()))); |
| 240 } | 242 } |
| 241 | 243 |
| 242 } // namespace extensions | 244 } // namespace extensions |
| OLD | NEW |