/*
 * Performs simple initialization of various variables. This version
 * can dump the hacks from previous versions - it only has to work
 * with the 5.0 browser
 */
JavaPluginFactory5::JavaPluginFactory5(IPluginServiceProvider* pProvider)  {
    trace("JavaPluginFactory5:Constructor\n");
    JD_INIT_REFCNT();
    plugin_manager = NULL;
    jvm_manager = NULL;
    cookieStorage = NULL;
    isInitialized = false;
    is_java_vm_started = 0;
    g_plugin_factory = this;
    factory_monitor = g_unixService->JD_NewMonitor();
    javaVM = new JavaVM5(this);
    proxy_support = new ProxySupport5(javaVM);
    cookieSupport = new CookieSupport(javaVM);
    /* Allocate and zero out array  memory since NULL is used to 
       indicate an empty spot */
    int inst_sz = PLUGIN_INSTANCE_COUNT * sizeof(JavaPluginInstance5 *);
    plugin_instances = (JavaPluginInstance5 **)	malloc(inst_sz);
    memset(plugin_instances, 0, inst_sz);
    int env_sz = MAX_ENVS * sizeof(RemoteJNIEnv*);
    int proxy_env_sz = MAX_ENVS * sizeof(JNIEnv*);
    current_envs = (RemoteJNIEnv**) malloc(env_sz);
    current_proxy_envs = (JNIEnv**) malloc(proxy_env_sz);
    memset(current_envs, 0, env_sz);
    memset(current_proxy_envs, 0, proxy_env_sz);
    pluginNameString=(char*)malloc(100);
    memset(pluginNameString, 0, sizeof(pluginNameString));
    service_provider = pProvider;
    if (service_provider)
      service_provider->AddRef();

    CJavaConsole::Create((IJVMPlugin*)this, this, jISupportsIID, (void**)&m_pIJVMConsole);
}
//=--------------------------------------------------------------------------=
// CPluginServiceProvider::CPluginServiceProvider
//=--------------------------------------------------------------------------=
//
CPluginServiceProvider::CPluginServiceProvider(ISupports* pProvider)
: mMgr(pProvider)
{
    JD_INIT_REFCNT();

    if (mMgr)
	mMgr->AddRef();
}
//=--------------------------------------------------------------------------=
// CNSAdapter_PluginManager::CNSAdapter_PluginManager
//=--------------------------------------------------------------------------=
// params:  
//
// notes:
//
CNSAdapter_PluginManager::CNSAdapter_PluginManager(nsIPluginManager* pPluginManager) : 
    m_pPluginManager(pPluginManager) 
{
    TRACE("CNSAdapter_PluginManager::CNSAdapter_PluginManager\n");
    JD_INIT_REFCNT();

    if (m_pPluginManager)
	m_pPluginManager->AddRef();
}
//=--------------------------------------------------------------------------=
// CNSAdapter_SecurityContextPeer::CNSAdapter_SecurityContextPeer
//=--------------------------------------------------------------------------=
//
// notes :
//	
CNSAdapter_SecurityContextPeer::CNSAdapter_SecurityContextPeer(nsISecurityContext* pSecurityContext) : 
m_pSecurityContext(pSecurityContext) 
{
    TRACE("CNSAdapter_SecurityContextPeer::CNSAdapter_SecurityContextPeer\n");

    JD_INIT_REFCNT();

    if (m_pSecurityContext)
	m_pSecurityContext->AddRef();
}
//------------------------------------------------------------------------------------
// CNS7Adapter_PluginServiceProvider::CNS7Adapter_PluginServiceProvider
//------------------------------------------------------------------------------------
CNS7Adapter_PluginServiceProvider::CNS7Adapter_PluginServiceProvider(nsISupports* pProvider)
    : mService(NULL),
      m_pPluginManager(NULL), m_pJVMManager(NULL), m_pComponentManager(NULL),
      m_pPluginManagerAdapter(NULL), m_pJVMManagerAdapter(NULL)

