|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef mozAutoDocUpdate_h_ |
|
6 #define mozAutoDocUpdate_h_ |
|
7 |
|
8 #include "nsContentUtils.h" // For AddScriptBlocker() and RemoveScriptBlocker(). |
|
9 #include "nsIDocument.h" |
|
10 #include "nsIDocumentObserver.h" |
|
11 |
|
12 /** |
|
13 * Helper class to automatically handle batching of document updates. This |
|
14 * class will call BeginUpdate on construction and EndUpdate on destruction on |
|
15 * the given document with the given update type. The document could be null, |
|
16 * in which case no updates will be called. The constructor also takes a |
|
17 * boolean that can be set to false to prevent notifications. |
|
18 */ |
|
19 class MOZ_STACK_CLASS mozAutoDocUpdate |
|
20 { |
|
21 public: |
|
22 mozAutoDocUpdate(nsIDocument* aDocument, nsUpdateType aUpdateType, |
|
23 bool aNotify) : |
|
24 mDocument(aNotify ? aDocument : nullptr), |
|
25 mUpdateType(aUpdateType) |
|
26 { |
|
27 if (mDocument) { |
|
28 mDocument->BeginUpdate(mUpdateType); |
|
29 } |
|
30 else { |
|
31 nsContentUtils::AddScriptBlocker(); |
|
32 } |
|
33 } |
|
34 |
|
35 ~mozAutoDocUpdate() |
|
36 { |
|
37 if (mDocument) { |
|
38 mDocument->EndUpdate(mUpdateType); |
|
39 } |
|
40 else { |
|
41 nsContentUtils::RemoveScriptBlocker(); |
|
42 } |
|
43 } |
|
44 |
|
45 private: |
|
46 nsCOMPtr<nsIDocument> mDocument; |
|
47 nsUpdateType mUpdateType; |
|
48 }; |
|
49 |
|
50 #define MOZ_AUTO_DOC_UPDATE_PASTE2(tok,line) tok##line |
|
51 #define MOZ_AUTO_DOC_UPDATE_PASTE(tok,line) \ |
|
52 MOZ_AUTO_DOC_UPDATE_PASTE2(tok,line) |
|
53 #define MOZ_AUTO_DOC_UPDATE(doc,type,notify) \ |
|
54 mozAutoDocUpdate MOZ_AUTO_DOC_UPDATE_PASTE(_autoDocUpdater_, __LINE__) \ |
|
55 (doc,type,notify) |
|
56 |
|
57 |
|
58 /** |
|
59 * Creates an update batch only under certain conditions. |
|
60 * Use this rather than mozAutoDocUpdate when you expect inner updates |
|
61 * to notify but you don't always want to spec cycles creating a batch. |
|
62 * This is needed to avoid having this batch always create a blocker, |
|
63 * but then have inner mozAutoDocUpdate call the last EndUpdate before. |
|
64 * we remove that blocker. See bug 423269. |
|
65 */ |
|
66 class MOZ_STACK_CLASS mozAutoDocConditionalContentUpdateBatch |
|
67 { |
|
68 public: |
|
69 mozAutoDocConditionalContentUpdateBatch(nsIDocument* aDocument, |
|
70 bool aNotify) : |
|
71 mDocument(aNotify ? aDocument : nullptr) |
|
72 { |
|
73 if (mDocument) { |
|
74 mDocument->BeginUpdate(UPDATE_CONTENT_MODEL); |
|
75 } |
|
76 } |
|
77 |
|
78 ~mozAutoDocConditionalContentUpdateBatch() |
|
79 { |
|
80 if (mDocument) { |
|
81 mDocument->EndUpdate(UPDATE_CONTENT_MODEL); |
|
82 } |
|
83 } |
|
84 |
|
85 private: |
|
86 nsCOMPtr<nsIDocument> mDocument; |
|
87 }; |
|
88 |
|
89 #endif |