1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "ScriptProfile.h"
29
30#if ENABLE(JAVASCRIPT_DEBUGGER)
31#include "InspectorValues.h"
32#include "JSDOMBinding.h"
33#include <profiler/Profile.h>
34#include <profiler/ProfileNode.h>
35
36namespace WebCore {
37
38PassRefPtr<ScriptProfile> ScriptProfile::create(PassRefPtr<JSC::Profile> profile)
39{
40    if (!profile)
41        return 0;
42    return adoptRef(new ScriptProfile(profile));
43}
44
45ScriptProfile::ScriptProfile(PassRefPtr<JSC::Profile> profile)
46    : m_profile(profile)
47{
48}
49
50ScriptProfile::~ScriptProfile()
51{
52}
53
54String ScriptProfile::title() const
55{
56    return ustringToString(m_profile->title());
57}
58
59unsigned int ScriptProfile::uid() const
60{
61    return m_profile->uid();
62}
63
64ScriptProfileNode* ScriptProfile::head() const
65{
66    return m_profile->head();
67}
68
69#if ENABLE(INSPECTOR)
70static PassRefPtr<InspectorObject> buildInspectorObjectFor(const JSC::ProfileNode* node)
71{
72    RefPtr<InspectorObject> result = InspectorObject::create();
73
74    result->setString("functionName", ustringToString(node->functionName()));
75    result->setString("url", ustringToString(node->url()));
76    result->setNumber("lineNumber", node->lineNumber());
77    result->setNumber("totalTime", node->totalTime());
78    result->setNumber("selfTime", node->selfTime());
79    result->setNumber("numberOfCalls", node->numberOfCalls());
80    result->setBoolean("visible", node->visible());
81    result->setNumber("callUID", node->callIdentifier().hash());
82
83    RefPtr<InspectorArray> childrenArray = InspectorArray::create();
84    typedef Vector<RefPtr<JSC::ProfileNode> > ProfileNodesList;
85    const ProfileNodesList& children = node->children();
86    ProfileNodesList::const_iterator end = children.end();
87    for (ProfileNodesList::const_iterator iter = children.begin(); iter != end; ++iter)
88        childrenArray->pushObject(buildInspectorObjectFor(iter->get()));
89    result->setArray("children", childrenArray);
90
91    return result;
92}
93
94PassRefPtr<InspectorObject> ScriptProfile::buildInspectorObjectForHead() const
95{
96    return buildInspectorObjectFor(m_profile->head());
97}
98#endif
99
100} // namespace WebCore
101
102#endif // ENABLE(JAVASCRIPT_DEBUGGER)
103