1 /* 2 * Copyright (C) 2011 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/InspectorInstrumentation.h" 33 34 #include "core/inspector/InspectorAgent.h" 35 #include "core/inspector/InspectorCSSAgent.h" 36 #include "core/inspector/InspectorConsoleAgent.h" 37 #include "core/inspector/InspectorController.h" 38 #include "core/inspector/InspectorDebuggerAgent.h" 39 #include "core/inspector/InspectorProfilerAgent.h" 40 #include "core/inspector/InspectorResourceAgent.h" 41 #include "core/inspector/InspectorTimelineAgent.h" 42 #include "core/inspector/InstrumentingAgents.h" 43 #include "core/inspector/WorkerInspectorController.h" 44 #include "core/loader/cache/FetchInitiatorInfo.h" 45 #include "core/workers/WorkerGlobalScope.h" 46 47 namespace WebCore { 48 49 namespace { 50 static HashSet<InstrumentingAgents*>* instrumentingAgentsSet = 0; 51 } 52 53 namespace InspectorInstrumentation { 54 int FrontendCounter::s_frontendCounter = 0; 55 } 56 57 InspectorInstrumentationCookie::InspectorInstrumentationCookie() 58 : m_instrumentingAgents(0) 59 , m_timelineAgentId(0) 60 { 61 } 62 63 InspectorInstrumentationCookie::InspectorInstrumentationCookie(InstrumentingAgents* agents, int timelineAgentId) 64 : m_instrumentingAgents(agents) 65 , m_timelineAgentId(timelineAgentId) 66 { 67 } 68 69 InspectorInstrumentationCookie::InspectorInstrumentationCookie(const InspectorInstrumentationCookie& other) 70 : m_instrumentingAgents(other.m_instrumentingAgents) 71 , m_timelineAgentId(other.m_timelineAgentId) 72 { 73 } 74 75 InspectorInstrumentationCookie& InspectorInstrumentationCookie::operator=(const InspectorInstrumentationCookie& other) 76 { 77 if (this != &other) { 78 m_instrumentingAgents = other.m_instrumentingAgents; 79 m_timelineAgentId = other.m_timelineAgentId; 80 } 81 return *this; 82 } 83 84 InspectorInstrumentationCookie::~InspectorInstrumentationCookie() 85 { 86 } 87 88 namespace InspectorInstrumentation { 89 90 bool isDebuggerPausedImpl(InstrumentingAgents* instrumentingAgents) 91 { 92 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent()) 93 return debuggerAgent->isPaused(); 94 return false; 95 } 96 97 void continueAfterPingLoaderImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, ResourceRequest& request, const ResourceResponse& response) 98 { 99 willSendRequestImpl(instrumentingAgents, identifier, loader, request, response, FetchInitiatorInfo()); 100 } 101 102 void didReceiveResourceResponseButCanceledImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r) 103 { 104 InspectorInstrumentationCookie cookie = willReceiveResourceResponse(frame, identifier, r); 105 didReceiveResourceResponse(cookie, identifier, loader, r, 0); 106 } 107 108 void continueAfterXFrameOptionsDeniedImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r) 109 { 110 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r); 111 } 112 113 void continueWithPolicyDownloadImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r) 114 { 115 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r); 116 } 117 118 void continueWithPolicyIgnoreImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r) 119 { 120 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r); 121 } 122 123 void willDestroyResourceImpl(Resource* cachedResource) 124 { 125 if (!instrumentingAgentsSet) 126 return; 127 HashSet<InstrumentingAgents*>::iterator end = instrumentingAgentsSet->end(); 128 for (HashSet<InstrumentingAgents*>::iterator it = instrumentingAgentsSet->begin(); it != end; ++it) { 129 InstrumentingAgents* instrumentingAgents = *it; 130 if (InspectorResourceAgent* inspectorResourceAgent = instrumentingAgents->inspectorResourceAgent()) 131 inspectorResourceAgent->willDestroyResource(cachedResource); 132 } 133 } 134 135 bool profilerEnabledImpl(InstrumentingAgents* instrumentingAgents) 136 { 137 if (InspectorProfilerAgent* profilerAgent = instrumentingAgents->inspectorProfilerAgent()) 138 return profilerAgent->enabled(); 139 return false; 140 } 141 142 bool collectingHTMLParseErrorsImpl(InstrumentingAgents* instrumentingAgents) 143 { 144 if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent()) 145 return inspectorAgent->hasFrontend(); 146 return false; 147 } 148 149 bool canvasAgentEnabled(ScriptExecutionContext* scriptExecutionContext) 150 { 151 InstrumentingAgents* instrumentingAgents = instrumentingAgentsForScriptExecutionContext(scriptExecutionContext); 152 return instrumentingAgents && instrumentingAgents->inspectorCanvasAgent(); 153 } 154 155 bool consoleAgentEnabled(ScriptExecutionContext* scriptExecutionContext) 156 { 157 InstrumentingAgents* instrumentingAgents = instrumentingAgentsForScriptExecutionContext(scriptExecutionContext); 158 InspectorConsoleAgent* consoleAgent = instrumentingAgents ? instrumentingAgents->inspectorConsoleAgent() : 0; 159 return consoleAgent && consoleAgent->enabled(); 160 } 161 162 bool timelineAgentEnabled(ScriptExecutionContext* scriptExecutionContext) 163 { 164 InstrumentingAgents* instrumentingAgents = instrumentingAgentsForScriptExecutionContext(scriptExecutionContext); 165 return instrumentingAgents && instrumentingAgents->inspectorTimelineAgent(); 166 } 167 168 void registerInstrumentingAgents(InstrumentingAgents* instrumentingAgents) 169 { 170 if (!instrumentingAgentsSet) 171 instrumentingAgentsSet = new HashSet<InstrumentingAgents*>(); 172 instrumentingAgentsSet->add(instrumentingAgents); 173 } 174 175 void unregisterInstrumentingAgents(InstrumentingAgents* instrumentingAgents) 176 { 177 if (!instrumentingAgentsSet) 178 return; 179 instrumentingAgentsSet->remove(instrumentingAgents); 180 if (instrumentingAgentsSet->isEmpty()) { 181 delete instrumentingAgentsSet; 182 instrumentingAgentsSet = 0; 183 } 184 } 185 186 InspectorTimelineAgent* retrieveTimelineAgent(const InspectorInstrumentationCookie& cookie) 187 { 188 if (!cookie.instrumentingAgents()) 189 return 0; 190 InspectorTimelineAgent* timelineAgent = cookie.instrumentingAgents()->inspectorTimelineAgent(); 191 if (timelineAgent && cookie.hasMatchingTimelineAgentId(timelineAgent->id())) 192 return timelineAgent; 193 return 0; 194 } 195 196 InstrumentingAgents* instrumentingAgentsForPage(Page* page) 197 { 198 if (!page) 199 return 0; 200 return instrumentationForPage(page); 201 } 202 203 InstrumentingAgents* instrumentingAgentsForRenderObject(RenderObject* renderer) 204 { 205 return instrumentingAgentsForFrame(renderer->frame()); 206 } 207 208 InstrumentingAgents* instrumentingAgentsForWorkerGlobalScope(WorkerGlobalScope* workerGlobalScope) 209 { 210 if (!workerGlobalScope) 211 return 0; 212 return instrumentationForWorkerGlobalScope(workerGlobalScope); 213 } 214 215 InstrumentingAgents* instrumentingAgentsForNonDocumentContext(ScriptExecutionContext* context) 216 { 217 if (context->isWorkerGlobalScope()) 218 return instrumentationForWorkerGlobalScope(toWorkerGlobalScope(context)); 219 return 0; 220 } 221 222 bool cssErrorFilter(const CSSParserString& content, int propertyId, int errorType) 223 { 224 return InspectorCSSAgent::cssErrorFilter(content, propertyId, errorType); 225 } 226 227 } // namespace InspectorInstrumentation 228 229 namespace InstrumentationEvents { 230 const char PaintSetup[] = "PaintSetup"; 231 const char PaintLayer[] = "PaintLayer"; 232 const char RasterTask[] = "RasterTask"; 233 const char ImageDecodeTask[] = "ImageDecodeTask"; 234 const char Paint[] = "Paint"; 235 const char Layer[] = "Layer"; 236 const char BeginFrame[] = "BeginFrame"; 237 const char UpdateLayer[] = "UpdateLayer"; 238 }; 239 240 namespace InstrumentationEventArguments { 241 const char LayerId[] = "layerId"; 242 const char LayerTreeId[] = "layerTreeId"; 243 const char NodeId[] = "nodeId"; 244 const char PageId[] = "pageId"; 245 }; 246 247 InstrumentingAgents* instrumentationForPage(Page* page) 248 { 249 ASSERT(isMainThread()); 250 if (InspectorController* controller = page->inspectorController()) 251 return controller->m_instrumentingAgents.get(); 252 return 0; 253 } 254 255 InstrumentingAgents* instrumentationForWorkerGlobalScope(WorkerGlobalScope* workerGlobalScope) 256 { 257 if (WorkerInspectorController* controller = workerGlobalScope->workerInspectorController()) 258 return controller->m_instrumentingAgents.get(); 259 return 0; 260 } 261 262 } // namespace WebCore 263 264