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 (f466abbf4a32)

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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=4 sw=2 et 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/. */

/*

  An implementation for the XUL document. This implementation serves
  as the basis for generating an NGLayout content model.

  Notes
  -----

  1. We do some monkey business in the document observer methods to
     keep the element map in sync for HTML elements. Why don't we just
     do it for _all_ elements? Well, in the case of XUL elements,
     which may be lazily created during frame construction, the
     document observer methods will never be called because we'll be
     adding the XUL nodes into the content model "quietly".

*/

#include "mozilla/ArrayUtils.h"

#include <algorithm>

#include "XULDocument.h"

#include "nsError.h"
#include "nsView.h"
#include "nsViewManager.h"
#include "nsIContentViewer.h"
#include "nsIStreamListener.h"
#include "nsITimer.h"
#include "nsDocShell.h"
#include "nsGkAtoms.h"
#include "nsXMLContentSink.h"
#include "nsXULContentSink.h"
#include "nsXULContentUtils.h"
#include "nsIStringEnumerator.h"
#include "nsDocElementCreatedNotificationRunner.h"
#include "nsNetUtil.h"
#include "nsParserCIID.h"
#include "nsString.h"
#include "nsPIDOMWindow.h"
#include "nsPIWindowRoot.h"
#include "nsXULElement.h"
#include "nsXULPrototypeCache.h"
#include "mozilla/Logging.h"
#include "nsIFrame.h"
#include "nsXBLService.h"
#include "nsCExternalHandlerService.h"
#include "nsMimeTypes.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#include "nsContentList.h"
#include "nsISimpleEnumerator.h"
#include "nsIScriptGlobalObject.h"
#include "nsIScriptSecurityManager.h"
#include "nsNodeInfoManager.h"
#include "nsContentCreatorFunctions.h"
#include "nsContentUtils.h"
#include "nsIParser.h"
#include "nsCharsetSource.h"
#include "mozilla/StyleSheetInlines.h"
#include "mozilla/css/Loader.h"
#include "nsIScriptError.h"
#include "nsIStyleSheetLinkingElement.h"
#include "nsIObserverService.h"
#include "nsNodeUtils.h"
#include "nsIXULWindow.h"
#include "nsXULPopupManager.h"
#include "nsCCUncollectableMarker.h"
#include "nsURILoader.h"
#include "mozilla/BasicEvents.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/NodeInfoInlines.h"
#include "mozilla/dom/ProcessingInstruction.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/XULDocumentBinding.h"
#include "mozilla/dom/XULPersist.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/LoadInfo.h"
#include "mozilla/Preferences.h"
#include "nsTextNode.h"
#include "nsJSUtils.h"
#include "js/CompilationAndEvaluation.h"
#include "js/SourceText.h"
#include "mozilla/dom/URL.h"
#include "nsIContentPolicy.h"
#include "mozAutoDocUpdate.h"
#include "xpcpublic.h"
#include "mozilla/StyleSheet.h"
#include "mozilla/StyleSheetInlines.h"
#include "nsIConsoleService.h"

#include "mozilla/parser/PrototypeDocumentParser.h"
#include "mozilla/dom/PrototypeDocumentContentSink.h"

using namespace mozilla;
using namespace mozilla::dom;

//----------------------------------------------------------------------
//
// CIDs
//

static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);

//----------------------------------------------------------------------
//
// Statics
//

int32_t XULDocument::gRefCnt = 0;

LazyLogModule XULDocument::gXULLog("XULDocument");

//----------------------------------------------------------------------
//
// ctors & dtors
//

namespace mozilla {
namespace dom {

XULDocument::XULDocument(void)
    : XMLDocument("application/vnd.mozilla.xul+xml") {
  // Override the default in Document
  mCharacterSet = UTF_8_ENCODING;

  mDefaultElementType = kNameSpaceID_XUL;
  mType = eXUL;

  mAllowXULXBL = eTriTrue;
}

XULDocument::~XULDocument() {}

}  // namespace dom
}  // namespace mozilla

nsresult NS_NewXULDocument(Document** result) {
  MOZ_ASSERT(result != nullptr, "null ptr");
  if (!result) return NS_ERROR_NULL_POINTER;

  RefPtr<XULDocument> doc = new XULDocument();

  nsresult rv;
  if (NS_FAILED(rv = doc->Init())) {
    return rv;
  }

  doc.forget(result);
  return NS_OK;
}

