DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


#include "MediaDocument.h"
#include "nsGkAtoms.h"
#include "nsRect.h"
#include "nsPresContext.h"
#include "nsViewManager.h"
#include "nsViewManager.h"
#include "nsITextToSubURI.h"
#include "nsIURL.h"
#include "nsIDocShell.h"
#include "nsCharsetSource.h"  // kCharsetFrom* macro definition
#include "nsNodeInfoManager.h"
#include "nsNodeInfoManager.h"
#include "nsContentUtils.h"
#include "nsDocElementCreatedNotificationRunner.h"
#include "mozilla/PresShell.h"
#include "mozilla/Services.h"
#include "nsServiceManagerUtils.h"
#include "nsIPrincipal.h"
#include "nsIMultiPartChannel.h"
#include "nsProxyRelease.h"


namespace mozilla {
namespace dom {

MediaDocumentStreamListener::MediaDocumentStreamListener(
    MediaDocument* aDocument)
    : mDocument(aDocument) {}


MediaDocumentStreamListener::~MediaDocumentStreamListener() {
MediaDocumentStreamListener::~MediaDocumentStreamListener() {
  if (mDocument && !NS_IsMainThread()) {
    nsCOMPtr<nsIEventTarget> mainTarget(do_GetMainThread());
    NS_ProxyRelease("MediaDocumentStreamListener::mDocument", mainTarget,
    NS_ProxyRelease("MediaDocumentStreamListener::mDocument", mainTarget,
                    mDocument.forget());
  }
}

NS_IMPL_ISUPPORTS(MediaDocumentStreamListener, nsIRequestObserver,
NS_IMPL_ISUPPORTS(MediaDocumentStreamListener, nsIRequestObserver,
                  nsIStreamListener, nsIThreadRetargetableStreamListener)

void MediaDocumentStreamListener::SetStreamListener(
    nsIStreamListener* aListener) {
  mNextStream = aListener;
  mNextStream = aListener;
}

NS_IMETHODIMP
MediaDocumentStreamListener::OnStartRequest(nsIRequest* request) {
  NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
  NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);

  mDocument->StartLayout();

  if (mNextStream) {
    return mNextStream->OnStartRequest(request);
    return mNextStream->OnStartRequest(request);
  }

  return NS_ERROR_PARSED_DATA_CACHED;
}
}

NS_IMETHODIMP
MediaDocumentStreamListener::OnStopRequest(nsIRequest* request,
                                           nsresult status) {
  nsresult rv = NS_OK;
  nsresult rv = NS_OK;
  if (mNextStream) {
    rv = mNextStream->OnStopRequest(request, status);
  }

  // Don't release mDocument here if we're in the middle of a multipart
  // Don't release mDocument here if we're in the middle of a multipart
  // response.
  bool lastPart = true;
  nsCOMPtr<nsIMultiPartChannel> mpchan(do_QueryInterface(request));
  if (mpchan) {
    mpchan->GetIsLastPart(&lastPart);
  }


  if (lastPart) {
    mDocument = nullptr;
  }
  return rv;
}
}

NS_IMETHODIMP
MediaDocumentStreamListener::OnDataAvailable(nsIRequest* request,
                                             nsIInputStream* inStr,
                                             uint64_t sourceOffset,
                                             uint64_t sourceOffset,
                                             uint32_t count) {
  if (mNextStream) {
    return mNextStream->OnDataAvailable(request, inStr, sourceOffset, count);
  }
  }

  return NS_OK;
}

NS_IMETHODIMP
NS_IMETHODIMP
MediaDocumentStreamListener::CheckListenerChain() {
  nsCOMPtr<nsIThreadRetargetableStreamListener> retargetable =
      do_QueryInterface(mNextStream);
  if (retargetable) {
    return retargetable->CheckListenerChain();
    return retargetable->CheckListenerChain();
  }
  return NS_ERROR_NO_INTERFACE;
}

// default format names for MediaDocument.
// default format names for MediaDocument.
const char* const MediaDocument::sFormatNames[4] = {
    "MediaTitleWithNoInfo",  // eWithNoInfo
    "MediaTitleWithFile",    // eWithFile
    "",                      // eWithDim
    ""                       // eWithDimAndFile
    ""                       // eWithDimAndFile
};

MediaDocument::MediaDocument()
    : nsHTMLDocument(), mDidInitialDocumentSetup(false) {}
    : nsHTMLDocument(), mDidInitialDocumentSetup(false) {}
MediaDocument::~MediaDocument() = default;

nsresult MediaDocument::Init() {
  nsresult rv = nsHTMLDocument::Init();
  NS_ENSURE_SUCCESS(rv, rv);
  NS_ENSURE_SUCCESS(rv, rv);

  mIsSyntheticDocument = true;

  return NS_OK;
}
}

