Bug 1020244 - AnonymousContent API and implementation; r=smaug
authorPatrick Brosset <pbrosset@mozilla.com>
Tue, 28 Oct 2014 11:15:21 +0100
changeset 212698 e661e86180ea014bfdb6bc03ce00c1c73ff9dd8f
parent 212675 9d42e896570c1b5494f29846a20c5ab097b5b112
child 212699 8a8090c2051edf8d3578a68fc3e5582774b67efe
push id51042
push userryanvm@gmail.com
push dateTue, 28 Oct 2014 20:25:03 +0000
treeherdermozilla-inbound@53d84829b2b8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1020244
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1020244 - AnonymousContent API and implementation; r=smaug
dom/base/AnonymousContent.cpp
dom/base/AnonymousContent.h
dom/base/moz.build
dom/bindings/Bindings.conf
dom/webidl/AnonymousContent.webidl
dom/webidl/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/base/AnonymousContent.cpp
@@ -0,0 +1,133 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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 "AnonymousContent.h"
+#include "mozilla/dom/Element.h"
+#include "mozilla/dom/AnonymousContentBinding.h"
+#include "nsCycleCollectionParticipant.h"
+#include "nsIDocument.h"
+#include "nsIDOMHTMLCollection.h"
+#include "nsStyledElement.h"
+
+namespace mozilla {
+namespace dom {
+
+// Ref counting and cycle collection
+NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AnonymousContent, AddRef)
+NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AnonymousContent, Release)
+NS_IMPL_CYCLE_COLLECTION(AnonymousContent, mContentNode)
+
+AnonymousContent::AnonymousContent(Element* aContentNode) :
+  mContentNode(aContentNode)
+{}
+
+AnonymousContent::~AnonymousContent()
+{
+}
+
+nsCOMPtr<Element>
+AnonymousContent::GetContentNode()
+{
+  return mContentNode;
+}
+
+void
+AnonymousContent::SetTextContentForElement(const nsAString& aElementId,
+                                           const nsAString& aText,
+                                           ErrorResult& aRv)
+{
+  Element* element = GetElementById(aElementId);
+  if (!element) {
+    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
+    return;
+  }
+
+  element->SetTextContent(aText, aRv);
+}
+
+void
+AnonymousContent::GetTextContentForElement(const nsAString& aElementId,
+                                           DOMString& aText,
+                                           ErrorResult& aRv)
+{
+  Element* element = GetElementById(aElementId);
+  if (!element) {
+    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
+    return;
+  }
+
+  element->GetTextContent(aText, aRv);
+}
+
+void
+AnonymousContent::SetAttributeForElement(const nsAString& aElementId,
+                                         const nsAString& aName,
+                                         const nsAString& aValue,
+                                         ErrorResult& aRv)
+{
+  Element* element = GetElementById(aElementId);
+  if (!element) {
+    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
+    return;
+  }
+
+  element->SetAttribute(aName, aValue, aRv);
+}
+
+void
+AnonymousContent::GetAttributeForElement(const nsAString& aElementId,
+                                         const nsAString& aName,
+                                         DOMString& aValue,
+                                         ErrorResult& aRv)
+{
+  Element* element = GetElementById(aElementId);
+  if (!element) {
+    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
+    return;
+  }
+
+  element->GetAttribute(aName, aValue);
+}
+
+void
+AnonymousContent::RemoveAttributeForElement(const nsAString& aElementId,
+                                            const nsAString& aName,
+                                            ErrorResult& aRv)
+{
+  Element* element = GetElementById(aElementId);
+  if (!element) {
+    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
+    return;
+  }
+
+  element->RemoveAttribute(aName, aRv);
+}
+
+Element*
+AnonymousContent::GetElementById(const nsAString& aElementId)
+{
+  // This can be made faster in the future if needed.
+  nsCOMPtr<nsIAtom> elementId = do_GetAtom(aElementId);
+  for (nsIContent* kid = mContentNode->GetFirstChild(); kid;
+       kid = kid->GetNextNode(mContentNode)) {
+    if (!kid->IsElement()) {
+      continue;
+    }
+    nsIAtom* id = kid->AsElement()->GetID();
+    if (id && id == elementId) {
+      return kid->AsElement();
+    }
+  }
+  return nullptr;
+}
+
+JSObject*
+AnonymousContent::WrapObject(JSContext* aCx)
+{
+  return AnonymousContentBinding::Wrap(aCx, this);
+}
+
+} // dom namespace
+} // mozilla namespace
new file mode 100644
--- /dev/null
+++ b/dom/base/AnonymousContent.h
@@ -0,0 +1,63 @@
+/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ *
+ * 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/. */
+
+#ifndef mozilla_dom_AnonymousContent_h
+#define mozilla_dom_AnonymousContent_h
+
+#include "mozilla/dom/Element.h"
+#include "nsCycleCollectionParticipant.h"
+#include "nsICSSDeclaration.h"
+#include "nsIDocument.h"
+
+namespace mozilla {
+namespace dom {
+
+class Element;
+
+class AnonymousContent MOZ_FINAL
+{
+public:
+  // Ref counting and cycle collection
+  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AnonymousContent)
+  NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(AnonymousContent)
+
+  explicit AnonymousContent(Element* aContentNode);
+  nsCOMPtr<Element> GetContentNode();
+  JSObject* WrapObject(JSContext* aCx);
+
+  // WebIDL methods
+  void SetTextContentForElement(const nsAString& aElementId,
+                                const nsAString& aText,
+                                ErrorResult& aRv);
+
+  void GetTextContentForElement(const nsAString& aElementId,
+                                DOMString& aText,
+                                ErrorResult& aRv);
+
+  void SetAttributeForElement(const nsAString& aElementId,
+                              const nsAString& aName,
+                              const nsAString& aValue,
+                              ErrorResult& aRv);
+
+  void GetAttributeForElement(const nsAString& aElementId,
+                              const nsAString& aName,
+                              DOMString& aValue,
+                              ErrorResult& aRv);
+
+  void RemoveAttributeForElement(const nsAString& aElementId,
+                                 const nsAString& aName,
+                                 ErrorResult& aRv);
+
+private:
+  ~AnonymousContent();
+  Element* GetElementById(const nsAString& aElementId);
+  nsCOMPtr<Element> mContentNode;
+};
+
+} // dom namespace
+} // mozilla namespace
+
+#endif // mozilla_dom_AnonymousContent_h
--- a/dom/base/moz.build
+++ b/dom/base/moz.build
@@ -137,16 +137,17 @@ if CONFIG['MOZ_WEBRTC']:
     ]
 
 EXPORTS.mozilla += [
     'CORSMode.h',
     'FeedWriterEnabled.h',
 ]
 
 EXPORTS.mozilla.dom += [
+    'AnonymousContent.h',
     'Attr.h',
     'BarProps.h',
     'BlobSet.h',
     'ChildIterator.h',
     'Comment.h',
     'Console.h',
     'DirectionalityUtils.h',
     'DocumentFragment.h',
@@ -191,16 +192,17 @@ EXPORTS.mozilla.dom += [
     'Text.h',
     'TreeWalker.h',
     'URL.h',
     'URLSearchParams.h',
     'WebSocket.h',
 ]
 
 UNIFIED_SOURCES += [
+    'AnonymousContent.cpp',
     'Attr.cpp',
     'BarProps.cpp',
     'ChildIterator.cpp',
     'Comment.cpp',
     'CompositionStringSynthesizer.cpp',
     'Console.cpp',
     'Crypto.cpp',
     'DirectionalityUtils.cpp',
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -96,16 +96,20 @@ DOMInterfaces = {
 'MozAbortablePromise': {
     'nativeType': 'mozilla::dom::AbortablePromise',
 },
 
 'AbstractWorker': {
     'concrete': False
 },
 
+'AnonymousContent': {
+    'wrapperCache': False
+},
+
 'ArchiveReader': {
     'nativeType': 'mozilla::dom::archivereader::ArchiveReader',
 },
 
 'ArchiveRequest': {
     'nativeType': 'mozilla::dom::archivereader::ArchiveRequest',
 },
 
new file mode 100644
--- /dev/null
+++ b/dom/webidl/AnonymousContent.webidl
@@ -0,0 +1,56 @@
+/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/.
+ */
+
+/*
+ * This file declares the AnonymousContent interface which is used to
+ * manipulate content that has been inserted into the document's canvasFrame
+ * anonymous container.
+ * See Document.insertAnonymousContent.
+ *
+ * This API never returns a reference to the actual inserted DOM node on
+ * purpose. This is to make sure the content cannot be randomly changed and the
+ * DOM cannot be traversed from the node, so that Gecko can remain in control of
+ * the inserted content.
+ */
+
+[ChromeOnly]
+interface AnonymousContent {
+  /**
+   * Get the text content of an element inside this custom anonymous content.
+   */
+  [Throws]
+  DOMString getTextContentForElement(DOMString elementId);
+
+  /**
+   * Set the text content of an element inside this custom anonymous content.
+   */
+  [Throws]
+  void setTextContentForElement(DOMString elementId, DOMString text);
+
+  /**
+   * Get the value of an attribute of an element inside this custom anonymous
+   * content.
+   */
+  [Throws]
+  DOMString? getAttributeForElement(DOMString elementId,
+                                    DOMString attributeName);
+
+  /**
+   * Set the value of an attribute of an element inside this custom anonymous
+   * content.
+   */
+  [Throws]
+  void setAttributeForElement(DOMString elementId,
+                              DOMString attributeName,
+                              DOMString value);
+
+  /**
+   * Remove an attribute from an element inside this custom anonymous content.
+   */
+  [Throws]
+  void removeAttributeForElement(DOMString elementId,
+                                 DOMString attributeName);
+};
--- a/dom/webidl/moz.build
+++ b/dom/webidl/moz.build
@@ -22,16 +22,17 @@ WEBIDL_FILES = [
     'AlarmsManager.webidl',
     'AnalyserNode.webidl',
     'Animatable.webidl',
     'Animation.webidl',
     'AnimationEffect.webidl',
     'AnimationEvent.webidl',
     'AnimationPlayer.webidl',
     'AnimationTimeline.webidl',
+    'AnonymousContent.webidl',
     'AppInfo.webidl',
     'AppNotificationServiceOptions.webidl',
     'Apps.webidl',
     'APZTestData.webidl',
     'ArchiveReader.webidl',
     'ArchiveRequest.webidl',
     'Attr.webidl',
     'AudioBuffer.webidl',