Implement android_webview url intercepting.
[chromium-blink-merge.git] / cc / scoped_thread_proxy.h
blob802046dcfa858a6dbe09b4d0896adac1b99f10f3
1 // Copyright 2011 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 CCScopedThreadProxy_h
6 #define CCScopedThreadProxy_h
8 #include "CCThreadTask.h"
9 #include "base/threading/platform_thread.h"
10 #include <wtf/OwnPtr.h>
11 #include <wtf/PassOwnPtr.h>
12 #include <wtf/ThreadSafeRefCounted.h>
14 namespace cc {
16 // This class is a proxy used to post tasks to an target thread from any other thread. The proxy may be shut down at
17 // any point from the target thread after which no more tasks posted to the proxy will run. In other words, all
18 // tasks posted via a proxy are scoped to the lifecycle of the proxy. Use this when posting tasks to an object that
19 // might die with tasks in flight.
21 // The proxy must be created and shut down from the target thread, tasks may be posted from any thread.
23 // Implementation note: Unlike ScopedRunnableMethodFactory in Chromium, pending tasks are not cancelled by actually
24 // destroying the proxy. Instead each pending task holds a reference to the proxy to avoid maintaining an explicit
25 // list of outstanding tasks.
26 class CCScopedThreadProxy : public ThreadSafeRefCounted<CCScopedThreadProxy> {
27 public:
28 static PassRefPtr<CCScopedThreadProxy> create(CCThread* targetThread)
30 ASSERT(base::PlatformThread::CurrentId() == targetThread->threadID());
31 return adoptRef(new CCScopedThreadProxy(targetThread));
34 ~CCScopedThreadProxy();
36 // Can be called from any thread. Posts a task to the target thread that runs unless
37 // shutdown() is called before it runs.
38 void postTask(PassOwnPtr<CCThread::Task> task)
40 ref();
41 m_targetThread->postTask(createCCThreadTask(this, &CCScopedThreadProxy::runTaskIfNotShutdown, task));
44 void shutdown()
46 ASSERT(base::PlatformThread::CurrentId() == m_targetThread->threadID());
47 ASSERT(!m_shutdown);
48 m_shutdown = true;
51 private:
52 explicit CCScopedThreadProxy(CCThread* targetThread);
54 void runTaskIfNotShutdown(PassOwnPtr<CCThread::Task> popTask)
56 OwnPtr<CCThread::Task> task = popTask;
57 // If our shutdown flag is set, it's possible that m_targetThread has already been destroyed so don't
58 // touch it.
59 if (m_shutdown) {
60 deref();
61 return;
63 ASSERT(base::PlatformThread::CurrentId() == m_targetThread->threadID());
64 task->performTask();
65 deref();
68 CCThread* m_targetThread;
69 bool m_shutdown; // Only accessed on the target thread
74 #endif