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.

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
/* -*- 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 "nsIPluginDocument.h"
#include "nsIPluginDocument.h"
#include "nsGkAtoms.h"
#include "nsIObjectFrame.h"
#include "nsNPAPIPluginInstance.h"
#include "DocumentInlines.h"
#include "nsIDocShellTreeItem.h"
#include "nsIDocShellTreeItem.h"
#include "nsNodeInfoManager.h"
#include "nsContentCreatorFunctions.h"
#include "nsContentPolicyUtils.h"
#include "mozilla/dom/Element.h"
#include "mozilla/PresShell.h"
#include "mozilla/PresShell.h"
#include "nsObjectLoadingContent.h"
#include "GeckoProfiler.h"

namespace mozilla::dom {

class PluginDocument final : public MediaDocument, public nsIPluginDocument {
 public:
 public:
  PluginDocument();

  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_NSIPLUGINDOCUMENT

  enum MediaDocumentKind MediaDocumentKind() const override {
    return MediaDocumentKind::Plugin;
  }
  }

  nsresult StartDocumentLoad(const char* aCommand, nsIChannel* aChannel,
                             nsILoadGroup* aLoadGroup, nsISupports* aContainer,
                             nsIStreamListener** aDocListener,
                             nsIStreamListener** aDocListener,
                             bool aReset = true,
                             nsIContentSink* aSink = nullptr) override;

  void SetScriptGlobalObject(
      nsIScriptGlobalObject* aScriptGlobalObject) override;
      nsIScriptGlobalObject* aScriptGlobalObject) override;
  bool CanSavePresentation(nsIRequest* aNewRequest,
                           uint16_t& aBFCacheStatus) override;

  const nsCString& GetType() const { return mMimeType; }
  Element* GetPluginContent() { return mPluginContent; }
  Element* GetPluginContent() { return mPluginContent; }

  virtual void Destroy() override {
    if (mStreamListener) {
      mStreamListener->DropDocumentRef();
    }
    }
    MediaDocument::Destroy();
  }

  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PluginDocument, MediaDocument)
 protected:
 protected:
  ~PluginDocument() override;

  nsresult CreateSyntheticPluginDocument();

  nsCOMPtr<Element> mPluginContent;
  RefPtr<MediaDocumentStreamListener> mStreamListener;
  nsCString mMimeType;
};
};

class PluginStreamListener : public MediaDocumentStreamListener {
 public:
  explicit PluginStreamListener(PluginDocument* aDoc)
      : MediaDocumentStreamListener(aDoc), mPluginDoc(aDoc) {}
  NS_IMETHOD OnStartRequest(nsIRequest* request) override;


 private:
  RefPtr<PluginDocument> mPluginDoc;
};

NS_IMETHODIMP
NS_IMETHODIMP
PluginStreamListener::OnStartRequest(nsIRequest* request) {
  AUTO_PROFILER_LABEL("PluginStreamListener::OnStartRequest", NETWORK);

  nsCOMPtr<nsIContent> embed = mPluginDoc->GetPluginContent();
  nsCOMPtr<nsIObjectLoadingContent> objlc = do_QueryInterface(embed);
  nsCOMPtr<nsIObjectLoadingContent> objlc = do_QueryInterface(embed);
  nsCOMPtr<nsIStreamListener> objListener = do_QueryInterface(objlc);

  if (!objListener) {
    MOZ_ASSERT_UNREACHABLE(
        "PluginStreamListener without appropriate content "
        "PluginStreamListener without appropriate content "
        "node");
    return NS_BINDING_ABORTED;
  }

  SetStreamListener(objListener);
  SetStreamListener(objListener);

  // Sets up the ObjectLoadingContent tag as if it is waiting for a
  // channel, so it can proceed with a load normally once it gets OnStartRequest
  nsresult rv = objlc->InitializeFromChannel(request);
  nsresult rv = objlc->InitializeFromChannel(request);
  if (NS_FAILED(rv)) {
    MOZ_ASSERT_UNREACHABLE("InitializeFromChannel failed");
    return rv;
  }
  }

  // Note that because we're now hooked up to a plugin listener, this will
  // likely spawn a plugin, which may re-enter.
  return MediaDocumentStreamListener::OnStartRequest(request);
}
}

PluginDocument::PluginDocument() = default;

PluginDocument::~PluginDocument() = default;


NS_IMPL_CYCLE_COLLECTION_INHERITED(PluginDocument, MediaDocument,
                                   mPluginContent)

NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(PluginDocument, MediaDocument,
                                             nsIPluginDocument)
                                             nsIPluginDocument)

void PluginDocument::SetScriptGlobalObject(
    nsIScriptGlobalObject* aScriptGlobalObject) {
  // Set the script global object on the superclass before doing
  // anything that might require it....
  // anything that might require it....
  MediaDocument::SetScriptGlobalObject(aScriptGlobalObject);

  if (aScriptGlobalObject) {
    if (!InitialSetupHasBeenDone()) {
      // Create synthetic document
      // Create synthetic document
#ifdef DEBUG
      nsresult rv =
#endif
          CreateSyntheticPluginDocument();
      NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create synthetic document");
      InitialSetupDone();
    }
  } else {
    mStreamListener = nullptr;
    mStreamListener = nullptr;
  }
}

bool PluginDocument::CanSavePresentation(nsIRequest* aNewRequest,
                                         uint16_t& aBFCacheStatus) {
                                         uint16_t& aBFCacheStatus) {
  // Full-page plugins cannot be cached, currently, because we don't have
  // the stream listener data to feed to the plugin instance.
  return false;
}


nsresult PluginDocument::StartDocumentLoad(const char* aCommand,
                                           nsIChannel* aChannel,
                                           nsILoadGroup* aLoadGroup,
                                           nsISupports* aContainer,
                                           nsIStreamListener** aDocListener,
                                           nsIStreamListener** aDocListener,
                                           bool aReset, nsIContentSink* aSink) {
  // do not allow message panes to host full-page plugins
  // returning an error causes helper apps to take over
  nsCOMPtr<nsIDocShellTreeItem> dsti(do_QueryInterface(aContainer));
  if (dsti) {
  if (dsti) {
    bool isMsgPane = false;
    dsti->NameEquals(NS_LITERAL_STRING("messagepane"), &isMsgPane);
    if (isMsgPane) {
      return NS_ERROR_FAILURE;
    }
    }
  }

  nsresult rv = MediaDocument::StartDocumentLoad(
      aCommand, aChannel, aLoadGroup, aContainer, aDocListener, aReset, aSink);
  if (NS_FAILED(rv)) {
  if (NS_FAILED(rv)) {
    return rv;
  }


  rv = aChannel->GetContentType(mMimeType);
  if (NS_FAILED(rv)) {
    return rv;
  }


  MediaDocument::UpdateTitleAndCharset(mMimeType, aChannel);

  mStreamListener = new PluginStreamListener(this);
  NS_ASSERTION(aDocListener, "null aDocListener");
  NS_ADDREF(*aDocListener = mStreamListener);
  NS_ADDREF(*aDocListener = mStreamListener);

  return rv;
}

nsresult PluginDocument::CreateSyntheticPluginDocument() {
nsresult PluginDocument::CreateSyntheticPluginDocument() {
  NS_ASSERTION(!GetPresShell() || !GetPresShell()->DidInitialize(),
               "Creating synthetic plugin document content too late");

  // make our generic document
  nsresult rv = MediaDocument::CreateSyntheticDocument();
  nsresult rv = MediaDocument::CreateSyntheticDocument();
  NS_ENSURE_SUCCESS(rv, rv);
  // then attach our plugin

  RefPtr<Element> body = GetBodyElement();
  if (!body) {
    NS_WARNING("no body on plugin document!");
    return NS_ERROR_FAILURE;
    return NS_ERROR_FAILURE;
  }

  // remove margins from body
  NS_NAMED_LITERAL_STRING(zero, "0");
  body->SetAttr(kNameSpaceID_None, nsGkAtoms::marginwidth, zero, false);
  body->SetAttr(kNameSpaceID_None, nsGkAtoms::marginwidth, zero, false);
  body->SetAttr(kNameSpaceID_None, nsGkAtoms::marginheight, zero, false);

  // make plugin content
  // make plugin content
  RefPtr<mozilla::dom::NodeInfo> nodeInfo;
  nodeInfo = mNodeInfoManager->GetNodeInfo(
      nsGkAtoms::embed, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);
  rv = NS_NewHTMLElement(getter_AddRefs(mPluginContent), nodeInfo.forget(),
  rv = NS_NewHTMLElement(getter_AddRefs(mPluginContent), nodeInfo.forget(),
                         NOT_FROM_PARSER);
  NS_ENSURE_SUCCESS(rv, rv);

  // make it a named element
  mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::name,
  mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::name,
                          NS_LITERAL_STRING("plugin"), false);

  // fill viewport and auto-resize
  NS_NAMED_LITERAL_STRING(percent100, "100%");
  mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::width, percent100,
  mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::width, percent100,
                          false);
  mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::height, percent100,
                          false);

  // set URL
  // set URL
  nsAutoCString src;
  mDocumentURI->GetSpec(src);
  mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::src,
                          NS_ConvertUTF8toUTF16(src), false);


  // set mime type
  mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
                          NS_ConvertUTF8toUTF16(mMimeType), false);

  // nsHTML(Shared)ObjectElement does not kick off a load on BindToTree if it is
  // nsHTML(Shared)ObjectElement does not kick off a load on BindToTree if it is
  // to a PluginDocument
  body->AppendChildTo(mPluginContent, false);

  return NS_OK;
}
}

NS_IMETHODIMP
PluginDocument::Print() {
  NS_ENSURE_TRUE(mPluginContent, NS_ERROR_FAILURE);


  nsIObjectFrame* objectFrame =
      do_QueryFrame(mPluginContent->GetPrimaryFrame());
  if (objectFrame) {
    RefPtr<nsNPAPIPluginInstance> pi = objectFrame->GetPluginInstance();
    if (pi) {
    if (pi) {
      NPPrint npprint;
      npprint.mode = NP_FULL;
      npprint.print.fullPrint.pluginPrinted = false;
      npprint.print.fullPrint.printOne = false;
      npprint.print.fullPrint.platformPrint = nullptr;
      npprint.print.fullPrint.platformPrint = nullptr;

      pi->Print(&npprint);
    }
  }


  return NS_OK;
}

}  // namespace mozilla::dom

nsresult NS_NewPluginDocument(mozilla::dom::Document** aResult) {
  auto* doc = new mozilla::dom::PluginDocument();
  auto* doc = new mozilla::dom::PluginDocument();

  NS_ADDREF(doc);
  nsresult rv = doc->Init();

  if (NS_FAILED(rv)) {
  if (NS_FAILED(rv)) {
    NS_RELEASE(doc);
  }

  *aResult = doc;


  return rv;
}