nsresult MediaDocument::StartDocumentLoad(const char* aCommand,
                                          nsIChannel* aChannel,
                                          nsILoadGroup* aLoadGroup,
                                          nsISupports* aContainer,
                                          nsISupports* aContainer,
                                          nsIStreamListener** aDocListener,
                                          bool aReset, nsIContentSink* aSink) {
  nsresult rv = Document::StartDocumentLoad(
      aCommand, aChannel, aLoadGroup, aContainer, aDocListener, aReset, aSink);
  if (NS_FAILED(rv)) {
  if (NS_FAILED(rv)) {
    return rv;
  }

  // We try to set the charset of the current document to that of the
  // 'genuine' (as opposed to an intervening 'chrome') parent document
  // 'genuine' (as opposed to an intervening 'chrome') parent document
  // that may be in a different window/tab. Even if we fail here,
  // we just return NS_OK because another attempt is made in
  // |UpdateTitleAndCharset| and the worst thing possible is a mangled
  // filename in the titlebar and the file picker.

  // Note that we
  // exclude UTF-8 as 'invalid' because UTF-8 is likely to be the charset
  // exclude UTF-8 as 'invalid' because UTF-8 is likely to be the charset
  // of a chrome document that has nothing to do with the actual content
  // whose charset we want to know. Even if "the actual content" is indeed
  // in UTF-8, we don't lose anything because the default empty value is
  // considered synonymous with UTF-8.


  nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aContainer));

  // not being able to set the charset is not critical.
  // not being able to set the charset is not critical.
  NS_ENSURE_TRUE(docShell, NS_OK);

  const Encoding* encoding;
  int32_t source;
  nsCOMPtr<nsIPrincipal> principal;
  nsCOMPtr<nsIPrincipal> principal;
  // opening in a new tab
  docShell->GetParentCharset(encoding, &source, getter_AddRefs(principal));

  if (encoding && encoding != UTF_8_ENCODING &&
      NodePrincipal()->Equals(principal)) {
      NodePrincipal()->Equals(principal)) {
    SetDocumentCharacterSetSource(source);
    SetDocumentCharacterSet(WrapNotNull(encoding));
  }

  return NS_OK;
  return NS_OK;
}

void MediaDocument::InitialSetupDone() {
  MOZ_ASSERT(GetReadyStateEnum() == Document::READYSTATE_LOADING,
             "Bad readyState: we should still be doing our initial load");
             "Bad readyState: we should still be doing our initial load");
  mDidInitialDocumentSetup = true;
  nsContentUtils::AddScriptRunner(
      new nsDocElementCreatedNotificationRunner(this));
  SetReadyStateInternal(Document::READYSTATE_INTERACTIVE);
}

