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 #include "components/scheduler/renderer/throttling_helper.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "components/scheduler/base/real_time_domain.h" | |
9 #include "components/scheduler/base/virtual_time_domain.h" | |
10 #include "components/scheduler/child/scheduler_tqm_delegate.h" | |
11 #include "components/scheduler/renderer/renderer_scheduler_impl.h" | |
12 #include "components/scheduler/renderer/web_frame_scheduler_impl.h" | |
13 #include "third_party/WebKit/public/platform/WebFrameScheduler.h" | |
14 | |
15 namespace scheduler { | |
16 | |
17 ThrottlingHelper::ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, | |
18 const char* tracing_category) | |
19 : renderer_scheduler_(renderer_scheduler), | |
20 tracing_category_(tracing_category), | |
21 time_domain_(new VirtualTimeDomain(renderer_scheduler->real_time_domain() | |
22 ->CreateLazyNow() | |
23 .Now())), | |
24 pending_pump_throttled_tasks_(false), | |
25 work_to_do_(false), | |
26 weak_factory_(this) { | |
27 pump_throttled_tasks_closure_ = base::Bind( | |
28 &ThrottlingHelper::PumpThrottledTasks, weak_factory_.GetWeakPtr()); | |
29 renderer_scheduler_->RegisterTimeDomain(time_domain_); | |
30 time_domain_->SetObserver(this); | |
31 } | |
32 | |
33 ThrottlingHelper::~ThrottlingHelper() { | |
34 time_domain_->SetObserver(nullptr); | |
35 renderer_scheduler_->UnregisterTimeDomain(time_domain_); | |
36 } | |
37 | |
38 void ThrottlingHelper::ThrottleBackgroundFrame( | |
39 WebFrameSchedulerImpl* frame_scheduler) { | |
40 background_frame_schedulers_.insert(frame_scheduler); | |
41 frame_scheduler->SetPageInBackground(true); | |
42 | |
43 { | |
44 base::AutoLock lock(lock_); | |
45 work_to_do_ |= frame_scheduler->PendingBackgroundTasks(); | |
46 MaybeSchedulePumpThrottledTasksLocked(); | |
47 } | |
48 } | |
49 | |
50 void ThrottlingHelper::UnthrottleForegroundFrame( | |
51 WebFrameSchedulerImpl* frame_scheduler) { | |
52 background_frame_schedulers_.erase(frame_scheduler); | |
53 frame_scheduler->SetPageInBackground(false); | |
54 | |
55 if (background_frame_schedulers_.empty()) { | |
56 base::AutoLock lock(lock_); | |
57 work_to_do_ = false; | |
58 } | |
59 } | |
60 | |
61 void ThrottlingHelper::Unregister(WebFrameSchedulerImpl* frame_scheduler) { | |
62 background_frame_schedulers_.erase(frame_scheduler); | |
63 | |
64 if (background_frame_schedulers_.empty()) { | |
65 base::AutoLock lock(lock_); | |
66 work_to_do_ = false; | |
67 } | |
68 } | |
69 | |
70 void ThrottlingHelper::OnRegisterAsUpdatableTaskQueue() { | |
71 base::AutoLock lock(lock_); | |
72 TRACE_EVENT0(tracing_category_, | |
73 "ThrottlingHelper::OnRegisterAsUpdatableTaskQueue"); | |
74 work_to_do_ = true; | |
75 MaybeSchedulePumpThrottledTasksLocked(); | |
76 } | |
77 | |
78 void ThrottlingHelper::OnRequestWakeup() { | |
79 base::AutoLock lock(lock_); | |
80 TRACE_EVENT0(tracing_category_, | |
81 "ThrottlingHelper::OnRegisterAsUpdatableTaskQueue"); | |
Sami
2015/11/24 12:48:48
OnRequestWakeup
alex clarke (OOO till 29th)
2015/11/25 12:29:36
Done.
| |
82 work_to_do_ = true; | |
83 MaybeSchedulePumpThrottledTasksLocked(); | |
84 } | |
85 | |
86 void ThrottlingHelper::PumpThrottledTasks() { | |
87 TRACE_EVENT0(tracing_category_, "ThrottlingHelper::PumpThrottledTasks"); | |
88 time_domain_->AdvanceTo( | |
89 renderer_scheduler_->real_time_domain()->CreateLazyNow().Now()); | |
90 work_to_do_ = false; | |
Sami
2015/11/24 12:48:48
Should this be inside the lock too?
alex clarke (OOO till 29th)
2015/11/25 12:29:36
I got rid of the locking.
| |
91 for (WebFrameSchedulerImpl* frame_scheduler : background_frame_schedulers_) { | |
92 work_to_do_ |= frame_scheduler->PumpBackgroundTasks(); | |
93 } | |
94 | |
95 { | |
96 base::AutoLock lock(lock_); | |
97 pending_pump_throttled_tasks_ = false; | |
98 MaybeSchedulePumpThrottledTasksLocked(); | |
99 } | |
100 } | |
101 | |
102 /* static */ | |
103 base::TimeDelta ThrottlingHelper::DelayToNextRunTimeInSeconds( | |
104 base::TimeTicks now) { | |
105 const base::TimeDelta one_second = base::TimeDelta::FromSeconds(1); | |
Sami
2015/11/24 12:48:48
Could we use TimeTicks::SnappedToNextTick?
alex clarke (OOO till 29th)
2015/11/25 12:29:36
It doesn't seem to do what I wanted. Specifically
Sami
2015/11/25 17:12:25
Ah, different boundary condition, got it.
alex clarke (OOO till 29th)
2015/11/26 18:43:27
Acknowledged.
| |
106 return one_second - ((now - base::TimeTicks()) % one_second); | |
107 } | |
108 | |
109 void ThrottlingHelper::MaybeSchedulePumpThrottledTasksLocked() { | |
110 if (pending_pump_throttled_tasks_ || !work_to_do_) | |
Sami
2015/11/24 12:48:48
Assert that we're holding the lock?
alex clarke (OOO till 29th)
2015/11/25 12:29:36
Acknowledged.
| |
111 return; | |
112 | |
113 pending_pump_throttled_tasks_ = true; | |
114 renderer_scheduler_->ControlTaskRunner()->PostDelayedTask( | |
115 FROM_HERE, pump_throttled_tasks_closure_, | |
116 DelayToNextRunTimeInSeconds( | |
117 renderer_scheduler_->real_time_domain()->CreateLazyNow().Now())); | |
Sami
2015/11/24 12:48:48
Looks like real_time_domain() is main thread only
alex clarke (OOO till 29th)
2015/11/25 12:29:36
Acknowledged.
| |
118 } | |
119 | |
120 } // namespace scheduler | |
OLD | NEW |