namespace mozilla {
namespace dom {

//----------------------------------------------------------------------
//
// nsISupports interface
//

NS_IMPL_CYCLE_COLLECTION_CLASS(XULDocument)

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(XULDocument, XMLDocument)
  NS_ASSERTION(
      !nsCCUncollectableMarker::InGeneration(cb, tmp->GetMarkedCCGeneration()),
      "Shouldn't traverse XULDocument!");
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(XULDocument, XMLDocument)
  // XXX We should probably unlink all the objects we traverse.
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(XULDocument, XMLDocument)

//----------------------------------------------------------------------
//
// Document interface
//

void XULDocument::Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup) {
  MOZ_ASSERT_UNREACHABLE("Reset");
}

void XULDocument::ResetToURI(nsIURI* aURI, nsILoadGroup* aLoadGroup,
                             nsIPrincipal* aPrincipal,
                             nsIPrincipal* aStoragePrincipal) {
  MOZ_ASSERT_UNREACHABLE("ResetToURI");
}

void XULDocument::SetContentType(const nsAString& aContentType) {
  NS_ASSERTION(
      aContentType.EqualsLiteral("application/vnd.mozilla.xul+xml"),
      "xul-documents always has content-type application/vnd.mozilla.xul+xml");
  // Don't do anything, xul always has the mimetype
  // application/vnd.mozilla.xul+xml
}

// This is called when the master document begins loading, whether it's
// being cached or not.
nsresult XULDocument::StartDocumentLoad(const char* aCommand,
                                        nsIChannel* aChannel,
                                        nsILoadGroup* aLoadGroup,
                                        nsISupports* aContainer,
                                        nsIStreamListener** aDocListener,
                                        bool aReset, nsIContentSink* aSink) {
  if (MOZ_LOG_TEST(gXULLog, LogLevel::Warning)) {
    nsCOMPtr<nsIURI> uri;
    nsresult rv = aChannel->GetOriginalURI(getter_AddRefs(uri));
    if (NS_SUCCEEDED(rv)) {
      nsAutoCString urlspec;
      rv = uri->GetSpec(urlspec);
      if (NS_SUCCEEDED(rv)) {
        MOZ_LOG(gXULLog, LogLevel::Warning,
                ("xul: load document '%s'", urlspec.get()));
      }
    }
  }

  MOZ_ASSERT(GetReadyStateEnum() == Document::READYSTATE_UNINITIALIZED,
             "Bad readyState");
  SetReadyStateInternal(READYSTATE_LOADING);

  mDocumentLoadGroup = do_GetWeakReference(aLoadGroup);

  mChannel = aChannel;

  // Get the URI.  Note that this should match nsDocShell::OnLoadingSite
  nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(mDocumentURI));
  NS_ENSURE_SUCCESS(rv, rv);

  mOriginalURI = mDocumentURI;

  // Get the document's principal
  nsCOMPtr<nsIPrincipal> principal;
  nsCOMPtr<nsIPrincipal> storagePrincipal;
  rv = nsContentUtils::GetSecurityManager()->GetChannelResultPrincipals(
      mChannel, getter_AddRefs(principal), getter_AddRefs(storagePrincipal));
  NS_ENSURE_SUCCESS(rv, rv);

  bool equal = principal->Equals(storagePrincipal);

  principal = MaybeDowngradePrincipal(principal);
  if (equal) {
    storagePrincipal = principal;
  } else {
    storagePrincipal = MaybeDowngradePrincipal(storagePrincipal);
  }

  SetPrincipals(principal, storagePrincipal);

  ResetStylesheetsToURI(mDocumentURI);

  RetrieveRelevantHeaders(aChannel);

  mParser = new mozilla::parser::PrototypeDocumentParser(mDocumentURI, this);
  nsCOMPtr<nsIStreamListener> listener = mParser->GetStreamListener();
  listener.forget(aDocListener);

  nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aContainer));
  nsCOMPtr<nsIContentSink> sink;
  NS_NewPrototypeDocumentContentSink(getter_AddRefs(sink), this, mDocumentURI,
                                     docShell, aChannel);
  mParser->SetContentSink(sink);

  mParser->Parse(mDocumentURI, nullptr, (void*)this);

  return NS_OK;
}

void XULDocument::EndLoad() { Document::EndLoad(); }

//----------------------------------------------------------------------
//
// nsINode interface
//

nsresult XULDocument::Clone(mozilla::dom::NodeInfo* aNodeInfo,
                            nsINode** aResult) const {
  // We don't allow cloning of a XUL document
  *aResult = nullptr;
  return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
}

//----------------------------------------------------------------------
//
// Implementation methods
//

nsresult XULDocument::Init() {
  nsresult rv = XMLDocument::Init();
  NS_ENSURE_SUCCESS(rv, rv);

  if (gRefCnt++ == 0) {
    // ensure that the XUL prototype cache is instantiated successfully,
    // so that we can use nsXULPrototypeCache::GetInstance() without
    // null-checks in the rest of the class.
    nsXULPrototypeCache* cache = nsXULPrototypeCache::GetInstance();
    if (!cache) {
      NS_ERROR("Could not instantiate nsXULPrototypeCache");
      return NS_ERROR_FAILURE;
    }
  }

  return NS_OK;
}

JSObject* XULDocument::WrapNode(JSContext* aCx,
                                JS::Handle<JSObject*> aGivenProto) {
  return XULDocument_Binding::Wrap(aCx, this, aGivenProto);
}

}  // namespace dom
}  // namespace mozilla