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 33 #include "InspectorPageAgent.h" 34 35 #if ENABLE(INSPECTOR) 36 37 #include "CachedResourceLoader.h" 38 #include "Cookie.h" 39 #include "CookieJar.h" 40 #include "DocumentLoader.h" 41 #include "Frame.h" 42 #include "FrameLoadRequest.h" 43 #include "InjectedScriptManager.h" 44 #include "InspectorFrontend.h" 45 #include "InspectorValues.h" 46 #include "InstrumentingAgents.h" 47 #include "Page.h" 48 #include "ScriptObject.h" 49 #include "UserGestureIndicator.h" 50 #include "WindowFeatures.h" 51 #include <wtf/CurrentTime.h> 52 #include <wtf/ListHashSet.h> 53 54 namespace WebCore { 55 56 PassOwnPtr<InspectorPageAgent> InspectorPageAgent::create(InstrumentingAgents* instrumentingAgents, Page* inspectedPage, InjectedScriptManager* injectedScriptManager) 57 { 58 return adoptPtr(new InspectorPageAgent(instrumentingAgents, inspectedPage, injectedScriptManager)); 59 } 60 61 InspectorPageAgent::InspectorPageAgent(InstrumentingAgents* instrumentingAgents, Page* inspectedPage, InjectedScriptManager* injectedScriptManager) 62 : m_instrumentingAgents(instrumentingAgents) 63 , m_inspectedPage(inspectedPage) 64 , m_injectedScriptManager(injectedScriptManager) 65 , m_frontend(0) 66 { 67 } 68 69 void InspectorPageAgent::setFrontend(InspectorFrontend* frontend) 70 { 71 m_frontend = frontend; 72 m_instrumentingAgents->setInspectorPageAgent(this); 73 74 // Initialize Web Inspector title. 75 m_frontend->page()->inspectedURLChanged(m_inspectedPage->mainFrame()->document()->url().string()); 76 77 } 78 79 void InspectorPageAgent::clearFrontend() 80 { 81 m_instrumentingAgents->setInspectorPageAgent(0); 82 m_userAgentOverride = ""; 83 m_frontend = 0; 84 } 85 86 void InspectorPageAgent::addScriptToEvaluateOnLoad(ErrorString*, const String& source) 87 { 88 m_scriptsToEvaluateOnLoad.append(source); 89 } 90 91 void InspectorPageAgent::removeAllScriptsToEvaluateOnLoad(ErrorString*) 92 { 93 m_scriptsToEvaluateOnLoad.clear(); 94 } 95 96 void InspectorPageAgent::reloadPage(ErrorString*, const bool* const optionalIgnoreCache) 97 { 98 m_inspectedPage->mainFrame()->loader()->reload(optionalIgnoreCache ? *optionalIgnoreCache : false); 99 } 100 101 void InspectorPageAgent::openInInspectedWindow(ErrorString*, const String& url) 102 { 103 Frame* mainFrame = m_inspectedPage->mainFrame(); 104 105 FrameLoadRequest request(mainFrame->document()->securityOrigin(), ResourceRequest(), "_blank"); 106 107 bool created; 108 WindowFeatures windowFeatures; 109 Frame* newFrame = WebCore::createWindow(mainFrame, mainFrame, request, windowFeatures, created); 110 if (!newFrame) 111 return; 112 113 UserGestureIndicator indicator(DefinitelyProcessingUserGesture); 114 newFrame->loader()->setOpener(mainFrame); 115 newFrame->page()->setOpenedByDOM(); 116 newFrame->loader()->changeLocation(mainFrame->document()->securityOrigin(), newFrame->loader()->completeURL(url), "", false, false); 117 } 118 119 void InspectorPageAgent::setUserAgentOverride(ErrorString*, const String& userAgent) 120 { 121 m_userAgentOverride = userAgent; 122 } 123 124 void InspectorPageAgent::applyUserAgentOverride(String* userAgent) const 125 { 126 if (!m_userAgentOverride.isEmpty()) 127 *userAgent = m_userAgentOverride; 128 } 129 130 static PassRefPtr<InspectorObject> buildObjectForCookie(const Cookie& cookie) 131 { 132 RefPtr<InspectorObject> value = InspectorObject::create(); 133 value->setString("name", cookie.name); 134 value->setString("value", cookie.value); 135 value->setString("domain", cookie.domain); 136 value->setString("path", cookie.path); 137 value->setNumber("expires", cookie.expires); 138 value->setNumber("size", (cookie.name.length() + cookie.value.length())); 139 value->setBoolean("httpOnly", cookie.httpOnly); 140 value->setBoolean("secure", cookie.secure); 141 value->setBoolean("session", cookie.session); 142 return value; 143 } 144 145 static PassRefPtr<InspectorArray> buildArrayForCookies(ListHashSet<Cookie>& cookiesList) 146 { 147 RefPtr<InspectorArray> cookies = InspectorArray::create(); 148 149 ListHashSet<Cookie>::iterator end = cookiesList.end(); 150 ListHashSet<Cookie>::iterator it = cookiesList.begin(); 151 for (int i = 0; it != end; ++it, i++) 152 cookies->pushObject(buildObjectForCookie(*it)); 153 154 return cookies; 155 } 156 157 void InspectorPageAgent::getCookies(ErrorString*, RefPtr<InspectorArray>* cookies, WTF::String* cookiesString) 158 { 159 // If we can get raw cookies. 160 ListHashSet<Cookie> rawCookiesList; 161 162 // If we can't get raw cookies - fall back to String representation 163 String stringCookiesList; 164 165 // Return value to getRawCookies should be the same for every call because 166 // the return value is platform/network backend specific, and the call will 167 // always return the same true/false value. 168 bool rawCookiesImplemented = false; 169 170 for (Frame* frame = m_inspectedPage->mainFrame(); frame; frame = frame->tree()->traverseNext(m_inspectedPage->mainFrame())) { 171 Document* document = frame->document(); 172 const CachedResourceLoader::DocumentResourceMap& allResources = document->cachedResourceLoader()->allCachedResources(); 173 CachedResourceLoader::DocumentResourceMap::const_iterator end = allResources.end(); 174 for (CachedResourceLoader::DocumentResourceMap::const_iterator it = allResources.begin(); it != end; ++it) { 175 Vector<Cookie> docCookiesList; 176 rawCookiesImplemented = getRawCookies(document, KURL(ParsedURLString, it->second->url()), docCookiesList); 177 178 if (!rawCookiesImplemented) { 179 // FIXME: We need duplication checking for the String representation of cookies. 180 ExceptionCode ec = 0; 181 stringCookiesList += document->cookie(ec); 182 // Exceptions are thrown by cookie() in sandboxed frames. That won't happen here 183 // because "document" is the document of the main frame of the page. 184 ASSERT(!ec); 185 } else { 186 int cookiesSize = docCookiesList.size(); 187 for (int i = 0; i < cookiesSize; i++) { 188 if (!rawCookiesList.contains(docCookiesList[i])) 189 rawCookiesList.add(docCookiesList[i]); 190 } 191 } 192 } 193 } 194 195 if (rawCookiesImplemented) 196 *cookies = buildArrayForCookies(rawCookiesList); 197 else 198 *cookiesString = stringCookiesList; 199 } 200 201 void InspectorPageAgent::deleteCookie(ErrorString*, const String& cookieName, const String& domain) 202 { 203 for (Frame* frame = m_inspectedPage->mainFrame(); frame; frame = frame->tree()->traverseNext(m_inspectedPage->mainFrame())) { 204 Document* document = frame->document(); 205 if (document->url().host() != domain) 206 continue; 207 const CachedResourceLoader::DocumentResourceMap& allResources = document->cachedResourceLoader()->allCachedResources(); 208 CachedResourceLoader::DocumentResourceMap::const_iterator end = allResources.end(); 209 for (CachedResourceLoader::DocumentResourceMap::const_iterator it = allResources.begin(); it != end; ++it) 210 WebCore::deleteCookie(document, KURL(ParsedURLString, it->second->url()), cookieName); 211 } 212 } 213 214 void InspectorPageAgent::inspectedURLChanged(const String& url) 215 { 216 m_frontend->page()->inspectedURLChanged(url); 217 } 218 219 void InspectorPageAgent::restore() 220 { 221 inspectedURLChanged(m_inspectedPage->mainFrame()->document()->url().string()); 222 } 223 224 void InspectorPageAgent::didCommitLoad(const String& url) 225 { 226 inspectedURLChanged(url); 227 } 228 229 void InspectorPageAgent::domContentEventFired() 230 { 231 m_frontend->page()->domContentEventFired(currentTime()); 232 } 233 234 void InspectorPageAgent::loadEventFired() 235 { 236 m_frontend->page()->loadEventFired(currentTime()); 237 } 238 239 void InspectorPageAgent::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld* world) 240 { 241 if (world != mainThreadNormalWorld()) 242 return; 243 244 if (frame == m_inspectedPage->mainFrame()) 245 m_injectedScriptManager->discardInjectedScripts(); 246 247 if (m_scriptsToEvaluateOnLoad.size()) { 248 ScriptState* scriptState = mainWorldScriptState(frame); 249 for (Vector<String>::iterator it = m_scriptsToEvaluateOnLoad.begin(); 250 it != m_scriptsToEvaluateOnLoad.end(); ++it) { 251 m_injectedScriptManager->injectScript(*it, scriptState); 252 } 253 } 254 } 255 256 } // namespace WebCore 257 258 #endif // ENABLE(INSPECTOR) 259