void BrowserAuthenticationListener::OnAuthenticationRequired(OpAuthenticationCallback* callback, OpWindowCommander *commander, DesktopWindow* parent_window)
{
	// Check if there already exists an authentication request with same authentication
	for (unsigned i = 0; i < m_open_dialogs.GetCount(); i++)
	{
		PasswordDialog* dialog_current = m_open_dialogs.Get(i);
		OpAuthenticationCallback* callback_current = dialog_current->GetCallback();

		// if same authentication don't launch another password dialog (on the same page)
		if (callback && callback->IsSameAuth(callback_current) && dialog_current->GetParentDesktopWindow() == parent_window)
			return;
	}

	PasswordDialog *dialog = OP_NEW(PasswordDialog, (callback, commander));
	if (!dialog)
	{
		callback->CancelAuthentication();
		return; //FALSE;
	}

	RETURN_VOID_IF_ERROR(dialog->Init(parent_window));

	if (OpStatus::IsError(m_open_dialogs.Add(dialog)))
	{
		dialog->CloseDialog(TRUE, TRUE);
		return;
	}

	dialog->SetDialogListener(this);

	// Inform OpPage about pending authentication 
	if (parent_window && parent_window->GetBrowserView())
	{	
		OpPage *page = parent_window->GetBrowserView()->GetOpPage();
		if (page)
			page->SetAuthIsPending(true);
	
		// Update document icon
		parent_window->GetBrowserView()->UpdateWindowImage(TRUE);
		parent_window->SetWidgetImage(parent_window->GetBrowserView()->GetFavIcon(FALSE));
	}

	return;
}
void BrowserAuthenticationListener::ClosePasswordDialogs(const URL_ID authid, PasswordDialog* request_from)
{
	OpVector<PasswordDialog> still_opened_dialogs;

	// will close all passworddialogs with the supplied id, except the one that request was from
	for (unsigned i = 0; i < m_open_dialogs.GetCount(); i++)
	{
		PasswordDialog* dialog = m_open_dialogs.Get(i);
		if (dialog->GetAuthID() == authid && dialog != request_from)
		{
			dialog->CloseDialog(FALSE);
		}
		else 
		{
			still_opened_dialogs.Add(dialog);
		}
	}

	// This code updates m_open_dialogs list to contain only opened dialogs.
	// Relying on 'OnClose()' to call 'm_open_dialogs.RemoveByItem()' is not enough,
	// because some events (like OnAuthenticationRequired) may be called before 'OnClose()' gets called,
	// and we may want to access the good version of m_open_dialogs.
	m_open_dialogs.Swap(still_opened_dialogs);
}