blob: e695139d5b42613d1e8c2411ddae34f2b7ad15ce [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/callback.h"
#include "base/threading/thread.h"
#include "ios/web/public/web_thread.h"
namespace web {
class WebTestSuiteListener;
class WebThreadImpl : public WebThread, public base::Thread {
// Construct a WebThreadImpl with the supplied identifier. It is an error
// to construct a WebThreadImpl that already exists.
explicit WebThreadImpl(WebThread::ID identifier);
// Special constructor for the main (UI) thread and unittests. If a
// |message_loop| is provied, we use a dummy thread here since the main
// thread already exists.
WebThreadImpl(WebThread::ID identifier, base::MessageLoop* message_loop);
~WebThreadImpl() override;
bool Start();
bool StartWithOptions(const Options& options);
bool StartAndWaitForTesting();
// Creates and registers a TaskExecutor that facilitates posting tasks to a
// WebThread via //base/task/post_task.h.
static void CreateTaskExecutor();
// Unregister and delete the TaskExecutor after a test.
static void ResetTaskExecutorForTesting();
// Resets globals for |identifier|. Used in tests to clear global state that
// would otherwise leak to the next test. Globals are not otherwise fully
// cleaned up in ~WebThreadImpl() as there are subtle differences between
// UNINITIALIZED and SHUTDOWN state (e.g. globals.task_runners are kept around
// on shutdown). Must be called after ~WebThreadImpl() for the given
// |identifier|.
// Also unregisters and deletes the TaskExecutor.
static void ResetGlobalsForTesting(WebThread::ID identifier);
void Init() override;
void Run(base::RunLoop* run_loop) override;
void CleanUp() override;
// This class implements all the functionality of the public WebThread
// functions, but state is stored in the WebThreadImpl to keep
// the API cleaner. Therefore make WebThread a friend class.
friend class WebThread;
// The following are unique function names that makes it possible to tell
// the thread id from the callstack alone in crash dumps.
void UIThreadRun(base::RunLoop* run_loop);
void IOThreadRun(base::RunLoop* run_loop);
// Common initialization code for the constructors.
void Initialize();
// For testing.
friend class TestWebThreadBundle;
friend class TestWebThreadBundleImpl;
friend class WebTestSuiteListener;
// The identifier of this thread. Only one thread can exist with a given
// identifier at a given time.
ID identifier_;
} // namespace web