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.
5 #ifndef CONTENT_PUBLIC_CHILD_WORKER_THREAD_H_
6 #define CONTENT_PUBLIC_CHILD_WORKER_THREAD_H_
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "content/common/content_export.h"
14 // Utility functions for worker threads, for example service worker threads.
16 // This allows getting the thread IDs for service worker threads, then later
17 // posting tasks back to them.
18 class CONTENT_EXPORT WorkerThread
{
20 // Observes worker thread lifetime.
21 class CONTENT_EXPORT Observer
{
23 virtual ~Observer() {}
25 // Notifies the observer that the current worker thread is about to be
28 // The worker state may have already been destroyed. To observe that, use
29 // ContentRendererClient::WillDestroyServiceWorkerContextOnWorkerThread.
30 virtual void WillStopCurrentWorkerThread() {}
33 // Adds/removes an Observer. Observers are stored per-thread, so it is only
34 // valid to call these from a worker thread, and events will be dispatched on
35 // that worker's thread.
36 static void AddObserver(Observer
* observer
);
37 static void RemoveObserver(Observer
* observer
);
39 // Returns the thread ID for the current worker thread, or 0 if this is not a
40 // worker thread (for example, the render thread). Worker thread IDs will
42 static int GetCurrentId();
44 // Posts a task to the worker thread with ID |id|. ID must be > 0.
45 static void PostTask(int id
, const base::Closure
& task
);
49 DISALLOW_COPY_AND_ASSIGN(WorkerThread
);
52 } // namespace content
54 #endif // CONTENT_PUBLIC_CHILD_WORKER_THREAD_H_