Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / public / platform / WebScheduler.h
blob122918693a863641b39726789d7690050ba2440a
1 // Copyright 2014 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.
5 #ifndef WebScheduler_h
6 #define WebScheduler_h
8 #include "WebCommon.h"
9 #include "public/platform/WebTaskRunner.h"
10 #include "public/platform/WebThread.h"
12 namespace blink {
14 class WebFrameHostScheduler;
15 class WebTraceLocation;
17 // This class is used to submit tasks and pass other information from Blink to
18 // the platform's scheduler.
19 class BLINK_PLATFORM_EXPORT WebScheduler {
20 public:
21 virtual ~WebScheduler() { }
23 // Called to prevent any more pending tasks from running. Must be called on
24 // the associated WebThread.
25 virtual void shutdown() { }
27 // Returns true if there is high priority work pending on the associated WebThread
28 // and the caller should yield to let the scheduler service that work.
29 // Must be called on the associated WebThread.
30 virtual bool shouldYieldForHighPriorityWork() { return false; }
32 // Returns true if a currently running idle task could exceed its deadline
33 // without impacting user experience too much. This should only be used if
34 // there is a task which cannot be pre-empted and is likely to take longer
35 // than the largest expected idle task deadline. It should NOT be polled to
36 // check whether more work can be performed on the current idle task after
37 // its deadline has expired - post a new idle task for the continuation of
38 // the work in this case.
39 // Must be called from the associated WebThread.
40 virtual bool canExceedIdleDeadlineIfRequired() { return false; }
42 // Schedule an idle task to run the associated WebThread. For non-critical
43 // tasks which may be reordered relative to other task types and may be
44 // starved for an arbitrarily long time if no idle time is available.
45 // Takes ownership of |IdleTask|. Can be called from any thread.
46 virtual void postIdleTask(const WebTraceLocation&, WebThread::IdleTask*) { }
48 // Like postIdleTask but guarantees that the posted task will not run
49 // nested within an already-running task. Posting an idle task as
50 // non-nestable may not affect when the task gets run, or it could
51 // make it run later than it normally would, but it won't make it
52 // run earlier than it normally would.
53 virtual void postNonNestableIdleTask(const WebTraceLocation&, WebThread::IdleTask*) { }
55 // Like postIdleTask but does not run the idle task until after some other
56 // task has run. This enables posting of a task which won't stop the Blink
57 // main thread from sleeping, but will start running after it wakes up.
58 // Takes ownership of |IdleTask|. Can be called from any thread.
59 virtual void postIdleTaskAfterWakeup(const WebTraceLocation&, WebThread::IdleTask*) { }
61 // Schedule a timer task to be run on the the associated WebThread. Timer Tasks
62 // tasks usually have the default priority, but may be delayed
63 // when the user is interacting with the device.
64 // |monotonicTime| is in the timebase of WTF::monotonicallyIncreasingTime().
65 // Takes ownership of |WebTaskRunner::Task|. Can be called from any thread.
66 // TODO(alexclarke): Move timer throttling for background pages to the
67 // chromium side and remove this.
68 virtual void postTimerTaskAt(const WebTraceLocation&, WebTaskRunner::Task*, double monotonicTime) {}
70 // Returns a WebTaskRunner for loading tasks. Can be called from any thread.
71 virtual WebTaskRunner* loadingTaskRunner() { return nullptr; }
73 // Returns a WebTaskRunner for timer tasks. Can be called from any thread.
74 virtual WebTaskRunner* timerTaskRunner() { return nullptr; }
76 // Creates a new WebFrameHostScheduler. Must be called from the associated WebThread.
77 virtual WebFrameHostScheduler* createFrameHostScheduler() { return nullptr; }
79 // Suspends the timer queue and increments the timer queue suspension count.
80 // May only be called from the main thread.
81 virtual void suspendTimerQueue() { }
83 // Decrements the timer queue suspension count and re-enables the timer queue
84 // if the suspension count is zero and the current scheduler policy allows it.
85 virtual void resumeTimerQueue() { }
87 #ifdef INSIDE_BLINK
88 // Helpers for posting bound functions as tasks.
89 typedef Function<void(double deadlineSeconds)> IdleTask;
91 void postIdleTask(const WebTraceLocation&, PassOwnPtr<IdleTask>);
92 void postNonNestableIdleTask(const WebTraceLocation&, PassOwnPtr<IdleTask>);
93 void postIdleTaskAfterWakeup(const WebTraceLocation&, PassOwnPtr<IdleTask>);
94 #endif
97 } // namespace blink
99 #endif // WebScheduler_h