15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2012 Google, Inc. All Rights Reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * are met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    documentation and/or other materials provided with the distribution.
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef RefCountedSupplement_h
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define RefCountedSupplement_h
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
291e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "platform/Supplementable.h"
307757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch#include "wtf/RefCounted.h"
317757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch#include "wtf/RefPtr.h"
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace WebCore {
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<class T, class S>
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class RefCountedSupplement : public RefCounted<S> {
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public:
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    typedef RefCountedSupplement<T, S> ThisType;
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    virtual ~RefCountedSupplement() { }
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    virtual void hostDestroyed() { }
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    class Wrapper : public Supplement<T> {
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    public:
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        explicit Wrapper(PassRefPtr<ThisType> wrapped) : m_wrapped(wrapped) { }
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        virtual ~Wrapper() { m_wrapped->hostDestroyed();  }
47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#if !ASSERT_DISABLED || defined(ADDRESS_SANITIZER)
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        virtual bool isRefCountedWrapper() const OVERRIDE { return true; }
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        ThisType* wrapped() const { return m_wrapped.get(); }
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    private:
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        RefPtr<ThisType> m_wrapped;
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    };
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
56926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    static void provideTo(Supplementable<T>* host, const char* key, PassRefPtr<ThisType> supplement)
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    {
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        host->provideSupplement(key, adoptPtr(new Wrapper(supplement)));
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
61926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    static ThisType* from(Supplementable<T>* host, const char* key)
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    {
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        Supplement<T>* found = host->requireSupplement(key);
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        if (!found)
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return 0;
66926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        ASSERT_WITH_SECURITY_IMPLICATION(found->isRefCountedWrapper());
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return static_cast<Wrapper*>(found)->wrapped();
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)};
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} // namespace WebCore
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif // RefCountedSupplement_h
74