forked from rnewman/services-central-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsPresShell.cpp
8908 lines (7646 loc) · 286 KB
/
nsPresShell.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=2 sw=2 et tw=78:
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Steve Clark <buster@netscape.com>
* Håkan Waara <hwaara@chello.se>
* Dan Rosen <dr@netscape.com>
* Daniel Glazman <glazman@netscape.com>
* Mats Palmgren <matspal@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK *****
*
* This Original Code has been modified by IBM Corporation.
* Modifications made by IBM described herein are
* Copyright (c) International Business Machines
* Corporation, 2000
*
* Modifications to Mozilla code or documentation
* identified per MPL Section 3.3
*
* Date Modified by Description of modification
* 05/03/2000 IBM Corp. Observer events for reflow states
*/
/* a presentation of a document, part 2 */
#include "nsIPresShell.h"
#include "nsPresContext.h"
#include "nsIContent.h"
#include "mozilla/dom/Element.h"
#include "nsIDocument.h"
#include "nsIDOMXULDocument.h"
#include "nsStubDocumentObserver.h"
#include "nsStyleSet.h"
#include "nsCSSStyleSheet.h" // XXX for UA sheet loading hack, can this go away please?
#include "nsIDOMCSSStyleSheet.h" // for Pref-related rule management (bugs 22963,20760,31816)
#include "nsINameSpaceManager.h" // for Pref-related rule management (bugs 22963,20760,31816)
#include "nsIServiceManager.h"
#include "nsFrame.h"
#include "nsIViewManager.h"
#include "nsCRT.h"
#include "nsCRTGlue.h"
#include "prlog.h"
#include "prmem.h"
#include "prprf.h"
#include "prinrval.h"
#include "nsTArray.h"
#include "nsCOMArray.h"
#include "nsHashtable.h"
#include "nsIViewObserver.h"
#include "nsContainerFrame.h"
#include "nsIDeviceContext.h"
#include "nsEventStateManager.h"
#include "nsDOMEvent.h"
#include "nsGUIEvent.h"
#include "nsHTMLParts.h"
#include "nsContentUtils.h"
#include "nsISelection.h"
#include "nsISelectionController.h"
#include "nsISelectionPrivate.h"
#include "nsLayoutCID.h"
#include "nsGkAtoms.h"
#include "nsIDOMRange.h"
#include "nsIDOMDocument.h"
#include "nsIDOMNode.h"
#include "nsIDOM3Node.h"
#include "nsIDOMNodeList.h"
#include "nsIDOMElement.h"
#include "nsRange.h"
#include "nsCSSPseudoElements.h"
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsReadableUtils.h"
#include "nsUnicharUtils.h"
#include "nsWeakReference.h"
#include "nsIPageSequenceFrame.h"
#include "nsCaret.h"
#include "nsIDOMHTMLDocument.h"
#include "nsIXPointer.h"
#include "nsIDOMXMLDocument.h"
#include "nsIParser.h"
#include "nsParserCIID.h"
#include "nsFrameSelection.h"
#include "nsIDOMNSHTMLInputElement.h" //optimization for ::DoXXX commands
#include "nsIDOMNSHTMLTextAreaElement.h"
#include "nsViewsCID.h"
#include "nsPresArena.h"
#include "nsFrameManager.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsILayoutHistoryState.h"
#include "nsILineIterator.h" // for ScrollContentIntoView
#include "nsWeakPtr.h"
#include "pldhash.h"
#include "nsIObserverService.h"
#include "nsIObserver.h"
#include "nsIDocShell.h" // for reflow observation
#include "nsIBaseWindow.h"
#include "nsLayoutErrors.h"
#include "nsLayoutUtils.h"
#include "nsCSSRendering.h"
// for |#ifdef DEBUG| code
#include "prenv.h"
#include "nsIAttribute.h"
#include "nsIGlobalHistory2.h"
#include "nsDisplayList.h"
#include "nsIRegion.h"
#include "nsRegion.h"
#ifdef MOZ_REFLOW_PERF
#include "nsIRenderingContext.h"
#include "nsIFontMetrics.h"
#endif
#include "nsIReflowCallback.h"
#include "nsPIDOMWindow.h"
#include "nsFocusManager.h"
#include "nsIPluginInstance.h"
#include "nsIObjectFrame.h"
#include "nsIObjectLoadingContent.h"
#include "nsNetUtil.h"
#include "nsEventDispatcher.h"
#include "nsThreadUtils.h"
#include "nsStyleSheetService.h"
#include "gfxImageSurface.h"
#include "gfxContext.h"
#ifdef MOZ_MEDIA
#include "nsHTMLMediaElement.h"
#endif
#ifdef MOZ_SMIL
#include "nsSMILAnimationController.h"
#endif
// Drag & Drop, Clipboard
#include "nsWidgetsCID.h"
#include "nsIClipboard.h"
#include "nsIClipboardHelper.h"
#include "nsIDocShellTreeItem.h"
#include "nsIURI.h"
#include "nsIScrollableFrame.h"
#include "prtime.h"
#include "prlong.h"
#include "nsIDragService.h"
#include "nsCopySupport.h"
#include "nsIDOMHTMLAnchorElement.h"
#include "nsIDOMHTMLAreaElement.h"
#include "nsIDOMHTMLLinkElement.h"
#include "nsITimer.h"
#ifdef ACCESSIBILITY
#include "nsIAccessibilityService.h"
#include "nsAccessible.h"
#endif
// For style data reconstruction
#include "nsStyleChangeList.h"
#include "nsCSSFrameConstructor.h"
#ifdef MOZ_XUL
#include "nsMenuFrame.h"
#include "nsTreeBodyFrame.h"
#include "nsIBoxObject.h"
#include "nsITreeBoxObject.h"
#include "nsMenuPopupFrame.h"
#include "nsITreeColumns.h"
#include "nsIDOMXULMultSelectCntrlEl.h"
#include "nsIDOMXULSelectCntrlItemEl.h"
#include "nsIDOMXULMenuListElement.h"
#endif
#include "nsPlaceholderFrame.h"
#include "nsCanvasFrame.h"
// Content viewer interfaces
#include "nsIContentViewer.h"
#include "imgIEncoder.h"
#include "gfxPlatform.h"
#include "mozilla/FunctionTimer.h"
#ifdef NS_FUNCTION_TIMER
#define NS_TIME_FUNCTION_DECLARE_DOCURL \
nsCAutoString docURL__("N/A"); \
nsIURI *uri__ = mDocument->GetDocumentURI(); \
if (uri__) uri__->GetSpec(docURL__);
#define NS_TIME_FUNCTION_WITH_DOCURL \
NS_TIME_FUNCTION_DECLARE_DOCURL \
NS_TIME_FUNCTION_MIN_FMT(1.0, \
"%s (line %d) (document: %s)", MOZ_FUNCTION_NAME, \
__LINE__, docURL__.get())
#else
#define NS_TIME_FUNCTION_WITH_DOCURL do{} while(0)
#endif
#include "nsContentCID.h"
static NS_DEFINE_IID(kRangeCID, NS_RANGE_CID);
/* for NS_MEMORY_REPORTER_IMPLEMENT */
#include "nsIMemoryReporter.h"
using namespace mozilla;
using namespace mozilla::layers;
using namespace mozilla::dom;
PRBool nsIPresShell::gIsAccessibilityActive = PR_FALSE;
CapturingContentInfo nsIPresShell::gCaptureInfo;
// convert a color value to a string, in the CSS format #RRGGBB
// * - initially created for bugs 31816, 20760, 22963
static void ColorToString(nscolor aColor, nsAutoString &aString);
// Class ID's
static NS_DEFINE_CID(kFrameSelectionCID, NS_FRAMESELECTION_CID);
// RangePaintInfo is used to paint ranges to offscreen buffers
struct RangePaintInfo {
nsCOMPtr<nsIRange> mRange;
nsDisplayListBuilder mBuilder;
nsDisplayList mList;
// offset of builder's reference frame to the root frame
nsPoint mRootOffset;
RangePaintInfo(nsIRange* aRange, nsIFrame* aFrame)
: mRange(aRange), mBuilder(aFrame, PR_FALSE, PR_FALSE)
{
MOZ_COUNT_CTOR(RangePaintInfo);
}
~RangePaintInfo()
{
mList.DeleteAll();
MOZ_COUNT_DTOR(RangePaintInfo);
}
};
#undef NOISY
// ----------------------------------------------------------------------
#ifdef NS_DEBUG
// Set the environment variable GECKO_VERIFY_REFLOW_FLAGS to one or
// more of the following flags (comma separated) for handy debug
// output.
static PRUint32 gVerifyReflowFlags;
struct VerifyReflowFlags {
const char* name;
PRUint32 bit;
};
static const VerifyReflowFlags gFlags[] = {
{ "verify", VERIFY_REFLOW_ON },
{ "reflow", VERIFY_REFLOW_NOISY },
{ "all", VERIFY_REFLOW_ALL },
{ "list-commands", VERIFY_REFLOW_DUMP_COMMANDS },
{ "noisy-commands", VERIFY_REFLOW_NOISY_RC },
{ "really-noisy-commands", VERIFY_REFLOW_REALLY_NOISY_RC },
{ "resize", VERIFY_REFLOW_DURING_RESIZE_REFLOW },
};
#define NUM_VERIFY_REFLOW_FLAGS (sizeof(gFlags) / sizeof(gFlags[0]))
static void
ShowVerifyReflowFlags()
{
printf("Here are the available GECKO_VERIFY_REFLOW_FLAGS:\n");
const VerifyReflowFlags* flag = gFlags;
const VerifyReflowFlags* limit = gFlags + NUM_VERIFY_REFLOW_FLAGS;
while (flag < limit) {
printf(" %s\n", flag->name);
++flag;
}
printf("Note: GECKO_VERIFY_REFLOW_FLAGS is a comma separated list of flag\n");
printf("names (no whitespace)\n");
}
#endif
//========================================================================
//========================================================================
//========================================================================
#ifdef MOZ_REFLOW_PERF
class ReflowCountMgr;
static const char kGrandTotalsStr[] = "Grand Totals";
// Counting Class
class ReflowCounter {
public:
ReflowCounter(ReflowCountMgr * aMgr = nsnull);
~ReflowCounter();
void ClearTotals();
void DisplayTotals(const char * aStr);
void DisplayDiffTotals(const char * aStr);
void DisplayHTMLTotals(const char * aStr);
void Add() { mTotal++; }
void Add(PRUint32 aTotal) { mTotal += aTotal; }
void CalcDiffInTotals();
void SetTotalsCache();
void SetMgr(ReflowCountMgr * aMgr) { mMgr = aMgr; }
PRUint32 GetTotal() { return mTotal; }
protected:
void DisplayTotals(PRUint32 aTotal, const char * aTitle);
void DisplayHTMLTotals(PRUint32 aTotal, const char * aTitle);
PRUint32 mTotal;
PRUint32 mCacheTotal;
ReflowCountMgr * mMgr; // weak reference (don't delete)
};
// Counting Class
class IndiReflowCounter {
public:
IndiReflowCounter(ReflowCountMgr * aMgr = nsnull)
: mFrame(nsnull),
mCount(0),
mMgr(aMgr),
mCounter(aMgr),
mHasBeenOutput(PR_FALSE)
{}
virtual ~IndiReflowCounter() {}
nsAutoString mName;
nsIFrame * mFrame; // weak reference (don't delete)
PRInt32 mCount;
ReflowCountMgr * mMgr; // weak reference (don't delete)
ReflowCounter mCounter;
PRBool mHasBeenOutput;
};
//--------------------
// Manager Class
//--------------------
class ReflowCountMgr {
public:
ReflowCountMgr();
virtual ~ReflowCountMgr();
void ClearTotals();
void ClearGrandTotals();
void DisplayTotals(const char * aStr);
void DisplayHTMLTotals(const char * aStr);
void DisplayDiffsInTotals(const char * aStr);
void Add(const char * aName, nsIFrame * aFrame);
ReflowCounter * LookUp(const char * aName);
void PaintCount(const char * aName, nsIRenderingContext* aRenderingContext, nsPresContext* aPresContext, nsIFrame * aFrame, PRUint32 aColor);
FILE * GetOutFile() { return mFD; }
PLHashTable * GetIndiFrameHT() { return mIndiFrameCounts; }
void SetPresContext(nsPresContext * aPresContext) { mPresContext = aPresContext; } // weak reference
void SetPresShell(nsIPresShell* aPresShell) { mPresShell= aPresShell; } // weak reference
void SetDumpFrameCounts(PRBool aVal) { mDumpFrameCounts = aVal; }
void SetDumpFrameByFrameCounts(PRBool aVal) { mDumpFrameByFrameCounts = aVal; }
void SetPaintFrameCounts(PRBool aVal) { mPaintFrameByFrameCounts = aVal; }
PRBool IsPaintingFrameCounts() { return mPaintFrameByFrameCounts; }
protected:
void DisplayTotals(PRUint32 aTotal, PRUint32 * aDupArray, char * aTitle);
void DisplayHTMLTotals(PRUint32 aTotal, PRUint32 * aDupArray, char * aTitle);
static PRIntn RemoveItems(PLHashEntry *he, PRIntn i, void *arg);
static PRIntn RemoveIndiItems(PLHashEntry *he, PRIntn i, void *arg);
void CleanUp();
// stdout Output Methods
static PRIntn DoSingleTotal(PLHashEntry *he, PRIntn i, void *arg);
static PRIntn DoSingleIndi(PLHashEntry *he, PRIntn i, void *arg);
void DoGrandTotals();
void DoIndiTotalsTree();
// HTML Output Methods
static PRIntn DoSingleHTMLTotal(PLHashEntry *he, PRIntn i, void *arg);
void DoGrandHTMLTotals();
// Zero Out the Totals
static PRIntn DoClearTotals(PLHashEntry *he, PRIntn i, void *arg);
// Displays the Diff Totals
static PRIntn DoDisplayDiffTotals(PLHashEntry *he, PRIntn i, void *arg);
PLHashTable * mCounts;
PLHashTable * mIndiFrameCounts;
FILE * mFD;
PRBool mDumpFrameCounts;
PRBool mDumpFrameByFrameCounts;
PRBool mPaintFrameByFrameCounts;
PRBool mCycledOnce;
// Root Frame for Individual Tracking
nsPresContext * mPresContext;
nsIPresShell* mPresShell;
// ReflowCountMgr gReflowCountMgr;
};
#endif
//========================================================================
// comment out to hide caret
#define SHOW_CARET
// The upper bound on the amount of time to spend reflowing, in
// microseconds. When this bound is exceeded and reflow commands are
// still queued up, a reflow event is posted. The idea is for reflow
// to not hog the processor beyond the time specifed in
// gMaxRCProcessingTime. This data member is initialized from the
// layout.reflow.timeslice pref.
#define NS_MAX_REFLOW_TIME 1000000
static PRInt32 gMaxRCProcessingTime = -1;
#define MARK_INCREMENT 50
#define BLOCK_INCREMENT 4044 /* a bit under 4096, for malloc overhead */
/**A block of memory that the stack will
* chop up and hand out
*/
struct StackBlock {
// a block of memory. Note that this must be first so that it will
// be aligned.
char mBlock[BLOCK_INCREMENT];
// another block of memory that would only be created
// if our stack overflowed. Yes we have the ability
// to grow on a stack overflow
StackBlock* mNext;
StackBlock() : mNext(nsnull) { }
~StackBlock() { }
};
/* we hold an array of marks. A push pushes a mark on the stack
* a pop pops it off.
*/
struct StackMark {
// the block of memory we are currently handing out chunks of
StackBlock* mBlock;
// our current position in the memory
size_t mPos;
};
/* A stack arena allows a stack based interface to a block of memory.
* It should be used when you need to allocate some temporary memory that
* you will immediately return.
*/
class StackArena {
public:
StackArena();
~StackArena();
nsresult Init() { return mBlocks ? NS_OK : NS_ERROR_OUT_OF_MEMORY; }
// Memory management functions
void* Allocate(size_t aSize);
void Push();
void Pop();
PRUint32 Size() {
PRUint32 result = 0;
StackBlock *block = mBlocks;
while (block) {
result += sizeof(StackBlock);
block = block->mNext;
}
return result;
}
private:
// our current position in memory
size_t mPos;
// a list of memory block. Usually there is only one
// but if we overrun our stack size we can get more memory.
StackBlock* mBlocks;
// the current block of memory we are passing our chucks of
StackBlock* mCurBlock;
// our stack of mark where push has been called
StackMark* mMarks;
// the current top of the mark list
PRUint32 mStackTop;
// the size of the mark array
PRUint32 mMarkLength;
};
StackArena::StackArena()
{
mMarkLength = 0;
mMarks = nsnull;
// allocate our stack memory
mBlocks = new StackBlock();
mCurBlock = mBlocks;
mStackTop = 0;
mPos = 0;
}
StackArena::~StackArena()
{
// free up our data
delete[] mMarks;
while(mBlocks)
{
StackBlock* toDelete = mBlocks;
mBlocks = mBlocks->mNext;
delete toDelete;
}
}
void
StackArena::Push()
{
// Resize the mark array if we overrun it. Failure to allocate the
// mark array is not fatal; we just won't free to that mark. This
// allows callers not to worry about error checking.
if (mStackTop >= mMarkLength)
{
PRUint32 newLength = mStackTop + MARK_INCREMENT;
StackMark* newMarks = new StackMark[newLength];
if (newMarks) {
if (mMarkLength)
memcpy(newMarks, mMarks, sizeof(StackMark)*mMarkLength);
// Fill in any marks that we couldn't allocate during a prior call
// to Push().
for (; mMarkLength < mStackTop; ++mMarkLength) {
NS_NOTREACHED("should only hit this on out-of-memory");
newMarks[mMarkLength].mBlock = mCurBlock;
newMarks[mMarkLength].mPos = mPos;
}
delete [] mMarks;
mMarks = newMarks;
mMarkLength = newLength;
}
}
// set a mark at the top (if we can)
NS_ASSERTION(mStackTop < mMarkLength, "out of memory");
if (mStackTop < mMarkLength) {
mMarks[mStackTop].mBlock = mCurBlock;
mMarks[mStackTop].mPos = mPos;
}
mStackTop++;
}
void*
StackArena::Allocate(size_t aSize)
{
NS_ASSERTION(mStackTop > 0, "Allocate called without Push");
// make sure we are aligned. Beard said 8 was safer then 4.
// Round size to multiple of 8
aSize = PR_ROUNDUP(aSize, 8);
// if the size makes the stack overflow. Grab another block for the stack
if (mPos + aSize >= BLOCK_INCREMENT)
{
NS_ASSERTION(aSize <= BLOCK_INCREMENT,"Requested memory is greater that our block size!!");
if (mCurBlock->mNext == nsnull)
mCurBlock->mNext = new StackBlock();
mCurBlock = mCurBlock->mNext;
mPos = 0;
}
// return the chunk they need.
void *result = mCurBlock->mBlock + mPos;
mPos += aSize;
return result;
}
void
StackArena::Pop()
{
// pop off the mark
NS_ASSERTION(mStackTop > 0, "unmatched pop");
mStackTop--;
if (mStackTop >= mMarkLength) {
// We couldn't allocate the marks array at the time of the push, so
// we don't know where we're freeing to.
NS_NOTREACHED("out of memory");
if (mStackTop == 0) {
// But we do know if we've completely pushed the stack.
mCurBlock = mBlocks;
mPos = 0;
}
return;
}
#ifdef DEBUG
// Mark the "freed" memory with 0xdd to help with debugging of memory
// allocation problems.
{
StackBlock *block = mMarks[mStackTop].mBlock, *block_end = mCurBlock;
size_t pos = mMarks[mStackTop].mPos;
for (; block != block_end; block = block->mNext, pos = 0) {
memset(block->mBlock + pos, 0xdd, sizeof(block->mBlock) - pos);
}
memset(block->mBlock + pos, 0xdd, mPos - pos);
}
#endif
mCurBlock = mMarks[mStackTop].mBlock;
mPos = mMarks[mStackTop].mPos;
}
struct nsCallbackEventRequest
{
nsIReflowCallback* callback;
nsCallbackEventRequest* next;
};
// ----------------------------------------------------------------------------
#define ASSERT_REFLOW_SCHEDULED_STATE() \
NS_ASSERTION(mReflowScheduled == \
GetPresContext()->RefreshDriver()-> \
IsRefreshObserver(this, Flush_Layout), "Unexpected state")
class nsPresShellEventCB;
class nsAutoCauseReflowNotifier;
class PresShell : public nsIPresShell, public nsIViewObserver,
public nsStubDocumentObserver,
public nsISelectionController, public nsIObserver,
public nsSupportsWeakReference,
public nsARefreshObserver
{
public:
PresShell();
NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
// nsISupports
NS_DECL_ISUPPORTS
// nsIPresShell
virtual NS_HIDDEN_(nsresult) Init(nsIDocument* aDocument,
nsPresContext* aPresContext,
nsIViewManager* aViewManager,
nsStyleSet* aStyleSet,
nsCompatibility aCompatMode);
virtual NS_HIDDEN_(void) Destroy();
virtual NS_HIDDEN_(void*) AllocateFrame(nsQueryFrame::FrameIID aCode,
size_t aSize);
virtual NS_HIDDEN_(void) FreeFrame(nsQueryFrame::FrameIID aCode,
void* aChunk);
virtual NS_HIDDEN_(void*) AllocateMisc(size_t aSize);
virtual NS_HIDDEN_(void) FreeMisc(size_t aSize, void* aChunk);
// Dynamic stack memory allocation
virtual NS_HIDDEN_(void) PushStackMemory();
virtual NS_HIDDEN_(void) PopStackMemory();
virtual NS_HIDDEN_(void*) AllocateStackMemory(size_t aSize);
virtual NS_HIDDEN_(nsresult) SetPreferenceStyleRules(PRBool aForceReflow);
NS_IMETHOD GetSelection(SelectionType aType, nsISelection** aSelection);
virtual nsISelection* GetCurrentSelection(SelectionType aType);
NS_IMETHOD SetDisplaySelection(PRInt16 aToggle);
NS_IMETHOD GetDisplaySelection(PRInt16 *aToggle);
NS_IMETHOD ScrollSelectionIntoView(SelectionType aType, SelectionRegion aRegion, PRBool aIsSynchronous);
NS_IMETHOD RepaintSelection(SelectionType aType);
virtual NS_HIDDEN_(void) BeginObservingDocument();
virtual NS_HIDDEN_(void) EndObservingDocument();
virtual NS_HIDDEN_(nsresult) InitialReflow(nscoord aWidth, nscoord aHeight);
virtual NS_HIDDEN_(nsresult) ResizeReflow(nscoord aWidth, nscoord aHeight);
virtual NS_HIDDEN_(void) StyleChangeReflow();
virtual NS_HIDDEN_(nsIPageSequenceFrame*) GetPageSequenceFrame() const;
virtual NS_HIDDEN_(nsIFrame*) GetRealPrimaryFrameFor(nsIContent* aContent) const;
virtual NS_HIDDEN_(nsIFrame*) GetPlaceholderFrameFor(nsIFrame* aFrame) const;
virtual NS_HIDDEN_(void) FrameNeedsReflow(nsIFrame *aFrame, IntrinsicDirty aIntrinsicDirty,
nsFrameState aBitToAdd);
virtual NS_HIDDEN_(void) FrameNeedsToContinueReflow(nsIFrame *aFrame);
virtual NS_HIDDEN_(void) CancelAllPendingReflows();
virtual NS_HIDDEN_(PRBool) IsSafeToFlush() const;
virtual NS_HIDDEN_(void) FlushPendingNotifications(mozFlushType aType);
/**
* Recreates the frames for a node
*/
virtual NS_HIDDEN_(nsresult) RecreateFramesFor(nsIContent* aContent);
/**
* Post a callback that should be handled after reflow has finished.
*/
virtual NS_HIDDEN_(nsresult) PostReflowCallback(nsIReflowCallback* aCallback);
virtual NS_HIDDEN_(void) CancelReflowCallback(nsIReflowCallback* aCallback);
virtual NS_HIDDEN_(void) ClearFrameRefs(nsIFrame* aFrame);
virtual NS_HIDDEN_(nsresult) CreateRenderingContext(nsIFrame *aFrame,
nsIRenderingContext** aContext);
virtual NS_HIDDEN_(nsresult) GoToAnchor(const nsAString& aAnchorName, PRBool aScroll);
virtual NS_HIDDEN_(nsresult) ScrollToAnchor();
virtual NS_HIDDEN_(nsresult) ScrollContentIntoView(nsIContent* aContent,
PRIntn aVPercent,
PRIntn aHPercent);
virtual PRBool ScrollFrameRectIntoView(nsIFrame* aFrame,
const nsRect& aRect,
PRIntn aVPercent,
PRIntn aHPercent,
PRUint32 aFlags);
virtual nsRectVisibility GetRectVisibility(nsIFrame *aFrame,
const nsRect &aRect,
nscoord aMinTwips) const;
virtual NS_HIDDEN_(void) SetIgnoreFrameDestruction(PRBool aIgnore);
virtual NS_HIDDEN_(void) NotifyDestroyingFrame(nsIFrame* aFrame);
virtual NS_HIDDEN_(nsresult) GetLinkLocation(nsIDOMNode* aNode, nsAString& aLocationString) const;
virtual NS_HIDDEN_(nsresult) CaptureHistoryState(nsILayoutHistoryState** aLayoutHistoryState, PRBool aLeavingPage);
virtual NS_HIDDEN_(void) UnsuppressPainting();
virtual nsresult GetAgentStyleSheets(nsCOMArray<nsIStyleSheet>& aSheets);
virtual nsresult SetAgentStyleSheets(const nsCOMArray<nsIStyleSheet>& aSheets);
virtual nsresult AddOverrideStyleSheet(nsIStyleSheet *aSheet);
virtual nsresult RemoveOverrideStyleSheet(nsIStyleSheet *aSheet);
virtual NS_HIDDEN_(nsresult) HandleEventWithTarget(nsEvent* aEvent, nsIFrame* aFrame,
nsIContent* aContent,
nsEventStatus* aStatus);
virtual NS_HIDDEN_(nsIFrame*) GetEventTargetFrame();
virtual NS_HIDDEN_(already_AddRefed<nsIContent>) GetEventTargetContent(nsEvent* aEvent);
virtual nsresult ReconstructFrames(void);
virtual void Freeze();
virtual void Thaw();
virtual void FireOrClearDelayedEvents(PRBool aFireEvents);
virtual nsIFrame* GetFrameForPoint(nsIFrame* aFrame, nsPoint aPt);
virtual NS_HIDDEN_(nsresult) RenderDocument(const nsRect& aRect, PRUint32 aFlags,
nscolor aBackgroundColor,
gfxContext* aThebesContext);
virtual already_AddRefed<gfxASurface> RenderNode(nsIDOMNode* aNode,
nsIntRegion* aRegion,
nsIntPoint& aPoint,
nsIntRect* aScreenRect);
virtual already_AddRefed<gfxASurface> RenderSelection(nsISelection* aSelection,
nsIntPoint& aPoint,
nsIntRect* aScreenRect);
virtual already_AddRefed<nsPIDOMWindow> GetRootWindow();
//nsIViewObserver interface
NS_IMETHOD Paint(nsIView* aDisplayRoot,
nsIView* aViewToPaint,
nsIWidget* aWidget,
const nsRegion& aDirtyRegion,
const nsIntRegion& aIntDirtyRegion,
PRBool aPaintDefaultBackground,
PRBool aWillSendDidPaint);
NS_IMETHOD HandleEvent(nsIView* aView,
nsGUIEvent* aEvent,
nsEventStatus* aEventStatus);
virtual NS_HIDDEN_(nsresult) HandleDOMEventWithTarget(nsIContent* aTargetContent,
nsEvent* aEvent,
nsEventStatus* aStatus);
virtual NS_HIDDEN_(nsresult) HandleDOMEventWithTarget(nsIContent* aTargetContent,
nsIDOMEvent* aEvent,
nsEventStatus* aStatus);
NS_IMETHOD ResizeReflow(nsIView *aView, nscoord aWidth, nscoord aHeight);
NS_IMETHOD_(PRBool) IsVisible();
NS_IMETHOD_(void) WillPaint(PRBool aWillSendDidPaint);
NS_IMETHOD_(void) DidPaint();
NS_IMETHOD_(void) DispatchSynthMouseMove(nsGUIEvent *aEvent,
PRBool aFlushOnHoverChange);
NS_IMETHOD_(void) ClearMouseCapture(nsIView* aView);
// caret handling
virtual NS_HIDDEN_(already_AddRefed<nsCaret>) GetCaret() const;
virtual NS_HIDDEN_(void) MaybeInvalidateCaretPosition();
NS_IMETHOD SetCaretEnabled(PRBool aInEnable);
NS_IMETHOD SetCaretReadOnly(PRBool aReadOnly);
NS_IMETHOD GetCaretEnabled(PRBool *aOutEnabled);
NS_IMETHOD SetCaretVisibilityDuringSelection(PRBool aVisibility);
NS_IMETHOD GetCaretVisible(PRBool *_retval);
virtual void SetCaret(nsCaret *aNewCaret);
virtual void RestoreCaret();
NS_IMETHOD SetSelectionFlags(PRInt16 aInEnable);
NS_IMETHOD GetSelectionFlags(PRInt16 *aOutEnable);
// nsISelectionController
NS_IMETHOD CharacterMove(PRBool aForward, PRBool aExtend);
NS_IMETHOD CharacterExtendForDelete();
NS_IMETHOD CharacterExtendForBackspace();
NS_IMETHOD WordMove(PRBool aForward, PRBool aExtend);
NS_IMETHOD WordExtendForDelete(PRBool aForward);
NS_IMETHOD LineMove(PRBool aForward, PRBool aExtend);
NS_IMETHOD IntraLineMove(PRBool aForward, PRBool aExtend);
NS_IMETHOD PageMove(PRBool aForward, PRBool aExtend);
NS_IMETHOD ScrollPage(PRBool aForward);
NS_IMETHOD ScrollLine(PRBool aForward);
NS_IMETHOD ScrollHorizontal(PRBool aLeft);
NS_IMETHOD CompleteScroll(PRBool aForward);
NS_IMETHOD CompleteMove(PRBool aForward, PRBool aExtend);
NS_IMETHOD SelectAll();
NS_IMETHOD CheckVisibility(nsIDOMNode *node, PRInt16 startOffset, PRInt16 EndOffset, PRBool *_retval);
// nsIDocumentObserver
virtual void BeginUpdate(nsIDocument* aDocument, nsUpdateType aUpdateType);
virtual void EndUpdate(nsIDocument* aDocument, nsUpdateType aUpdateType);
virtual void BeginLoad(nsIDocument* aDocument);
virtual void EndLoad(nsIDocument* aDocument);
virtual void ContentStatesChanged(nsIDocument* aDocument,
nsIContent* aContent1,
nsIContent* aContent2,
PRInt32 aStateMask);
virtual void DocumentStatesChanged(nsIDocument* aDocument,
PRInt32 aStateMask);
virtual void StyleSheetAdded(nsIDocument* aDocument,
nsIStyleSheet* aStyleSheet,
PRBool aDocumentSheet);
virtual void StyleSheetRemoved(nsIDocument* aDocument,
nsIStyleSheet* aStyleSheet,
PRBool aDocumentSheet);
virtual void StyleSheetApplicableStateChanged(nsIDocument* aDocument,
nsIStyleSheet* aStyleSheet,
PRBool aApplicable);
virtual void StyleRuleChanged(nsIDocument* aDocument,
nsIStyleSheet* aStyleSheet,
nsIStyleRule* aOldStyleRule,
nsIStyleRule* aNewStyleRule);
virtual void StyleRuleAdded(nsIDocument* aDocument,
nsIStyleSheet* aStyleSheet,
nsIStyleRule* aStyleRule);
virtual void StyleRuleRemoved(nsIDocument* aDocument,
nsIStyleSheet* aStyleSheet,
nsIStyleRule* aStyleRule);
// nsIMutationObserver
NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTEWILLCHANGE
NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
NS_DECL_NSIOBSERVER
// nsARefreshObserver
virtual void WillRefresh(mozilla::TimeStamp aTime);
#ifdef MOZ_REFLOW_PERF
virtual NS_HIDDEN_(void) DumpReflows();
virtual NS_HIDDEN_(void) CountReflows(const char * aName, nsIFrame * aFrame);
virtual NS_HIDDEN_(void) PaintCount(const char * aName,
nsIRenderingContext* aRenderingContext,
nsPresContext* aPresContext,
nsIFrame * aFrame,
PRUint32 aColor);
virtual NS_HIDDEN_(void) SetPaintFrameCount(PRBool aOn);
virtual PRBool IsPaintingFrameCounts();
#endif
#ifdef DEBUG
virtual void ListStyleContexts(nsIFrame *aRootFrame, FILE *out,
PRInt32 aIndent = 0);
virtual void ListStyleSheets(FILE *out, PRInt32 aIndent = 0);
virtual void VerifyStyleTree();
#endif
#ifdef PR_LOGGING
static PRLogModuleInfo* gLog;
#endif
virtual NS_HIDDEN_(void) DisableNonTestMouseEvents(PRBool aDisable);
virtual void UpdateCanvasBackground();
virtual nsresult AddCanvasBackgroundColorItem(nsDisplayListBuilder& aBuilder,
nsDisplayList& aList,
nsIFrame* aFrame,
const nsRect& aBounds,
nscolor aBackstopColor,
PRBool aForceDraw);
protected:
virtual ~PresShell();
void HandlePostedReflowCallbacks(PRBool aInterruptible);
void CancelPostedReflowCallbacks();
void UnsuppressAndInvalidate();
void WillCauseReflow() {
nsContentUtils::AddScriptBlocker();
++mChangeNestCount;
}
nsresult DidCauseReflow();
friend class nsAutoCauseReflowNotifier;
void WillDoReflow();
void DidDoReflow(PRBool aInterruptible);
// ProcessReflowCommands returns whether we processed all our dirty roots
// without interruptions.
PRBool ProcessReflowCommands(PRBool aInterruptible);
// MaybeScheduleReflow checks if posting a reflow is needed, then checks if
// the last reflow was interrupted. In the interrupted case ScheduleReflow is
// called off a timer, otherwise it is called directly.
void MaybeScheduleReflow();
// Actually schedules a reflow. This should only be called by
// MaybeScheduleReflow and the reflow timer ScheduleReflowOffTimer
// sets up.
void ScheduleReflow();
// DoReflow returns whether the reflow finished without interruption
PRBool DoReflow(nsIFrame* aFrame, PRBool aInterruptible);
#ifdef DEBUG
void DoVerifyReflow();
void VerifyHasDirtyRootAncestor(nsIFrame* aFrame);
#endif
// Helper for ScrollContentIntoView
void DoScrollContentIntoView(nsIContent* aContent,
PRIntn aVPercent,
PRIntn aHPercent);
friend class nsPresShellEventCB;
PRBool mCaretEnabled;
#ifdef NS_DEBUG
nsresult CloneStyleSet(nsStyleSet* aSet, nsStyleSet** aResult);
PRBool VerifyIncrementalReflow();
PRBool mInVerifyReflow;
void ShowEventTargetDebug();
#endif