Backed out changeset 9157ea42ff41 (bug 914925) for Android/B2G test bustage.
[gecko.git] / dom / base / nsContentPermissionHelper.cpp
blob811b5b37e870d14440eb1537bea9d7aee51ddd76
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsContentPermissionHelper.h"
6 #include "nsIContentPermissionPrompt.h"
7 #include "nsCOMPtr.h"
8 #include "nsIDOMElement.h"
9 #include "nsIPrincipal.h"
10 #include "mozilla/dom/Element.h"
11 #include "mozilla/unused.h"
13 using mozilla::unused; // <snicker>
14 using namespace mozilla::dom;
16 nsContentPermissionRequestProxy::nsContentPermissionRequestProxy()
18 MOZ_COUNT_CTOR(nsContentPermissionRequestProxy);
21 nsContentPermissionRequestProxy::~nsContentPermissionRequestProxy()
23 MOZ_COUNT_DTOR(nsContentPermissionRequestProxy);
26 nsresult
27 nsContentPermissionRequestProxy::Init(const nsACString & type,
28 const nsACString & access,
29 ContentPermissionRequestParent* parent)
31 NS_ASSERTION(parent, "null parent");
32 mParent = parent;
33 mType = type;
34 mAccess = access;
36 nsCOMPtr<nsIContentPermissionPrompt> prompt = do_CreateInstance(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
37 if (!prompt) {
38 return NS_ERROR_FAILURE;
41 prompt->Prompt(this);
42 return NS_OK;
45 void
46 nsContentPermissionRequestProxy::OnParentDestroyed()
48 mParent = nullptr;
51 NS_IMPL_ISUPPORTS1(nsContentPermissionRequestProxy, nsIContentPermissionRequest)
53 NS_IMETHODIMP
54 nsContentPermissionRequestProxy::GetType(nsACString & aType)
56 aType = mType;
57 return NS_OK;
60 NS_IMETHODIMP
61 nsContentPermissionRequestProxy::GetAccess(nsACString & aAccess)
63 aAccess = mAccess;
64 return NS_OK;
67 NS_IMETHODIMP
68 nsContentPermissionRequestProxy::GetWindow(nsIDOMWindow * *aRequestingWindow)
70 NS_ENSURE_ARG_POINTER(aRequestingWindow);
71 *aRequestingWindow = nullptr; // ipc doesn't have a window
72 return NS_OK;
75 NS_IMETHODIMP
76 nsContentPermissionRequestProxy::GetPrincipal(nsIPrincipal * *aRequestingPrincipal)
78 NS_ENSURE_ARG_POINTER(aRequestingPrincipal);
79 if (mParent == nullptr) {
80 return NS_ERROR_FAILURE;
83 NS_ADDREF(*aRequestingPrincipal = mParent->mPrincipal);
84 return NS_OK;
87 NS_IMETHODIMP
88 nsContentPermissionRequestProxy::GetElement(nsIDOMElement * *aRequestingElement)
90 NS_ENSURE_ARG_POINTER(aRequestingElement);
91 if (mParent == nullptr) {
92 return NS_ERROR_FAILURE;
95 nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(mParent->mElement);
96 elem.forget(aRequestingElement);
97 return NS_OK;
100 NS_IMETHODIMP
101 nsContentPermissionRequestProxy::Cancel()
103 if (mParent == nullptr) {
104 return NS_ERROR_FAILURE;
107 unused << ContentPermissionRequestParent::Send__delete__(mParent, false);
108 mParent = nullptr;
109 return NS_OK;
112 NS_IMETHODIMP
113 nsContentPermissionRequestProxy::Allow()
115 if (mParent == nullptr) {
116 return NS_ERROR_FAILURE;
118 unused << ContentPermissionRequestParent::Send__delete__(mParent, true);
119 mParent = nullptr;
120 return NS_OK;
123 namespace mozilla {
124 namespace dom {
126 ContentPermissionRequestParent::ContentPermissionRequestParent(const nsACString& aType,
127 const nsACString& aAccess,
128 Element* aElement,
129 const IPC::Principal& aPrincipal)
131 MOZ_COUNT_CTOR(ContentPermissionRequestParent);
133 mPrincipal = aPrincipal;
134 mElement = aElement;
135 mType = aType;
136 mAccess = aAccess;
139 ContentPermissionRequestParent::~ContentPermissionRequestParent()
141 MOZ_COUNT_DTOR(ContentPermissionRequestParent);
144 bool
145 ContentPermissionRequestParent::Recvprompt()
147 mProxy = new nsContentPermissionRequestProxy();
148 NS_ASSERTION(mProxy, "Alloc of request proxy failed");
149 if (NS_FAILED(mProxy->Init(mType, mAccess, this))) {
150 mProxy->Cancel();
152 return true;
155 void
156 ContentPermissionRequestParent::ActorDestroy(ActorDestroyReason why)
158 if (mProxy) {
159 mProxy->OnParentDestroyed();
163 } // namespace dom
164 } // namespace mozilla