summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/scheduler/worker/worker_scheduler_proxy.h
blob: 60ed4b6426328c247162ee9c246eafbd2d37bb14 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2018 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_WORKER_WORKER_SCHEDULER_PROXY_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_WORKER_WORKER_SCHEDULER_PROXY_H_

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_checker.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/service_manager/public/cpp/connector.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/scheduler/main_thread/frame_origin_type.h"
#include "third_party/blink/renderer/platform/scheduler/public/frame_or_worker_scheduler.h"
#include "third_party/blink/renderer/platform/scheduler/renderer/frame_status.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"

namespace blink {
namespace scheduler {
class WorkerScheduler;

// Helper class for communication between parent scheduler (may be a frame
// scheduler on the main thread or another woker scheduler on a worker thread)
// and worker scheduler (worker thread).
//
// It's owned by DedicatedWorkerThread and is created and destroyed
// on the parent thread. It's passed to WorkerScheduler during its
// construction. Given that DedicatedWorkerThread object outlives worker thread,
// this class outlives worker thread too.
class PLATFORM_EXPORT WorkerSchedulerProxy
    : public FrameOrWorkerScheduler::Observer {
 public:
  explicit WorkerSchedulerProxy(FrameOrWorkerScheduler* scheduler);
  ~WorkerSchedulerProxy() override;

  void OnWorkerSchedulerCreated(
      base::WeakPtr<WorkerScheduler> worker_scheduler);

  void OnLifecycleStateChanged(
      SchedulingLifecycleState lifecycle_state) override;

  // Accessed only during init.
  SchedulingLifecycleState lifecycle_state() const {
    DCHECK(!initialized_);
    return lifecycle_state_;
  }

  // Accessed only during init.
  base::Optional<FrameOriginType> parent_frame_type() const {
    DCHECK(!initialized_);
    return parent_frame_type_;
  }

  // Accessed only during init.
  ukm::SourceId ukm_source_id() const {
    DCHECK(!initialized_);
    return ukm_source_id_;
  }

  // Accessed only during init.
  std::unique_ptr<service_manager::Connector> TakeConnector() {
    DCHECK(!initialized_);
#if DCHECK_IS_ON()
    DCHECK(!connector_taken_);
    connector_taken_ = true;
#endif
    return std::move(connector_);
  }

  // Accessed only during init.
  FrameStatus initial_frame_status() const {
    DCHECK(!initialized_);
    return initial_frame_status_;
  }

 private:
  // Can be accessed only from the worker thread.
  base::WeakPtr<WorkerScheduler> worker_scheduler_;

  // Const after init on the worker thread.
  scoped_refptr<base::SingleThreadTaskRunner> worker_thread_task_runner_;

  SchedulingLifecycleState lifecycle_state_ =
      SchedulingLifecycleState::kNotThrottled;

  std::unique_ptr<FrameOrWorkerScheduler::LifecycleObserverHandle>
      throttling_observer_handle_;

  bool initialized_ = false;
  base::Optional<FrameOriginType> parent_frame_type_;
  FrameStatus initial_frame_status_ = FrameStatus::kNone;
  ukm::SourceId ukm_source_id_ = ukm::kInvalidSourceId;
  std::unique_ptr<service_manager::Connector> connector_;

#if DCHECK_IS_ON()
  bool connector_taken_ = false;
#endif

  THREAD_CHECKER(parent_thread_checker_);

  DISALLOW_COPY_AND_ASSIGN(WorkerSchedulerProxy);
};

}  // namespace scheduler
}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_WORKER_WORKER_SCHEDULER_PROXY_H_