Chromium Code Reviews| Index: Source/bindings/v8/UnsafePersistent.h |
| diff --git a/Source/modules/webmidi/MIDIErrorCallback.cpp b/Source/bindings/v8/UnsafePersistent.h |
| similarity index 63% |
| copy from Source/modules/webmidi/MIDIErrorCallback.cpp |
| copy to Source/bindings/v8/UnsafePersistent.h |
| index 0557f1d01c17606f418bf1f5fccdf495e6707cc0..3b6618bb4623b316021a514552d6645947e8353f 100644 |
| --- a/Source/modules/webmidi/MIDIErrorCallback.cpp |
| +++ b/Source/bindings/v8/UnsafePersistent.h |
| @@ -28,44 +28,41 @@ |
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| */ |
| -#include "config.h" |
| -#include "modules/webmidi/MIDIErrorCallback.h" |
| +#ifndef UnsafePersistent_h |
| +#define UnsafePersistent_h |
| -#include "core/dom/DOMError.h" |
| -#include "core/dom/ScriptExecutionContext.h" |
| +#include <v8.h> |
| namespace WebCore { |
| -namespace { |
| +// An unsafe way to pass Persistent handles around. Do not use unless you know |
| +// what you're doing. UnsafePersistent is only safe to use when we know that the |
| +// memory pointed by the it is not going away: 1) When GC cannot happen while |
| +// the UnsafePersistent is alive or 2) when there is a strong Persistent keeping |
| +// the memory alive while the UnsafePersistent is alive. |
| -class DispatchCallbackTask : public ScriptExecutionContext::Task { |
| +// TODO: assert that GC doesn't happen during the lifetime of UnsafePersistent. |
|
abarth-chromium
2013/04/29 17:47:33
TODO -> FIXME
marja
2013/04/30 08:55:50
Done.
|
| +template<typename T> class UnsafePersistent { |
|
abarth-chromium
2013/04/29 17:47:33
I probably would have called this class UnsafeHand
marja
2013/04/30 08:55:50
After the future renaming, Local will be called Ha
|
| public: |
| - static PassOwnPtr<DispatchCallbackTask> create(PassRefPtr<MIDIErrorCallback> callback, PassRefPtr<DOMError> error) |
| - { |
| - return adoptPtr(new DispatchCallbackTask(callback, error)); |
| - } |
| + UnsafePersistent(T* value) : m_value(value) { } |
| - virtual void performTask(ScriptExecutionContext*) |
| + // The end result is generally unsafe to use, see the class level comment |
| + // for when it's safe to use. |
| + void makePersistentHandle(v8::Persistent<T>* handle) const |
|
abarth-chromium
2013/04/29 17:47:33
copyTo ?
marja
2013/04/30 08:55:50
Done.
|
| { |
| - m_callback->handleEvent(m_error.get()); |
| + T** rawValue = reinterpret_cast<T**>(handle); |
|
abarth-chromium
2013/04/29 17:47:33
Wow, that's scary stuff.
|
| + *rawValue = m_value; |
| } |
| -private: |
| - DispatchCallbackTask(PassRefPtr<MIDIErrorCallback> callback, PassRefPtr<DOMError> error) |
| - : m_callback(callback) |
| - , m_error(error) |
| + T* value() const |
| { |
| + return m_value; |
| } |
| - RefPtr<MIDIErrorCallback> m_callback; |
| - RefPtr<DOMError> m_error; |
| +private: |
| + T* m_value; |
| }; |
| -} // namespace |
| - |
| -void MIDIErrorCallback::scheduleCallback(ScriptExecutionContext* context, PassRefPtr<DOMError> error) |
| -{ |
| - context->postTask(DispatchCallbackTask::create(this, error)); |
| -} |
| - |
| } // namespace WebCore |
| + |
| +#endif // UnsafePersistent_h |