OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef HEADLESS_PUBLIC_UTIL_DETERMINISTIC_DISPATCHER_H_ | 5 #ifndef HEADLESS_PUBLIC_UTIL_DETERMINISTIC_DISPATCHER_H_ |
6 #define HEADLESS_PUBLIC_UTIL_DETERMINISTIC_DISPATCHER_H_ | 6 #define HEADLESS_PUBLIC_UTIL_DETERMINISTIC_DISPATCHER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "headless/public/util/url_request_dispatcher.h" | 16 #include "headless/public/util/url_request_dispatcher.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 | 18 |
19 namespace headless { | 19 namespace headless { |
20 | 20 |
21 class ManagedDispatchURLRequestJob; | 21 class ManagedDispatchURLRequestJob; |
22 | 22 |
23 // The purpose of this class is to queue up calls to OnHeadersComplete and | 23 // The purpose of this class is to queue up navigations and calls to |
24 // OnStartError and dispatch them in order of URLRequestJob creation. This | 24 // OnHeadersComplete / OnStartError and dispatch them in order of creation. This |
25 // helps make renders deterministic at the cost of slower page loads. | 25 // helps make renders deterministic at the cost of slower page loads. |
26 class DeterministicDispatcher : public URLRequestDispatcher { | 26 class DeterministicDispatcher : public URLRequestDispatcher { |
27 public: | 27 public: |
28 explicit DeterministicDispatcher( | 28 explicit DeterministicDispatcher( |
29 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner); | 29 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner); |
30 | 30 |
31 ~DeterministicDispatcher() override; | 31 ~DeterministicDispatcher() override; |
32 | 32 |
33 // UrlRequestDispatcher implementation: | 33 // UrlRequestDispatcher implementation: |
34 void JobCreated(ManagedDispatchURLRequestJob* job) override; | 34 void JobCreated(ManagedDispatchURLRequestJob* job) override; |
35 void JobKilled(ManagedDispatchURLRequestJob* job) override; | 35 void JobKilled(ManagedDispatchURLRequestJob* job) override; |
36 void JobFailed(ManagedDispatchURLRequestJob* job, net::Error error) override; | 36 void JobFailed(ManagedDispatchURLRequestJob* job, net::Error error) override; |
37 void DataReady(ManagedDispatchURLRequestJob* job) override; | 37 void DataReady(ManagedDispatchURLRequestJob* job) override; |
38 void JobDeleted(ManagedDispatchURLRequestJob* job) override; | 38 void JobDeleted(ManagedDispatchURLRequestJob* job) override; |
| 39 void NavigationRequested( |
| 40 std::unique_ptr<NavigationRequest> navigation_request) override; |
39 | 41 |
40 private: | 42 private: |
| 43 void MaybeDispatchNavigationJobLocked(); |
41 void MaybeDispatchJobLocked(); | 44 void MaybeDispatchJobLocked(); |
42 void MaybeDispatchJobOnIOThreadTask(); | 45 void MaybeDispatchJobOnIOThreadTask(); |
| 46 void NavigationDoneTask(); |
43 | 47 |
44 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; | 48 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; |
45 | 49 |
46 // Protects all members below. | 50 // Protects all members below. |
47 base::Lock lock_; | 51 base::Lock lock_; |
48 | 52 |
49 std::deque<ManagedDispatchURLRequestJob*> pending_requests_; | 53 // TODO(alexclarke): Use std::variant when c++17 is allowed in chromium. |
| 54 struct Request { |
| 55 Request(); |
| 56 explicit Request(ManagedDispatchURLRequestJob* url_request); |
| 57 explicit Request(std::unique_ptr<NavigationRequest> navigation_request); |
| 58 ~Request(); |
| 59 |
| 60 Request& operator=(Request&& other); |
| 61 |
| 62 ManagedDispatchURLRequestJob* url_request; // NOT OWNED |
| 63 std::unique_ptr<NavigationRequest> navigation_request; |
| 64 }; |
| 65 |
| 66 std::deque<Request> pending_requests_; |
50 | 67 |
51 using StatusMap = std::map<ManagedDispatchURLRequestJob*, net::Error>; | 68 using StatusMap = std::map<ManagedDispatchURLRequestJob*, net::Error>; |
52 StatusMap ready_status_map_; | 69 StatusMap ready_status_map_; |
53 | 70 |
54 // Whether or not a MaybeDispatchJobOnIoThreadTask has been posted on the | 71 // Whether or not a MaybeDispatchJobOnIoThreadTask has been posted on the |
55 // |io_thread_task_runner_| | 72 // |io_thread_task_runner_| |
56 bool dispatch_pending_; | 73 bool dispatch_pending_; |
| 74 bool navigation_in_progress_; |
57 | 75 |
58 base::WeakPtrFactory<DeterministicDispatcher> weak_ptr_factory_; | 76 base::WeakPtrFactory<DeterministicDispatcher> weak_ptr_factory_; |
59 | 77 |
60 DISALLOW_COPY_AND_ASSIGN(DeterministicDispatcher); | 78 DISALLOW_COPY_AND_ASSIGN(DeterministicDispatcher); |
61 }; | 79 }; |
62 | 80 |
63 } // namespace headless | 81 } // namespace headless |
64 | 82 |
65 #endif // HEADLESS_PUBLIC_UTIL_DETERMINISTIC_DISPATCHER_H_ | 83 #endif // HEADLESS_PUBLIC_UTIL_DETERMINISTIC_DISPATCHER_H_ |
OLD | NEW |