diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-19 06:42:14 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-19 06:42:14 +0000 |
commit | 3dda5851a121c2d424db0a731b7314fee3fd5a3d (patch) | |
tree | df69fe7c90dd41846de91d01d5e3f706967c0a28 /webkit/child | |
parent | 400aec2701f8152512dacf97e21559c24f8e6101 (diff) | |
download | chromium_src-3dda5851a121c2d424db0a731b7314fee3fd5a3d.zip chromium_src-3dda5851a121c2d424db0a731b7314fee3fd5a3d.tar.gz chromium_src-3dda5851a121c2d424db0a731b7314fee3fd5a3d.tar.bz2 |
Move WorkerTaskRunner to content/child.
This moves it from webkit/child to content/child, since src/webkit is
dying and it is a trivial move. WorkerTaskRunner has no
dependencies besides base, and nobody in webkit/ is using it, just
content.
BUG=265753
TEST=content_shell and content_unittests
R=jochen@chromium.org,jam@chromium.org,darin@chromium.org
TBR=darin
Review URL: https://codereview.chromium.org/165373004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251975 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/child')
-rw-r--r-- | webkit/child/webkit_child.gyp | 2 | ||||
-rw-r--r-- | webkit/child/worker_task_runner.cc | 111 | ||||
-rw-r--r-- | webkit/child/worker_task_runner.h | 59 | ||||
-rw-r--r-- | webkit/child/worker_task_runner_unittest.cc | 56 |
4 files changed, 0 insertions, 228 deletions
diff --git a/webkit/child/webkit_child.gyp b/webkit/child/webkit_child.gyp index bf992f7..fd161e7 100644 --- a/webkit/child/webkit_child.gyp +++ b/webkit/child/webkit_child.gyp @@ -85,8 +85,6 @@ 'weburlrequest_extradata_impl.h', 'weburlresponse_extradata_impl.cc', 'weburlresponse_extradata_impl.h', - 'worker_task_runner.cc', - 'worker_task_runner.h', ], # TODO(jschuh): crbug.com/167187 fix size_t to int truncations. 'msvs_disabled_warnings': [ 4267 ], diff --git a/webkit/child/worker_task_runner.cc b/webkit/child/worker_task_runner.cc deleted file mode 100644 index c0d4359..0000000 --- a/webkit/child/worker_task_runner.cc +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (c) 2012 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/lazy_instance.h" -#include "base/logging.h" -#include "base/memory/scoped_ptr.h" -#include "base/observer_list.h" -#include "webkit/child/worker_task_runner.h" - -using blink::WebWorkerRunLoop; - -namespace { - -class RunClosureTask : public WebWorkerRunLoop::Task { - public: - RunClosureTask(const base::Closure& task) : task_(task) {} - virtual ~RunClosureTask() {} - virtual void Run() { - task_.Run(); - } - private: - base::Closure task_; -}; - -} // unnamed namespace - -namespace webkit_glue { - -struct WorkerTaskRunner::ThreadLocalState { - ThreadLocalState(int id, const WebWorkerRunLoop& loop) - : id_(id), run_loop_(loop) { - } - int id_; - WebWorkerRunLoop run_loop_; - ObserverList<WorkerTaskRunner::Observer> stop_observers_; -}; - -WorkerTaskRunner::WorkerTaskRunner() { - // Start worker ids at 1, 0 is reserved for the main thread. - int id = id_sequence_.GetNext(); - DCHECK(!id); -} - -bool WorkerTaskRunner::PostTask( - int id, const base::Closure& closure) { - DCHECK(id > 0); - base::AutoLock locker(loop_map_lock_); - IDToLoopMap::iterator found = loop_map_.find(id); - if (found == loop_map_.end()) - return false; - return found->second.postTask(new RunClosureTask(closure)); -} - -int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) { - base::AutoLock locker(loop_map_lock_); - IDToLoopMap::iterator it; - for (it = loop_map_.begin(); it != loop_map_.end(); ++it) - it->second.postTask(new RunClosureTask(closure)); - return static_cast<int>(loop_map_.size()); -} - -int WorkerTaskRunner::CurrentWorkerId() { - if (!current_tls_.Get()) - return 0; - return current_tls_.Get()->id_; -} - -WorkerTaskRunner* WorkerTaskRunner::Instance() { - static base::LazyInstance<WorkerTaskRunner>::Leaky - worker_task_runner = LAZY_INSTANCE_INITIALIZER; - return worker_task_runner.Pointer(); -} - -void WorkerTaskRunner::AddStopObserver(Observer* obs) { - DCHECK(CurrentWorkerId() > 0); - current_tls_.Get()->stop_observers_.AddObserver(obs); -} - -void WorkerTaskRunner::RemoveStopObserver(Observer* obs) { - DCHECK(CurrentWorkerId() > 0); - current_tls_.Get()->stop_observers_.RemoveObserver(obs); -} - -WorkerTaskRunner::~WorkerTaskRunner() { -} - -void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { - DCHECK(!current_tls_.Get()); - int id = id_sequence_.GetNext(); - current_tls_.Set(new ThreadLocalState(id, loop)); - - base::AutoLock locker_(loop_map_lock_); - loop_map_[id] = loop; -} - -void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { - DCHECK(current_tls_.Get()); - FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, - OnWorkerRunLoopStopped()); - { - base::AutoLock locker(loop_map_lock_); - DCHECK(loop_map_[CurrentWorkerId()] == loop); - loop_map_.erase(CurrentWorkerId()); - } - delete current_tls_.Get(); - current_tls_.Set(NULL); -} - -} // namespace webkit_glue diff --git a/webkit/child/worker_task_runner.h b/webkit/child/worker_task_runner.h deleted file mode 100644 index bc7bc64..0000000 --- a/webkit/child/worker_task_runner.h +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) 2012 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. - -#ifndef WEBKIT_CHILD_WORKER_TASK_RUNNER_H_ -#define WEBKIT_CHILD_WORKER_TASK_RUNNER_H_ - -#include <map> - -#include "base/atomic_sequence_num.h" -#include "base/callback_forward.h" -#include "base/synchronization/lock.h" -#include "base/threading/thread_local.h" -#include "third_party/WebKit/public/platform/WebWorkerRunLoop.h" -#include "webkit/child/webkit_child_export.h" - -namespace webkit_glue { - -class WEBKIT_CHILD_EXPORT WorkerTaskRunner { - public: - WorkerTaskRunner(); - - bool PostTask(int id, const base::Closure& task); - int PostTaskToAllThreads(const base::Closure& task); - int CurrentWorkerId(); - static WorkerTaskRunner* Instance(); - - class WEBKIT_CHILD_EXPORT Observer { - public: - virtual ~Observer() {} - virtual void OnWorkerRunLoopStopped() = 0; - }; - // Add/Remove an observer that will get notified when the current worker run - // loop is stopping. This observer will not get notified when other threads - // are stopping. It's only valid to call these on a worker thread. - void AddStopObserver(Observer* observer); - void RemoveStopObserver(Observer* observer); - - void OnWorkerRunLoopStarted(const blink::WebWorkerRunLoop& loop); - void OnWorkerRunLoopStopped(const blink::WebWorkerRunLoop& loop); - - private: - friend class WorkerTaskRunnerTest; - - typedef std::map<int, blink::WebWorkerRunLoop> IDToLoopMap; - - ~WorkerTaskRunner(); - - struct ThreadLocalState; - base::ThreadLocalPointer<ThreadLocalState> current_tls_; - - base::AtomicSequenceNumber id_sequence_; - IDToLoopMap loop_map_; - base::Lock loop_map_lock_; -}; - -} // namespace webkit_glue - -#endif // WEBKIT_CHILD_WORKER_TASK_RUNNER_H_ diff --git a/webkit/child/worker_task_runner_unittest.cc b/webkit/child/worker_task_runner_unittest.cc deleted file mode 100644 index 9bfb0e3..0000000 --- a/webkit/child/worker_task_runner_unittest.cc +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2011 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/logging.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "webkit/child/worker_task_runner.h" - -namespace webkit_glue { - -class WorkerTaskRunnerTest : public testing::Test { - public: - void FakeStart() { - task_runner_.OnWorkerRunLoopStarted(blink::WebWorkerRunLoop()); - } - void FakeStop() { - task_runner_.OnWorkerRunLoopStopped(blink::WebWorkerRunLoop()); - } - WorkerTaskRunner task_runner_; -}; - -class MockObserver : public WorkerTaskRunner::Observer { - public: - MOCK_METHOD0(OnWorkerRunLoopStopped, void()); - void RemoveSelfOnNotify() { - ON_CALL(*this, OnWorkerRunLoopStopped()).WillByDefault( - testing::Invoke(this, &MockObserver::RemoveSelf)); - } - void RemoveSelf() { - runner_->RemoveStopObserver(this); - } - WorkerTaskRunner* runner_; -}; - -TEST_F(WorkerTaskRunnerTest, BasicObservingAndWorkerId) { - ASSERT_EQ(0, task_runner_.CurrentWorkerId()); - MockObserver o; - EXPECT_CALL(o, OnWorkerRunLoopStopped()).Times(1); - FakeStart(); - task_runner_.AddStopObserver(&o); - ASSERT_LT(0, task_runner_.CurrentWorkerId()); - FakeStop(); -} - -TEST_F(WorkerTaskRunnerTest, CanRemoveSelfDuringNotification) { - MockObserver o; - o.RemoveSelfOnNotify(); - o.runner_ = &task_runner_; - EXPECT_CALL(o, OnWorkerRunLoopStopped()).Times(1); - FakeStart(); - task_runner_.AddStopObserver(&o); - FakeStop(); -} - -} |