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/fetch/FetchInitiatorInfo.h" 35 #include "core/inspector/InspectorAgent.h" 36 #include "core/inspector/InspectorCSSAgent.h" 37 #include "core/inspector/InspectorConsoleAgent.h" 38 #include "core/inspector/InspectorController.h" 39 #include "core/inspector/InspectorDebuggerAgent.h" 40 #include "core/inspector/InspectorProfilerAgent.h" 41 #include "core/inspector/InspectorResourceAgent.h" 42 #include "core/inspector/InspectorTimelineAgent.h" 43 #include "core/inspector/InstrumentingAgents.h" 44 #include "core/inspector/WorkerInspectorController.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 didReceiveResourceResponse(frame, identifier, loader, r, 0); 105 } 106 107 void continueAfterXFrameOptionsDeniedImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r) 108 { 109 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r); 110 } 111 112 void continueWithPolicyDownloadImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r) 113 { 114 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r); 115 } 116 117 void continueWithPolicyIgnoreImpl(Frame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r) 118 { 119 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r); 120 } 121 122 void willDestroyResourceImpl(Resource* cachedResource) 123 { 124 if (!instrumentingAgentsSet) 125 return; 126 HashSet<InstrumentingAgents*>::iterator end = instrumentingAgentsSet->end(); 127 for (HashSet<InstrumentingAgents*>::iterator it = instrumentingAgentsSet->begin(); it != end; ++it) { 128 InstrumentingAgents* instrumentingAgents = *it; 129 if (InspectorResourceAgent* inspectorResourceAgent = instrumentingAgents->inspectorResourceAgent()) 130 inspectorResourceAgent->willDestroyResource(cachedResource); 131 } 132 } 133 134 bool collectingHTMLParseErrorsImpl(InstrumentingAgents* instrumentingAgents) 135 { 136 if (InspectorAgent* inspectorAgent = instrumentingAgents->inspectorAgent()) 137 return inspectorAgent->hasFrontend(); 138 return false; 139 } 140 141 PassOwnPtr<ScriptSourceCode> preprocessImpl(InstrumentingAgents* instrumentingAgents, Frame* frame, const ScriptSourceCode& sourceCode) 142 { 143 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent()) 144 return debuggerAgent->preprocess(frame, sourceCode); 145 return PassOwnPtr<ScriptSourceCode>(); 146 } 147 148 String preprocessEventListenerImpl(InstrumentingAgents* instrumentingAgents, Frame* frame, const String& source, const String& url, const String& functionName) 149 { 150 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent()) 151 return debuggerAgent->preprocessEventListener(frame, source, url, functionName); 152 return source; 153 } 154 155 bool canvasAgentEnabled(ExecutionContext* executionContext) 156 { 157 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext); 158 return instrumentingAgents && instrumentingAgents->inspectorCanvasAgent(); 159 } 160 161 bool consoleAgentEnabled(ExecutionContext* executionContext) 162 { 163 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext); 164 InspectorConsoleAgent* consoleAgent = instrumentingAgents ? instrumentingAgents->inspectorConsoleAgent() : 0; 165 return consoleAgent && consoleAgent->enabled(); 166 } 167 168 bool timelineAgentEnabled(ExecutionContext* executionContext) 169 { 170 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext); 171 return instrumentingAgents && instrumentingAgents->inspectorTimelineAgent(); 172 } 173 174 void registerInstrumentingAgents(InstrumentingAgents* instrumentingAgents) 175 { 176 if (!instrumentingAgentsSet) 177 instrumentingAgentsSet = new HashSet<InstrumentingAgents*>(); 178 instrumentingAgentsSet->add(instrumentingAgents); 179 } 180 181 void unregisterInstrumentingAgents(InstrumentingAgents* instrumentingAgents) 182 { 183 if (!instrumentingAgentsSet) 184 return; 185 instrumentingAgentsSet->remove(instrumentingAgents); 186 if (instrumentingAgentsSet->isEmpty()) { 187 delete instrumentingAgentsSet; 188 instrumentingAgentsSet = 0; 189 } 190 } 191 192 InspectorTimelineAgent* retrieveTimelineAgent(const InspectorInstrumentationCookie& cookie) 193 { 194 if (!cookie.instrumentingAgents()) 195 return 0; 196 InspectorTimelineAgent* timelineAgent = cookie.instrumentingAgents()->inspectorTimelineAgent(); 197 if (timelineAgent && cookie.hasMatchingTimelineAgentId(timelineAgent->id())) 198 return timelineAgent; 199 return 0; 200 } 201 202 InstrumentingAgents* instrumentingAgentsFor(Page* page) 203 { 204 if (!page) 205 return 0; 206 return instrumentationForPage(page); 207 } 208 209 InstrumentingAgents* instrumentingAgentsFor(RenderObject* renderer) 210 { 211 return instrumentingAgentsFor(renderer->frame()); 212 } 213 214 InstrumentingAgents* instrumentingAgentsFor(WorkerGlobalScope* workerGlobalScope) 215 { 216 if (!workerGlobalScope) 217 return 0; 218 return instrumentationForWorkerGlobalScope(workerGlobalScope); 219 } 220 221 InstrumentingAgents* instrumentingAgentsForNonDocumentContext(ExecutionContext* context) 222 { 223 if (context->isWorkerGlobalScope()) 224 return instrumentationForWorkerGlobalScope(toWorkerGlobalScope(context)); 225 return 0; 226 } 227 228 } // namespace InspectorInstrumentation 229 230 namespace InstrumentationEvents { 231 const char PaintSetup[] = "PaintSetup"; 232 const char RasterTask[] = "RasterTask"; 233 const char Paint[] = "Paint"; 234 const char Layer[] = "Layer"; 235 const char BeginFrame[] = "BeginFrame"; 236 const char ActivateLayerTree[] = "ActivateLayerTree"; 237 }; 238 239 namespace InstrumentationEventArguments { 240 const char FrameId[] = "frameId"; 241 const char LayerId[] = "layerId"; 242 const char LayerTreeId[] = "layerTreeId"; 243 const char PageId[] = "pageId"; 244 }; 245 246 InstrumentingAgents* instrumentationForPage(Page* page) 247 { 248 ASSERT(isMainThread()); 249 return page->inspectorController().m_instrumentingAgents.get(); 250 } 251 252 InstrumentingAgents* instrumentationForWorkerGlobalScope(WorkerGlobalScope* workerGlobalScope) 253 { 254 if (WorkerInspectorController* controller = workerGlobalScope->workerInspectorController()) 255 return controller->m_instrumentingAgents.get(); 256 return 0; 257 } 258 259 } // namespace WebCore 260 261