|
Line 0
a/Source/WebCore/inspector/TimelineTraceEventProcessor.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2012 Google 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 are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Redistributions in binary form must reproduce the above |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of Google Inc. nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
|
| 33 |
#if ENABLE(INSPECTOR) |
| 34 |
|
| 35 |
#include "TimelineTraceEventProcessor.h" |
| 36 |
|
| 37 |
#include "InspectorClient.h" |
| 38 |
#include "InspectorTimelineAgent.h" |
| 39 |
|
| 40 |
#include <wtf/MainThread.h> |
| 41 |
#include <wtf/Vector.h> |
| 42 |
|
| 43 |
namespace WebCore { |
| 44 |
|
| 45 |
namespace { |
| 46 |
|
| 47 |
class TraceEventDispatcher { |
| 48 |
WTF_MAKE_NONCOPYABLE(TraceEventDispatcher); |
| 49 |
public: |
| 50 |
static TraceEventDispatcher* instance() |
| 51 |
{ |
| 52 |
DEFINE_STATIC_LOCAL(TraceEventDispatcher, instance, ()); |
| 53 |
return &instance; |
| 54 |
} |
| 55 |
|
| 56 |
void addProcessor(TimelineTraceEventProcessor* processor, InspectorClient* client) |
| 57 |
{ |
| 58 |
m_processors.append(processor); |
| 59 |
if (m_processors.size() == 1) |
| 60 |
client->setTraceEventCallback(dispatchEvent); |
| 61 |
} |
| 62 |
|
| 63 |
void removeProcessor(TimelineTraceEventProcessor* processor, InspectorClient* client) |
| 64 |
{ |
| 65 |
size_t index = m_processors.find(processor); |
| 66 |
if (index == notFound) { |
| 67 |
ASSERT_NOT_REACHED(); |
| 68 |
return; |
| 69 |
} |
| 70 |
m_processors[index] = m_processors.last(); |
| 71 |
m_processors.removeLast(); |
| 72 |
if (m_processors.isEmpty()) |
| 73 |
client->setTraceEventCallback(0); |
| 74 |
} |
| 75 |
|
| 76 |
private: |
| 77 |
TraceEventDispatcher() { } |
| 78 |
|
| 79 |
static void dispatchEvent(char phase, const unsigned char*, const char* name, unsigned long long id, |
| 80 |
int numArgs, const char* const* argNames, const unsigned char* argTypes, const unsigned long long* argValues, |
| 81 |
unsigned char flags) |
| 82 |
{ |
| 83 |
if (!isMainThread()) |
| 84 |
return; |
| 85 |
|
| 86 |
Vector<TimelineTraceEventProcessor*>& processors = instance()->m_processors; |
| 87 |
for (int i = 0, size = processors.size(); i < size; ++i) { |
| 88 |
processors[i]->processEvent(static_cast<TimelineTraceEventProcessor::TraceEventPhase>(phase), |
| 89 |
name, id, numArgs, argNames, argTypes, argValues, flags); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
Vector<TimelineTraceEventProcessor*> m_processors; |
| 94 |
}; |
| 95 |
|
| 96 |
} // namespce |
| 97 |
|
| 98 |
class TraceEventArguments { |
| 99 |
WTF_MAKE_NONCOPYABLE(TraceEventArguments); |
| 100 |
public: |
| 101 |
TraceEventArguments(int numArguments, const char* const* argumentNames, const unsigned char* argumentTypes, const unsigned long long* argumentValues) |
| 102 |
: m_size(numArguments) |
| 103 |
, m_argumentNames(argumentNames) |
| 104 |
, m_argumentTypes(argumentTypes) |
| 105 |
, m_argumentValues(argumentValues) |
| 106 |
{ |
| 107 |
} |
| 108 |
|
| 109 |
int size() const { return m_size; } |
| 110 |
|
| 111 |
bool asBool(const char* name) const |
| 112 |
{ |
| 113 |
return parameter(name, TypeBool).m_bool; |
| 114 |
} |
| 115 |
long long asInt(const char* name) const |
| 116 |
{ |
| 117 |
return parameter(name, TypeInt).m_int; |
| 118 |
} |
| 119 |
unsigned long long asUInt(const char* name) const |
| 120 |
{ |
| 121 |
return parameter(name, TypeUInt).m_uint; |
| 122 |
} |
| 123 |
double asDouble(const char* name) const |
| 124 |
{ |
| 125 |
return parameter(name, TypeDouble).m_double; |
| 126 |
} |
| 127 |
const char* asString(const char* name) const |
| 128 |
{ |
| 129 |
return parameter(name, TypeString).m_string; |
| 130 |
} |
| 131 |
|
| 132 |
private: |
| 133 |
// FIXME: use the definition in TraceEvent.h once we expose the latter to all plaforms. |
| 134 |
union TraceValueUnion { |
| 135 |
bool m_bool; |
| 136 |
unsigned long long m_uint; |
| 137 |
long long m_int; |
| 138 |
double m_double; |
| 139 |
const void* m_pointer; |
| 140 |
const char* m_string; |
| 141 |
}; |
| 142 |
|
| 143 |
enum TraceValueTypes { |
| 144 |
TypeBool = 1, |
| 145 |
TypeUInt = 2, |
| 146 |
TypeInt = 3, |
| 147 |
TypeDouble = 4, |
| 148 |
TypePointer = 5, |
| 149 |
TypeString = 6, |
| 150 |
TypeCopyString = 7 |
| 151 |
}; |
| 152 |
|
| 153 |
const TraceValueUnion& parameter(const char* name, TraceValueTypes expectedType) const |
| 154 |
{ |
| 155 |
static TraceValueUnion missingValue; |
| 156 |
|
| 157 |
for (int i = 0; i < m_size; ++i) { |
| 158 |
if (!strcmp(name, m_argumentNames[i])) { |
| 159 |
if (m_argumentTypes[i] != expectedType) { |
| 160 |
ASSERT_NOT_REACHED(); |
| 161 |
return missingValue; |
| 162 |
} |
| 163 |
return *reinterpret_cast<const TraceValueUnion*>(m_argumentValues + i); |
| 164 |
} |
| 165 |
} |
| 166 |
ASSERT_NOT_REACHED(); |
| 167 |
return missingValue; |
| 168 |
} |
| 169 |
|
| 170 |
int m_size; |
| 171 |
const char* const* m_argumentNames; |
| 172 |
const unsigned char* m_argumentTypes; |
| 173 |
const unsigned long long* m_argumentValues; |
| 174 |
}; |
| 175 |
|
| 176 |
TimelineTraceEventProcessor::TimelineTraceEventProcessor(InspectorTimelineAgent* timelineAgent, InspectorClient *client) |
| 177 |
: m_timelineAgent(timelineAgent) |
| 178 |
, m_inspectorClient(client) |
| 179 |
{ |
| 180 |
TraceEventDispatcher::instance()->addProcessor(this, m_inspectorClient); |
| 181 |
} |
| 182 |
|
| 183 |
TimelineTraceEventProcessor::~TimelineTraceEventProcessor() |
| 184 |
{ |
| 185 |
TraceEventDispatcher::instance()->removeProcessor(this, m_inspectorClient); |
| 186 |
} |
| 187 |
|
| 188 |
void TimelineTraceEventProcessor::processEvent(TraceEventPhase phase, const char* name, unsigned long long, |
| 189 |
int numArgs, const char* const* argNames, const unsigned char* argTypes, const unsigned long long* argValues, |
| 190 |
unsigned char) |
| 191 |
{ |
| 192 |
HashMap<String, EventTypeEntry>::iterator it = m_handlersByType.find(name); |
| 193 |
if (it == m_handlersByType.end()) |
| 194 |
return; |
| 195 |
TraceEventHandler handler = 0; |
| 196 |
switch (phase) { |
| 197 |
case TracePhaseBegin: |
| 198 |
handler = it->value.m_begin; |
| 199 |
break; |
| 200 |
case TracePhaseEnd: |
| 201 |
handler = it->value.m_end; |
| 202 |
break; |
| 203 |
case TracePhaseInstant: |
| 204 |
handler = it->value.m_instant; |
| 205 |
break; |
| 206 |
default: |
| 207 |
ASSERT_NOT_REACHED(); |
| 208 |
} |
| 209 |
if (!handler) { |
| 210 |
ASSERT_NOT_REACHED(); |
| 211 |
return; |
| 212 |
} |
| 213 |
TraceEventArguments arguments(numArgs, argNames, argTypes, argValues); |
| 214 |
(this->*handler)(name, phase, arguments); |
| 215 |
} |
| 216 |
|
| 217 |
} // namespace WebCore |
| 218 |
|
| 219 |
#endif // ENABLE(INSPECTOR) |