michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozAutoDocUpdate_h_ michael@0: #define mozAutoDocUpdate_h_ michael@0: michael@0: #include "nsContentUtils.h" // For AddScriptBlocker() and RemoveScriptBlocker(). michael@0: #include "nsIDocument.h" michael@0: #include "nsIDocumentObserver.h" michael@0: michael@0: /** michael@0: * Helper class to automatically handle batching of document updates. This michael@0: * class will call BeginUpdate on construction and EndUpdate on destruction on michael@0: * the given document with the given update type. The document could be null, michael@0: * in which case no updates will be called. The constructor also takes a michael@0: * boolean that can be set to false to prevent notifications. michael@0: */ michael@0: class MOZ_STACK_CLASS mozAutoDocUpdate michael@0: { michael@0: public: michael@0: mozAutoDocUpdate(nsIDocument* aDocument, nsUpdateType aUpdateType, michael@0: bool aNotify) : michael@0: mDocument(aNotify ? aDocument : nullptr), michael@0: mUpdateType(aUpdateType) michael@0: { michael@0: if (mDocument) { michael@0: mDocument->BeginUpdate(mUpdateType); michael@0: } michael@0: else { michael@0: nsContentUtils::AddScriptBlocker(); michael@0: } michael@0: } michael@0: michael@0: ~mozAutoDocUpdate() michael@0: { michael@0: if (mDocument) { michael@0: mDocument->EndUpdate(mUpdateType); michael@0: } michael@0: else { michael@0: nsContentUtils::RemoveScriptBlocker(); michael@0: } michael@0: } michael@0: michael@0: private: michael@0: nsCOMPtr mDocument; michael@0: nsUpdateType mUpdateType; michael@0: }; michael@0: michael@0: #define MOZ_AUTO_DOC_UPDATE_PASTE2(tok,line) tok##line michael@0: #define MOZ_AUTO_DOC_UPDATE_PASTE(tok,line) \ michael@0: MOZ_AUTO_DOC_UPDATE_PASTE2(tok,line) michael@0: #define MOZ_AUTO_DOC_UPDATE(doc,type,notify) \ michael@0: mozAutoDocUpdate MOZ_AUTO_DOC_UPDATE_PASTE(_autoDocUpdater_, __LINE__) \ michael@0: (doc,type,notify) michael@0: michael@0: michael@0: /** michael@0: * Creates an update batch only under certain conditions. michael@0: * Use this rather than mozAutoDocUpdate when you expect inner updates michael@0: * to notify but you don't always want to spec cycles creating a batch. michael@0: * This is needed to avoid having this batch always create a blocker, michael@0: * but then have inner mozAutoDocUpdate call the last EndUpdate before. michael@0: * we remove that blocker. See bug 423269. michael@0: */ michael@0: class MOZ_STACK_CLASS mozAutoDocConditionalContentUpdateBatch michael@0: { michael@0: public: michael@0: mozAutoDocConditionalContentUpdateBatch(nsIDocument* aDocument, michael@0: bool aNotify) : michael@0: mDocument(aNotify ? aDocument : nullptr) michael@0: { michael@0: if (mDocument) { michael@0: mDocument->BeginUpdate(UPDATE_CONTENT_MODEL); michael@0: } michael@0: } michael@0: michael@0: ~mozAutoDocConditionalContentUpdateBatch() michael@0: { michael@0: if (mDocument) { michael@0: mDocument->EndUpdate(UPDATE_CONTENT_MODEL); michael@0: } michael@0: } michael@0: michael@0: private: michael@0: nsCOMPtr mDocument; michael@0: }; michael@0: michael@0: #endif