1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "ActiveDOMCallback.h"
33
34 #include "ActiveDOMObject.h"
35 #include "ScriptExecutionContext.h"
36 #include <wtf/PassOwnPtr.h>
37 #include <wtf/ThreadingPrimitives.h>
38
39 namespace WebCore {
40
41 static void destroyOnContextThread(PassOwnPtr<ActiveDOMObjectCallbackImpl>);
42
43 class DestroyOnContextThreadTask : public ScriptExecutionContext::Task {
44 public:
create(PassOwnPtr<ActiveDOMObjectCallbackImpl> impl)45 static PassOwnPtr<DestroyOnContextThreadTask> create(PassOwnPtr<ActiveDOMObjectCallbackImpl> impl)
46 {
47 return adoptPtr(new DestroyOnContextThreadTask(impl));
48 }
49
performTask(ScriptExecutionContext *)50 virtual void performTask(ScriptExecutionContext*)
51 {
52 destroyOnContextThread(m_impl.release());
53 }
54
55 private:
DestroyOnContextThreadTask(PassOwnPtr<ActiveDOMObjectCallbackImpl> impl)56 DestroyOnContextThreadTask(PassOwnPtr<ActiveDOMObjectCallbackImpl> impl)
57 : m_impl(impl)
58 {
59 }
60
61 OwnPtr<ActiveDOMObjectCallbackImpl> m_impl;
62 };
63
64 class ActiveDOMObjectCallbackImpl : public ActiveDOMObject {
65 public:
ActiveDOMObjectCallbackImpl(ScriptExecutionContext * context)66 ActiveDOMObjectCallbackImpl(ScriptExecutionContext* context)
67 : ActiveDOMObject(context, this)
68 , m_suspended(false)
69 , m_stopped(false)
70 {
71 }
72
contextDestroyed()73 virtual void contextDestroyed()
74 {
75 MutexLocker locker(m_mutex);
76 ActiveDOMObject::contextDestroyed();
77 }
canSuspend() const78 virtual bool canSuspend() const { return false; }
suspend(ReasonForSuspension)79 virtual void suspend(ReasonForSuspension)
80 {
81 MutexLocker locker(m_mutex);
82 m_suspended = true;
83 }
resume()84 virtual void resume()
85 {
86 MutexLocker locker(m_mutex);
87 m_suspended = false;
88 }
stop()89 virtual void stop()
90 {
91 MutexLocker locker(m_mutex);
92 m_stopped = true;
93 }
canInvokeCallback()94 bool canInvokeCallback()
95 {
96 MutexLocker locker(m_mutex);
97 return (!m_suspended && !m_stopped);
98 }
scriptExecutionContext()99 ScriptExecutionContext* scriptExecutionContext()
100 {
101 MutexLocker locker(m_mutex);
102 return ActiveDOMObject::scriptExecutionContext();
103 }
mutex()104 Mutex& mutex() { return m_mutex; }
105
106 private:
107 Mutex m_mutex;
108 bool m_suspended;
109 bool m_stopped;
110 };
111
destroyOnContextThread(PassOwnPtr<ActiveDOMObjectCallbackImpl> impl)112 static void destroyOnContextThread(PassOwnPtr<ActiveDOMObjectCallbackImpl> impl)
113 {
114 OwnPtr<ActiveDOMObjectCallbackImpl> implOwnPtr = impl;
115
116 ScriptExecutionContext* context = implOwnPtr->scriptExecutionContext();
117 MutexLocker locker(implOwnPtr->mutex());
118 if (context && !context->isContextThread())
119 context->postTask(DestroyOnContextThreadTask::create(implOwnPtr.release()));
120 }
121
ActiveDOMCallback(ScriptExecutionContext * context)122 ActiveDOMCallback::ActiveDOMCallback(ScriptExecutionContext* context)
123 : m_impl(new ActiveDOMObjectCallbackImpl(context))
124 {
125 }
126
~ActiveDOMCallback()127 ActiveDOMCallback::~ActiveDOMCallback()
128 {
129 destroyOnContextThread(m_impl.release());
130 }
131
canInvokeCallback() const132 bool ActiveDOMCallback::canInvokeCallback() const
133 {
134 return m_impl->canInvokeCallback();
135 }
136
scriptExecutionContext() const137 ScriptExecutionContext* ActiveDOMCallback::scriptExecutionContext() const
138 {
139 return m_impl->scriptExecutionContext();
140 }
141
142 } // namespace WebCore
143