1 /* 2 * Copyright (C) 2009 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 #include "core/inspector/TimelineRecordFactory.h" 33 34 #include "bindings/v8/ScriptCallStackFactory.h" 35 #include "core/events/Event.h" 36 #include "core/inspector/ScriptCallStack.h" 37 #include "platform/geometry/FloatQuad.h" 38 #include "platform/geometry/LayoutRect.h" 39 #include "platform/network/ResourceRequest.h" 40 #include "platform/network/ResourceResponse.h" 41 #include "wtf/CurrentTime.h" 42 43 namespace WebCore { 44 45 PassRefPtr<JSONObject> TimelineRecordFactory::createGenericRecord(double startTime, int maxCallStackDepth, const String& type) 46 { 47 RefPtr<JSONObject> record = JSONObject::create(); 48 record->setNumber("startTime", startTime); 49 50 if (maxCallStackDepth) { 51 RefPtr<ScriptCallStack> stackTrace = createScriptCallStack(maxCallStackDepth, true); 52 if (stackTrace && stackTrace->size()) 53 record->setValue("stackTrace", stackTrace->buildInspectorArray()); 54 } 55 record->setString("type", type); 56 return record.release(); 57 } 58 59 PassRefPtr<JSONObject> TimelineRecordFactory::createBackgroundRecord(double startTime, const String& threadName, const String& type, PassRefPtr<JSONObject> data) 60 { 61 RefPtr<JSONObject> record = JSONObject::create(); 62 record->setNumber("startTime", startTime); 63 record->setString("thread", threadName); 64 record->setString("type", type); 65 record->setObject("data", data ? data : JSONObject::create()); 66 return record.release(); 67 } 68 69 PassRefPtr<JSONObject> TimelineRecordFactory::createGCEventData(size_t usedHeapSizeDelta) 70 { 71 RefPtr<JSONObject> data = JSONObject::create(); 72 data->setNumber("usedHeapSizeDelta", usedHeapSizeDelta); 73 return data.release(); 74 } 75 76 PassRefPtr<JSONObject> TimelineRecordFactory::createFunctionCallData(const String& scriptName, int scriptLine) 77 { 78 RefPtr<JSONObject> data = JSONObject::create(); 79 data->setString("scriptName", scriptName); 80 data->setNumber("scriptLine", scriptLine); 81 return data.release(); 82 } 83 84 PassRefPtr<JSONObject> TimelineRecordFactory::createEventDispatchData(const Event& event) 85 { 86 RefPtr<JSONObject> data = JSONObject::create(); 87 data->setString("type", event.type().string()); 88 return data.release(); 89 } 90 91 PassRefPtr<JSONObject> TimelineRecordFactory::createGenericTimerData(int timerId) 92 { 93 RefPtr<JSONObject> data = JSONObject::create(); 94 data->setNumber("timerId", timerId); 95 return data.release(); 96 } 97 98 PassRefPtr<JSONObject> TimelineRecordFactory::createTimerInstallData(int timerId, int timeout, bool singleShot) 99 { 100 RefPtr<JSONObject> data = JSONObject::create(); 101 data->setNumber("timerId", timerId); 102 data->setNumber("timeout", timeout); 103 data->setBoolean("singleShot", singleShot); 104 return data.release(); 105 } 106 107 PassRefPtr<JSONObject> TimelineRecordFactory::createXHRReadyStateChangeData(const String& url, int readyState) 108 { 109 RefPtr<JSONObject> data = JSONObject::create(); 110 data->setString("url", url); 111 data->setNumber("readyState", readyState); 112 return data.release(); 113 } 114 115 PassRefPtr<JSONObject> TimelineRecordFactory::createXHRLoadData(const String& url) 116 { 117 RefPtr<JSONObject> data = JSONObject::create(); 118 data->setString("url", url); 119 return data.release(); 120 } 121 122 PassRefPtr<JSONObject> TimelineRecordFactory::createEvaluateScriptData(const String& url, double lineNumber) 123 { 124 RefPtr<JSONObject> data = JSONObject::create(); 125 data->setString("url", url); 126 data->setNumber("lineNumber", lineNumber); 127 return data.release(); 128 } 129 130 PassRefPtr<JSONObject> TimelineRecordFactory::createTimeStampData(const String& message) 131 { 132 RefPtr<JSONObject> data = JSONObject::create(); 133 data->setString("message", message); 134 return data.release(); 135 } 136 137 PassRefPtr<JSONObject> TimelineRecordFactory::createScheduleResourceRequestData(const String& url) 138 { 139 RefPtr<JSONObject> data = JSONObject::create(); 140 data->setString("url", url); 141 return data.release(); 142 } 143 144 PassRefPtr<JSONObject> TimelineRecordFactory::createResourceSendRequestData(const String& requestId, const ResourceRequest& request) 145 { 146 RefPtr<JSONObject> data = JSONObject::create(); 147 data->setString("requestId", requestId); 148 data->setString("url", request.url().string()); 149 data->setString("requestMethod", request.httpMethod()); 150 return data.release(); 151 } 152 153 PassRefPtr<JSONObject> TimelineRecordFactory::createResourceReceiveResponseData(const String& requestId, const ResourceResponse& response) 154 { 155 RefPtr<JSONObject> data = JSONObject::create(); 156 data->setString("requestId", requestId); 157 data->setNumber("statusCode", response.httpStatusCode()); 158 data->setString("mimeType", response.mimeType()); 159 return data.release(); 160 } 161 162 PassRefPtr<JSONObject> TimelineRecordFactory::createResourceFinishData(const String& requestId, bool didFail, double finishTime) 163 { 164 RefPtr<JSONObject> data = JSONObject::create(); 165 data->setString("requestId", requestId); 166 data->setBoolean("didFail", didFail); 167 if (finishTime) 168 data->setNumber("networkTime", finishTime); 169 return data.release(); 170 } 171 172 PassRefPtr<JSONObject> TimelineRecordFactory::createReceiveResourceData(const String& requestId, int length) 173 { 174 RefPtr<JSONObject> data = JSONObject::create(); 175 data->setString("requestId", requestId); 176 data->setNumber("encodedDataLength", length); 177 return data.release(); 178 } 179 180 PassRefPtr<JSONObject> TimelineRecordFactory::createLayoutData(unsigned dirtyObjects, unsigned totalObjects, bool partialLayout) 181 { 182 RefPtr<JSONObject> data = JSONObject::create(); 183 data->setNumber("dirtyObjects", dirtyObjects); 184 data->setNumber("totalObjects", totalObjects); 185 data->setBoolean("partialLayout", partialLayout); 186 return data.release(); 187 } 188 189 PassRefPtr<JSONObject> TimelineRecordFactory::createDecodeImageData(const String& imageType) 190 { 191 RefPtr<JSONObject> data = JSONObject::create(); 192 data->setString("imageType", imageType); 193 return data.release(); 194 } 195 196 PassRefPtr<JSONObject> TimelineRecordFactory::createResizeImageData(bool shouldCache) 197 { 198 RefPtr<JSONObject> data = JSONObject::create(); 199 data->setBoolean("cached", shouldCache); 200 return data.release(); 201 } 202 203 PassRefPtr<JSONObject> TimelineRecordFactory::createMarkData(bool isMainFrame) 204 { 205 RefPtr<JSONObject> data = JSONObject::create(); 206 data->setBoolean("isMainFrame", isMainFrame); 207 return data.release(); 208 } 209 210 PassRefPtr<JSONObject> TimelineRecordFactory::createParseHTMLData(unsigned startLine) 211 { 212 RefPtr<JSONObject> data = JSONObject::create(); 213 data->setNumber("startLine", startLine); 214 return data.release(); 215 } 216 217 PassRefPtr<JSONObject> TimelineRecordFactory::createAnimationFrameData(int callbackId) 218 { 219 RefPtr<JSONObject> data = JSONObject::create(); 220 data->setNumber("id", callbackId); 221 return data.release(); 222 } 223 224 PassRefPtr<JSONObject> TimelineRecordFactory::createGPUTaskData(bool foreign, size_t usedGPUMemoryBytes) 225 { 226 RefPtr<JSONObject> data = JSONObject::create(); 227 data->setBoolean("foreign", foreign); 228 if (!foreign) 229 data->setNumber("usedGPUMemoryBytes", usedGPUMemoryBytes); 230 return data.release(); 231 } 232 233 static PassRefPtr<JSONArray> createQuad(const FloatQuad& quad) 234 { 235 RefPtr<JSONArray> array = JSONArray::create(); 236 array->pushNumber(quad.p1().x()); 237 array->pushNumber(quad.p1().y()); 238 array->pushNumber(quad.p2().x()); 239 array->pushNumber(quad.p2().y()); 240 array->pushNumber(quad.p3().x()); 241 array->pushNumber(quad.p3().y()); 242 array->pushNumber(quad.p4().x()); 243 array->pushNumber(quad.p4().y()); 244 return array.release(); 245 } 246 247 PassRefPtr<JSONObject> TimelineRecordFactory::createNodeData(long long nodeId) 248 { 249 RefPtr<JSONObject> data = JSONObject::create(); 250 if (nodeId) 251 data->setNumber("rootNode", nodeId); 252 return data.release(); 253 } 254 255 PassRefPtr<JSONObject> TimelineRecordFactory::createLayerData(long long rootNodeId) 256 { 257 return createNodeData(rootNodeId); 258 } 259 260 PassRefPtr<JSONObject> TimelineRecordFactory::createPaintData(const FloatQuad& quad, long long layerRootNodeId, int graphicsLayerId) 261 { 262 RefPtr<JSONObject> data = TimelineRecordFactory::createLayerData(layerRootNodeId); 263 data->setArray("clip", createQuad(quad)); 264 data->setNumber("layerId", graphicsLayerId); 265 return data.release(); 266 } 267 268 PassRefPtr<JSONObject> TimelineRecordFactory::createFrameData(int frameId) 269 { 270 RefPtr<JSONObject> data = JSONObject::create(); 271 data->setNumber("id", frameId); 272 return data.release(); 273 } 274 275 void TimelineRecordFactory::appendLayoutRoot(JSONObject* data, const FloatQuad& quad, long long rootNodeId) 276 { 277 data->setArray("root", createQuad(quad)); 278 if (rootNodeId) 279 data->setNumber("rootNode", rootNodeId); 280 } 281 282 void TimelineRecordFactory::appendStyleRecalcDetails(JSONObject* data, unsigned elementCount) 283 { 284 data->setNumber("elementCount", elementCount); 285 } 286 287 void TimelineRecordFactory::appendImageDetails(JSONObject* data, long long imageElementId, const String& url) 288 { 289 if (imageElementId) 290 data->setNumber("elementId", imageElementId); 291 if (!url.isEmpty()) 292 data->setString("url", url); 293 } 294 295 } // namespace WebCore 296 297