Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_LOADER_PREFETCH_BROWSERTEST_BASE_H_ |
| 6 | #define CONTENT_BROWSER_LOADER_PREFETCH_BROWSERTEST_BASE_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 11 | #include "base/functional/callback.h" |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/synchronization/lock.h" |
| 14 | #include "base/thread_annotations.h" |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 15 | #include "content/public/test/content_browser_test.h" |
| 16 | #include "net/test/embedded_test_server/http_request.h" |
| 17 | #include "net/test/embedded_test_server/http_response.h" |
| 18 | |
| 19 | namespace net { |
| 20 | namespace test_server { |
| 21 | class EmbeddedTestServer; |
| 22 | struct HttpRequest; |
| 23 | } // namespace test_server |
| 24 | } // namespace net |
| 25 | |
| 26 | namespace content { |
| 27 | |
| 28 | class SignedExchangeHandlerFactory; |
| 29 | |
| 30 | class PrefetchBrowserTestBase : public ContentBrowserTest { |
| 31 | public: |
| 32 | struct ResponseEntry { |
| 33 | ResponseEntry(); |
Tsuyoshi Horo | 92499bf1 | 2019-06-05 04:21:21 | [diff] [blame] | 34 | ResponseEntry( |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 35 | const std::string& content, |
| 36 | const std::string& content_types = "text/html", |
Dominic Farolino | 693fd72d | 2019-08-05 11:09:44 | [diff] [blame] | 37 | const std::vector<std::pair<std::string, std::string>>& headers = {}, |
| 38 | net::HttpStatusCode code = net::HTTP_OK); |
Tsuyoshi Horo | 92499bf1 | 2019-06-05 04:21:21 | [diff] [blame] | 39 | ResponseEntry(const ResponseEntry&) = delete; |
| 40 | ResponseEntry(ResponseEntry&& other); |
| 41 | ResponseEntry& operator=(const ResponseEntry&) = delete; |
| 42 | ResponseEntry& operator=(ResponseEntry&&); |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 43 | ~ResponseEntry(); |
| 44 | |
| 45 | std::string content; |
| 46 | std::string content_type; |
| 47 | std::vector<std::pair<std::string, std::string>> headers; |
Dominic Farolino | 693fd72d | 2019-08-05 11:09:44 | [diff] [blame] | 48 | net::HttpStatusCode code; |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | struct ScopedSignedExchangeHandlerFactory { |
| 52 | explicit ScopedSignedExchangeHandlerFactory( |
| 53 | SignedExchangeHandlerFactory* factory); |
| 54 | ~ScopedSignedExchangeHandlerFactory(); |
| 55 | }; |
| 56 | |
| 57 | PrefetchBrowserTestBase(); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 58 | |
| 59 | PrefetchBrowserTestBase(const PrefetchBrowserTestBase&) = delete; |
| 60 | PrefetchBrowserTestBase& operator=(const PrefetchBrowserTestBase&) = delete; |
| 61 | |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 62 | ~PrefetchBrowserTestBase() override; |
| 63 | |
| 64 | void SetUpOnMainThread() override; |
| 65 | |
| 66 | protected: |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 67 | class RequestCounter : public base::RefCountedThreadSafe<RequestCounter> { |
| 68 | public: |
| 69 | // Create a counter that is to be incremented when |path| on the |
| 70 | // |test_server| is accessed. |waiter| can be optionally specified that will |
| 71 | // be run after the counter is incremented. The counter value can be |
| 72 | // obtained via GetRequestCount(). This class works across threads, |
| 73 | // GetRequestCount can be called on any threads. |
| 74 | static scoped_refptr<RequestCounter> CreateAndMonitor( |
| 75 | net::EmbeddedTestServer* test_server, |
| 76 | const std::string& path, |
| 77 | base::RunLoop* waiter = nullptr); |
| 78 | RequestCounter(const std::string& path, base::RunLoop* waiter); |
| 79 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 80 | RequestCounter(const RequestCounter&) = delete; |
| 81 | RequestCounter& operator=(const RequestCounter&) = delete; |
| 82 | |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 83 | int GetRequestCount(); |
| 84 | |
| 85 | private: |
| 86 | friend base::RefCountedThreadSafe<RequestCounter>; |
| 87 | ~RequestCounter(); |
| 88 | |
| 89 | void OnRequest(const net::test_server::HttpRequest& request); |
| 90 | |
| 91 | base::OnceClosure waiter_closure_; |
| 92 | const std::string path_; |
| 93 | int request_count_ GUARDED_BY(lock_) = 0; |
| 94 | base::Lock lock_; |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 95 | }; |
| 96 | |
Tsuyoshi Horo | 92499bf1 | 2019-06-05 04:21:21 | [diff] [blame] | 97 | void RegisterResponse(const std::string& url, ResponseEntry&& entry); |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 98 | |
| 99 | std::unique_ptr<net::test_server::HttpResponse> ServeResponses( |
| 100 | const net::test_server::HttpRequest& request); |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 101 | void OnPrefetchURLLoaderCalled(); |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 102 | |
| 103 | void RegisterRequestHandler( |
| 104 | net::test_server::EmbeddedTestServer* test_server); |
| 105 | void NavigateToURLAndWaitTitle(const GURL& url, const std::string& title); |
| 106 | void WaitUntilLoaded(const GURL& url); |
| 107 | |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 108 | int GetPrefetchURLLoaderCallCount(); |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 109 | |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 110 | // Register a callback to be called just before the `PrefetchURLLoader` is |
| 111 | // created and started. |
| 112 | void RegisterPrefetchLoaderCallback(base::OnceClosure callback); |
| 113 | |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 114 | private: |
| 115 | std::map<std::string, ResponseEntry> response_map_; |
| 116 | |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 117 | base::OnceClosure prefetch_loader_callback_; |
| 118 | |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 119 | int prefetch_url_loader_called_ GUARDED_BY(lock_) = 0; |
| 120 | base::Lock lock_; |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | } // namespace content |
| 124 | |
| 125 | #endif // CONTENT_BROWSER_LOADER_PREFETCH_BROWSERTEST_BASE_H_ |