Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / dom / base / nsContentPermissionHelper.cpp
blob54aca92e9252955260c5e0746e2efdfa29f074be
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is mozilla.org code.
16 * The Initial Developer of the Original Code is
17 * The Mozilla Foundation
18 * Portions created by the Initial Developer are Copyright (C) 2010
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Doug Turner <dougt@mozilla.com> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsContentPermissionHelper.h"
39 #include "nsIContentPermissionPrompt.h"
40 #include "nsCOMPtr.h"
41 #include "nsIDOMWindow.h"
42 #include "nsIDOMElement.h"
44 #include "mozilla/unused.h"
46 using mozilla::unused; // <snicker>
48 nsContentPermissionRequestProxy::nsContentPermissionRequestProxy()
50 MOZ_COUNT_CTOR(nsContentPermissionRequestProxy);
53 nsContentPermissionRequestProxy::~nsContentPermissionRequestProxy()
55 MOZ_COUNT_DTOR(nsContentPermissionRequestProxy);
58 nsresult
59 nsContentPermissionRequestProxy::Init(const nsACString & type,
60 mozilla::dom::ContentPermissionRequestParent* parent)
62 NS_ASSERTION(parent, "null parent");
63 mParent = parent;
64 mType = type;
66 nsCOMPtr<nsIContentPermissionPrompt> prompt = do_GetService(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
67 if (!prompt) {
68 return NS_ERROR_FAILURE;
71 prompt->Prompt(this);
72 return NS_OK;
75 void
76 nsContentPermissionRequestProxy::OnParentDestroyed()
78 mParent = nsnull;
81 NS_IMPL_ISUPPORTS1(nsContentPermissionRequestProxy, nsIContentPermissionRequest);
83 NS_IMETHODIMP
84 nsContentPermissionRequestProxy::GetType(nsACString & aType)
86 aType = mType;
87 return NS_OK;
90 NS_IMETHODIMP
91 nsContentPermissionRequestProxy::GetWindow(nsIDOMWindow * *aRequestingWindow)
93 NS_ENSURE_ARG_POINTER(aRequestingWindow);
94 *aRequestingWindow = nsnull; // ipc doesn't have a window
95 return NS_OK;
98 NS_IMETHODIMP
99 nsContentPermissionRequestProxy::GetUri(nsIURI * *aRequestingURI)
101 NS_ENSURE_ARG_POINTER(aRequestingURI);
102 if (mParent == nsnull)
103 return NS_ERROR_FAILURE;
105 NS_ADDREF(*aRequestingURI = mParent->mURI);
106 return NS_OK;
109 NS_IMETHODIMP
110 nsContentPermissionRequestProxy::GetElement(nsIDOMElement * *aRequestingElement)
112 NS_ENSURE_ARG_POINTER(aRequestingElement);
113 if (mParent == nsnull)
114 return NS_ERROR_FAILURE;
115 NS_ADDREF(*aRequestingElement = mParent->mElement);
116 return NS_OK;
119 NS_IMETHODIMP
120 nsContentPermissionRequestProxy::Cancel()
122 if (mParent == nsnull)
123 return NS_ERROR_FAILURE;
124 unused << mozilla::dom::ContentPermissionRequestParent::Send__delete__(mParent, false);
125 mParent = nsnull;
126 return NS_OK;
129 NS_IMETHODIMP
130 nsContentPermissionRequestProxy::Allow()
132 if (mParent == nsnull)
133 return NS_ERROR_FAILURE;
134 unused << mozilla::dom::ContentPermissionRequestParent::Send__delete__(mParent, true);
135 mParent = nsnull;
136 return NS_OK;
139 namespace mozilla {
140 namespace dom {
142 ContentPermissionRequestParent::ContentPermissionRequestParent(const nsACString& aType,
143 nsIDOMElement *aElement,
144 const IPC::URI& aUri)
146 MOZ_COUNT_CTOR(ContentPermissionRequestParent);
148 mURI = aUri;
149 mElement = aElement;
150 mType = aType;
153 ContentPermissionRequestParent::~ContentPermissionRequestParent()
155 MOZ_COUNT_DTOR(ContentPermissionRequestParent);
158 bool
159 ContentPermissionRequestParent::Recvprompt()
161 mProxy = new nsContentPermissionRequestProxy();
162 NS_ASSERTION(mProxy, "Alloc of request proxy failed");
163 if (NS_FAILED(mProxy->Init(mType, this)))
164 mProxy->Cancel();
165 return true;
168 void
169 ContentPermissionRequestParent::ActorDestroy(ActorDestroyReason why)
171 mProxy->OnParentDestroyed();
174 } // namespace dom
175 } // namespace mozilla