content/html/document/src/MediaDocument.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 "MediaDocument.h"
michael@0 7 #include "nsGkAtoms.h"
michael@0 8 #include "nsRect.h"
michael@0 9 #include "nsPresContext.h"
michael@0 10 #include "nsIPresShell.h"
michael@0 11 #include "nsIScrollable.h"
michael@0 12 #include "nsViewManager.h"
michael@0 13 #include "nsITextToSubURI.h"
michael@0 14 #include "nsIURL.h"
michael@0 15 #include "nsIContentViewer.h"
michael@0 16 #include "nsIMarkupDocumentViewer.h"
michael@0 17 #include "nsIDocShell.h"
michael@0 18 #include "nsCharsetSource.h" // kCharsetFrom* macro definition
michael@0 19 #include "nsNodeInfoManager.h"
michael@0 20 #include "nsContentUtils.h"
michael@0 21 #include "nsDocElementCreatedNotificationRunner.h"
michael@0 22 #include "mozilla/Services.h"
michael@0 23 #include "nsServiceManagerUtils.h"
michael@0 24 #include "nsIPrincipal.h"
michael@0 25
michael@0 26 namespace mozilla {
michael@0 27 namespace dom {
michael@0 28
michael@0 29 MediaDocumentStreamListener::MediaDocumentStreamListener(MediaDocument *aDocument)
michael@0 30 {
michael@0 31 mDocument = aDocument;
michael@0 32 }
michael@0 33
michael@0 34 MediaDocumentStreamListener::~MediaDocumentStreamListener()
michael@0 35 {
michael@0 36 }
michael@0 37
michael@0 38
michael@0 39 NS_IMPL_ISUPPORTS(MediaDocumentStreamListener,
michael@0 40 nsIRequestObserver,
michael@0 41 nsIStreamListener)
michael@0 42
michael@0 43
michael@0 44 void
michael@0 45 MediaDocumentStreamListener::SetStreamListener(nsIStreamListener *aListener)
michael@0 46 {
michael@0 47 mNextStream = aListener;
michael@0 48 }
michael@0 49
michael@0 50 NS_IMETHODIMP
michael@0 51 MediaDocumentStreamListener::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
michael@0 52 {
michael@0 53 NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
michael@0 54
michael@0 55 mDocument->StartLayout();
michael@0 56
michael@0 57 if (mNextStream) {
michael@0 58 return mNextStream->OnStartRequest(request, ctxt);
michael@0 59 }
michael@0 60
michael@0 61 return NS_BINDING_ABORTED;
michael@0 62 }
michael@0 63
michael@0 64 NS_IMETHODIMP
michael@0 65 MediaDocumentStreamListener::OnStopRequest(nsIRequest* request,
michael@0 66 nsISupports *ctxt,
michael@0 67 nsresult status)
michael@0 68 {
michael@0 69 nsresult rv = NS_OK;
michael@0 70 if (mNextStream) {
michael@0 71 rv = mNextStream->OnStopRequest(request, ctxt, status);
michael@0 72 }
michael@0 73
michael@0 74 // No more need for our document so clear our reference and prevent leaks
michael@0 75 mDocument = nullptr;
michael@0 76
michael@0 77 return rv;
michael@0 78 }
michael@0 79
michael@0 80 NS_IMETHODIMP
michael@0 81 MediaDocumentStreamListener::OnDataAvailable(nsIRequest* request,
michael@0 82 nsISupports *ctxt,
michael@0 83 nsIInputStream *inStr,
michael@0 84 uint64_t sourceOffset,
michael@0 85 uint32_t count)
michael@0 86 {
michael@0 87 if (mNextStream) {
michael@0 88 return mNextStream->OnDataAvailable(request, ctxt, inStr, sourceOffset, count);
michael@0 89 }
michael@0 90
michael@0 91 return NS_OK;
michael@0 92 }
michael@0 93
michael@0 94 // default format names for MediaDocument.
michael@0 95 const char* const MediaDocument::sFormatNames[4] =
michael@0 96 {
michael@0 97 "MediaTitleWithNoInfo", // eWithNoInfo
michael@0 98 "MediaTitleWithFile", // eWithFile
michael@0 99 "", // eWithDim
michael@0 100 "" // eWithDimAndFile
michael@0 101 };
michael@0 102
michael@0 103 MediaDocument::MediaDocument()
michael@0 104 : nsHTMLDocument(),
michael@0 105 mDocumentElementInserted(false)
michael@0 106 {
michael@0 107 }
michael@0 108 MediaDocument::~MediaDocument()
michael@0 109 {
michael@0 110 }
michael@0 111
michael@0 112 nsresult
michael@0 113 MediaDocument::Init()
michael@0 114 {
michael@0 115 nsresult rv = nsHTMLDocument::Init();
michael@0 116 NS_ENSURE_SUCCESS(rv, rv);
michael@0 117
michael@0 118 // Create a bundle for the localization
michael@0 119 nsCOMPtr<nsIStringBundleService> stringService =
michael@0 120 mozilla::services::GetStringBundleService();
michael@0 121 if (stringService) {
michael@0 122 stringService->CreateBundle(NSMEDIADOCUMENT_PROPERTIES_URI,
michael@0 123 getter_AddRefs(mStringBundle));
michael@0 124 }
michael@0 125
michael@0 126 mIsSyntheticDocument = true;
michael@0 127
michael@0 128 return NS_OK;
michael@0 129 }
michael@0 130
michael@0 131 nsresult
michael@0 132 MediaDocument::StartDocumentLoad(const char* aCommand,
michael@0 133 nsIChannel* aChannel,
michael@0 134 nsILoadGroup* aLoadGroup,
michael@0 135 nsISupports* aContainer,
michael@0 136 nsIStreamListener** aDocListener,
michael@0 137 bool aReset,
michael@0 138 nsIContentSink* aSink)
michael@0 139 {
michael@0 140 nsresult rv = nsDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup,
michael@0 141 aContainer, aDocListener, aReset,
michael@0 142 aSink);
michael@0 143 if (NS_FAILED(rv)) {
michael@0 144 return rv;
michael@0 145 }
michael@0 146
michael@0 147 // We try to set the charset of the current document to that of the
michael@0 148 // 'genuine' (as opposed to an intervening 'chrome') parent document
michael@0 149 // that may be in a different window/tab. Even if we fail here,
michael@0 150 // we just return NS_OK because another attempt is made in
michael@0 151 // |UpdateTitleAndCharset| and the worst thing possible is a mangled
michael@0 152 // filename in the titlebar and the file picker.
michael@0 153
michael@0 154 // Note that we
michael@0 155 // exclude UTF-8 as 'invalid' because UTF-8 is likely to be the charset
michael@0 156 // of a chrome document that has nothing to do with the actual content
michael@0 157 // whose charset we want to know. Even if "the actual content" is indeed
michael@0 158 // in UTF-8, we don't lose anything because the default empty value is
michael@0 159 // considered synonymous with UTF-8.
michael@0 160
michael@0 161 nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aContainer));
michael@0 162
michael@0 163 // not being able to set the charset is not critical.
michael@0 164 NS_ENSURE_TRUE(docShell, NS_OK);
michael@0 165
michael@0 166 nsAutoCString charset;
michael@0 167 int32_t source;
michael@0 168 nsCOMPtr<nsIPrincipal> principal;
michael@0 169 // opening in a new tab
michael@0 170 docShell->GetParentCharset(charset, &source, getter_AddRefs(principal));
michael@0 171
michael@0 172 if (!charset.IsEmpty() &&
michael@0 173 !charset.Equals("UTF-8") &&
michael@0 174 NodePrincipal()->Equals(principal)) {
michael@0 175 SetDocumentCharacterSetSource(source);
michael@0 176 SetDocumentCharacterSet(charset);
michael@0 177 }
michael@0 178
michael@0 179 return NS_OK;
michael@0 180 }
michael@0 181
michael@0 182 void
michael@0 183 MediaDocument::BecomeInteractive()
michael@0 184 {
michael@0 185 // In principle, if we knew the readyState code to work, we could infer
michael@0 186 // restoration from GetReadyStateEnum() == nsIDocument::READYSTATE_COMPLETE.
michael@0 187 bool restoring = false;
michael@0 188 nsPIDOMWindow* window = GetWindow();
michael@0 189 if (window) {
michael@0 190 nsIDocShell* docShell = window->GetDocShell();
michael@0 191 if (docShell) {
michael@0 192 docShell->GetRestoringDocument(&restoring);
michael@0 193 }
michael@0 194 }
michael@0 195 if (!restoring) {
michael@0 196 MOZ_ASSERT(GetReadyStateEnum() == nsIDocument::READYSTATE_LOADING,
michael@0 197 "Bad readyState");
michael@0 198 SetReadyStateInternal(nsIDocument::READYSTATE_INTERACTIVE);
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 nsresult
michael@0 203 MediaDocument::CreateSyntheticDocument()
michael@0 204 {
michael@0 205 // Synthesize an empty html document
michael@0 206 nsresult rv;
michael@0 207
michael@0 208 nsCOMPtr<nsINodeInfo> nodeInfo;
michael@0 209 nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::html, nullptr,
michael@0 210 kNameSpaceID_XHTML,
michael@0 211 nsIDOMNode::ELEMENT_NODE);
michael@0 212
michael@0 213 nsRefPtr<nsGenericHTMLElement> root = NS_NewHTMLHtmlElement(nodeInfo.forget());
michael@0 214 NS_ENSURE_TRUE(root, NS_ERROR_OUT_OF_MEMORY);
michael@0 215
michael@0 216 NS_ASSERTION(GetChildCount() == 0, "Shouldn't have any kids");
michael@0 217 rv = AppendChildTo(root, false);
michael@0 218 NS_ENSURE_SUCCESS(rv, rv);
michael@0 219
michael@0 220 nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::head, nullptr,
michael@0 221 kNameSpaceID_XHTML,
michael@0 222 nsIDOMNode::ELEMENT_NODE);
michael@0 223
michael@0 224 // Create a <head> so our title has somewhere to live
michael@0 225 nsRefPtr<nsGenericHTMLElement> head = NS_NewHTMLHeadElement(nodeInfo.forget());
michael@0 226 NS_ENSURE_TRUE(head, NS_ERROR_OUT_OF_MEMORY);
michael@0 227
michael@0 228 nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::meta, nullptr,
michael@0 229 kNameSpaceID_XHTML,
michael@0 230 nsIDOMNode::ELEMENT_NODE);
michael@0 231
michael@0 232 nsRefPtr<nsGenericHTMLElement> metaContent = NS_NewHTMLMetaElement(nodeInfo.forget());
michael@0 233 NS_ENSURE_TRUE(metaContent, NS_ERROR_OUT_OF_MEMORY);
michael@0 234 metaContent->SetAttr(kNameSpaceID_None, nsGkAtoms::name,
michael@0 235 NS_LITERAL_STRING("viewport"),
michael@0 236 true);
michael@0 237
michael@0 238 metaContent->SetAttr(kNameSpaceID_None, nsGkAtoms::content,
michael@0 239 NS_LITERAL_STRING("width=device-width; height=device-height;"),
michael@0 240 true);
michael@0 241 head->AppendChildTo(metaContent, false);
michael@0 242
michael@0 243 root->AppendChildTo(head, false);
michael@0 244
michael@0 245 nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::body, nullptr,
michael@0 246 kNameSpaceID_XHTML,
michael@0 247 nsIDOMNode::ELEMENT_NODE);
michael@0 248
michael@0 249 nsRefPtr<nsGenericHTMLElement> body = NS_NewHTMLBodyElement(nodeInfo.forget());
michael@0 250 NS_ENSURE_TRUE(body, NS_ERROR_OUT_OF_MEMORY);
michael@0 251
michael@0 252 root->AppendChildTo(body, false);
michael@0 253
michael@0 254 return NS_OK;
michael@0 255 }
michael@0 256
michael@0 257 nsresult
michael@0 258 MediaDocument::StartLayout()
michael@0 259 {
michael@0 260 mMayStartLayout = true;
michael@0 261 nsCOMPtr<nsIPresShell> shell = GetShell();
michael@0 262 // Don't mess with the presshell if someone has already handled
michael@0 263 // its initial reflow.
michael@0 264 if (shell && !shell->DidInitialize()) {
michael@0 265 nsRect visibleArea = shell->GetPresContext()->GetVisibleArea();
michael@0 266 nsresult rv = shell->Initialize(visibleArea.width, visibleArea.height);
michael@0 267 NS_ENSURE_SUCCESS(rv, rv);
michael@0 268 }
michael@0 269
michael@0 270 return NS_OK;
michael@0 271 }
michael@0 272
michael@0 273 void
michael@0 274 MediaDocument::GetFileName(nsAString& aResult)
michael@0 275 {
michael@0 276 aResult.Truncate();
michael@0 277
michael@0 278 nsCOMPtr<nsIURL> url = do_QueryInterface(mDocumentURI);
michael@0 279 if (!url)
michael@0 280 return;
michael@0 281
michael@0 282 nsAutoCString fileName;
michael@0 283 url->GetFileName(fileName);
michael@0 284 if (fileName.IsEmpty())
michael@0 285 return;
michael@0 286
michael@0 287 nsAutoCString docCharset;
michael@0 288 // Now that the charset is set in |StartDocumentLoad| to the charset of
michael@0 289 // the document viewer instead of a bogus value ("ISO-8859-1" set in
michael@0 290 // |nsDocument|'s ctor), the priority is given to the current charset.
michael@0 291 // This is necessary to deal with a media document being opened in a new
michael@0 292 // window or a new tab, in which case |originCharset| of |nsIURI| is not
michael@0 293 // reliable.
michael@0 294 if (mCharacterSetSource != kCharsetUninitialized) {
michael@0 295 docCharset = mCharacterSet;
michael@0 296 } else {
michael@0 297 // resort to |originCharset|
michael@0 298 url->GetOriginCharset(docCharset);
michael@0 299 SetDocumentCharacterSet(docCharset);
michael@0 300 }
michael@0 301
michael@0 302 nsresult rv;
michael@0 303 nsCOMPtr<nsITextToSubURI> textToSubURI =
michael@0 304 do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
michael@0 305 if (NS_SUCCEEDED(rv)) {
michael@0 306 // UnEscapeURIForUI always succeeds
michael@0 307 textToSubURI->UnEscapeURIForUI(docCharset, fileName, aResult);
michael@0 308 } else {
michael@0 309 CopyUTF8toUTF16(fileName, aResult);
michael@0 310 }
michael@0 311 }
michael@0 312
michael@0 313 nsresult
michael@0 314 MediaDocument::LinkStylesheet(const nsAString& aStylesheet)
michael@0 315 {
michael@0 316 nsCOMPtr<nsINodeInfo> nodeInfo;
michael@0 317 nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::link, nullptr,
michael@0 318 kNameSpaceID_XHTML,
michael@0 319 nsIDOMNode::ELEMENT_NODE);
michael@0 320
michael@0 321 nsRefPtr<nsGenericHTMLElement> link = NS_NewHTMLLinkElement(nodeInfo.forget());
michael@0 322 NS_ENSURE_TRUE(link, NS_ERROR_OUT_OF_MEMORY);
michael@0 323
michael@0 324 link->SetAttr(kNameSpaceID_None, nsGkAtoms::rel,
michael@0 325 NS_LITERAL_STRING("stylesheet"), true);
michael@0 326
michael@0 327 link->SetAttr(kNameSpaceID_None, nsGkAtoms::href, aStylesheet, true);
michael@0 328
michael@0 329 Element* head = GetHeadElement();
michael@0 330 return head->AppendChildTo(link, false);
michael@0 331 }
michael@0 332
michael@0 333 void
michael@0 334 MediaDocument::UpdateTitleAndCharset(const nsACString& aTypeStr,
michael@0 335 const char* const* aFormatNames,
michael@0 336 int32_t aWidth, int32_t aHeight,
michael@0 337 const nsAString& aStatus)
michael@0 338 {
michael@0 339 nsXPIDLString fileStr;
michael@0 340 GetFileName(fileStr);
michael@0 341
michael@0 342 NS_ConvertASCIItoUTF16 typeStr(aTypeStr);
michael@0 343 nsXPIDLString title;
michael@0 344
michael@0 345 if (mStringBundle) {
michael@0 346 // if we got a valid size (not all media have a size)
michael@0 347 if (aWidth != 0 && aHeight != 0) {
michael@0 348 nsAutoString widthStr;
michael@0 349 nsAutoString heightStr;
michael@0 350 widthStr.AppendInt(aWidth);
michael@0 351 heightStr.AppendInt(aHeight);
michael@0 352 // If we got a filename, display it
michael@0 353 if (!fileStr.IsEmpty()) {
michael@0 354 const char16_t *formatStrings[4] = {fileStr.get(), typeStr.get(),
michael@0 355 widthStr.get(), heightStr.get()};
michael@0 356 NS_ConvertASCIItoUTF16 fmtName(aFormatNames[eWithDimAndFile]);
michael@0 357 mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 4,
michael@0 358 getter_Copies(title));
michael@0 359 }
michael@0 360 else {
michael@0 361 const char16_t *formatStrings[3] = {typeStr.get(), widthStr.get(),
michael@0 362 heightStr.get()};
michael@0 363 NS_ConvertASCIItoUTF16 fmtName(aFormatNames[eWithDim]);
michael@0 364 mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 3,
michael@0 365 getter_Copies(title));
michael@0 366 }
michael@0 367 }
michael@0 368 else {
michael@0 369 // If we got a filename, display it
michael@0 370 if (!fileStr.IsEmpty()) {
michael@0 371 const char16_t *formatStrings[2] = {fileStr.get(), typeStr.get()};
michael@0 372 NS_ConvertASCIItoUTF16 fmtName(aFormatNames[eWithFile]);
michael@0 373 mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 2,
michael@0 374 getter_Copies(title));
michael@0 375 }
michael@0 376 else {
michael@0 377 const char16_t *formatStrings[1] = {typeStr.get()};
michael@0 378 NS_ConvertASCIItoUTF16 fmtName(aFormatNames[eWithNoInfo]);
michael@0 379 mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 1,
michael@0 380 getter_Copies(title));
michael@0 381 }
michael@0 382 }
michael@0 383 }
michael@0 384
michael@0 385 // set it on the document
michael@0 386 if (aStatus.IsEmpty()) {
michael@0 387 SetTitle(title);
michael@0 388 }
michael@0 389 else {
michael@0 390 nsXPIDLString titleWithStatus;
michael@0 391 const nsPromiseFlatString& status = PromiseFlatString(aStatus);
michael@0 392 const char16_t *formatStrings[2] = {title.get(), status.get()};
michael@0 393 NS_NAMED_LITERAL_STRING(fmtName, "TitleWithStatus");
michael@0 394 mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 2,
michael@0 395 getter_Copies(titleWithStatus));
michael@0 396 SetTitle(titleWithStatus);
michael@0 397 }
michael@0 398 }
michael@0 399
michael@0 400 void
michael@0 401 MediaDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aGlobalObject)
michael@0 402 {
michael@0 403 nsHTMLDocument::SetScriptGlobalObject(aGlobalObject);
michael@0 404 if (!mDocumentElementInserted && aGlobalObject) {
michael@0 405 mDocumentElementInserted = true;
michael@0 406 nsContentUtils::AddScriptRunner(
michael@0 407 new nsDocElementCreatedNotificationRunner(this));
michael@0 408 }
michael@0 409 }
michael@0 410
michael@0 411 } // namespace dom
michael@0 412 } // namespace mozilla

mercurial