diff --git a/content/base/src/nsDOMMutationObserver.cpp b/content/base/src/nsDOMMutationObserver.cpp index 1187f1b1ffe..f79d1836fd4 100644 --- a/content/base/src/nsDOMMutationObserver.cpp +++ b/content/base/src/nsDOMMutationObserver.cpp @@ -4,15 +4,15 @@ * 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 "nsDOMMutationObserver.h" -#include "nsDOMClassInfoID.h" +#include "nsDOMMutationObserver.h" + +#include "mozilla/dom/OwningNonNull.h" #include "nsError.h" #include "nsIScriptGlobalObject.h" #include "nsContentUtils.h" #include "nsThreadUtils.h" #include "nsIDOMMutationEvent.h" #include "nsTextFragment.h" -#include "jsapi.h" #include "nsServiceManagerUtils.h" nsTArray >* diff --git a/content/base/src/nsDocument.cpp b/content/base/src/nsDocument.cpp index 45455d794c1..126a31b5530 100644 --- a/content/base/src/nsDocument.cpp +++ b/content/base/src/nsDocument.cpp @@ -190,11 +190,12 @@ #include "nsIAppsService.h" #include "mozilla/dom/BindingUtils.h" #include "mozilla/dom/DocumentFragment.h" -#include "mozilla/dom/WebComponentsBinding.h" #include "mozilla/dom/HTMLBodyElement.h" #include "mozilla/dom/HTMLInputElement.h" #include "mozilla/dom/NodeFilterBinding.h" +#include "mozilla/dom/OwningNonNull.h" #include "mozilla/dom/UndoManager.h" +#include "mozilla/dom/WebComponentsBinding.h" #include "nsFrame.h" #include "nsDOMCaretPosition.h" #include "nsIDOMHTMLTextAreaElement.h" diff --git a/content/media/webaudio/AudioContext.cpp b/content/media/webaudio/AudioContext.cpp index 2dcfc514e81..979ebdd0e43 100644 --- a/content/media/webaudio/AudioContext.cpp +++ b/content/media/webaudio/AudioContext.cpp @@ -5,12 +5,14 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "AudioContext.h" + #include "nsPIDOMWindow.h" #include "mozilla/ErrorResult.h" #include "mozilla/dom/AnalyserNode.h" #include "mozilla/dom/AudioContextBinding.h" #include "mozilla/dom/HTMLMediaElement.h" #include "mozilla/dom/OfflineAudioContextBinding.h" +#include "mozilla/dom/OwningNonNull.h" #include "MediaStreamGraph.h" #include "AudioDestinationNode.h" #include "AudioBufferSourceNode.h" diff --git a/dom/bindings/BindingUtils.h b/dom/bindings/BindingUtils.h index febdea2943e..78e8137b157 100644 --- a/dom/bindings/BindingUtils.h +++ b/dom/bindings/BindingUtils.h @@ -1423,60 +1423,6 @@ AppendNamedPropertyIds(JSContext* cx, JS::Handle proxy, nsTArray& names, bool shadowPrototypeProperties, JS::AutoIdVector& props); -template -class OwningNonNull -{ -public: - OwningNonNull() -#ifdef DEBUG - : inited(false) -#endif - {} - - operator T&() { - MOZ_ASSERT(inited); - MOZ_ASSERT(ptr, "OwningNonNull was set to null"); - return *ptr; - } - - void operator=(T* t) { - init(t); - } - - void operator=(const already_AddRefed& t) { - init(t); - } - - already_AddRefed forget() { -#ifdef DEBUG - inited = false; -#endif - return ptr.forget(); - } - - // Make us work with smart-ptr helpers that expect a get() - T* get() const { - MOZ_ASSERT(inited); - MOZ_ASSERT(ptr); - return ptr; - } - -protected: - template - void init(U t) { - ptr = t; - MOZ_ASSERT(ptr); -#ifdef DEBUG - inited = true; -#endif - } - - nsRefPtr ptr; -#ifdef DEBUG - bool inited; -#endif -}; - // A struct that has the same layout as an nsDependentString but much // faster constructor and destructor behavior struct FakeDependentString { diff --git a/dom/bindings/Codegen.py b/dom/bindings/Codegen.py index 73b8c47075f..a93d7a8c7a5 100644 --- a/dom/bindings/Codegen.py +++ b/dom/bindings/Codegen.py @@ -8803,11 +8803,12 @@ class CGBindingRoot(CGThing): enums = config.getEnums(webIDLFile) cgthings = [ CGEnum(e) for e in enums ] - bindingHeaders["mozilla/dom/BindingUtils.h"] = ( - descriptors or callbackDescriptors or dictionaries or - mainCallbacks or workerCallbacks) + hasCode = (descriptors or callbackDescriptors or dictionaries or + mainCallbacks or workerCallbacks) + bindingHeaders["mozilla/dom/BindingUtils.h"] = hasCode + bindingHeaders["mozilla/dom/OwningNonNull.h"] = hasCode bindingHeaders["mozilla/dom/BindingDeclarations.h"] = ( - not bindingHeaders["mozilla/dom/BindingUtils.h"] and enums) + not hasCode and enums) bindingHeaders["WrapperFactory.h"] = descriptors bindingHeaders["mozilla/dom/DOMJSClass.h"] = descriptors @@ -10522,6 +10523,7 @@ struct PrototypeTraits; config.getCallbacks(), config) includes.add("mozilla/dom/BindingUtils.h") + includes.add("mozilla/dom/OwningNonNull.h") includes.add("mozilla/dom/UnionMember.h") # Wrap all of that in our namespaces. diff --git a/dom/bindings/OwningNonNull.h b/dom/bindings/OwningNonNull.h new file mode 100644 index 00000000000..5de76405b61 --- /dev/null +++ b/dom/bindings/OwningNonNull.h @@ -0,0 +1,80 @@ +/* -*- 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/. */ + +/* A class for non-null strong pointers to reference-counted objects. */ + +#ifndef mozilla_dom_OwningNonNull_h +#define mozilla_dom_OwningNonNull_h + +#include "nsAutoPtr.h" + +namespace mozilla { +namespace dom { + +template +class OwningNonNull +{ +public: + OwningNonNull() +#ifdef DEBUG + : mInited(false) +#endif + {} + + operator T&() + { + MOZ_ASSERT(mInited); + MOZ_ASSERT(mPtr, "OwningNonNull was set to null"); + return *mPtr; + } + + void operator=(T* aValue) + { + init(aValue); + } + + void operator=(const already_AddRefed& aValue) + { + init(aValue); + } + + already_AddRefed forget() + { +#ifdef DEBUG + mInited = false; +#endif + return mPtr.forget(); + } + + // Make us work with smart pointer helpers that expect a get(). + T* get() const + { + MOZ_ASSERT(mInited); + MOZ_ASSERT(mPtr); + return mPtr; + } + +protected: + template + void init(U aValue) + { + mPtr = aValue; + MOZ_ASSERT(mPtr); +#ifdef DEBUG + mInited = true; +#endif + } + + nsRefPtr mPtr; +#ifdef DEBUG + bool mInited; +#endif +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_OwningNonNull_h diff --git a/dom/bindings/moz.build b/dom/bindings/moz.build index a1e9eeb3502..03ffedc0962 100644 --- a/dom/bindings/moz.build +++ b/dom/bindings/moz.build @@ -25,6 +25,7 @@ EXPORTS.mozilla.dom += [ 'JSSlots.h', 'NonRefcountedDOMObject.h', 'Nullable.h', + 'OwningNonNull.h', 'PrimitiveConversions.h', 'TypedArray.h', 'UnionMember.h', diff --git a/dom/promise/Promise.cpp b/dom/promise/Promise.cpp index 8077a01e0ce..88de734c25c 100644 --- a/dom/promise/Promise.cpp +++ b/dom/promise/Promise.cpp @@ -7,6 +7,7 @@ #include "mozilla/dom/Promise.h" #include "jsfriendapi.h" +#include "mozilla/dom/OwningNonNull.h" #include "mozilla/dom/PromiseBinding.h" #include "mozilla/dom/PromiseResolver.h" #include "mozilla/Preferences.h" diff --git a/dom/src/notification/Notification.cpp b/dom/src/notification/Notification.cpp index 156ee638591..36dbf4c5993 100644 --- a/dom/src/notification/Notification.cpp +++ b/dom/src/notification/Notification.cpp @@ -4,6 +4,7 @@ #include "PCOMContentPermissionRequestChild.h" #include "mozilla/dom/Notification.h" +#include "mozilla/dom/OwningNonNull.h" #include "mozilla/Preferences.h" #include "TabChild.h" #include "nsContentUtils.h"