Commit 7f6dea74 authored by momohatt's avatar momohatt Committed by Commit Bot

ServiceWorker: Make NextEventId() thread-safe

NextEventId(), which is used in ServiceWorkerTimeoutTimer is not thread-safe.
I fixed this problem by using AtomicSequenceNumber.
This ensures event ids dispatched to service workers in a process are unique in the process.

Bug: 869706
Change-Id: Ia19aa180007479387463d3d0725dae052da17632
Reviewed-on: https://chromium-review.googlesource.com/1158092
Commit-Queue: Momoko Hattori <momohatt@google.com>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579749}
parent 647f1a50
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "content/renderer/service_worker/service_worker_timeout_timer.h" #include "content/renderer/service_worker/service_worker_timeout_timer.h"
#include "base/atomic_sequence_num.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/time/default_tick_clock.h" #include "base/time/default_tick_clock.h"
#include "base/time/time.h" #include "base/time/time.h"
...@@ -17,9 +18,10 @@ namespace { ...@@ -17,9 +18,10 @@ namespace {
int NextEventId() { int NextEventId() {
// Event id should not start from zero since HashMap in Blink requires // Event id should not start from zero since HashMap in Blink requires
// non-zero keys. // non-zero keys.
static int s_next_event_id = 1; static base::AtomicSequenceNumber s_event_id_sequence;
CHECK_LT(s_next_event_id, std::numeric_limits<int>::max()); int next_event_id = s_event_id_sequence.GetNext() + 1;
return s_next_event_id++; CHECK_LT(next_event_id, std::numeric_limits<int>::max());
return next_event_id;
} }
} // namespace } // namespace
......
...@@ -51,8 +51,9 @@ class CONTENT_EXPORT ServiceWorkerTimeoutTimer { ...@@ -51,8 +51,9 @@ class CONTENT_EXPORT ServiceWorkerTimeoutTimer {
~ServiceWorkerTimeoutTimer(); ~ServiceWorkerTimeoutTimer();
// StartEvent() should be called at the beginning of an event. It returns an // StartEvent() should be called at the beginning of an event. It returns an
// event id. The event id should be passed to EndEvent() when the event has // event id, which is unique among threads in the same process.
// finished. If there are pending tasks queued by PushPendingTask(), they will // The event id should be passed to EndEvent() when the event has finished.
// If there are pending tasks queued by PushPendingTask(), they will
// run in order synchronouslly in StartEvent(). // run in order synchronouslly in StartEvent().
// See the class comment to know when |abort_callback| runs. // See the class comment to know when |abort_callback| runs.
int StartEvent(base::OnceCallback<void(int /* event_id */)> abort_callback); int StartEvent(base::OnceCallback<void(int /* event_id */)> abort_callback);
......
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