Back to index

lightning-sunbird  0.9+nobinonly
nsDOMPopupBlockedEvent.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Steve Clark (buster@netscape.com)
00024  *   Ilya Konstantinov (mozilla-code@future.shiny.co.il)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsDOMPopupBlockedEvent.h"
00041 #include "nsIURI.h"
00042 #include "nsContentUtils.h"
00043 #include "nsIWebNavigation.h"
00044 #include "nsIInterfaceRequestorUtils.h"
00045 
00046 nsDOMPopupBlockedEvent::nsDOMPopupBlockedEvent(nsPresContext* aPresContext,
00047                                                nsPopupBlockedEvent* aEvent)
00048   : nsDOMEvent(aPresContext, aEvent ? aEvent :
00049                new nsPopupBlockedEvent(PR_FALSE, 0))
00050 {
00051   NS_ASSERTION(mEvent->eventStructType == NS_POPUPBLOCKED_EVENT, "event type mismatch");
00052 
00053   if (aEvent) {
00054     mEventIsInternal = PR_FALSE;
00055   }
00056   else {
00057     mEventIsInternal = PR_TRUE;
00058     mEvent->time = PR_Now();
00059   }
00060 }
00061 
00062 nsDOMPopupBlockedEvent::~nsDOMPopupBlockedEvent() 
00063 {
00064   if (mEventIsInternal) {
00065     if (mEvent->eventStructType == NS_POPUPBLOCKED_EVENT) {
00066       nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent);
00067       NS_IF_RELEASE(event->mRequestingWindowURI);
00068       NS_IF_RELEASE(event->mPopupWindowURI);
00069     }
00070   }
00071 }
00072 
00073 NS_IMPL_ADDREF_INHERITED(nsDOMPopupBlockedEvent, nsDOMEvent)
00074 NS_IMPL_RELEASE_INHERITED(nsDOMPopupBlockedEvent, nsDOMEvent)
00075 
00076 NS_INTERFACE_MAP_BEGIN(nsDOMPopupBlockedEvent)
00077   NS_INTERFACE_MAP_ENTRY(nsIDOMPopupBlockedEvent)
00078   NS_INTERFACE_MAP_ENTRY(nsIDOMPopupBlockedEvent_MOZILLA_1_8_BRANCH)
00079   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(PopupBlockedEvent)
00080 NS_INTERFACE_MAP_END_INHERITING(nsDOMEvent)
00081 
00082 NS_IMETHODIMP
00083 nsDOMPopupBlockedEvent::InitPopupBlockedEvent(const nsAString & aTypeArg,
00084                             PRBool aCanBubbleArg, PRBool aCancelableArg,
00085                             nsIURI *aRequestingWindowURI,
00086                             nsIURI *aPopupWindowURI,
00087                             const nsAString & aPopupWindowFeatures)
00088 {
00089   nsresult rv = nsDOMEvent::InitEvent(aTypeArg, aCanBubbleArg, aCancelableArg);
00090   NS_ENSURE_SUCCESS(rv, rv);
00091 
00092   switch (mEvent->eventStructType)
00093   {
00094     case NS_POPUPBLOCKED_EVENT:
00095     {
00096        nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent);
00097        event->mRequestingWindowURI = aRequestingWindowURI;
00098        event->mPopupWindowURI = aPopupWindowURI;
00099        NS_IF_ADDREF(event->mRequestingWindowURI);
00100        NS_IF_ADDREF(event->mPopupWindowURI);
00101        event->mPopupWindowFeatures = aPopupWindowFeatures;
00102        break;
00103     }
00104     default:
00105        break;
00106   }
00107 
00108   return NS_OK;
00109 }
00110 
00111 NS_IMETHODIMP
00112 nsDOMPopupBlockedEvent::InitPopupBlockedEvent(const nsAString & aTypeArg,
00113                             PRBool aCanBubbleArg, PRBool aCancelableArg,
00114                             nsIDOMWindow *aRequestingWindow,
00115                             nsIURI *aPopupWindowURI,
00116                             const nsAString & aPopupWindowFeatures)
00117 {
00118   nsresult rv = nsDOMEvent::InitEvent(aTypeArg, aCanBubbleArg, aCancelableArg);
00119   NS_ENSURE_SUCCESS(rv, rv);
00120 
00121   switch (mEvent->eventStructType)
00122   {
00123     case NS_POPUPBLOCKED_EVENT:
00124     {
00125        nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent);
00126        event->mRequestingWindow = do_GetWeakReference(aRequestingWindow);
00127        event->mPopupWindowURI = aPopupWindowURI;
00128        NS_IF_ADDREF(event->mPopupWindowURI);
00129 
00130        /*
00131         * If this method was callable from script, this could be a
00132         * security exploit right here, but since it's not, we're ok.
00133         */
00134        event->mRequestingWindowURI = nsnull;
00135        if (aRequestingWindow) {
00136          nsCOMPtr<nsIWebNavigation> webNav =
00137            do_GetInterface(aRequestingWindow);
00138          if (webNav)
00139            webNav->GetCurrentURI(&event->mRequestingWindowURI);
00140        }
00141 
00142        event->mPopupWindowFeatures = aPopupWindowFeatures;
00143        break;
00144     }
00145     default:
00146        break;
00147   }
00148 
00149   return NS_OK;
00150 }
00151 
00152 NS_IMETHODIMP
00153 nsDOMPopupBlockedEvent::GetRequestingWindowURI(nsIURI **aRequestingWindowURI)
00154 {
00155   NS_ENSURE_ARG_POINTER(aRequestingWindowURI);
00156   if (mEvent->eventStructType == NS_POPUPBLOCKED_EVENT) {
00157     nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent);
00158     *aRequestingWindowURI = event->mRequestingWindowURI;
00159     NS_IF_ADDREF(*aRequestingWindowURI);
00160     return NS_OK;
00161   }
00162   *aRequestingWindowURI = 0;
00163   return NS_OK;  // Don't throw an exception
00164 }
00165 
00166 NS_IMETHODIMP
00167 nsDOMPopupBlockedEvent::GetRequestingWindow(nsIDOMWindow **aRequestingWindow)
00168 {
00169   if (mEvent->eventStructType == NS_POPUPBLOCKED_EVENT) {
00170     nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent);
00171     CallQueryReferent(event->mRequestingWindow.get(), aRequestingWindow);
00172   } else {
00173     *aRequestingWindow = 0;
00174   }
00175 
00176   return NS_OK;
00177 }
00178 
00179 NS_IMETHODIMP
00180 nsDOMPopupBlockedEvent::GetPopupWindowURI(nsIURI **aPopupWindowURI)
00181 {
00182   NS_ENSURE_ARG_POINTER(aPopupWindowURI);
00183   if (mEvent->eventStructType == NS_POPUPBLOCKED_EVENT) {
00184     nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent);
00185     *aPopupWindowURI = event->mPopupWindowURI;
00186     NS_IF_ADDREF(*aPopupWindowURI);
00187     return NS_OK;
00188   }
00189   *aPopupWindowURI = 0;
00190   return NS_OK;  // Don't throw an exception
00191 }
00192 
00193 NS_IMETHODIMP
00194 nsDOMPopupBlockedEvent::GetPopupWindowFeatures(nsAString &aPopupWindowFeatures)
00195 {
00196   if (mEvent->eventStructType == NS_POPUPBLOCKED_EVENT) {
00197     nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent);
00198     aPopupWindowFeatures = event->mPopupWindowFeatures;
00199     return NS_OK;
00200   }
00201   aPopupWindowFeatures.Truncate();
00202   return NS_OK;  // Don't throw an exception
00203 }
00204 
00205 nsresult NS_NewDOMPopupBlockedEvent(nsIDOMEvent** aInstancePtrResult,
00206                                     nsPresContext* aPresContext,
00207                                     nsPopupBlockedEvent *aEvent) 
00208 {
00209   nsDOMPopupBlockedEvent* it = new nsDOMPopupBlockedEvent(aPresContext, aEvent);
00210   if (nsnull == it) {
00211     return NS_ERROR_OUT_OF_MEMORY;
00212   }
00213 
00214   return CallQueryInterface(it, aInstancePtrResult);
00215 }