Commit 9a17290b authored by Hayato Ito's avatar Hayato Ito Committed by Commit Bot

ServiceWorkerEventQueue: Prefer separate functions to one PushTask(Task) function

Follow-up of the review comment:
https://crrev.com/c/1906452/6/third_party/blink/renderer/modules/service_worker/service_worker_timeout_timer.h#109

Prefer separate functions, EnqueueNormal(...) and EnqueuePending(...),
to just one PushTask(Task) function, so that callers don't have to
create a Task on caller sides.

This CL also renames ServiceWorkerEventQueue::Task to
ServiceWorkerEventQueue::Event, and does necessary changes accordingly.
We no longer use a word of *task* in ServiceWorkerEventQueue because using
both tasks and events in ServiceWorkerEventQueue is slightly confusing.
It would be better to use Event consistently because its meaning is clear
in this context.

Bug: 965802

Change-Id: I18b133429e4636eef69cfc4e79c33b13be978ef7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1948368
Commit-Queue: Hayato Ito <hayato@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721885}
parent d32dda5b
...@@ -80,61 +80,73 @@ void ServiceWorkerEventQueue::Start() { ...@@ -80,61 +80,73 @@ void ServiceWorkerEventQueue::Start() {
WTF::Unretained(this))); WTF::Unretained(this)));
} }
void ServiceWorkerEventQueue::PushTask(std::unique_ptr<Task> task) { void ServiceWorkerEventQueue::EnqueueNormal(
DCHECK(task->type != Task::Type::Pending || did_idle_timeout()); StartCallback start_callback,
bool can_start_processing_tasks = AbortCallback abort_callback,
!processing_tasks_ && task->type != Task::Type::Pending; base::Optional<base::TimeDelta> custom_timeout) {
task_queue_.emplace_back(std::move(task)); EnqueueEvent(std::make_unique<Event>(
if (!can_start_processing_tasks) Event::Type::Normal, std::move(start_callback), std::move(abort_callback),
std::move(custom_timeout)));
}
void ServiceWorkerEventQueue::EnqueuePending(
StartCallback start_callback,
AbortCallback abort_callback,
base::Optional<base::TimeDelta> custom_timeout) {
EnqueueEvent(std::make_unique<Event>(
Event::Type::Pending, std::move(start_callback),
std::move(abort_callback), std::move(custom_timeout)));
}
void ServiceWorkerEventQueue::EnqueueEvent(std::unique_ptr<Event> event) {
DCHECK(event->type != Event::Type::Pending || did_idle_timeout());
bool can_start_processing_events =
!processing_events_ && event->type != Event::Type::Pending;
queue_.emplace_back(std::move(event));
if (!can_start_processing_events)
return; return;
if (did_idle_timeout()) { if (did_idle_timeout()) {
idle_time_ = base::TimeTicks(); idle_time_ = base::TimeTicks();
did_idle_timeout_ = false; did_idle_timeout_ = false;
} }
ProcessTasks(); ProcessEvents();
} }
void ServiceWorkerEventQueue::ProcessTasks() { void ServiceWorkerEventQueue::ProcessEvents() {
DCHECK(!processing_tasks_); DCHECK(!processing_events_);
processing_tasks_ = true; processing_events_ = true;
while (!task_queue_.IsEmpty()) { while (!queue_.IsEmpty()) {
StartTask(task_queue_.TakeFirst()); StartEvent(queue_.TakeFirst());
} }
processing_tasks_ = false; processing_events_ = false;
// We have to check HasInflightEvent() and may trigger // We have to check HasInflightEvent() and may trigger
// OnNoInflightEvent() here because StartTask() can call EndEvent() // OnNoInflightEvent() here because StartEvent() can call EndEvent()
// synchronously, and EndEvent() never triggers OnNoInflightEvent() // synchronously, and EndEvent() never triggers OnNoInflightEvent()
// while ProcessTasks() is running. // while ProcessEvents() is running.
if (!HasInflightEvent()) if (!HasInflightEvent())
OnNoInflightEvent(); OnNoInflightEvent();
} }
void ServiceWorkerEventQueue::StartTask(std::unique_ptr<Task> task) { void ServiceWorkerEventQueue::StartEvent(std::unique_ptr<Event> event) {
int event_id = StartEvent(std::move(task->abort_callback),
task->custom_timeout.value_or(kEventTimeout));
std::move(task->start_callback).Run(event_id);
}
int ServiceWorkerEventQueue::StartEvent(AbortCallback abort_callback,
base::TimeDelta timeout) {
idle_time_ = base::TimeTicks(); idle_time_ = base::TimeTicks();
const int event_id = NextEventId(); const int event_id = NextEventId();
auto add_result = id_event_map_.insert( DCHECK(!HasEvent(event_id));
id_event_map_.insert(
event_id, std::make_unique<EventInfo>( event_id, std::make_unique<EventInfo>(
tick_clock_->NowTicks() + timeout, tick_clock_->NowTicks() +
WTF::Bind(std::move(abort_callback), event_id))); event->custom_timeout.value_or(kEventTimeout),
DCHECK(add_result.is_new_entry); WTF::Bind(std::move(event->abort_callback), event_id)));
return event_id; std::move(event->start_callback).Run(event_id);
} }
void ServiceWorkerEventQueue::EndEvent(int event_id) { void ServiceWorkerEventQueue::EndEvent(int event_id) {
DCHECK(HasEvent(event_id)); DCHECK(HasEvent(event_id));
id_event_map_.erase(event_id); id_event_map_.erase(event_id);
// Check |processing_tasks_| here because EndEvent() can be called // Check |processing_events_| here because EndEvent() can be called
// synchronously in StartTask(). We don't want to trigger // synchronously in StartEvent(). We don't want to trigger
// OnNoInflightEvent() while ProcessTasks() is running. // OnNoInflightEvent() while ProcessEvents() is running.
if (!processing_tasks_ && !HasInflightEvent()) if (!processing_events_ && !HasInflightEvent())
OnNoInflightEvent(); OnNoInflightEvent();
} }
...@@ -207,8 +219,8 @@ bool ServiceWorkerEventQueue::HasInflightEvent() const { ...@@ -207,8 +219,8 @@ bool ServiceWorkerEventQueue::HasInflightEvent() const {
return !id_event_map_.IsEmpty() || num_of_stay_awake_tokens_ > 0; return !id_event_map_.IsEmpty() || num_of_stay_awake_tokens_ > 0;
} }
ServiceWorkerEventQueue::Task::Task( ServiceWorkerEventQueue::Event::Event(
ServiceWorkerEventQueue::Task::Type type, ServiceWorkerEventQueue::Event::Type type,
StartCallback start_callback, StartCallback start_callback,
AbortCallback abort_callback, AbortCallback abort_callback,
base::Optional<base::TimeDelta> custom_timeout) base::Optional<base::TimeDelta> custom_timeout)
...@@ -217,7 +229,7 @@ ServiceWorkerEventQueue::Task::Task( ...@@ -217,7 +229,7 @@ ServiceWorkerEventQueue::Task::Task(
abort_callback(std::move(abort_callback)), abort_callback(std::move(abort_callback)),
custom_timeout(custom_timeout) {} custom_timeout(custom_timeout) {}
ServiceWorkerEventQueue::Task::~Task() = default; ServiceWorkerEventQueue::Event::~Event() = default;
ServiceWorkerEventQueue::EventInfo::EventInfo( ServiceWorkerEventQueue::EventInfo::EventInfo(
base::TimeTicks expiration_time, base::TimeTicks expiration_time,
......
...@@ -24,8 +24,11 @@ class TickClock; ...@@ -24,8 +24,11 @@ class TickClock;
namespace blink { namespace blink {
// ServiceWorkerEventQueue manages two types of timeouts: the long standing // ServiceWorkerEventQueue manages events dispatched on ServiceWorkerGlobalScope
// event timeout and the idle timeout. // and their timeouts.
//
// There are two types of timeouts: the long standing event timeout
// and the idle timeout.
// //
// 1) Event timeout: when an event starts, StartEvent() records the expiration // 1) Event timeout: when an event starts, StartEvent() records the expiration
// time of the event (kEventTimeout). If EndEvent() has not been called within // time of the event (kEventTimeout). If EndEvent() has not been called within
...@@ -70,45 +73,20 @@ class MODULES_EXPORT ServiceWorkerEventQueue { ...@@ -70,45 +73,20 @@ class MODULES_EXPORT ServiceWorkerEventQueue {
using StartCallback = base::OnceCallback<void(int /* event_id */)>; using StartCallback = base::OnceCallback<void(int /* event_id */)>;
// Represents an event dispatch task, which can be queued into |task_queue_|. // EndEvent() must be called when an event finishes, with |event_id|
struct MODULES_EXPORT Task { // which StartCallback received.
enum class Type {
// A normal task is pushed to the end of the task queue and triggers
// processing of the queue.
Normal,
// A pending task, which does not start until a normal task is
// pushed. A pending task should be used if the idle timeout
// occurred (did_idle_timeout() returns true). In practice, the
// caller pushes pending tasks after the service worker
// requested the browser to terminate it due to idleness. These
// tasks run once PushTask() is called due to a new event from
// the browser, signalling that the browser decided not to
// terminate the worker.
Pending,
};
Task(Type type,
StartCallback start_callback,
AbortCallback abort_callback,
base::Optional<base::TimeDelta> custom_timeout);
~Task();
Type type;
// Callback which is run when the event_queue starts this task. The
// callback receives |event_id|, which is the result of
// StartEvent(). When an event finishes, EndEvent() should be
// called with the given |event_id|.
StartCallback start_callback;
// Callback which is run when the event_queue aborts a started task.
AbortCallback abort_callback;
// The custom timeout value.
base::Optional<base::TimeDelta> custom_timeout;
};
void EndEvent(int event_id); void EndEvent(int event_id);
// Push the task to the queue, and tasks in the queue can run synchronously. // Enqueues a Normal event. See ServiceWorkerEventQueue::Event to know the
// See also Task's comment. // meaning of each parameter.
void PushTask(std::unique_ptr<Task> task); void EnqueueNormal(StartCallback start_callback,
AbortCallback abort_callback,
base::Optional<base::TimeDelta> custom_timeout);
// Similar to EnqueueNormal(), but enqueues a Pending event.
void EnqueuePending(StartCallback start_callback,
AbortCallback abort_callback,
base::Optional<base::TimeDelta> custom_timeout);
// Returns true if |event_id| was started and hasn't ended. // Returns true if |event_id| was started and hasn't ended.
bool HasEvent(int event_id) const; bool HasEvent(int event_id) const;
...@@ -140,10 +118,45 @@ class MODULES_EXPORT ServiceWorkerEventQueue { ...@@ -140,10 +118,45 @@ class MODULES_EXPORT ServiceWorkerEventQueue {
base::TimeDelta::FromSeconds(30); base::TimeDelta::FromSeconds(30);
private: private:
// StartEvent() should be called at the beginning of an event. It returns an // Represents an event dispatch, which can be queued into |queue_|.
// event id, which is unique among threads in the same process. struct Event {
// The event id should be passed to EndEvent() when the event has finished. enum class Type {
int StartEvent(AbortCallback abort_callback, base::TimeDelta timeout); // A normal event is enqueued to the end of the event queue and
// triggers processing of the queue.
Normal,
// A pending event which does not start until a normal event is
// pushed. A pending event should be used if the idle timeout
// occurred (did_idle_timeout() returns true). In practice, the
// caller enqueues pending events after the service worker
// requested the browser to terminate it due to idleness. These
// events run when a new event comes from the browser,
// signalling that the browser decided not to terminate the
// worker.
Pending,
};
Event(Type type,
StartCallback start_callback,
AbortCallback abort_callback,
base::Optional<base::TimeDelta> custom_timeout);
~Event();
Type type;
// Callback which is run when the event queue starts this event. The
// callback receives |event_id|. When an event finishes,
// EndEvent() should be called with the given |event_id|.
StartCallback start_callback;
// Callback which is run when a started event is aborted.
AbortCallback abort_callback;
// The custom timeout value.
base::Optional<base::TimeDelta> custom_timeout;
};
// Enqueues the event to |queue_|, and run events in the queue or sometimes
// later synchronously, depending on the type of events.
void EnqueueEvent(std::unique_ptr<Event> event);
// Starts a single event.
void StartEvent(std::unique_ptr<Event> event);
// Updates the internal states and fires timeout callbacks if any. // Updates the internal states and fires timeout callbacks if any.
void UpdateStatus(); void UpdateStatus();
...@@ -159,11 +172,8 @@ class MODULES_EXPORT ServiceWorkerEventQueue { ...@@ -159,11 +172,8 @@ class MODULES_EXPORT ServiceWorkerEventQueue {
// Returns true if there are running events. // Returns true if there are running events.
bool HasInflightEvent() const; bool HasInflightEvent() const;
// Processes all tasks in |task_queue_|. // Processes all events in |queue_|.
void ProcessTasks(); void ProcessEvents();
// Start a single task.
void StartTask(std::unique_ptr<Task> task);
struct EventInfo { struct EventInfo {
EventInfo(base::TimeTicks expiration_time, EventInfo(base::TimeTicks expiration_time,
...@@ -195,13 +205,13 @@ class MODULES_EXPORT ServiceWorkerEventQueue { ...@@ -195,13 +205,13 @@ class MODULES_EXPORT ServiceWorkerEventQueue {
// StartEvent() is called. // StartEvent() is called.
bool did_idle_timeout_ = false; bool did_idle_timeout_ = false;
// Tasks that are to be run in the next ProcessTasks() call. // Event queue to where all events are enqueued.
Deque<std::unique_ptr<Task>> task_queue_; Deque<std::unique_ptr<Event>> queue_;
// Set to true during running ProcessTasks(). This is used for avoiding to // Set to true during running ProcessEvents(). This is used for avoiding to
// invoke |idle_callback_| or to re-enter ProcessTasks() when calling // invoke |idle_callback_| or to re-enter ProcessEvents() when calling
// ProcessTasks(). // ProcessEvents().
bool processing_tasks_ = false; bool processing_events_ = false;
// The number of the living StayAwakeToken. See also class comments. // The number of the living StayAwakeToken. See also class comments.
int num_of_stay_awake_tokens_ = 0; int num_of_stay_awake_tokens_ = 0;
......
...@@ -32,31 +32,48 @@ class MockEvent { ...@@ -32,31 +32,48 @@ class MockEvent {
bool Started() const { return event_id_.has_value(); } bool Started() const { return event_id_.has_value(); }
std::unique_ptr<ServiceWorkerEventQueue::Task> CreateTask() { void EnqueueTo(ServiceWorkerEventQueue* event_queue) {
return std::make_unique<ServiceWorkerEventQueue::Task>( event_queue->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&MockEvent::Start, weak_factory_.GetWeakPtr()), WTF::Bind(&MockEvent::Start, weak_factory_.GetWeakPtr()),
WTF::Bind(&MockEvent::Abort, weak_factory_.GetWeakPtr()), WTF::Bind(&MockEvent::Abort, weak_factory_.GetWeakPtr()),
base::nullopt); base::nullopt);
} }
std::unique_ptr<ServiceWorkerEventQueue::Task> CreatePendingTask() { void EnqueuePendingTo(ServiceWorkerEventQueue* event_queue) {
return std::make_unique<ServiceWorkerEventQueue::Task>( event_queue->EnqueuePending(
ServiceWorkerEventQueue::Task::Type::Pending,
WTF::Bind(&MockEvent::Start, weak_factory_.GetWeakPtr()), WTF::Bind(&MockEvent::Start, weak_factory_.GetWeakPtr()),
WTF::Bind(&MockEvent::Abort, weak_factory_.GetWeakPtr()), WTF::Bind(&MockEvent::Abort, weak_factory_.GetWeakPtr()),
base::nullopt); base::nullopt);
} }
std::unique_ptr<ServiceWorkerEventQueue::Task> CreateTaskWithCustomTimeout( void EnqueueWithCustomTimeoutTo(ServiceWorkerEventQueue* event_queue,
base::TimeDelta custom_timeout) { base::TimeDelta custom_timeout) {
return std::make_unique<ServiceWorkerEventQueue::Task>( event_queue->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&MockEvent::Start, weak_factory_.GetWeakPtr()), WTF::Bind(&MockEvent::Start, weak_factory_.GetWeakPtr()),
WTF::Bind(&MockEvent::Abort, weak_factory_.GetWeakPtr()), WTF::Bind(&MockEvent::Abort, weak_factory_.GetWeakPtr()),
custom_timeout); custom_timeout);
} }
void EnqueuePendingDispatchingEventTo(ServiceWorkerEventQueue* event_queue,
String tag,
Vector<String>* out_tags) {
event_queue->EnqueuePending(
WTF::Bind(
[](ServiceWorkerEventQueue* event_queue, MockEvent* event,
String tag, Vector<String>* out_tags, int /* event id */) {
event->EnqueueTo(event_queue);
EXPECT_FALSE(event_queue->did_idle_timeout());
// Event dispatched inside of a pending event should not run
// immediately.
EXPECT_FALSE(event->Started());
EXPECT_FALSE(event->status().has_value());
out_tags->emplace_back(std::move(tag));
},
WTF::Unretained(event_queue), WTF::Unretained(this), std::move(tag),
WTF::Unretained(out_tags)),
base::DoNothing(), base::nullopt);
}
private: private:
void Start(int event_id) { void Start(int event_id) {
EXPECT_FALSE(Started()); EXPECT_FALSE(Started());
...@@ -79,29 +96,6 @@ base::RepeatingClosure CreateReceiverWithCalledFlag(bool* out_is_called) { ...@@ -79,29 +96,6 @@ base::RepeatingClosure CreateReceiverWithCalledFlag(bool* out_is_called) {
WTF::Unretained(out_is_called)); WTF::Unretained(out_is_called));
} }
std::unique_ptr<ServiceWorkerEventQueue::Task>
CreatePendingTaskDispatchingEvent(ServiceWorkerEventQueue* event_queue,
MockEvent* event,
String tag,
Vector<String>* out_tags) {
return std::make_unique<ServiceWorkerEventQueue::Task>(
ServiceWorkerEventQueue::Task::Type::Pending,
WTF::Bind(
[](ServiceWorkerEventQueue* event_queue, MockEvent* event, String tag,
Vector<String>* out_tags, int /* event id */) {
event_queue->PushTask(event->CreateTask());
EXPECT_FALSE(event_queue->did_idle_timeout());
// Event dispatched inside of a pending task should not run
// immediately.
EXPECT_FALSE(event->Started());
EXPECT_FALSE(event->status().has_value());
out_tags->emplace_back(std::move(tag));
},
WTF::Unretained(event_queue), WTF::Unretained(event), std::move(tag),
WTF::Unretained(out_tags)),
base::DoNothing(), base::nullopt);
}
} // namespace } // namespace
using StayAwakeToken = ServiceWorkerEventQueue::StayAwakeToken; using StayAwakeToken = ServiceWorkerEventQueue::StayAwakeToken;
...@@ -133,49 +127,49 @@ TEST_F(ServiceWorkerEventQueueTest, IdleTimer) { ...@@ -133,49 +127,49 @@ TEST_F(ServiceWorkerEventQueueTest, IdleTimer) {
base::TimeDelta::FromSeconds(1); base::TimeDelta::FromSeconds(1);
bool is_idle = false; bool is_idle = false;
ServiceWorkerEventQueue timer(CreateReceiverWithCalledFlag(&is_idle), ServiceWorkerEventQueue event_queue(CreateReceiverWithCalledFlag(&is_idle),
task_runner()->GetMockTickClock()); task_runner()->GetMockTickClock());
task_runner()->FastForwardBy(kIdleInterval); task_runner()->FastForwardBy(kIdleInterval);
// Nothing should happen since the event queue has not started yet. // Nothing should happen since the event queue has not started yet.
EXPECT_FALSE(is_idle); EXPECT_FALSE(is_idle);
timer.Start(); event_queue.Start();
task_runner()->FastForwardBy(kIdleInterval); task_runner()->FastForwardBy(kIdleInterval);
// |idle_callback| should be fired since there is no event. // |idle_callback| should be fired since there is no event.
EXPECT_TRUE(is_idle); EXPECT_TRUE(is_idle);
is_idle = false; is_idle = false;
MockEvent event1; MockEvent event1;
timer.PushTask(event1.CreateTask()); event1.EnqueueTo(&event_queue);
task_runner()->FastForwardBy(kIdleInterval); task_runner()->FastForwardBy(kIdleInterval);
// Nothing happens since there is an inflight event. // Nothing happens since there is an inflight event.
EXPECT_FALSE(is_idle); EXPECT_FALSE(is_idle);
MockEvent event2; MockEvent event2;
timer.PushTask(event2.CreateTask()); event2.EnqueueTo(&event_queue);
task_runner()->FastForwardBy(kIdleInterval); task_runner()->FastForwardBy(kIdleInterval);
// Nothing happens since there are two inflight events. // Nothing happens since there are two inflight events.
EXPECT_FALSE(is_idle); EXPECT_FALSE(is_idle);
timer.EndEvent(event2.event_id()); event_queue.EndEvent(event2.event_id());
task_runner()->FastForwardBy(kIdleInterval); task_runner()->FastForwardBy(kIdleInterval);
// Nothing happens since there is an inflight event. // Nothing happens since there is an inflight event.
EXPECT_FALSE(is_idle); EXPECT_FALSE(is_idle);
timer.EndEvent(event1.event_id()); event_queue.EndEvent(event1.event_id());
task_runner()->FastForwardBy(kIdleInterval); task_runner()->FastForwardBy(kIdleInterval);
// |idle_callback| should be fired. // |idle_callback| should be fired.
EXPECT_TRUE(is_idle); EXPECT_TRUE(is_idle);
is_idle = false; is_idle = false;
MockEvent event3; MockEvent event3;
timer.PushTask(event3.CreateTask()); event3.EnqueueTo(&event_queue);
task_runner()->FastForwardBy(kIdleInterval); task_runner()->FastForwardBy(kIdleInterval);
// Nothing happens since there is an inflight event. // Nothing happens since there is an inflight event.
EXPECT_FALSE(is_idle); EXPECT_FALSE(is_idle);
std::unique_ptr<StayAwakeToken> token = timer.CreateStayAwakeToken(); std::unique_ptr<StayAwakeToken> token = event_queue.CreateStayAwakeToken();
timer.EndEvent(event3.event_id()); event_queue.EndEvent(event3.event_id());
task_runner()->FastForwardBy(kIdleInterval); task_runner()->FastForwardBy(kIdleInterval);
// Nothing happens since there is a living StayAwakeToken. // Nothing happens since there is a living StayAwakeToken.
EXPECT_FALSE(is_idle); EXPECT_FALSE(is_idle);
...@@ -195,11 +189,11 @@ TEST_F(ServiceWorkerEventQueueTest, InflightEventBeforeStart) { ...@@ -195,11 +189,11 @@ TEST_F(ServiceWorkerEventQueueTest, InflightEventBeforeStart) {
base::TimeDelta::FromSeconds(1); base::TimeDelta::FromSeconds(1);
bool is_idle = false; bool is_idle = false;
ServiceWorkerEventQueue timer(CreateReceiverWithCalledFlag(&is_idle), ServiceWorkerEventQueue event_queue(CreateReceiverWithCalledFlag(&is_idle),
task_runner()->GetMockTickClock()); task_runner()->GetMockTickClock());
MockEvent event; MockEvent event;
timer.PushTask(event.CreateTask()); event.EnqueueTo(&event_queue);
timer.Start(); event_queue.Start();
task_runner()->FastForwardBy(kIdleInterval); task_runner()->FastForwardBy(kIdleInterval);
// Nothing happens since there is an inflight event. // Nothing happens since there is an inflight event.
EXPECT_FALSE(is_idle); EXPECT_FALSE(is_idle);
...@@ -217,20 +211,20 @@ TEST_F(ServiceWorkerEventQueueTest, InflightEventBeforeStart) { ...@@ -217,20 +211,20 @@ TEST_F(ServiceWorkerEventQueueTest, InflightEventBeforeStart) {
// In the first UpdateStatus() the idle callback should be triggered. // In the first UpdateStatus() the idle callback should be triggered.
TEST_F(ServiceWorkerEventQueueTest, EventFinishedBeforeStart) { TEST_F(ServiceWorkerEventQueueTest, EventFinishedBeforeStart) {
bool is_idle = false; bool is_idle = false;
ServiceWorkerEventQueue timer(CreateReceiverWithCalledFlag(&is_idle), ServiceWorkerEventQueue event_queue(CreateReceiverWithCalledFlag(&is_idle),
task_runner()->GetMockTickClock()); task_runner()->GetMockTickClock());
// Start and finish an event before starting the timer. // Start and finish an event before starting the timer.
MockEvent event; MockEvent event;
timer.PushTask(event.CreateTask()); event.EnqueueTo(&event_queue);
task_runner()->FastForwardBy(base::TimeDelta::FromSeconds(1)); task_runner()->FastForwardBy(base::TimeDelta::FromSeconds(1));
timer.EndEvent(event.event_id()); event_queue.EndEvent(event.event_id());
// Move the time ticks to almost before |idle_time_| so that |idle_callback| // Move the time ticks to almost before |idle_time_| so that |idle_callback|
// will get called at the first update check. // will get called at the first update check.
task_runner()->FastForwardBy(ServiceWorkerEventQueue::kIdleDelay - task_runner()->FastForwardBy(ServiceWorkerEventQueue::kIdleDelay -
base::TimeDelta::FromSeconds(1)); base::TimeDelta::FromSeconds(1));
timer.Start(); event_queue.Start();
// Make sure the timer calls UpdateStatus(). // Make sure the timer calls UpdateStatus().
task_runner()->FastForwardBy(ServiceWorkerEventQueue::kUpdateInterval + task_runner()->FastForwardBy(ServiceWorkerEventQueue::kUpdateInterval +
...@@ -246,8 +240,8 @@ TEST_F(ServiceWorkerEventQueueTest, EventTimer) { ...@@ -246,8 +240,8 @@ TEST_F(ServiceWorkerEventQueueTest, EventTimer) {
event_queue.Start(); event_queue.Start();
MockEvent event1, event2; MockEvent event1, event2;
event_queue.PushTask(event1.CreateTask()); event1.EnqueueTo(&event_queue);
event_queue.PushTask(event2.CreateTask()); event2.EnqueueTo(&event_queue);
task_runner()->FastForwardBy(ServiceWorkerEventQueue::kUpdateInterval + task_runner()->FastForwardBy(ServiceWorkerEventQueue::kUpdateInterval +
base::TimeDelta::FromSeconds(1)); base::TimeDelta::FromSeconds(1));
...@@ -268,12 +262,12 @@ TEST_F(ServiceWorkerEventQueueTest, CustomTimeouts) { ...@@ -268,12 +262,12 @@ TEST_F(ServiceWorkerEventQueueTest, CustomTimeouts) {
task_runner()->GetMockTickClock()); task_runner()->GetMockTickClock());
event_queue.Start(); event_queue.Start();
MockEvent event1, event2; MockEvent event1, event2;
event_queue.PushTask(event1.CreateTaskWithCustomTimeout( event1.EnqueueWithCustomTimeoutTo(&event_queue,
ServiceWorkerEventQueue::kUpdateInterval - ServiceWorkerEventQueue::kUpdateInterval -
base::TimeDelta::FromSeconds(1))); base::TimeDelta::FromSeconds(1));
event_queue.PushTask(event2.CreateTaskWithCustomTimeout( event2.EnqueueWithCustomTimeoutTo(
ServiceWorkerEventQueue::kUpdateInterval * 2 - &event_queue, ServiceWorkerEventQueue::kUpdateInterval * 2 -
base::TimeDelta::FromSeconds(1))); base::TimeDelta::FromSeconds(1));
task_runner()->FastForwardBy(ServiceWorkerEventQueue::kUpdateInterval + task_runner()->FastForwardBy(ServiceWorkerEventQueue::kUpdateInterval +
base::TimeDelta::FromSeconds(1)); base::TimeDelta::FromSeconds(1));
...@@ -297,7 +291,7 @@ TEST_F(ServiceWorkerEventQueueTest, BecomeIdleAfterAbort) { ...@@ -297,7 +291,7 @@ TEST_F(ServiceWorkerEventQueueTest, BecomeIdleAfterAbort) {
event_queue.Start(); event_queue.Start();
MockEvent event; MockEvent event;
event_queue.PushTask(event.CreateTask()); event.EnqueueTo(&event_queue);
task_runner()->FastForwardBy(ServiceWorkerEventQueue::kEventTimeout + task_runner()->FastForwardBy(ServiceWorkerEventQueue::kEventTimeout +
ServiceWorkerEventQueue::kUpdateInterval + ServiceWorkerEventQueue::kUpdateInterval +
base::TimeDelta::FromSeconds(1)); base::TimeDelta::FromSeconds(1));
...@@ -315,8 +309,9 @@ TEST_F(ServiceWorkerEventQueueTest, AbortAllOnDestruction) { ...@@ -315,8 +309,9 @@ TEST_F(ServiceWorkerEventQueueTest, AbortAllOnDestruction) {
task_runner()->GetMockTickClock()); task_runner()->GetMockTickClock());
event_queue.Start(); event_queue.Start();
event_queue.PushTask(event1.CreateTask()); event1.EnqueueTo(&event_queue);
event_queue.PushTask(event2.CreateTask()); event2.EnqueueTo(&event_queue);
task_runner()->FastForwardBy(ServiceWorkerEventQueue::kUpdateInterval + task_runner()->FastForwardBy(ServiceWorkerEventQueue::kUpdateInterval +
base::TimeDelta::FromSeconds(1)); base::TimeDelta::FromSeconds(1));
...@@ -342,12 +337,12 @@ TEST_F(ServiceWorkerEventQueueTest, PushPendingTask) { ...@@ -342,12 +337,12 @@ TEST_F(ServiceWorkerEventQueueTest, PushPendingTask) {
EXPECT_TRUE(event_queue.did_idle_timeout()); EXPECT_TRUE(event_queue.did_idle_timeout());
MockEvent pending_event; MockEvent pending_event;
event_queue.PushTask(pending_event.CreatePendingTask()); pending_event.EnqueuePendingTo(&event_queue);
EXPECT_FALSE(pending_event.Started()); EXPECT_FALSE(pending_event.Started());
// Start a new event. PushTask() should run the pending tasks. // Start a new event. PushTask() should run the pending tasks.
MockEvent event; MockEvent event;
event_queue.PushTask(event.CreateTask()); event.EnqueueTo(&event_queue);
EXPECT_FALSE(event_queue.did_idle_timeout()); EXPECT_FALSE(event_queue.did_idle_timeout());
EXPECT_TRUE(pending_event.Started()); EXPECT_TRUE(pending_event.Started());
} }
...@@ -363,15 +358,13 @@ TEST_F(ServiceWorkerEventQueueTest, RunPendingTasksWithZeroIdleTimerDelay) { ...@@ -363,15 +358,13 @@ TEST_F(ServiceWorkerEventQueueTest, RunPendingTasksWithZeroIdleTimerDelay) {
MockEvent event1, event2; MockEvent event1, event2;
Vector<String> handled_tasks; Vector<String> handled_tasks;
event_queue.PushTask(CreatePendingTaskDispatchingEvent(&event_queue, &event1, event1.EnqueuePendingDispatchingEventTo(&event_queue, "1", &handled_tasks);
"1", &handled_tasks)); event2.EnqueuePendingDispatchingEventTo(&event_queue, "2", &handled_tasks);
event_queue.PushTask(CreatePendingTaskDispatchingEvent(&event_queue, &event2,
"2", &handled_tasks));
EXPECT_TRUE(handled_tasks.IsEmpty()); EXPECT_TRUE(handled_tasks.IsEmpty());
// Start a new event. PushTask() should run the pending tasks. // Start a new event. PushTask() should run the pending tasks.
MockEvent event; MockEvent event;
event_queue.PushTask(event.CreateTask()); event.EnqueueTo(&event_queue);
EXPECT_FALSE(event_queue.did_idle_timeout()); EXPECT_FALSE(event_queue.did_idle_timeout());
ASSERT_EQ(2u, handled_tasks.size()); ASSERT_EQ(2u, handled_tasks.size());
EXPECT_EQ("1", handled_tasks[0]); EXPECT_EQ("1", handled_tasks[0]);
...@@ -400,7 +393,7 @@ TEST_F(ServiceWorkerEventQueueTest, SetIdleTimerDelayToZero) { ...@@ -400,7 +393,7 @@ TEST_F(ServiceWorkerEventQueueTest, SetIdleTimerDelayToZero) {
task_runner()->GetMockTickClock()); task_runner()->GetMockTickClock());
event_queue.Start(); event_queue.Start();
MockEvent event; MockEvent event;
event_queue.PushTask(event.CreateTask()); event.EnqueueTo(&event_queue);
event_queue.SetIdleTimerDelayToZero(); event_queue.SetIdleTimerDelayToZero();
// Nothing happens since there is an inflight event. // Nothing happens since there is an inflight event.
EXPECT_FALSE(is_idle); EXPECT_FALSE(is_idle);
...@@ -416,8 +409,8 @@ TEST_F(ServiceWorkerEventQueueTest, SetIdleTimerDelayToZero) { ...@@ -416,8 +409,8 @@ TEST_F(ServiceWorkerEventQueueTest, SetIdleTimerDelayToZero) {
task_runner()->GetMockTickClock()); task_runner()->GetMockTickClock());
event_queue.Start(); event_queue.Start();
MockEvent event1, event2; MockEvent event1, event2;
event_queue.PushTask(event1.CreateTask()); event1.EnqueueTo(&event_queue);
event_queue.PushTask(event2.CreateTask()); event2.EnqueueTo(&event_queue);
event_queue.SetIdleTimerDelayToZero(); event_queue.SetIdleTimerDelayToZero();
// Nothing happens since there are two inflight events. // Nothing happens since there are two inflight events.
EXPECT_FALSE(is_idle); EXPECT_FALSE(is_idle);
......
...@@ -1305,11 +1305,9 @@ void ServiceWorkerGlobalScope::OnRequestedTermination(bool will_be_terminated) { ...@@ -1305,11 +1305,9 @@ void ServiceWorkerGlobalScope::OnRequestedTermination(bool will_be_terminated) {
// Push a dummy task to run all of queued tasks. This updates the // Push a dummy task to run all of queued tasks. This updates the
// idle timer too. // idle timer too.
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(WTF::Bind(&ServiceWorkerEventQueue::EndEvent,
ServiceWorkerEventQueue::Task::Type::Normal, WTF::Unretained(event_queue_.get())),
WTF::Bind(&ServiceWorkerEventQueue::EndEvent, base::DoNothing(), base::nullopt);
WTF::Unretained(event_queue_.get())),
base::DoNothing(), base::nullopt));
} }
bool ServiceWorkerGlobalScope::RequestedTermination() const { bool ServiceWorkerGlobalScope::RequestedTermination() const {
...@@ -1440,13 +1438,21 @@ void ServiceWorkerGlobalScope::DispatchFetchEventForSubresource( ...@@ -1440,13 +1438,21 @@ void ServiceWorkerGlobalScope::DispatchFetchEventForSubresource(
RequestedTermination() ? "true" : "false"); RequestedTermination() ? "true" : "false");
network::mojom::blink::CrossOriginEmbedderPolicy requestor_coep = network::mojom::blink::CrossOriginEmbedderPolicy requestor_coep =
controller_receivers_.current_context(); controller_receivers_.current_context();
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( if (RequestedTermination()) {
RequestedTermination() ? ServiceWorkerEventQueue::Task::Type::Pending event_queue_->EnqueuePending(
: ServiceWorkerEventQueue::Task::Type::Normal, WTF::Bind(&ServiceWorkerGlobalScope::StartFetchEvent,
WTF::Bind(&ServiceWorkerGlobalScope::StartFetchEvent, WrapWeakPersistent(this), std::move(params),
WrapWeakPersistent(this), std::move(params), requestor_coep, std::move(requestor_coep), std::move(response_callback),
std::move(response_callback), std::move(callback)), std::move(callback)),
CreateAbortCallback(&fetch_event_callbacks_), base::nullopt)); CreateAbortCallback(&fetch_event_callbacks_), base::nullopt);
} else {
event_queue_->EnqueueNormal(
WTF::Bind(&ServiceWorkerGlobalScope::StartFetchEvent,
WrapWeakPersistent(this), std::move(params),
std::move(requestor_coep), std::move(response_callback),
std::move(callback)),
CreateAbortCallback(&fetch_event_callbacks_), base::nullopt);
}
} }
void ServiceWorkerGlobalScope::Clone( void ServiceWorkerGlobalScope::Clone(
...@@ -1514,13 +1520,12 @@ void ServiceWorkerGlobalScope::ResumeEvaluation() { ...@@ -1514,13 +1520,12 @@ void ServiceWorkerGlobalScope::ResumeEvaluation() {
void ServiceWorkerGlobalScope::DispatchInstallEvent( void ServiceWorkerGlobalScope::DispatchInstallEvent(
DispatchInstallEventCallback callback) { DispatchInstallEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartInstallEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartInstallEvent,
WrapWeakPersistent(this), std::move(callback)), WrapWeakPersistent(this), std::move(callback)),
CreateAbortCallback(&install_event_callbacks_, CreateAbortCallback(&install_event_callbacks_,
false /* has_fetch_handler */), false /* has_fetch_handler */),
base::nullopt)); base::nullopt);
} }
void ServiceWorkerGlobalScope::StartInstallEvent( void ServiceWorkerGlobalScope::StartInstallEvent(
...@@ -1546,11 +1551,10 @@ void ServiceWorkerGlobalScope::StartInstallEvent( ...@@ -1546,11 +1551,10 @@ void ServiceWorkerGlobalScope::StartInstallEvent(
void ServiceWorkerGlobalScope::DispatchActivateEvent( void ServiceWorkerGlobalScope::DispatchActivateEvent(
DispatchActivateEventCallback callback) { DispatchActivateEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartActivateEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartActivateEvent,
WrapWeakPersistent(this), std::move(callback)), WrapWeakPersistent(this), std::move(callback)),
CreateAbortCallback(&activate_event_callbacks_), base::nullopt)); CreateAbortCallback(&activate_event_callbacks_), base::nullopt);
} }
void ServiceWorkerGlobalScope::StartActivateEvent( void ServiceWorkerGlobalScope::StartActivateEvent(
...@@ -1575,13 +1579,12 @@ void ServiceWorkerGlobalScope::DispatchBackgroundFetchAbortEvent( ...@@ -1575,13 +1579,12 @@ void ServiceWorkerGlobalScope::DispatchBackgroundFetchAbortEvent(
mojom::blink::BackgroundFetchRegistrationPtr registration, mojom::blink::BackgroundFetchRegistrationPtr registration,
DispatchBackgroundFetchAbortEventCallback callback) { DispatchBackgroundFetchAbortEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartBackgroundFetchAbortEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartBackgroundFetchAbortEvent,
WrapWeakPersistent(this), std::move(registration), WrapWeakPersistent(this), std::move(registration),
std::move(callback)), std::move(callback)),
CreateAbortCallback(&background_fetch_abort_event_callbacks_), CreateAbortCallback(&background_fetch_abort_event_callbacks_),
base::nullopt)); base::nullopt);
} }
void ServiceWorkerGlobalScope::StartBackgroundFetchAbortEvent( void ServiceWorkerGlobalScope::StartBackgroundFetchAbortEvent(
...@@ -1619,13 +1622,12 @@ void ServiceWorkerGlobalScope::DispatchBackgroundFetchClickEvent( ...@@ -1619,13 +1622,12 @@ void ServiceWorkerGlobalScope::DispatchBackgroundFetchClickEvent(
mojom::blink::BackgroundFetchRegistrationPtr registration, mojom::blink::BackgroundFetchRegistrationPtr registration,
DispatchBackgroundFetchClickEventCallback callback) { DispatchBackgroundFetchClickEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartBackgroundFetchClickEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartBackgroundFetchClickEvent,
WrapWeakPersistent(this), std::move(registration), WrapWeakPersistent(this), std::move(registration),
std::move(callback)), std::move(callback)),
CreateAbortCallback(&background_fetch_click_event_callbacks_), CreateAbortCallback(&background_fetch_click_event_callbacks_),
base::nullopt)); base::nullopt);
} }
void ServiceWorkerGlobalScope::StartBackgroundFetchClickEvent( void ServiceWorkerGlobalScope::StartBackgroundFetchClickEvent(
...@@ -1658,13 +1660,12 @@ void ServiceWorkerGlobalScope::DispatchBackgroundFetchFailEvent( ...@@ -1658,13 +1660,12 @@ void ServiceWorkerGlobalScope::DispatchBackgroundFetchFailEvent(
mojom::blink::BackgroundFetchRegistrationPtr registration, mojom::blink::BackgroundFetchRegistrationPtr registration,
DispatchBackgroundFetchFailEventCallback callback) { DispatchBackgroundFetchFailEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartBackgroundFetchFailEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartBackgroundFetchFailEvent,
WrapWeakPersistent(this), std::move(registration), WrapWeakPersistent(this), std::move(registration),
std::move(callback)), std::move(callback)),
CreateAbortCallback(&background_fetch_fail_event_callbacks_), CreateAbortCallback(&background_fetch_fail_event_callbacks_),
base::nullopt)); base::nullopt);
} }
void ServiceWorkerGlobalScope::StartBackgroundFetchFailEvent( void ServiceWorkerGlobalScope::StartBackgroundFetchFailEvent(
...@@ -1703,13 +1704,11 @@ void ServiceWorkerGlobalScope::DispatchBackgroundFetchSuccessEvent( ...@@ -1703,13 +1704,11 @@ void ServiceWorkerGlobalScope::DispatchBackgroundFetchSuccessEvent(
mojom::blink::BackgroundFetchRegistrationPtr registration, mojom::blink::BackgroundFetchRegistrationPtr registration,
DispatchBackgroundFetchSuccessEventCallback callback) { DispatchBackgroundFetchSuccessEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartBackgroundFetchSuccessEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartBackgroundFetchSuccessEvent,
WrapWeakPersistent(this), std::move(registration), WrapWeakPersistent(this), std::move(registration),
std::move(callback)), std::move(callback)),
CreateAbortCallback(&background_fetched_event_callbacks_), CreateAbortCallback(&background_fetched_event_callbacks_), base::nullopt);
base::nullopt));
} }
void ServiceWorkerGlobalScope::StartBackgroundFetchSuccessEvent( void ServiceWorkerGlobalScope::StartBackgroundFetchSuccessEvent(
...@@ -1748,12 +1747,11 @@ void ServiceWorkerGlobalScope::DispatchExtendableMessageEvent( ...@@ -1748,12 +1747,11 @@ void ServiceWorkerGlobalScope::DispatchExtendableMessageEvent(
mojom::blink::ExtendableMessageEventPtr event, mojom::blink::ExtendableMessageEventPtr event,
DispatchExtendableMessageEventCallback callback) { DispatchExtendableMessageEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartExtendableMessageEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartExtendableMessageEvent,
WrapWeakPersistent(this), std::move(event), WrapWeakPersistent(this), std::move(event),
std::move(callback)), std::move(callback)),
CreateAbortCallback(&message_event_callbacks_), base::nullopt)); CreateAbortCallback(&message_event_callbacks_), base::nullopt);
} }
void ServiceWorkerGlobalScope::StartExtendableMessageEvent( void ServiceWorkerGlobalScope::StartExtendableMessageEvent(
...@@ -1779,13 +1777,12 @@ void ServiceWorkerGlobalScope::DispatchFetchEventForMainResource( ...@@ -1779,13 +1777,12 @@ void ServiceWorkerGlobalScope::DispatchFetchEventForMainResource(
DCHECK(IsContextThread()); DCHECK(IsContextThread());
// We can use kNone as a |requestor_coep| for the main resource because it // We can use kNone as a |requestor_coep| for the main resource because it
// must be the same origin. // must be the same origin.
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartFetchEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartFetchEvent,
WrapWeakPersistent(this), std::move(params), WrapWeakPersistent(this), std::move(params),
network::mojom::blink::CrossOriginEmbedderPolicy::kNone, network::mojom::blink::CrossOriginEmbedderPolicy::kNone,
std::move(response_callback), std::move(callback)), std::move(response_callback), std::move(callback)),
CreateAbortCallback(&fetch_event_callbacks_), base::nullopt)); CreateAbortCallback(&fetch_event_callbacks_), base::nullopt);
} }
void ServiceWorkerGlobalScope::DispatchNotificationClickEvent( void ServiceWorkerGlobalScope::DispatchNotificationClickEvent(
...@@ -1795,14 +1792,12 @@ void ServiceWorkerGlobalScope::DispatchNotificationClickEvent( ...@@ -1795,14 +1792,12 @@ void ServiceWorkerGlobalScope::DispatchNotificationClickEvent(
const String& reply, const String& reply,
DispatchNotificationClickEventCallback callback) { DispatchNotificationClickEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartNotificationClickEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartNotificationClickEvent,
WrapWeakPersistent(this), notification_id, WrapWeakPersistent(this), notification_id,
std::move(notification_data), action_index, reply, std::move(notification_data), action_index, reply,
std::move(callback)), std::move(callback)),
CreateAbortCallback(&notification_click_event_callbacks_), CreateAbortCallback(&notification_click_event_callbacks_), base::nullopt);
base::nullopt));
} }
void ServiceWorkerGlobalScope::StartNotificationClickEvent( void ServiceWorkerGlobalScope::StartNotificationClickEvent(
...@@ -1840,13 +1835,11 @@ void ServiceWorkerGlobalScope::DispatchNotificationCloseEvent( ...@@ -1840,13 +1835,11 @@ void ServiceWorkerGlobalScope::DispatchNotificationCloseEvent(
mojom::blink::NotificationDataPtr notification_data, mojom::blink::NotificationDataPtr notification_data,
DispatchNotificationCloseEventCallback callback) { DispatchNotificationCloseEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartNotificationCloseEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartNotificationCloseEvent,
WrapWeakPersistent(this), notification_id, WrapWeakPersistent(this), notification_id,
std::move(notification_data), std::move(callback)), std::move(notification_data), std::move(callback)),
CreateAbortCallback(&notification_close_event_callbacks_), CreateAbortCallback(&notification_close_event_callbacks_), base::nullopt);
base::nullopt));
} }
void ServiceWorkerGlobalScope::StartNotificationCloseEvent( void ServiceWorkerGlobalScope::StartNotificationCloseEvent(
...@@ -1878,13 +1871,12 @@ void ServiceWorkerGlobalScope::DispatchPushEvent( ...@@ -1878,13 +1871,12 @@ void ServiceWorkerGlobalScope::DispatchPushEvent(
const String& payload, const String& payload,
DispatchPushEventCallback callback) { DispatchPushEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartPushEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartPushEvent,
WrapWeakPersistent(this), std::move(payload), WrapWeakPersistent(this), std::move(payload),
std::move(callback)), std::move(callback)),
CreateAbortCallback(&push_event_callbacks_), CreateAbortCallback(&push_event_callbacks_),
base::TimeDelta::FromSeconds(mojom::blink::kPushEventTimeoutSeconds))); base::TimeDelta::FromSeconds(mojom::blink::kPushEventTimeoutSeconds));
} }
void ServiceWorkerGlobalScope::StartPushEvent( void ServiceWorkerGlobalScope::StartPushEvent(
...@@ -1911,13 +1903,12 @@ void ServiceWorkerGlobalScope::DispatchPushSubscriptionChangeEvent( ...@@ -1911,13 +1903,12 @@ void ServiceWorkerGlobalScope::DispatchPushSubscriptionChangeEvent(
mojom::blink::PushSubscriptionPtr new_subscription, mojom::blink::PushSubscriptionPtr new_subscription,
DispatchPushSubscriptionChangeEventCallback callback) { DispatchPushSubscriptionChangeEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartPushSubscriptionChangeEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartPushSubscriptionChangeEvent,
WrapWeakPersistent(this), std::move(old_subscription), WrapWeakPersistent(this), std::move(old_subscription),
std::move(new_subscription), std::move(callback)), std::move(new_subscription), std::move(callback)),
CreateAbortCallback(&push_subscription_change_event_callbacks_), CreateAbortCallback(&push_subscription_change_event_callbacks_),
base::TimeDelta::FromSeconds(mojom::blink::kPushEventTimeoutSeconds))); base::TimeDelta::FromSeconds(mojom::blink::kPushEventTimeoutSeconds));
} }
void ServiceWorkerGlobalScope::StartPushSubscriptionChangeEvent( void ServiceWorkerGlobalScope::StartPushSubscriptionChangeEvent(
...@@ -1948,12 +1939,11 @@ void ServiceWorkerGlobalScope::DispatchSyncEvent( ...@@ -1948,12 +1939,11 @@ void ServiceWorkerGlobalScope::DispatchSyncEvent(
base::TimeDelta timeout, base::TimeDelta timeout,
DispatchSyncEventCallback callback) { DispatchSyncEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartSyncEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartSyncEvent,
WrapWeakPersistent(this), std::move(tag), last_chance, WrapWeakPersistent(this), std::move(tag), last_chance,
std::move(callback)), std::move(callback)),
CreateAbortCallback(&sync_event_callbacks_), timeout)); CreateAbortCallback(&sync_event_callbacks_), timeout);
} }
void ServiceWorkerGlobalScope::StartSyncEvent( void ServiceWorkerGlobalScope::StartSyncEvent(
...@@ -1981,11 +1971,10 @@ void ServiceWorkerGlobalScope::DispatchPeriodicSyncEvent( ...@@ -1981,11 +1971,10 @@ void ServiceWorkerGlobalScope::DispatchPeriodicSyncEvent(
base::TimeDelta timeout, base::TimeDelta timeout,
DispatchPeriodicSyncEventCallback callback) { DispatchPeriodicSyncEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartPeriodicSyncEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartPeriodicSyncEvent,
WrapWeakPersistent(this), std::move(tag), std::move(callback)), WrapWeakPersistent(this), std::move(tag), std::move(callback)),
CreateAbortCallback(&periodic_sync_event_callbacks_), timeout)); CreateAbortCallback(&periodic_sync_event_callbacks_), timeout);
} }
void ServiceWorkerGlobalScope::StartPeriodicSyncEvent( void ServiceWorkerGlobalScope::StartPeriodicSyncEvent(
...@@ -2012,12 +2001,11 @@ void ServiceWorkerGlobalScope::DispatchAbortPaymentEvent( ...@@ -2012,12 +2001,11 @@ void ServiceWorkerGlobalScope::DispatchAbortPaymentEvent(
response_callback, response_callback,
DispatchAbortPaymentEventCallback callback) { DispatchAbortPaymentEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartAbortPaymentEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartAbortPaymentEvent,
WrapWeakPersistent(this), std::move(response_callback), WrapWeakPersistent(this), std::move(response_callback),
std::move(callback)), std::move(callback)),
CreateAbortCallback(&abort_payment_event_callbacks_), base::nullopt)); CreateAbortCallback(&abort_payment_event_callbacks_), base::nullopt);
} }
void ServiceWorkerGlobalScope::StartAbortPaymentEvent( void ServiceWorkerGlobalScope::StartAbortPaymentEvent(
...@@ -2057,12 +2045,11 @@ void ServiceWorkerGlobalScope::DispatchCanMakePaymentEvent( ...@@ -2057,12 +2045,11 @@ void ServiceWorkerGlobalScope::DispatchCanMakePaymentEvent(
response_callback, response_callback,
DispatchCanMakePaymentEventCallback callback) { DispatchCanMakePaymentEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartCanMakePaymentEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartCanMakePaymentEvent,
WrapWeakPersistent(this), std::move(event_data), WrapWeakPersistent(this), std::move(event_data),
std::move(response_callback), std::move(callback)), std::move(response_callback), std::move(callback)),
CreateAbortCallback(&can_make_payment_event_callbacks_), base::nullopt)); CreateAbortCallback(&can_make_payment_event_callbacks_), base::nullopt);
} }
void ServiceWorkerGlobalScope::StartCanMakePaymentEvent( void ServiceWorkerGlobalScope::StartCanMakePaymentEvent(
...@@ -2105,12 +2092,11 @@ void ServiceWorkerGlobalScope::DispatchPaymentRequestEvent( ...@@ -2105,12 +2092,11 @@ void ServiceWorkerGlobalScope::DispatchPaymentRequestEvent(
response_callback, response_callback,
DispatchPaymentRequestEventCallback callback) { DispatchPaymentRequestEventCallback callback) {
DCHECK(IsContextThread()); DCHECK(IsContextThread());
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartPaymentRequestEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartPaymentRequestEvent,
WrapWeakPersistent(this), std::move(event_data), WrapWeakPersistent(this), std::move(event_data),
std::move(response_callback), std::move(callback)), std::move(response_callback), std::move(callback)),
CreateAbortCallback(&payment_request_event_callbacks_), base::nullopt)); CreateAbortCallback(&payment_request_event_callbacks_), base::nullopt);
} }
void ServiceWorkerGlobalScope::StartPaymentRequestEvent( void ServiceWorkerGlobalScope::StartPaymentRequestEvent(
...@@ -2166,12 +2152,11 @@ void ServiceWorkerGlobalScope::StartPaymentRequestEvent( ...@@ -2166,12 +2152,11 @@ void ServiceWorkerGlobalScope::StartPaymentRequestEvent(
void ServiceWorkerGlobalScope::DispatchCookieChangeEvent( void ServiceWorkerGlobalScope::DispatchCookieChangeEvent(
network::mojom::blink::CookieChangeInfoPtr change, network::mojom::blink::CookieChangeInfoPtr change,
DispatchCookieChangeEventCallback callback) { DispatchCookieChangeEventCallback callback) {
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartCookieChangeEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartCookieChangeEvent,
WrapWeakPersistent(this), std::move(change), WrapWeakPersistent(this), std::move(change),
std::move(callback)), std::move(callback)),
CreateAbortCallback(&cookie_change_event_callbacks_), base::nullopt)); CreateAbortCallback(&cookie_change_event_callbacks_), base::nullopt);
} }
void ServiceWorkerGlobalScope::StartCookieChangeEvent( void ServiceWorkerGlobalScope::StartCookieChangeEvent(
...@@ -2207,11 +2192,10 @@ void ServiceWorkerGlobalScope::StartCookieChangeEvent( ...@@ -2207,11 +2192,10 @@ void ServiceWorkerGlobalScope::StartCookieChangeEvent(
void ServiceWorkerGlobalScope::DispatchContentDeleteEvent( void ServiceWorkerGlobalScope::DispatchContentDeleteEvent(
const String& id, const String& id,
DispatchContentDeleteEventCallback callback) { DispatchContentDeleteEventCallback callback) {
event_queue_->PushTask(std::make_unique<ServiceWorkerEventQueue::Task>( event_queue_->EnqueueNormal(
ServiceWorkerEventQueue::Task::Type::Normal,
WTF::Bind(&ServiceWorkerGlobalScope::StartContentDeleteEvent, WTF::Bind(&ServiceWorkerGlobalScope::StartContentDeleteEvent,
WrapWeakPersistent(this), id, std::move(callback)), WrapWeakPersistent(this), id, std::move(callback)),
CreateAbortCallback(&content_delete_callbacks_), base::nullopt)); CreateAbortCallback(&content_delete_callbacks_), base::nullopt);
} }
void ServiceWorkerGlobalScope::StartContentDeleteEvent( void ServiceWorkerGlobalScope::StartContentDeleteEvent(
......
...@@ -458,7 +458,7 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final ...@@ -458,7 +458,7 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
void NoteRespondedToFetchEvent(const KURL& request_url); void NoteRespondedToFetchEvent(const KURL& request_url);
// Dispatches the event synchronously. Enqueued by Dispatch*Event methods to // Dispatches the event synchronously. Enqueued by Dispatch*Event methods to
// the timeout timer, and executed immediately or sometimes later. // the event queue, and executed immediately or sometimes later.
void StartFetchEvent( void StartFetchEvent(
mojom::blink::DispatchFetchEventParamsPtr params, mojom::blink::DispatchFetchEventParamsPtr params,
network::mojom::blink::CrossOriginEmbedderPolicy requestor_coep, network::mojom::blink::CrossOriginEmbedderPolicy requestor_coep,
...@@ -641,8 +641,8 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final ...@@ -641,8 +641,8 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
// optimizations in these cases. // optimizations in these cases.
HashMap<KURL, int> unresponded_fetch_event_counts_; HashMap<KURL, int> unresponded_fetch_event_counts_;
// Timer triggered when the service worker considers it should be stopped or // ServiceWorker event queue where all events are queued before
// an event should be aborted. // they are dispatched.
std::unique_ptr<ServiceWorkerEventQueue> event_queue_; std::unique_ptr<ServiceWorkerEventQueue> event_queue_;
// InitializeGlobalScope() pauses the top level script evaluation when this // InitializeGlobalScope() pauses the top level script evaluation when this
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment