embedding/browser/webBrowser/nsContextMenuInfo.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsContextMenuInfo.h"
michael@0 7
michael@0 8 #include "nsIImageLoadingContent.h"
michael@0 9 #include "imgLoader.h"
michael@0 10 #include "nsIDOMDocument.h"
michael@0 11 #include "nsIDOMHTMLDocument.h"
michael@0 12 #include "nsIDOMHTMLElement.h"
michael@0 13 #include "nsIDOMHTMLHtmlElement.h"
michael@0 14 #include "nsIDOMHTMLAnchorElement.h"
michael@0 15 #include "nsIDOMHTMLImageElement.h"
michael@0 16 #include "nsIDOMHTMLAreaElement.h"
michael@0 17 #include "nsIDOMHTMLLinkElement.h"
michael@0 18 #include "nsIDOMWindow.h"
michael@0 19 #include "nsIDOMCSSStyleDeclaration.h"
michael@0 20 #include "nsIDOMCSSValue.h"
michael@0 21 #include "nsIDOMCSSPrimitiveValue.h"
michael@0 22 #include "nsNetUtil.h"
michael@0 23 #include "nsUnicharUtils.h"
michael@0 24 #include "nsIDocument.h"
michael@0 25 #include "nsIPrincipal.h"
michael@0 26 #include "nsIChannelPolicy.h"
michael@0 27 #include "nsIContentSecurityPolicy.h"
michael@0 28 #include "nsIContentPolicy.h"
michael@0 29 #include "nsAutoPtr.h"
michael@0 30 #include "imgRequestProxy.h"
michael@0 31 #include "mozIThirdPartyUtil.h"
michael@0 32
michael@0 33 //*****************************************************************************
michael@0 34 // class nsContextMenuInfo
michael@0 35 //*****************************************************************************
michael@0 36
michael@0 37 NS_IMPL_ISUPPORTS(nsContextMenuInfo, nsIContextMenuInfo)
michael@0 38
michael@0 39 nsContextMenuInfo::nsContextMenuInfo()
michael@0 40 {
michael@0 41 }
michael@0 42
michael@0 43 nsContextMenuInfo::~nsContextMenuInfo()
michael@0 44 {
michael@0 45 }
michael@0 46
michael@0 47 /* readonly attribute nsIDOMEvent mouseEvent; */
michael@0 48 NS_IMETHODIMP
michael@0 49 nsContextMenuInfo::GetMouseEvent(nsIDOMEvent **aEvent)
michael@0 50 {
michael@0 51 NS_ENSURE_ARG_POINTER(aEvent);
michael@0 52 NS_IF_ADDREF(*aEvent = mMouseEvent);
michael@0 53 return NS_OK;
michael@0 54 }
michael@0 55
michael@0 56 /* readonly attribute nsIDOMNode targetNode; */
michael@0 57 NS_IMETHODIMP
michael@0 58 nsContextMenuInfo::GetTargetNode(nsIDOMNode **aNode)
michael@0 59 {
michael@0 60 NS_ENSURE_ARG_POINTER(aNode);
michael@0 61 NS_IF_ADDREF(*aNode = mDOMNode);
michael@0 62 return NS_OK;
michael@0 63 }
michael@0 64
michael@0 65 /* readonly attribute AString associatedLink; */
michael@0 66 NS_IMETHODIMP
michael@0 67 nsContextMenuInfo::GetAssociatedLink(nsAString& aHRef)
michael@0 68 {
michael@0 69 NS_ENSURE_STATE(mAssociatedLink);
michael@0 70 aHRef.Truncate(0);
michael@0 71
michael@0 72 nsCOMPtr<nsIDOMElement> content(do_QueryInterface(mAssociatedLink));
michael@0 73 nsAutoString localName;
michael@0 74 if (content)
michael@0 75 content->GetLocalName(localName);
michael@0 76
michael@0 77 nsCOMPtr<nsIDOMElement> linkContent;
michael@0 78 ToLowerCase(localName);
michael@0 79 if (localName.EqualsLiteral("a") ||
michael@0 80 localName.EqualsLiteral("area") ||
michael@0 81 localName.EqualsLiteral("link")) {
michael@0 82 bool hasAttr;
michael@0 83 content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
michael@0 84 if (hasAttr) {
michael@0 85 linkContent = content;
michael@0 86 nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
michael@0 87 if (anchor)
michael@0 88 anchor->GetHref(aHRef);
michael@0 89 else {
michael@0 90 nsCOMPtr<nsIDOMHTMLAreaElement> area(do_QueryInterface(linkContent));
michael@0 91 if (area)
michael@0 92 area->GetHref(aHRef);
michael@0 93 else {
michael@0 94 nsCOMPtr<nsIDOMHTMLLinkElement> link(do_QueryInterface(linkContent));
michael@0 95 if (link)
michael@0 96 link->GetHref(aHRef);
michael@0 97 }
michael@0 98 }
michael@0 99 }
michael@0 100 }
michael@0 101 else {
michael@0 102 nsCOMPtr<nsIDOMNode> curr;
michael@0 103 mAssociatedLink->GetParentNode(getter_AddRefs(curr));
michael@0 104 while (curr) {
michael@0 105 content = do_QueryInterface(curr);
michael@0 106 if (!content)
michael@0 107 break;
michael@0 108 content->GetLocalName(localName);
michael@0 109 ToLowerCase(localName);
michael@0 110 if (localName.EqualsLiteral("a")) {
michael@0 111 bool hasAttr;
michael@0 112 content->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
michael@0 113 if (hasAttr) {
michael@0 114 linkContent = content;
michael@0 115 nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
michael@0 116 if (anchor)
michael@0 117 anchor->GetHref(aHRef);
michael@0 118 }
michael@0 119 else
michael@0 120 linkContent = nullptr; // Links can't be nested.
michael@0 121 break;
michael@0 122 }
michael@0 123
michael@0 124 nsCOMPtr<nsIDOMNode> temp = curr;
michael@0 125 temp->GetParentNode(getter_AddRefs(curr));
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 return NS_OK;
michael@0 130 }
michael@0 131
michael@0 132 /* readonly attribute imgIContainer imageContainer; */
michael@0 133 NS_IMETHODIMP
michael@0 134 nsContextMenuInfo::GetImageContainer(imgIContainer **aImageContainer)
michael@0 135 {
michael@0 136 NS_ENSURE_ARG_POINTER(aImageContainer);
michael@0 137 NS_ENSURE_STATE(mDOMNode);
michael@0 138
michael@0 139 nsCOMPtr<imgIRequest> request;
michael@0 140 GetImageRequest(mDOMNode, getter_AddRefs(request));
michael@0 141 if (request)
michael@0 142 return request->GetImage(aImageContainer);
michael@0 143
michael@0 144 return NS_ERROR_FAILURE;
michael@0 145 }
michael@0 146
michael@0 147 /* readonly attribute nsIURI imageSrc; */
michael@0 148 NS_IMETHODIMP
michael@0 149 nsContextMenuInfo::GetImageSrc(nsIURI **aURI)
michael@0 150 {
michael@0 151 NS_ENSURE_ARG_POINTER(aURI);
michael@0 152 NS_ENSURE_STATE(mDOMNode);
michael@0 153
michael@0 154 nsCOMPtr<nsIImageLoadingContent> content(do_QueryInterface(mDOMNode));
michael@0 155 NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
michael@0 156 return content->GetCurrentURI(aURI);
michael@0 157 }
michael@0 158
michael@0 159 /* readonly attribute imgIContainer backgroundImageContainer; */
michael@0 160 NS_IMETHODIMP
michael@0 161 nsContextMenuInfo::GetBackgroundImageContainer(imgIContainer **aImageContainer)
michael@0 162 {
michael@0 163 NS_ENSURE_ARG_POINTER(aImageContainer);
michael@0 164 NS_ENSURE_STATE(mDOMNode);
michael@0 165
michael@0 166 nsRefPtr<imgRequestProxy> request;
michael@0 167 GetBackgroundImageRequest(mDOMNode, getter_AddRefs(request));
michael@0 168 if (request)
michael@0 169 return request->GetImage(aImageContainer);
michael@0 170
michael@0 171 return NS_ERROR_FAILURE;
michael@0 172 }
michael@0 173
michael@0 174 /* readonly attribute nsIURI backgroundImageSrc; */
michael@0 175 NS_IMETHODIMP
michael@0 176 nsContextMenuInfo::GetBackgroundImageSrc(nsIURI **aURI)
michael@0 177 {
michael@0 178 NS_ENSURE_ARG_POINTER(aURI);
michael@0 179 NS_ENSURE_STATE(mDOMNode);
michael@0 180
michael@0 181 nsRefPtr<imgRequestProxy> request;
michael@0 182 GetBackgroundImageRequest(mDOMNode, getter_AddRefs(request));
michael@0 183 if (request)
michael@0 184 return request->GetURI(aURI);
michael@0 185
michael@0 186 return NS_ERROR_FAILURE;
michael@0 187 }
michael@0 188
michael@0 189 //*****************************************************************************
michael@0 190
michael@0 191 nsresult
michael@0 192 nsContextMenuInfo::GetImageRequest(nsIDOMNode *aDOMNode, imgIRequest **aRequest)
michael@0 193 {
michael@0 194 NS_ENSURE_ARG(aDOMNode);
michael@0 195 NS_ENSURE_ARG_POINTER(aRequest);
michael@0 196
michael@0 197 // Get content
michael@0 198 nsCOMPtr<nsIImageLoadingContent> content(do_QueryInterface(aDOMNode));
michael@0 199 NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
michael@0 200
michael@0 201 return content->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
michael@0 202 aRequest);
michael@0 203 }
michael@0 204
michael@0 205 bool
michael@0 206 nsContextMenuInfo::HasBackgroundImage(nsIDOMNode * aDOMNode)
michael@0 207 {
michael@0 208 NS_ENSURE_TRUE(aDOMNode, false);
michael@0 209
michael@0 210 nsRefPtr<imgRequestProxy> request;
michael@0 211 GetBackgroundImageRequest(aDOMNode, getter_AddRefs(request));
michael@0 212
michael@0 213 return (request != nullptr);
michael@0 214 }
michael@0 215
michael@0 216 nsresult
michael@0 217 nsContextMenuInfo::GetBackgroundImageRequest(nsIDOMNode *aDOMNode, imgRequestProxy **aRequest)
michael@0 218 {
michael@0 219
michael@0 220 NS_ENSURE_ARG(aDOMNode);
michael@0 221 NS_ENSURE_ARG_POINTER(aRequest);
michael@0 222
michael@0 223 nsCOMPtr<nsIDOMNode> domNode = aDOMNode;
michael@0 224
michael@0 225 // special case for the <html> element: if it has no background-image
michael@0 226 // we'll defer to <body>
michael@0 227 nsCOMPtr<nsIDOMHTMLHtmlElement> htmlElement = do_QueryInterface(domNode);
michael@0 228 if (htmlElement) {
michael@0 229 nsCOMPtr<nsIDOMHTMLElement> element = do_QueryInterface(domNode);
michael@0 230 nsAutoString nameSpace;
michael@0 231 element->GetNamespaceURI(nameSpace);
michael@0 232 if (nameSpace.IsEmpty()) {
michael@0 233 nsresult rv = GetBackgroundImageRequestInternal(domNode, aRequest);
michael@0 234 if (NS_SUCCEEDED(rv) && *aRequest)
michael@0 235 return NS_OK;
michael@0 236
michael@0 237 // no background-image found
michael@0 238 nsCOMPtr<nsIDOMDocument> document;
michael@0 239 domNode->GetOwnerDocument(getter_AddRefs(document));
michael@0 240 nsCOMPtr<nsIDOMHTMLDocument> htmlDocument(do_QueryInterface(document));
michael@0 241 NS_ENSURE_TRUE(htmlDocument, NS_ERROR_FAILURE);
michael@0 242
michael@0 243 nsCOMPtr<nsIDOMHTMLElement> body;
michael@0 244 htmlDocument->GetBody(getter_AddRefs(body));
michael@0 245 domNode = do_QueryInterface(body);
michael@0 246 NS_ENSURE_TRUE(domNode, NS_ERROR_FAILURE);
michael@0 247 }
michael@0 248 }
michael@0 249 return GetBackgroundImageRequestInternal(domNode, aRequest);
michael@0 250 }
michael@0 251
michael@0 252 nsresult
michael@0 253 nsContextMenuInfo::GetBackgroundImageRequestInternal(nsIDOMNode *aDOMNode, imgRequestProxy **aRequest)
michael@0 254 {
michael@0 255 NS_ENSURE_ARG_POINTER(aDOMNode);
michael@0 256
michael@0 257 nsCOMPtr<nsIDOMNode> domNode = aDOMNode;
michael@0 258 nsCOMPtr<nsIDOMNode> parentNode;
michael@0 259
michael@0 260 nsCOMPtr<nsIDOMDocument> document;
michael@0 261 domNode->GetOwnerDocument(getter_AddRefs(document));
michael@0 262 NS_ENSURE_TRUE(document, NS_ERROR_FAILURE);
michael@0 263
michael@0 264 nsCOMPtr<nsIDOMWindow> window;
michael@0 265 document->GetDefaultView(getter_AddRefs(window));
michael@0 266 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
michael@0 267
michael@0 268 nsCOMPtr<nsIDOMCSSPrimitiveValue> primitiveValue;
michael@0 269 nsAutoString bgStringValue;
michael@0 270
michael@0 271 // get Content Security Policy to pass to LoadImage
michael@0 272 nsCOMPtr<nsIDocument> doc(do_QueryInterface(document));
michael@0 273 nsCOMPtr<nsIPrincipal> principal;
michael@0 274 nsCOMPtr<nsIChannelPolicy> channelPolicy;
michael@0 275 nsCOMPtr<nsIContentSecurityPolicy> csp;
michael@0 276 NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
michael@0 277
michael@0 278 principal = doc->NodePrincipal();
michael@0 279 nsresult rv = principal->GetCsp(getter_AddRefs(csp));
michael@0 280 NS_ENSURE_SUCCESS(rv, rv);
michael@0 281 if (csp) {
michael@0 282 channelPolicy = do_CreateInstance("@mozilla.org/nschannelpolicy;1");
michael@0 283 channelPolicy->SetContentSecurityPolicy(csp);
michael@0 284 channelPolicy->SetLoadType(nsIContentPolicy::TYPE_IMAGE);
michael@0 285 }
michael@0 286
michael@0 287 while (true) {
michael@0 288 nsCOMPtr<nsIDOMElement> domElement(do_QueryInterface(domNode));
michael@0 289 // bail for the parent node of the root element or null argument
michael@0 290 if (!domElement)
michael@0 291 break;
michael@0 292
michael@0 293 nsCOMPtr<nsIDOMCSSStyleDeclaration> computedStyle;
michael@0 294 window->GetComputedStyle(domElement, EmptyString(),
michael@0 295 getter_AddRefs(computedStyle));
michael@0 296 if (computedStyle) {
michael@0 297 nsCOMPtr<nsIDOMCSSValue> cssValue;
michael@0 298 computedStyle->GetPropertyCSSValue(NS_LITERAL_STRING("background-image"),
michael@0 299 getter_AddRefs(cssValue));
michael@0 300 primitiveValue = do_QueryInterface(cssValue);
michael@0 301 if (primitiveValue) {
michael@0 302 primitiveValue->GetStringValue(bgStringValue);
michael@0 303 if (!bgStringValue.EqualsLiteral("none")) {
michael@0 304 nsCOMPtr<nsIURI> bgUri;
michael@0 305 NS_NewURI(getter_AddRefs(bgUri), bgStringValue);
michael@0 306 NS_ENSURE_TRUE(bgUri, NS_ERROR_FAILURE);
michael@0 307
michael@0 308 nsRefPtr<imgLoader> il = imgLoader::GetInstance();
michael@0 309 NS_ENSURE_TRUE(il, NS_ERROR_FAILURE);
michael@0 310 nsCOMPtr<nsIURI> firstPartyIsolationURI;
michael@0 311 nsCOMPtr<mozIThirdPartyUtil> thirdPartySvc
michael@0 312 = do_GetService(THIRDPARTYUTIL_CONTRACTID);
michael@0 313 thirdPartySvc->GetFirstPartyIsolationURI(nullptr, doc,
michael@0 314 getter_AddRefs(firstPartyIsolationURI));
michael@0 315 return il->LoadImage(bgUri, firstPartyIsolationURI, nullptr, principal, nullptr,
michael@0 316 nullptr, nullptr, nsIRequest::LOAD_NORMAL,
michael@0 317 nullptr, channelPolicy, EmptyString(), aRequest);
michael@0 318 }
michael@0 319 }
michael@0 320
michael@0 321 // bail if we encounter non-transparent background-color
michael@0 322 computedStyle->GetPropertyCSSValue(NS_LITERAL_STRING("background-color"),
michael@0 323 getter_AddRefs(cssValue));
michael@0 324 primitiveValue = do_QueryInterface(cssValue);
michael@0 325 if (primitiveValue) {
michael@0 326 primitiveValue->GetStringValue(bgStringValue);
michael@0 327 if (!bgStringValue.EqualsLiteral("transparent"))
michael@0 328 return NS_ERROR_FAILURE;
michael@0 329 }
michael@0 330 }
michael@0 331
michael@0 332 domNode->GetParentNode(getter_AddRefs(parentNode));
michael@0 333 domNode = parentNode;
michael@0 334 }
michael@0 335
michael@0 336 return NS_ERROR_FAILURE;
michael@0 337 }

mercurial