1/*
2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#import "WebCoreStatistics.h"
30
31#import "DOMElementInternal.h"
32#import "WebCache.h"
33#import "WebFrameInternal.h"
34#import <JavaScriptCore/JSLock.h>
35#import <JavaScriptCore/MemoryStatistics.h>
36#import <WebCore/FontCache.h>
37#import <WebCore/Frame.h>
38#import <WebCore/GCController.h>
39#import <WebCore/GlyphPageTreeNode.h>
40#import <WebCore/GraphicsContext.h>
41#import <WebCore/IconDatabase.h>
42#import <WebCore/JSDOMWindow.h>
43#import <WebCore/PageCache.h>
44#import <WebCore/PageConsole.h>
45#import <WebCore/PrintContext.h>
46#import <WebCore/RenderTreeAsText.h>
47#import <WebCore/RenderView.h>
48
49using namespace JSC;
50using namespace WebCore;
51
52@implementation WebCoreStatistics
53
54+ (NSArray *)statistics
55{
56    return [WebCache statistics];
57}
58
59+ (size_t)javaScriptObjectsCount
60{
61    JSLockHolder lock(JSDOMWindow::commonVM());
62    return JSDOMWindow::commonVM().heap.objectCount();
63}
64
65+ (size_t)javaScriptGlobalObjectsCount
66{
67    JSLockHolder lock(JSDOMWindow::commonVM());
68    return JSDOMWindow::commonVM().heap.globalObjectCount();
69}
70
71+ (size_t)javaScriptProtectedObjectsCount
72{
73    JSLockHolder lock(JSDOMWindow::commonVM());
74    return JSDOMWindow::commonVM().heap.protectedObjectCount();
75}
76
77+ (size_t)javaScriptProtectedGlobalObjectsCount
78{
79    JSLockHolder lock(JSDOMWindow::commonVM());
80    return JSDOMWindow::commonVM().heap.protectedGlobalObjectCount();
81}
82
83+ (NSCountedSet *)javaScriptProtectedObjectTypeCounts
84{
85    JSLockHolder lock(JSDOMWindow::commonVM());
86
87    NSCountedSet *result = [NSCountedSet set];
88
89    OwnPtr<TypeCountSet> counts(JSDOMWindow::commonVM().heap.protectedObjectTypeCounts());
90    HashCountedSet<const char*>::iterator end = counts->end();
91    for (HashCountedSet<const char*>::iterator it = counts->begin(); it != end; ++it)
92        for (unsigned i = 0; i < it->value; ++i)
93            [result addObject:[NSString stringWithUTF8String:it->key]];
94
95    return result;
96}
97
98+ (NSCountedSet *)javaScriptObjectTypeCounts
99{
100    JSLockHolder lock(JSDOMWindow::commonVM());
101
102    NSCountedSet *result = [NSCountedSet set];
103
104    OwnPtr<TypeCountSet> counts(JSDOMWindow::commonVM().heap.objectTypeCounts());
105    HashCountedSet<const char*>::iterator end = counts->end();
106    for (HashCountedSet<const char*>::iterator it = counts->begin(); it != end; ++it)
107        for (unsigned i = 0; i < it->value; ++i)
108            [result addObject:[NSString stringWithUTF8String:it->key]];
109
110    return result;
111}
112
113+ (void)garbageCollectJavaScriptObjects
114{
115    gcController().garbageCollectNow();
116}
117
118+ (void)garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging:(BOOL)waitUntilDone
119{
120    gcController().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
121}
122
123+ (void)setJavaScriptGarbageCollectorTimerEnabled:(BOOL)enable
124{
125    gcController().setJavaScriptGarbageCollectorTimerEnabled(enable);
126}
127
128+ (size_t)iconPageURLMappingCount
129{
130    return iconDatabase().pageURLMappingCount();
131}
132
133+ (size_t)iconRetainedPageURLCount
134{
135    return iconDatabase().retainedPageURLCount();
136}
137
138+ (size_t)iconRecordCount
139{
140    return iconDatabase().iconRecordCount();
141}
142
143+ (size_t)iconsWithDataCount
144{
145    return iconDatabase().iconRecordCountWithData();
146}
147
148+ (size_t)cachedFontDataCount
149{
150    return fontCache().fontDataCount();
151}
152
153+ (size_t)cachedFontDataInactiveCount
154{
155    return fontCache().inactiveFontDataCount();
156}
157
158+ (void)purgeInactiveFontData
159{
160    fontCache().purgeInactiveFontData();
161}
162
163+ (size_t)glyphPageCount
164{
165    return GlyphPageTreeNode::treeGlyphPageCount();
166}
167
168+ (BOOL)shouldPrintExceptions
169{
170    JSLockHolder lock(JSDOMWindow::commonVM());
171    return PageConsole::shouldPrintExceptions();
172}
173
174+ (void)setShouldPrintExceptions:(BOOL)print
175{
176    JSLockHolder lock(JSDOMWindow::commonVM());
177    PageConsole::setShouldPrintExceptions(print);
178}
179
180+ (void)emptyCache
181{
182    [WebCache empty];
183}
184
185+ (void)setCacheDisabled:(BOOL)disabled
186{
187    [WebCache setDisabled:disabled];
188}
189
190+ (void)startIgnoringWebCoreNodeLeaks
191{
192    WebCore::Node::startIgnoringLeaks();
193}
194
195+ (void)stopIgnoringWebCoreNodeLeaks
196{
197    WebCore::Node::stopIgnoringLeaks();
198}
199
200+ (NSDictionary *)memoryStatistics
201{
202    WTF::FastMallocStatistics fastMallocStatistics = WTF::fastMallocStatistics();
203
204    JSLockHolder lock(JSDOMWindow::commonVM());
205    size_t heapSize = JSDOMWindow::commonVM().heap.size();
206    size_t heapFree = JSDOMWindow::commonVM().heap.capacity() - heapSize;
207    GlobalMemoryStatistics globalMemoryStats = globalMemoryStatistics();
208
209    return [NSDictionary dictionaryWithObjectsAndKeys:
210                [NSNumber numberWithInt:fastMallocStatistics.reservedVMBytes], @"FastMallocReservedVMBytes",
211                [NSNumber numberWithInt:fastMallocStatistics.committedVMBytes], @"FastMallocCommittedVMBytes",
212                [NSNumber numberWithInt:fastMallocStatistics.freeListBytes], @"FastMallocFreeListBytes",
213                [NSNumber numberWithInt:heapSize], @"JavaScriptHeapSize",
214                [NSNumber numberWithInt:heapFree], @"JavaScriptFreeSize",
215                [NSNumber numberWithUnsignedInt:(unsigned int)globalMemoryStats.stackBytes], @"JavaScriptStackSize",
216                [NSNumber numberWithUnsignedInt:(unsigned int)globalMemoryStats.JITBytes], @"JavaScriptJITSize",
217            nil];
218}
219
220+ (void)returnFreeMemoryToSystem
221{
222    WTF::releaseFastMallocFreeMemory();
223}
224
225+ (int)cachedPageCount
226{
227    return pageCache()->pageCount();
228}
229
230+ (int)cachedFrameCount
231{
232    return pageCache()->frameCount();
233}
234
235// Deprecated
236+ (int)autoreleasedPageCount
237{
238    return 0;
239}
240
241// Deprecated
242+ (size_t)javaScriptNoGCAllowedObjectsCount
243{
244    return 0;
245}
246
247+ (size_t)javaScriptReferencedObjectsCount
248{
249    JSLockHolder lock(JSDOMWindow::commonVM());
250    return JSDOMWindow::commonVM().heap.protectedObjectCount();
251}
252
253+ (NSSet *)javaScriptRootObjectClasses
254{
255    return [self javaScriptRootObjectTypeCounts];
256}
257
258+ (size_t)javaScriptInterpretersCount
259{
260    return [self javaScriptProtectedGlobalObjectsCount];
261}
262
263+ (NSCountedSet *)javaScriptRootObjectTypeCounts
264{
265    return [self javaScriptProtectedObjectTypeCounts];
266}
267
268@end
269
270@implementation WebFrame (WebKitDebug)
271
272- (NSString *)renderTreeAsExternalRepresentationForPrinting:(BOOL)forPrinting
273{
274    return externalRepresentation(_private->coreFrame, forPrinting ? RenderAsTextPrintingMode : RenderAsTextBehaviorNormal);
275}
276
277- (int)numberOfPagesWithPageWidth:(float)pageWidthInPixels pageHeight:(float)pageHeightInPixels
278{
279    return PrintContext::numberOfPages(_private->coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
280}
281
282- (void)printToCGContext:(CGContextRef)cgContext pageWidth:(float)pageWidthInPixels pageHeight:(float)pageHeightInPixels
283{
284    Frame* coreFrame = _private->coreFrame;
285    if (!coreFrame)
286        return;
287
288    GraphicsContext graphicsContext(cgContext);
289    PrintContext::spoolAllPagesWithBoundaries(coreFrame, graphicsContext, FloatSize(pageWidthInPixels, pageHeightInPixels));
290}
291
292@end
293