OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_SCHEDULER_RENDERER_THROTTLING_HELPER_H_ | |
6 #define COMPONENTS_SCHEDULER_RENDERER_THROTTLING_HELPER_H_ | |
7 | |
8 #include <set> | |
9 | |
10 #include "base/macros.h" | |
11 #include "components/scheduler/base/cancelable_closure_holder.h" | |
12 #include "components/scheduler/base/time_domain.h" | |
13 #include "components/scheduler/scheduler_export.h" | |
14 #include "third_party/WebKit/public/platform/WebViewScheduler.h" | |
15 | |
16 namespace scheduler { | |
17 | |
18 class RendererSchedulerImpl; | |
19 class ThrottledTimeDomain; | |
20 class WebFrameSchedulerImpl; | |
21 | |
22 class SCHEDULER_EXPORT ThrottlingHelper : public TimeDomain::Observer { | |
23 public: | |
24 ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, | |
25 const char* tracing_category); | |
26 | |
27 ~ThrottlingHelper() override; | |
28 | |
29 // TimeDomain::Observer implementation: | |
30 void OnTimeDomainHasImmediateWork() override; | |
31 void OnTimeDomainHasDelayedWork() override; | |
32 | |
33 // The purpose of this method is to make sure throttling doesn't conflict with | |
34 // enabling/disabling the queue for policy reasons. | |
35 // If |task_queue| is throttled then the ThrottlingHelper remembers the | |
36 // |enabled| setting. In addition if |enabled| is false then the queue is | |
37 // immediatly disabled. Otherwise if |task_queue| not throttled then | |
38 // TaskQueue::SetEnabled(enabled) is called. | |
39 void SetQueueEnabled(TaskQueue* task_queue, bool enabled); | |
40 | |
41 // Increments the throttled refcount and causes |task_queue| to be throttled | |
42 // if its not already throttled. | |
43 void IncreaseThrottleRefCount(TaskQueue* task_queue); | |
44 | |
45 // If the refcouint is non-zero it's decremented. If the throttled refcount | |
46 // becomes zero then |task_queue| is unthrottled. If the refcount was already | |
47 // zero this function does nothing. | |
48 void DecreaseThrottleRefCount(TaskQueue* task_queue); | |
49 | |
50 // Removes |task_queue| from |throttled_queues_|. | |
51 void UnregisterTaskQueue(TaskQueue* task_queue); | |
52 | |
53 // Tells the ThrottlingHelper we're using virtual time, which disables all | |
54 // throttling. | |
55 void EnableVirtualTime(); | |
56 | |
57 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); } | |
58 | |
59 static base::TimeTicks ThrottledRunTime(base::TimeTicks unthrottled_runtime); | |
60 | |
61 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; } | |
62 | |
63 private: | |
64 struct Metadata { | |
65 Metadata() : throttling_ref_count(0), enabled(false) {} | |
66 | |
67 Metadata(size_t ref_count, bool is_enabled) | |
68 : throttling_ref_count(ref_count), enabled(is_enabled) {} | |
69 | |
70 size_t throttling_ref_count; | |
71 bool enabled; | |
72 }; | |
73 using TaskQueueMap = std::map<TaskQueue*, Metadata>; | |
74 | |
75 void PumpThrottledTasks(); | |
76 | |
77 // Note |unthrottled_runtime| might be in the past. When this happens we | |
78 // compute the delay to the next runtime based on now rather than | |
79 // unthrottled_runtime. | |
80 void MaybeSchedulePumpThrottledTasksLocked( | |
81 const tracked_objects::Location& from_here, | |
82 base::TimeTicks now, | |
83 base::TimeTicks unthrottled_runtime); | |
84 | |
85 TaskQueueMap throttled_queues_; | |
86 base::Closure forward_immediate_work_closure_; | |
87 scoped_refptr<TaskQueue> task_runner_; | |
88 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED | |
89 base::TickClock* tick_clock_; // NOT OWNED | |
90 const char* tracing_category_; // NOT OWNED | |
91 std::unique_ptr<ThrottledTimeDomain> time_domain_; | |
92 | |
93 CancelableClosureHolder pump_throttled_tasks_closure_; | |
94 base::TimeTicks pending_pump_throttled_tasks_runtime_; | |
95 bool virtual_time_; | |
96 | |
97 base::WeakPtrFactory<ThrottlingHelper> weak_factory_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(ThrottlingHelper); | |
100 }; | |
101 | |
102 } // namespace scheduler | |
103 | |
104 #endif // COMPONENTS_SCHEDULER_RENDERER_THROTTLING_HELPER_H_ | |
OLD | NEW |