{
    TRACE("CNS7Adapter_PluginServiceProvider::CPluginServiceProviderAdpter");

    JD_INIT_REFCNT();

    // Try to obtain nsIServiceManager
    // We can obtain m_pPluginManager & m_pJVMManager now, however,
    // this will delay the start-up time, so it is better not to do this.
    pProvider->QueryInterface(kIServiceManagerIID, (void**)&mService);
}
//=--------------------------------------------------------------------------=
// CNS4Adapter_PluginInstancePeer::CNS4Adapter_PluginInstancePeer
//=--------------------------------------------------------------------------=
//
CNS4Adapter_PluginInstancePeer::CNS4Adapter_PluginInstancePeer(INS4AdapterPeer* peer,
							       NPP npp,
							       JDPluginMimeType typeString, 
							       JDUint16 attr_cnt, 
							       const char** attr_list, 
							       const char** val_list)
    : m_npp(npp), m_typeString(typeString), 
      m_attribute_cnt((JDUint16)0),
      m_attribute_list(NULL), m_values_list(NULL), m_pINS4AdapterPeer(NULL)
{
    TRACE("CNS4Adapter_PluginInstancePeer::CNS4Adapter_PluginInstancePeer\n");

    ASSERT(peer != NULL);

    // Set the reference count to 0.
    JD_INIT_REFCNT();

    m_pINS4AdapterPeer = peer;

    if (m_pINS4AdapterPeer != NULL)
	m_pINS4AdapterPeer->AddRef();

    m_attribute_list = (char**) m_pINS4AdapterPeer->NPN_MemAlloc(attr_cnt * sizeof(const char*));
    m_values_list = (char**) m_pINS4AdapterPeer->NPN_MemAlloc(attr_cnt * sizeof(const char*));

    int j = 0;
    if (m_attribute_list != NULL && m_values_list != NULL) 
    {
	for (int i = 0; i < attr_cnt; i++)   
	{
	    if (attr_list[i] != NULL && val_list[i] != NULL)
	    {
		m_attribute_list[j] = (char*) m_pINS4AdapterPeer->NPN_MemAlloc(strlen(attr_list[i]) + 1);
		if (m_attribute_list[j] != NULL)
		    strcpy(m_attribute_list[j], attr_list[i]);

		m_values_list[j] = (char*) m_pINS4AdapterPeer->NPN_MemAlloc(strlen(val_list[i]) + 1);
		if (m_values_list[j] != NULL)
		    strcpy(m_values_list[j], val_list[i]);

                j++;
            }
	}
    }
    m_attribute_cnt = j;
    
    
}       
//=--------------------------------------------------------------------------=
// CNS4Adapter_PluginStreamInfo::CNS4Adapter_PluginStreamInfo
//=--------------------------------------------------------------------------=
//
CNS4Adapter_PluginStreamInfo::CNS4Adapter_PluginStreamInfo(INS4AdapterPeer* peer, 
							   NPP instance, 
							   NPStream* stream, 
							   JDPluginMimeType mimeType, 
							   JDBool seekable)
    : mNPP(instance), mStream(stream), mURL(NULL),
      mMimeType(NULL), mSeekable(seekable), m_pINS4AdapterPeer(NULL)
{
    TRACE("CNS4Adapter_PluginStreamInfo::CNS4Adapter_PluginStreamInfo\n");

    ASSERT(peer != NULL);

    JD_INIT_REFCNT();

    // Make sure MIME type has been copied!!
    if (mimeType != NULL)
    {
	char* pMimeType = new char[strlen(mimeType) + 1];

	if (pMimeType != NULL)
	    strcpy(pMimeType, mimeType);

	mMimeType = pMimeType;
    }

    // Make sure URL has been copied!!
    if (stream->url != NULL)
    {
	char* pURL = new char[strlen(stream->url) + 1];

	if (mURL != NULL)
	    strcpy(pURL, stream->url);

	mURL = pURL;
    }

    m_pINS4AdapterPeer = peer;

    if (m_pINS4AdapterPeer != NULL)
	m_pINS4AdapterPeer->AddRef();
}
CNSAdapter_BrowserAuthenticator::CNSAdapter_BrowserAuthenticator(nsIJVMAuthTools* pJVMAuthTools){
    JD_INIT_REFCNT();
    m_spBrowserAuth = pJVMAuthTools;
    m_pAuthInfo = NULL;
}