summaryrefslogtreecommitdiff
path: root/chromium/headless/public/util/deterministic_dispatcher.h
blob: 88281981f8122c87b42e06c43327165b9f105a59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef HEADLESS_PUBLIC_UTIL_DETERMINISTIC_DISPATCHER_H_
#define HEADLESS_PUBLIC_UTIL_DETERMINISTIC_DISPATCHER_H_

#include <deque>
#include <map>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/lock.h"
#include "headless/public/headless_export.h"
#include "headless/public/util/url_request_dispatcher.h"
#include "net/base/net_errors.h"

namespace headless {

class ManagedDispatchURLRequestJob;

// The purpose of this class is to queue up navigations and calls to
// OnHeadersComplete / OnStartError and dispatch them in order of creation. This
// helps make renders deterministic at the cost of slower page loads.
class HEADLESS_EXPORT DeterministicDispatcher : public URLRequestDispatcher {
 public:
  explicit DeterministicDispatcher(
      scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner);

  ~DeterministicDispatcher() override;

  // UrlRequestDispatcher implementation:
  void JobCreated(ManagedDispatchURLRequestJob* job) override;
  void JobKilled(ManagedDispatchURLRequestJob* job) override;
  void JobFailed(ManagedDispatchURLRequestJob* job, net::Error error) override;
  void DataReady(ManagedDispatchURLRequestJob* job) override;
  void JobDeleted(ManagedDispatchURLRequestJob* job) override;
  void NavigationRequested(
      std::unique_ptr<NavigationRequest> navigation_request) override;

 private:
  void MaybeDispatchNavigationJobLocked();
  void MaybeDispatchJobLocked();
  void MaybeDispatchJobOnIOThreadTask();
  void NavigationDoneTask();

  scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_;

  // Protects all members below.
  base::Lock lock_;

  // TODO(alexclarke): Use std::variant when c++17 is allowed in chromium.
  struct Request {
    Request();
    explicit Request(ManagedDispatchURLRequestJob* url_request);
    explicit Request(std::unique_ptr<NavigationRequest> navigation_request);
    ~Request();

    Request& operator=(Request&& other);

    ManagedDispatchURLRequestJob* url_request;  // NOT OWNED
    std::unique_ptr<NavigationRequest> navigation_request;
  };

  std::deque<Request> pending_requests_;

  using StatusMap = std::map<ManagedDispatchURLRequestJob*, net::Error>;
  StatusMap ready_status_map_;

  // Whether or not a MaybeDispatchJobOnIoThreadTask has been posted on the
  // |io_thread_task_runner_|
  bool dispatch_pending_;
  bool navigation_in_progress_;

  base::WeakPtrFactory<DeterministicDispatcher> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(DeterministicDispatcher);
};

}  // namespace headless

#endif  // HEADLESS_PUBLIC_UTIL_DETERMINISTIC_DISPATCHER_H_