/*
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 Apple Inc. All rights reserved.
 *  Copyright (C) 2007 Samuel Weinig <sam@webkit.org>
 *  Copyright (C) 2009 Google, Inc. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef WebCoreJSClientData_h
#define WebCoreJSClientData_h

#include "DOMWrapperWorld.h"
#include "WebCoreBuiltinNames.h"
#include "WebCoreJSBuiltins.h"
#include "WebCoreTypedArrayController.h"
#include <wtf/HashSet.h>
#include <wtf/RefPtr.h>

#if ENABLE(FETCH_API)
#include "FetchInternalsBuiltinsWrapper.h"
#endif

#if ENABLE(STREAMS_API)
#include "ReadableStreamInternalsBuiltinsWrapper.h"
#include "StreamInternalsBuiltinsWrapper.h"
#include "WritableStreamInternalsBuiltinsWrapper.h"
#endif

namespace WebCore {

class JSVMClientData : public JSC::JSGlobalData::ClientData {
    WTF_MAKE_NONCOPYABLE(JSVMClientData); WTF_MAKE_FAST_ALLOCATED;
    friend class JSGlobalDataWorldIterator;
    friend void initNormalWorldClientData(JSC::JSGlobalData*);

public:
    explicit JSVMClientData(JSC::JSGlobalData& vm)
	: m_builtinNames(vm)
#if ENABLE(FETCH_API)
    , m_fetchInternalsBuiltinsNames(vm)
#endif
#if ENABLE(STREAMS_API)
    , m_readableStreamInternalsBuiltinsNames(vm)
    , m_streamInternalsBuiltinsNames(vm)
    , m_writableStreamInternalsBuiltinsNames(vm)
#endif
	{
#if ENABLE(FETCH_API)
    	m_fetchInternalsBuiltinsNames.exportNames();
#endif
#if ENABLE(STREAMS_API)
    	m_readableStreamInternalsBuiltinsNames.exportNames();
    	m_streamInternalsBuiltinsNames.exportNames();
    	m_writableStreamInternalsBuiltinsNames.exportNames();
#endif
	}

    virtual ~JSVMClientData()
    {
        ASSERT(m_worldSet.contains(m_normalWorld.get()));
        ASSERT(m_worldSet.size() == 1);
        ASSERT(m_normalWorld->hasOneRef());
        m_normalWorld.clear();
        ASSERT(m_worldSet.isEmpty());
    }

    DOMWrapperWorld* normalWorld() { return m_normalWorld.get(); }

    void getAllWorlds(Vector<RefPtr<DOMWrapperWorld> >& worlds)
    {
        copyToVector(m_worldSet, worlds);
    }

    void rememberWorld(DOMWrapperWorld* world)
    {
        ASSERT(!m_worldSet.contains(world));
        m_worldSet.add(world);
    }
    void forgetWorld(DOMWrapperWorld* world)
    {
        ASSERT(m_worldSet.contains(world));
        m_worldSet.remove(world);
    }

    WebCoreBuiltinNames& builtinNames() { return m_builtinNames; }

#if ENABLE(FETCH_API)
    FetchInternalsBuiltinsNames& fetchInternalsBuiltinsNames() { return m_fetchInternalsBuiltinsNames; }
#endif

#if ENABLE(STREAMS_API)
    ReadableStreamInternalsBuiltinsNames& readableStreamInternalsBuiltinsNames() { return m_readableStreamInternalsBuiltinsNames; }
    StreamInternalsBuiltinsNames& streamInternalsBuiltinsNames() { return m_streamInternalsBuiltinsNames; }
    WritableStreamInternalsBuiltinsNames& writableStreamInternalsBuiltinsNames() { return m_writableStreamInternalsBuiltinsNames; }
#endif

private:
    HashSet<DOMWrapperWorld*> m_worldSet;
    RefPtr<DOMWrapperWorld> m_normalWorld;

    WebCoreBuiltinNames m_builtinNames;

#if ENABLE(FETCH_API)
    FetchInternalsBuiltinsNames m_fetchInternalsBuiltinsNames;
#endif

#if ENABLE(STREAMS_API)
    ReadableStreamInternalsBuiltinsNames m_readableStreamInternalsBuiltinsNames;
    StreamInternalsBuiltinsNames m_streamInternalsBuiltinsNames;
    WritableStreamInternalsBuiltinsNames m_writableStreamInternalsBuiltinsNames;
#endif
};

inline void initNormalWorldClientData(JSC::JSGlobalData* vm)
{
	JSVMClientData* clientData = new JSVMClientData(*vm);
    vm->clientData = clientData; // ~JSGlobalData deletes this pointer.
    clientData->m_normalWorld = DOMWrapperWorld::create(vm, true);
    vm->m_typedArrayController = adoptRef(new WebCoreTypedArrayController());
}

} // namespace WebCore

#endif // WebCoreJSClientData_h
