content/base/src/mozAutoDocUpdate.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/mozAutoDocUpdate.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef mozAutoDocUpdate_h_
     1.9 +#define mozAutoDocUpdate_h_
    1.10 +
    1.11 +#include "nsContentUtils.h" // For AddScriptBlocker() and RemoveScriptBlocker().
    1.12 +#include "nsIDocument.h"
    1.13 +#include "nsIDocumentObserver.h"
    1.14 +
    1.15 +/**
    1.16 + * Helper class to automatically handle batching of document updates.  This
    1.17 + * class will call BeginUpdate on construction and EndUpdate on destruction on
    1.18 + * the given document with the given update type.  The document could be null,
    1.19 + * in which case no updates will be called.  The constructor also takes a
    1.20 + * boolean that can be set to false to prevent notifications.
    1.21 + */
    1.22 +class MOZ_STACK_CLASS mozAutoDocUpdate
    1.23 +{
    1.24 +public:
    1.25 +  mozAutoDocUpdate(nsIDocument* aDocument, nsUpdateType aUpdateType,
    1.26 +                   bool aNotify) :
    1.27 +    mDocument(aNotify ? aDocument : nullptr),
    1.28 +    mUpdateType(aUpdateType)
    1.29 +  {
    1.30 +    if (mDocument) {
    1.31 +      mDocument->BeginUpdate(mUpdateType);
    1.32 +    }
    1.33 +    else {
    1.34 +      nsContentUtils::AddScriptBlocker();
    1.35 +    }
    1.36 +  }
    1.37 +
    1.38 +  ~mozAutoDocUpdate()
    1.39 +  {
    1.40 +    if (mDocument) {
    1.41 +      mDocument->EndUpdate(mUpdateType);
    1.42 +    }
    1.43 +    else {
    1.44 +      nsContentUtils::RemoveScriptBlocker();
    1.45 +    }
    1.46 +  }
    1.47 +
    1.48 +private:
    1.49 +  nsCOMPtr<nsIDocument> mDocument;
    1.50 +  nsUpdateType mUpdateType;
    1.51 +};
    1.52 +
    1.53 +#define MOZ_AUTO_DOC_UPDATE_PASTE2(tok,line) tok##line
    1.54 +#define MOZ_AUTO_DOC_UPDATE_PASTE(tok,line) \
    1.55 +  MOZ_AUTO_DOC_UPDATE_PASTE2(tok,line)
    1.56 +#define MOZ_AUTO_DOC_UPDATE(doc,type,notify) \
    1.57 +  mozAutoDocUpdate MOZ_AUTO_DOC_UPDATE_PASTE(_autoDocUpdater_, __LINE__) \
    1.58 +  (doc,type,notify)
    1.59 +
    1.60 +
    1.61 +/**
    1.62 + * Creates an update batch only under certain conditions.
    1.63 + * Use this rather than mozAutoDocUpdate when you expect inner updates
    1.64 + * to notify but you don't always want to spec cycles creating a batch.
    1.65 + * This is needed to avoid having this batch always create a blocker,
    1.66 + * but then have inner mozAutoDocUpdate call the last EndUpdate before.
    1.67 + * we remove that blocker. See bug 423269.
    1.68 + */
    1.69 +class MOZ_STACK_CLASS mozAutoDocConditionalContentUpdateBatch
    1.70 +{
    1.71 +public:
    1.72 +  mozAutoDocConditionalContentUpdateBatch(nsIDocument* aDocument,
    1.73 +                                          bool aNotify) :
    1.74 +    mDocument(aNotify ? aDocument : nullptr)
    1.75 +  {
    1.76 +    if (mDocument) {
    1.77 +      mDocument->BeginUpdate(UPDATE_CONTENT_MODEL);
    1.78 +    }
    1.79 +  }
    1.80 +
    1.81 +  ~mozAutoDocConditionalContentUpdateBatch()
    1.82 +  {
    1.83 +    if (mDocument) {
    1.84 +      mDocument->EndUpdate(UPDATE_CONTENT_MODEL);
    1.85 +    }
    1.86 +  }
    1.87 +
    1.88 +private:
    1.89 +  nsCOMPtr<nsIDocument> mDocument;
    1.90 +};
    1.91 +
    1.92 +#endif

mercurial