nsresult MediaDocument::CreateSyntheticDocument() {
nsresult MediaDocument::CreateSyntheticDocument() {
  MOZ_ASSERT(!InitialSetupHasBeenDone());

  // Synthesize an empty html document
  nsresult rv;


  RefPtr<mozilla::dom::NodeInfo> nodeInfo;
  nodeInfo = mNodeInfoManager->GetNodeInfo(
      nsGkAtoms::html, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);

  RefPtr<nsGenericHTMLElement> root = NS_NewHTMLHtmlElement(nodeInfo.forget());
  RefPtr<nsGenericHTMLElement> root = NS_NewHTMLHtmlElement(nodeInfo.forget());
  NS_ENSURE_TRUE(root, NS_ERROR_OUT_OF_MEMORY);

  NS_ASSERTION(GetChildCount() == 0, "Shouldn't have any kids");
  rv = AppendChildTo(root, false);
  NS_ENSURE_SUCCESS(rv, rv);

  nodeInfo = mNodeInfoManager->GetNodeInfo(
      nsGkAtoms::head, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);

  // Create a <head> so our title has somewhere to live
  // Create a <head> so our title has somewhere to live
  RefPtr<nsGenericHTMLElement> head = NS_NewHTMLHeadElement(nodeInfo.forget());
  NS_ENSURE_TRUE(head, NS_ERROR_OUT_OF_MEMORY);

  nodeInfo = mNodeInfoManager->GetNodeInfo(
      nsGkAtoms::meta, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);

  RefPtr<nsGenericHTMLElement> metaContent =
  RefPtr<nsGenericHTMLElement> metaContent =
      NS_NewHTMLMetaElement(nodeInfo.forget());
  NS_ENSURE_TRUE(metaContent, NS_ERROR_OUT_OF_MEMORY);
  metaContent->SetAttr(kNameSpaceID_None, nsGkAtoms::name,
                       NS_LITERAL_STRING("viewport"), true);


  metaContent->SetAttr(
      kNameSpaceID_None, nsGkAtoms::content,
      NS_LITERAL_STRING("width=device-width; height=device-height;"), true);
  head->AppendChildTo(metaContent, false);


  root->AppendChildTo(head, false);

  nodeInfo = mNodeInfoManager->GetNodeInfo(
      nsGkAtoms::body, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);


  RefPtr<nsGenericHTMLElement> body = NS_NewHTMLBodyElement(nodeInfo.forget());
  NS_ENSURE_TRUE(body, NS_ERROR_OUT_OF_MEMORY);

  root->AppendChildTo(body, false);


  return NS_OK;
}

nsresult MediaDocument::StartLayout() {
nsresult MediaDocument::StartLayout() {
  mMayStartLayout = true;
  RefPtr<PresShell> presShell = GetPresShell();
  // Don't mess with the presshell if someone has already handled
  // its initial reflow.
  if (presShell && !presShell->DidInitialize()) {
    nsresult rv = presShell->Initialize();
    NS_ENSURE_SUCCESS(rv, rv);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  return NS_OK;
}


void MediaDocument::GetFileName(nsAString& aResult, nsIChannel* aChannel) {
  aResult.Truncate();

  if (aChannel) {
  if (aChannel) {
    aChannel->GetContentDispositionFilename(aResult);
    if (!aResult.IsEmpty()) return;
  }

  nsCOMPtr<nsIURL> url = do_QueryInterface(mDocumentURI);
  nsCOMPtr<nsIURL> url = do_QueryInterface(mDocumentURI);
  if (!url) return;

  nsAutoCString fileName;
  url->GetFileName(fileName);
  if (fileName.IsEmpty()) return;
  if (fileName.IsEmpty()) return;

  // Now that the charset is set in |StartDocumentLoad| to the charset of
  // the document viewer instead of a bogus value ("windows-1252" set in
  // |Document|'s ctor), the priority is given to the current charset.
  // This is necessary to deal with a media document being opened in a new
  // This is necessary to deal with a media document being opened in a new
  // window or a new tab.
  if (mCharacterSetSource == kCharsetUninitialized) {
    // resort to UTF-8
    SetDocumentCharacterSet(UTF_8_ENCODING);
  }
  }

  nsresult rv;
  nsCOMPtr<nsITextToSubURI> textToSubURI =
      do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
  if (NS_SUCCEEDED(rv)) {
  if (NS_SUCCEEDED(rv)) {
    // UnEscapeURIForUI always succeeds
    textToSubURI->UnEscapeURIForUI(fileName, aResult);
  } else {
    CopyUTF8toUTF16(fileName, aResult);
  }
  }
}
}

nsresult MediaDocument::LinkStylesheet(const nsAString& aStylesheet) {
  RefPtr<mozilla::dom::NodeInfo> nodeInfo;
  nodeInfo = mNodeInfoManager->GetNodeInfo(
      nsGkAtoms::link, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);
      nsGkAtoms::link, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);

  RefPtr<nsGenericHTMLElement> link = NS_NewHTMLLinkElement(nodeInfo.forget());
  NS_ENSURE_TRUE(link, NS_ERROR_OUT_OF_MEMORY);

  link->SetAttr(kNameSpaceID_None, nsGkAtoms::rel,
  link->SetAttr(kNameSpaceID_None, nsGkAtoms::rel,
                NS_LITERAL_STRING("stylesheet"), true);

  link->SetAttr(kNameSpaceID_None, nsGkAtoms::href, aStylesheet, true);

  Element* head = GetHeadElement();
  Element* head = GetHeadElement();
  return head->AppendChildTo(link, false);
}

nsresult MediaDocument::LinkScript(const nsAString& aScript) {
nsresult MediaDocument::LinkScript(const nsAString& aScript) {
  RefPtr<mozilla::dom::NodeInfo> nodeInfo;
  nodeInfo = mNodeInfoManager->GetNodeInfo(
      nsGkAtoms::script, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);

  RefPtr<nsGenericHTMLElement> script =
  RefPtr<nsGenericHTMLElement> script =
      NS_NewHTMLScriptElement(nodeInfo.forget());
  NS_ENSURE_TRUE(script, NS_ERROR_OUT_OF_MEMORY);

  script->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
                  NS_LITERAL_STRING("text/javascript"), true);
                  NS_LITERAL_STRING("text/javascript"), true);

  script->SetAttr(kNameSpaceID_None, nsGkAtoms::src, aScript, true);

  Element* head = GetHeadElement();
  return head->AppendChildTo(script, false);
  return head->AppendChildTo(script, false);
}

void MediaDocument::FormatStringFromName(const char* aName,
                                         const nsTArray<nsString>& aParams,
                                         nsAString& aResult) {
                                         nsAString& aResult) {
  bool spoofLocale = nsContentUtils::SpoofLocaleEnglish() && !AllowsL10n();
  if (!spoofLocale) {
  if (!spoofLocale) {
    if (!mStringBundle) {
      nsCOMPtr<nsIStringBundleService> stringService =
          mozilla::services::GetStringBundleService();
      if (stringService) {
        stringService->CreateBundle(NSMEDIADOCUMENT_PROPERTIES_URI,
                                    getter_AddRefs(mStringBundle));
      }
      }
    }
    if (mStringBundle) {
      mStringBundle->FormatStringFromName(aName, aParams, aResult);
    }
  } else {
  } else {
    if (!mStringBundleEnglish) {
      nsCOMPtr<nsIStringBundleService> stringService =
          mozilla::services::GetStringBundleService();
      if (stringService) {
        stringService->CreateBundle(NSMEDIADOCUMENT_PROPERTIES_URI_en_US,
        stringService->CreateBundle(NSMEDIADOCUMENT_PROPERTIES_URI_en_US,
                                    getter_AddRefs(mStringBundleEnglish));
      }
    }
    if (mStringBundleEnglish) {
      mStringBundleEnglish->FormatStringFromName(aName, aParams, aResult);
      mStringBundleEnglish->FormatStringFromName(aName, aParams, aResult);
    }
  }
}

void MediaDocument::UpdateTitleAndCharset(const nsACString& aTypeStr,
void MediaDocument::UpdateTitleAndCharset(const nsACString& aTypeStr,
                                          nsIChannel* aChannel,
                                          const char* const* aFormatNames,
                                          int32_t aWidth, int32_t aHeight,
                                          const nsAString& aStatus) {
                                          const nsAString& aStatus) {
  nsAutoString fileStr;
  GetFileName(fileStr, aChannel);

  NS_ConvertASCIItoUTF16 typeStr(aTypeStr);
  nsAutoString title;
  nsAutoString title;

  // if we got a valid size (not all media have a size)
  if (aWidth != 0 && aHeight != 0) {
    nsAutoString widthStr;
    nsAutoString heightStr;
    nsAutoString heightStr;
    widthStr.AppendInt(aWidth);
    heightStr.AppendInt(aHeight);
    // If we got a filename, display it
    if (!fileStr.IsEmpty()) {
      AutoTArray<nsString, 4> formatStrings = {fileStr, typeStr, widthStr,
      AutoTArray<nsString, 4> formatStrings = {fileStr, typeStr, widthStr,
                                               heightStr};
      FormatStringFromName(aFormatNames[eWithDimAndFile], formatStrings, title);
    } else {
      AutoTArray<nsString, 3> formatStrings = {typeStr, widthStr, heightStr};
      FormatStringFromName(aFormatNames[eWithDim], formatStrings, title);
      FormatStringFromName(aFormatNames[eWithDim], formatStrings, title);
    }
  } else {
    // If we got a filename, display it
    if (!fileStr.IsEmpty()) {
      AutoTArray<nsString, 2> formatStrings = {fileStr, typeStr};
      AutoTArray<nsString, 2> formatStrings = {fileStr, typeStr};
      FormatStringFromName(aFormatNames[eWithFile], formatStrings, title);
    } else {
      AutoTArray<nsString, 1> formatStrings = {typeStr};
      FormatStringFromName(aFormatNames[eWithNoInfo], formatStrings, title);
    }
    }
  }

  // set it on the document
  if (aStatus.IsEmpty()) {
    IgnoredErrorResult ignored;
    IgnoredErrorResult ignored;
    SetTitle(title, ignored);
  } else {
    nsAutoString titleWithStatus;
    AutoTArray<nsString, 2> formatStrings;
    formatStrings.AppendElement(title);
    formatStrings.AppendElement(title);
    formatStrings.AppendElement(aStatus);
    FormatStringFromName("TitleWithStatus", formatStrings, titleWithStatus);
    SetTitle(titleWithStatus, IgnoreErrors());
  }
}

}  // namespace dom
}  // namespace dom
}  // namespace mozilla