1/*
2 * Copyright (C) 2007 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 Computer, 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#include "config.h"
30#include "WebKitDLL.h"
31#include "WebKitStatistics.h"
32
33#include "WebKitStatisticsPrivate.h"
34#include <WebCore/BString.h>
35
36using namespace WebCore;
37
38int WebViewCount;
39int WebDataSourceCount;
40int WebFrameCount;
41int WebHTMLRepresentationCount;
42int WebFrameViewCount;
43
44// WebKitStatistics ---------------------------------------------------------------------------
45
46WebKitStatistics::WebKitStatistics()
47: m_refCount(0)
48{
49    gClassCount++;
50    gClassNameCount.add("WebKitStatistics");
51}
52
53WebKitStatistics::~WebKitStatistics()
54{
55    gClassCount--;
56    gClassNameCount.remove("WebKitStatistics");
57}
58
59WebKitStatistics* WebKitStatistics::createInstance()
60{
61    WebKitStatistics* instance = new WebKitStatistics();
62    instance->AddRef();
63    return instance;
64}
65
66// IUnknown -------------------------------------------------------------------
67
68HRESULT STDMETHODCALLTYPE WebKitStatistics::QueryInterface(REFIID riid, void** ppvObject)
69{
70    *ppvObject = 0;
71    if (IsEqualGUID(riid, IID_IUnknown))
72        *ppvObject = static_cast<WebKitStatistics*>(this);
73    else if (IsEqualGUID(riid, IID_IWebKitStatistics))
74        *ppvObject = static_cast<WebKitStatistics*>(this);
75    else
76        return E_NOINTERFACE;
77
78    AddRef();
79    return S_OK;
80}
81
82ULONG STDMETHODCALLTYPE WebKitStatistics::AddRef(void)
83{
84    return ++m_refCount;
85}
86
87ULONG STDMETHODCALLTYPE WebKitStatistics::Release(void)
88{
89    ULONG newRef = --m_refCount;
90    if (!newRef)
91        delete(this);
92
93    return newRef;
94}
95
96// IWebKitStatistics ------------------------------------------------------------------------------
97
98HRESULT STDMETHODCALLTYPE WebKitStatistics::webViewCount(
99    /* [retval][out] */ int *count)
100{
101    *count = WebViewCount;
102    return S_OK;
103}
104
105HRESULT STDMETHODCALLTYPE WebKitStatistics::frameCount(
106    /* [retval][out] */ int *count)
107{
108    *count = WebFrameCount;
109    return S_OK;
110}
111
112HRESULT STDMETHODCALLTYPE WebKitStatistics::dataSourceCount(
113    /* [retval][out] */ int *count)
114{
115    *count = WebDataSourceCount;
116    return S_OK;
117}
118
119HRESULT STDMETHODCALLTYPE WebKitStatistics::viewCount(
120    /* [retval][out] */ int *count)
121{
122    *count = WebFrameViewCount;
123    return S_OK;
124}
125
126HRESULT STDMETHODCALLTYPE WebKitStatistics::HTMLRepresentationCount(
127    /* [retval][out] */ int *count)
128{
129    *count = WebHTMLRepresentationCount;
130    return S_OK;
131}
132
133HRESULT STDMETHODCALLTYPE WebKitStatistics::comClassCount(
134    /* [retval][out] */ int *classCount)
135{
136    *classCount = gClassCount;
137    return S_OK;
138}
139
140HRESULT STDMETHODCALLTYPE WebKitStatistics::comClassNameCounts(
141    /* [retval][out] */ BSTR *output)
142{
143    typedef HashCountedSet<String>::const_iterator Iterator;
144    Iterator end = gClassNameCount.end();
145    Vector<UChar> vector;
146    for (Iterator current = gClassNameCount.begin(); current != end; ++current) {
147        append(vector, String::format("%4u", current->second));
148        vector.append('\t');
149        append(vector, static_cast<String>(current->first));
150        vector.append('\n');
151    }
152    *output = BString(String::adopt(vector)).release();
153    return S_OK;
154}
155