Subclass CachedFont for SVG fonts
[WebKit-https.git] / Source / WebCore / inspector / InspectorPageAgent.cpp
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 "InspectorPageAgent.h"
33
34 #if ENABLE(INSPECTOR)
35
36 #include "CachedCSSStyleSheet.h"
37 #include "CachedFont.h"
38 #include "CachedImage.h"
39 #include "CachedResource.h"
40 #include "CachedResourceLoader.h"
41 #include "CachedScript.h"
42 #include "Cookie.h"
43 #include "CookieJar.h"
44 #include "DOMImplementation.h"
45 #include "DOMPatchSupport.h"
46 #include "DOMWrapperWorld.h"
47 #include "Document.h"
48 #include "DocumentLoader.h"
49 #include "Frame.h"
50 #include "FrameLoader.h"
51 #include "FrameSnapshotting.h"
52 #include "FrameView.h"
53 #include "HTMLFrameOwnerElement.h"
54 #include "HTMLNames.h"
55 #include "ImageBuffer.h"
56 #include "InspectorClient.h"
57 #include "InspectorDOMAgent.h"
58 #include "InspectorInstrumentation.h"
59 #include "InspectorOverlay.h"
60 #include "InspectorTimelineAgent.h"
61 #include "InstrumentingAgents.h"
62 #include "MainFrame.h"
63 #include "MemoryCache.h"
64 #include "Page.h"
65 #include "ScriptController.h"
66 #include "SecurityOrigin.h"
67 #include "Settings.h"
68 #include "TextEncoding.h"
69 #include "TextResourceDecoder.h"
70 #include "UserGestureIndicator.h"
71 #include <bindings/ScriptValue.h>
72 #include <inspector/ContentSearchUtilities.h>
73 #include <inspector/IdentifiersFactory.h>
74 #include <inspector/InspectorValues.h>
75 #include <wtf/ListHashSet.h>
76 #include <wtf/text/Base64.h>
77 #include <wtf/text/StringBuilder.h>
78 #include <yarr/RegularExpression.h>
79
80 #if ENABLE(WEB_ARCHIVE) && USE(CF)
81 #include "LegacyWebArchive.h"
82 #endif
83
84 using namespace Inspector;
85
86 namespace WebCore {
87
88 static bool decodeBuffer(const char* buffer, unsigned size, const String& textEncodingName, String* result)
89 {
90     if (buffer) {
91         TextEncoding encoding(textEncodingName);
92         if (!encoding.isValid())
93             encoding = WindowsLatin1Encoding();
94         *result = encoding.decode(buffer, size);
95         return true;
96     }
97     return false;
98 }
99
100 static bool prepareCachedResourceBuffer(CachedResource* cachedResource, bool* hasZeroSize)
101 {
102     *hasZeroSize = false;
103     if (!cachedResource)
104         return false;
105
106     // Zero-sized resources don't have data at all -- so fake the empty buffer, instead of indicating error by returning 0.
107     if (!cachedResource->encodedSize()) {
108         *hasZeroSize = true;
109         return true;
110     }
111
112     return true;
113 }
114
115 static bool hasTextContent(CachedResource* cachedResource)
116 {
117     InspectorPageAgent::ResourceType type = InspectorPageAgent::cachedResourceType(*cachedResource);
118     return type == InspectorPageAgent::DocumentResource || type == InspectorPageAgent::StylesheetResource || type == InspectorPageAgent::ScriptResource || type == InspectorPageAgent::XHRResource;
119 }
120
121 static PassRefPtr<TextResourceDecoder> createXHRTextDecoder(const String& mimeType, const String& textEncodingName)
122 {
123     RefPtr<TextResourceDecoder> decoder;
124     if (!textEncodingName.isEmpty())
125         decoder = TextResourceDecoder::create("text/plain", textEncodingName);
126     else if (DOMImplementation::isXMLMIMEType(mimeType.lower())) {
127         decoder = TextResourceDecoder::create("application/xml");
128         decoder->useLenientXMLDecoding();
129     } else if (equalIgnoringCase(mimeType, "text/html"))
130         decoder = TextResourceDecoder::create("text/html", "UTF-8");
131     else
132         decoder = TextResourceDecoder::create("text/plain", "UTF-8");
133     return decoder;
134 }
135
136 bool InspectorPageAgent::cachedResourceContent(CachedResource* cachedResource, String* result, bool* base64Encoded)
137 {
138     bool hasZeroSize;
139     bool prepared = prepareCachedResourceBuffer(cachedResource, &hasZeroSize);
140     if (!prepared)
141         return false;
142
143     *base64Encoded = !hasTextContent(cachedResource);
144     if (*base64Encoded) {
145         RefPtr<SharedBuffer> buffer = hasZeroSize ? SharedBuffer::create() : cachedResource->resourceBuffer();
146         if (!buffer)
147             return false;
148         *result = base64Encode(buffer->data(), buffer->size());
149         return true;
150     }
151
152     if (hasZeroSize) {
153         *result = emptyString();
154         return true;
155     }
156
157     if (cachedResource) {
158         switch (cachedResource->type()) {
159         case CachedResource::CSSStyleSheet:
160             *result = downcast<CachedCSSStyleSheet>(*cachedResource).sheetText(false);
161             return true;
162         case CachedResource::Script:
163             *result = downcast<CachedScript>(*cachedResource).script();
164             return true;
165         case CachedResource::RawResource: {
166             auto* buffer = cachedResource->resourceBuffer();
167             if (!buffer)
168                 return false;
169             RefPtr<TextResourceDecoder> decoder = createXHRTextDecoder(cachedResource->response().mimeType(), cachedResource->response().textEncodingName());
170             // We show content for raw resources only for certain mime types (text, html and xml). Otherwise decoder will be null.
171             if (!decoder)
172                 return false;
173             *result = decoder->decodeAndFlush(buffer->data(), buffer->size());
174             return true;
175         }
176         default:
177             auto* buffer = cachedResource->resourceBuffer();
178             return decodeBuffer(buffer ? buffer->data() : nullptr, buffer ? buffer->size() : 0, cachedResource->encoding(), result);
179         }
180     }
181     return false;
182 }
183
184 bool InspectorPageAgent::mainResourceContent(Frame* frame, bool withBase64Encode, String* result)
185 {
186     RefPtr<SharedBuffer> buffer = frame->loader().documentLoader()->mainResourceData();
187     if (!buffer)
188         return false;
189     return InspectorPageAgent::dataContent(buffer->data(), buffer->size(), frame->document()->inputEncoding(), withBase64Encode, result);
190 }
191
192 // static
193 bool InspectorPageAgent::sharedBufferContent(PassRefPtr<SharedBuffer> buffer, const String& textEncodingName, bool withBase64Encode, String* result)
194 {
195     return dataContent(buffer ? buffer->data() : nullptr, buffer ? buffer->size() : 0, textEncodingName, withBase64Encode, result);
196 }
197
198 bool InspectorPageAgent::dataContent(const char* data, unsigned size, const String& textEncodingName, bool withBase64Encode, String* result)
199 {
200     if (withBase64Encode) {
201         *result = base64Encode(data, size);
202         return true;
203     }
204
205     return decodeBuffer(data, size, textEncodingName, result);
206 }
207
208 // static
209 void InspectorPageAgent::resourceContent(ErrorString& errorString, Frame* frame, const URL& url, String* result, bool* base64Encoded)
210 {
211     DocumentLoader* loader = assertDocumentLoader(errorString, frame);
212     if (!loader)
213         return;
214
215     RefPtr<SharedBuffer> buffer;
216     bool success = false;
217     if (equalIgnoringFragmentIdentifier(url, loader->url())) {
218         *base64Encoded = false;
219         success = mainResourceContent(frame, *base64Encoded, result);
220     }
221
222     if (!success)
223         success = cachedResourceContent(cachedResource(frame, url), result, base64Encoded);
224
225     if (!success)
226         errorString = ASCIILiteral("No resource with given URL found");
227 }
228
229 //static
230 String InspectorPageAgent::sourceMapURLForResource(CachedResource* cachedResource)
231 {
232     DEPRECATED_DEFINE_STATIC_LOCAL(String, sourceMapHTTPHeader, (ASCIILiteral("SourceMap")));
233     DEPRECATED_DEFINE_STATIC_LOCAL(String, sourceMapHTTPHeaderDeprecated, (ASCIILiteral("X-SourceMap")));
234
235     if (!cachedResource)
236         return String();
237
238     // Scripts are handled in a separate path.
239     if (cachedResource->type() != CachedResource::CSSStyleSheet)
240         return String();
241
242     String sourceMapHeader = cachedResource->response().httpHeaderField(sourceMapHTTPHeader);
243     if (!sourceMapHeader.isEmpty())
244         return sourceMapHeader;
245
246     sourceMapHeader = cachedResource->response().httpHeaderField(sourceMapHTTPHeaderDeprecated);
247     if (!sourceMapHeader.isEmpty())
248         return sourceMapHeader;
249
250     String content;
251     bool base64Encoded;
252     if (InspectorPageAgent::cachedResourceContent(cachedResource, &content, &base64Encoded) && !base64Encoded)
253         return ContentSearchUtilities::findStylesheetSourceMapURL(content);
254
255     return String();
256 }
257
258 CachedResource* InspectorPageAgent::cachedResource(Frame* frame, const URL& url)
259 {
260     CachedResource* cachedResource = frame->document()->cachedResourceLoader()->cachedResource(url);
261     if (!cachedResource) {
262         ResourceRequest request(url);
263 #if ENABLE(CACHE_PARTITIONING)
264         request.setDomainForCachePartition(frame->document()->topOrigin()->domainForCachePartition());
265 #endif
266         cachedResource = memoryCache()->resourceForRequest(request, frame->page()->sessionID());
267     }
268
269     return cachedResource;
270 }
271
272 Inspector::Protocol::Page::ResourceType InspectorPageAgent::resourceTypeJson(InspectorPageAgent::ResourceType resourceType)
273 {
274     switch (resourceType) {
275     case DocumentResource:
276         return Inspector::Protocol::Page::ResourceType::Document;
277     case ImageResource:
278         return Inspector::Protocol::Page::ResourceType::Image;
279     case FontResource:
280         return Inspector::Protocol::Page::ResourceType::Font;
281     case StylesheetResource:
282         return Inspector::Protocol::Page::ResourceType::Stylesheet;
283     case ScriptResource:
284         return Inspector::Protocol::Page::ResourceType::Script;
285     case XHRResource:
286         return Inspector::Protocol::Page::ResourceType::XHR;
287     case WebSocketResource:
288         return Inspector::Protocol::Page::ResourceType::WebSocket;
289     case OtherResource:
290         return Inspector::Protocol::Page::ResourceType::Other;
291     }
292     return Inspector::Protocol::Page::ResourceType::Other;
293 }
294
295 InspectorPageAgent::ResourceType InspectorPageAgent::cachedResourceType(const CachedResource& cachedResource)
296 {
297     switch (cachedResource.type()) {
298     case CachedResource::ImageResource:
299         return InspectorPageAgent::ImageResource;
300 #if ENABLE(SVG_FONTS)
301     case CachedResource::SVGFontResource:
302 #endif
303     case CachedResource::FontResource:
304         return InspectorPageAgent::FontResource;
305     case CachedResource::CSSStyleSheet:
306         // Fall through.
307 #if ENABLE(XSLT)
308     case CachedResource::XSLStyleSheet:
309 #endif
310         return InspectorPageAgent::StylesheetResource;
311     case CachedResource::Script:
312         return InspectorPageAgent::ScriptResource;
313     case CachedResource::RawResource:
314         return InspectorPageAgent::XHRResource;
315     case CachedResource::MainResource:
316         return InspectorPageAgent::DocumentResource;
317     default:
318         break;
319     }
320     return InspectorPageAgent::OtherResource;
321 }
322
323 Inspector::Protocol::Page::ResourceType InspectorPageAgent::cachedResourceTypeJson(const CachedResource& cachedResource)
324 {
325     return resourceTypeJson(cachedResourceType(cachedResource));
326 }
327
328 InspectorPageAgent::InspectorPageAgent(InstrumentingAgents* instrumentingAgents, Page* page, InspectorClient* client, InspectorOverlay* overlay)
329     : InspectorAgentBase(ASCIILiteral("Page"), instrumentingAgents)
330     , m_page(page)
331     , m_client(client)
332     , m_overlay(overlay)
333     , m_lastScriptIdentifier(0)
334     , m_enabled(false)
335     , m_isFirstLayoutAfterOnLoad(false)
336     , m_originalScriptExecutionDisabled(false)
337     , m_ignoreScriptsEnabledNotification(false)
338     , m_showPaintRects(false)
339 {
340 }
341
342 void InspectorPageAgent::didCreateFrontendAndBackend(Inspector::InspectorFrontendChannel* frontendChannel, InspectorBackendDispatcher* backendDispatcher)
343 {
344     m_frontendDispatcher = std::make_unique<InspectorPageFrontendDispatcher>(frontendChannel);
345     m_backendDispatcher = InspectorPageBackendDispatcher::create(backendDispatcher, this);
346 }
347
348 void InspectorPageAgent::willDestroyFrontendAndBackend(InspectorDisconnectReason)
349 {
350     m_frontendDispatcher = nullptr;
351     m_backendDispatcher.clear();
352
353     ErrorString unused;
354     disable(unused);
355 #if ENABLE(TOUCH_EVENTS)
356     updateTouchEventEmulationInPage(false);
357 #endif
358 }
359
360 double InspectorPageAgent::timestamp()
361 {
362     return m_instrumentingAgents->inspectorEnvironment().executionStopwatch()->elapsedTime();
363 }
364
365 void InspectorPageAgent::enable(ErrorString&)
366 {
367     m_enabled = true;
368     m_instrumentingAgents->setInspectorPageAgent(this);
369
370     if (Frame* frame = mainFrame())
371         m_originalScriptExecutionDisabled = !frame->settings().isScriptEnabled();
372 }
373
374 void InspectorPageAgent::disable(ErrorString&)
375 {
376     m_enabled = false;
377     m_scriptsToEvaluateOnLoad.clear();
378     m_instrumentingAgents->setInspectorPageAgent(nullptr);
379
380     ErrorString unused;
381     setScriptExecutionDisabled(unused, m_originalScriptExecutionDisabled);
382     setShowPaintRects(unused, false);
383     setEmulatedMedia(unused, emptyString());
384 }
385
386 void InspectorPageAgent::addScriptToEvaluateOnLoad(ErrorString&, const String& source, String* identifier)
387 {
388     if (!m_scriptsToEvaluateOnLoad)
389         m_scriptsToEvaluateOnLoad = InspectorObject::create();
390
391     // Assure we don't override existing ids -- m_lastScriptIdentifier could get out of sync WRT actual
392     // scripts once we restored the scripts from the cookie during navigation.
393     do {
394         *identifier = String::number(++m_lastScriptIdentifier);
395     } while (m_scriptsToEvaluateOnLoad->find(*identifier) != m_scriptsToEvaluateOnLoad->end());
396
397     m_scriptsToEvaluateOnLoad->setString(*identifier, source);
398 }
399
400 void InspectorPageAgent::removeScriptToEvaluateOnLoad(ErrorString& error, const String& identifier)
401 {
402     if (!m_scriptsToEvaluateOnLoad || m_scriptsToEvaluateOnLoad->find(identifier) == m_scriptsToEvaluateOnLoad->end()) {
403         error = ASCIILiteral("Script not found");
404         return;
405     }
406
407     m_scriptsToEvaluateOnLoad->remove(identifier);
408 }
409
410 void InspectorPageAgent::reload(ErrorString&, const bool* const optionalIgnoreCache, const String* optionalScriptToEvaluateOnLoad)
411 {
412     m_pendingScriptToEvaluateOnLoadOnce = optionalScriptToEvaluateOnLoad ? *optionalScriptToEvaluateOnLoad : "";
413     m_page->mainFrame().loader().reload(optionalIgnoreCache ? *optionalIgnoreCache : false);
414 }
415
416 void InspectorPageAgent::navigate(ErrorString&, const String& url)
417 {
418     UserGestureIndicator indicator(DefinitelyProcessingUserGesture);
419     Frame& frame = m_page->mainFrame();
420     frame.loader().changeLocation(frame.document()->securityOrigin(), frame.document()->completeURL(url), "", LockHistory::No, LockBackForwardList::No);
421 }
422
423 static PassRefPtr<Inspector::Protocol::Page::Cookie> buildObjectForCookie(const Cookie& cookie)
424 {
425     return Inspector::Protocol::Page::Cookie::create()
426         .setName(cookie.name)
427         .setValue(cookie.value)
428         .setDomain(cookie.domain)
429         .setPath(cookie.path)
430         .setExpires(cookie.expires)
431         .setSize((cookie.name.length() + cookie.value.length()))
432         .setHttpOnly(cookie.httpOnly)
433         .setSecure(cookie.secure)
434         .setSession(cookie.session)
435         .release();
436 }
437
438 static PassRefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>> buildArrayForCookies(ListHashSet<Cookie>& cookiesList)
439 {
440     RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>> cookies = Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>::create();
441
442     for (auto& cookie : cookiesList)
443         cookies->addItem(buildObjectForCookie(cookie));
444
445     return cookies;
446 }
447
448 static Vector<CachedResource*> cachedResourcesForFrame(Frame* frame)
449 {
450     Vector<CachedResource*> result;
451
452     for (auto& cachedResourceHandle : frame->document()->cachedResourceLoader()->allCachedResources().values()) {
453         auto* cachedResource = cachedResourceHandle.get();
454         if (cachedResource->resourceRequest().hiddenFromInspector())
455             continue;
456
457         switch (cachedResource->type()) {
458         case CachedResource::ImageResource:
459             // Skip images that were not auto loaded (images disabled in the user agent).
460 #if ENABLE(SVG_FONTS)
461         case CachedResource::SVGFontResource:
462 #endif
463         case CachedResource::FontResource:
464             // Skip fonts that were referenced in CSS but never used/downloaded.
465             if (cachedResource->stillNeedsLoad())
466                 continue;
467             break;
468         default:
469             // All other CachedResource types download immediately.
470             break;
471         }
472
473         result.append(cachedResource);
474     }
475
476     return result;
477 }
478
479 static Vector<URL> allResourcesURLsForFrame(Frame* frame)
480 {
481     Vector<URL> result;
482
483     result.append(frame->loader().documentLoader()->url());
484
485     for (auto* cachedResource : cachedResourcesForFrame(frame))
486         result.append(cachedResource->url());
487
488     return result;
489 }
490
491 void InspectorPageAgent::getCookies(ErrorString&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>>& cookies)
492 {
493     // If we can get raw cookies.
494     ListHashSet<Cookie> rawCookiesList;
495
496     // If we can't get raw cookies - fall back to String representation
497     StringBuilder stringCookiesList;
498
499     // Return value to getRawCookies should be the same for every call because
500     // the return value is platform/network backend specific, and the call will
501     // always return the same true/false value.
502     bool rawCookiesImplemented = false;
503
504     for (Frame* frame = mainFrame(); frame; frame = frame->tree().traverseNext(mainFrame())) {
505         Document* document = frame->document();
506
507         for (auto& url : allResourcesURLsForFrame(frame)) {
508             Vector<Cookie> docCookiesList;
509             rawCookiesImplemented = getRawCookies(document, URL(ParsedURLString, url), docCookiesList);
510
511             if (!rawCookiesImplemented) {
512                 // FIXME: We need duplication checking for the String representation of cookies.
513                 // Exceptions are thrown by cookie() in sandboxed frames. That won't happen here
514                 // because "document" is the document of the main frame of the page.
515                 stringCookiesList.append(document->cookie(ASSERT_NO_EXCEPTION));
516             } else {
517                 int cookiesSize = docCookiesList.size();
518                 for (int i = 0; i < cookiesSize; i++) {
519                     if (!rawCookiesList.contains(docCookiesList[i]))
520                         rawCookiesList.add(docCookiesList[i]);
521                 }
522             }
523         }
524     }
525
526     // FIXME: Do not return empty string/empty array. Make returns optional instead. https://bugs.webkit.org/show_bug.cgi?id=80855
527     if (rawCookiesImplemented)
528         cookies = buildArrayForCookies(rawCookiesList);
529     else
530         cookies = Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>::create();
531 }
532
533 void InspectorPageAgent::deleteCookie(ErrorString&, const String& cookieName, const String& url)
534 {
535     URL parsedURL(ParsedURLString, url);
536     for (Frame* frame = &m_page->mainFrame(); frame; frame = frame->tree().traverseNext(&m_page->mainFrame()))
537         WebCore::deleteCookie(frame->document(), parsedURL, cookieName);
538 }
539
540 void InspectorPageAgent::getResourceTree(ErrorString&, RefPtr<Inspector::Protocol::Page::FrameResourceTree>& object)
541 {
542     object = buildObjectForFrameTree(&m_page->mainFrame());
543 }
544
545 void InspectorPageAgent::getResourceContent(ErrorString& errorString, const String& frameId, const String& url, String* content, bool* base64Encoded)
546 {
547     Frame* frame = assertFrame(errorString, frameId);
548     if (!frame)
549         return;
550
551     resourceContent(errorString, frame, URL(ParsedURLString, url), content, base64Encoded);
552 }
553
554 static bool textContentForCachedResource(CachedResource* cachedResource, String* result)
555 {
556     if (hasTextContent(cachedResource)) {
557         String content;
558         bool base64Encoded;
559         if (InspectorPageAgent::cachedResourceContent(cachedResource, result, &base64Encoded)) {
560             ASSERT(!base64Encoded);
561             return true;
562         }
563     }
564     return false;
565 }
566
567 void InspectorPageAgent::searchInResource(ErrorString&, const String& frameId, const String& url, const String& query, const bool* const optionalCaseSensitive, const bool* const optionalIsRegex, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>>& results)
568 {
569     results = Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>::create();
570
571     bool isRegex = optionalIsRegex ? *optionalIsRegex : false;
572     bool caseSensitive = optionalCaseSensitive ? *optionalCaseSensitive : false;
573
574     Frame* frame = frameForId(frameId);
575     if (!frame)
576         return;
577
578     DocumentLoader* loader = frame->loader().documentLoader();
579     if (!loader)
580         return;
581
582     URL kurl(ParsedURLString, url);
583
584     String content;
585     bool success = false;
586     if (equalIgnoringFragmentIdentifier(kurl, loader->url()))
587         success = mainResourceContent(frame, false, &content);
588
589     if (!success) {
590         CachedResource* resource = cachedResource(frame, kurl);
591         if (resource)
592             success = textContentForCachedResource(resource, &content);
593     }
594
595     if (!success)
596         return;
597
598     results = ContentSearchUtilities::searchInTextByLines(content, query, caseSensitive, isRegex);
599 }
600
601 static PassRefPtr<Inspector::Protocol::Page::SearchResult> buildObjectForSearchResult(const String& frameId, const String& url, int matchesCount)
602 {
603     return Inspector::Protocol::Page::SearchResult::create()
604         .setUrl(url)
605         .setFrameId(frameId)
606         .setMatchesCount(matchesCount)
607         .release();
608 }
609
610 void InspectorPageAgent::searchInResources(ErrorString&, const String& text, const bool* const optionalCaseSensitive, const bool* const optionalIsRegex, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::SearchResult>>& results)
611 {
612     RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::SearchResult>> searchResults = Inspector::Protocol::Array<Inspector::Protocol::Page::SearchResult>::create();
613
614     bool isRegex = optionalIsRegex ? *optionalIsRegex : false;
615     bool caseSensitive = optionalCaseSensitive ? *optionalCaseSensitive : false;
616     JSC::Yarr::RegularExpression regex = ContentSearchUtilities::createSearchRegex(text, caseSensitive, isRegex);
617
618     for (Frame* frame = &m_page->mainFrame(); frame; frame = frame->tree().traverseNext(&m_page->mainFrame())) {
619         String content;
620
621         for (auto* cachedResource : cachedResourcesForFrame(frame)) {
622             if (textContentForCachedResource(cachedResource, &content)) {
623                 int matchesCount = ContentSearchUtilities::countRegularExpressionMatches(regex, content);
624                 if (matchesCount)
625                     searchResults->addItem(buildObjectForSearchResult(frameId(frame), cachedResource->url(), matchesCount));
626             }
627         }
628
629         if (mainResourceContent(frame, false, &content)) {
630             int matchesCount = ContentSearchUtilities::countRegularExpressionMatches(regex, content);
631             if (matchesCount)
632                 searchResults->addItem(buildObjectForSearchResult(frameId(frame), frame->document()->url(), matchesCount));
633         }
634     }
635
636     results = searchResults;
637 }
638
639 void InspectorPageAgent::setDocumentContent(ErrorString& errorString, const String& frameId, const String& html)
640 {
641     Frame* frame = assertFrame(errorString, frameId);
642     if (!frame)
643         return;
644
645     Document* document = frame->document();
646     if (!document) {
647         errorString = ASCIILiteral("No Document instance to set HTML for");
648         return;
649     }
650     DOMPatchSupport::patchDocument(document, html);
651 }
652
653 void InspectorPageAgent::setShowPaintRects(ErrorString&, bool show)
654 {
655     m_showPaintRects = show;
656     m_client->setShowPaintRects(show);
657
658     if (m_client->overridesShowPaintRects())
659         return;
660
661     m_overlay->setShowingPaintRects(show);
662 }
663
664 void InspectorPageAgent::getScriptExecutionStatus(ErrorString&, InspectorPageBackendDispatcherHandler::Result* status)
665 {
666     bool disabledByScriptController = false;
667     bool disabledInSettings = false;
668     Frame* frame = mainFrame();
669     if (frame) {
670         disabledByScriptController = !frame->script().canExecuteScripts(NotAboutToExecuteScript);
671         disabledInSettings = !frame->settings().isScriptEnabled();
672     }
673
674     if (!disabledByScriptController) {
675         *status = InspectorPageBackendDispatcherHandler::Result::Allowed;
676         return;
677     }
678
679     if (disabledInSettings)
680         *status = InspectorPageBackendDispatcherHandler::Result::Disabled;
681     else
682         *status = InspectorPageBackendDispatcherHandler::Result::Forbidden;
683 }
684
685 void InspectorPageAgent::setScriptExecutionDisabled(ErrorString&, bool value)
686 {
687     if (!mainFrame())
688         return;
689
690     m_ignoreScriptsEnabledNotification = true;
691     mainFrame()->settings().setScriptEnabled(!value);
692     m_ignoreScriptsEnabledNotification = false;
693 }
694
695 void InspectorPageAgent::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld& world)
696 {
697     if (&world != &mainThreadNormalWorld())
698         return;
699
700     if (!m_frontendDispatcher)
701         return;
702
703     if (m_scriptsToEvaluateOnLoad) {
704         for (auto& keyValuePair : *m_scriptsToEvaluateOnLoad) {
705             String scriptText;
706             if (keyValuePair.value->asString(scriptText))
707                 frame->script().executeScript(scriptText);
708         }
709     }
710
711     if (!m_scriptToEvaluateOnLoadOnce.isEmpty())
712         frame->script().executeScript(m_scriptToEvaluateOnLoadOnce);
713 }
714
715 void InspectorPageAgent::domContentEventFired()
716 {
717     m_isFirstLayoutAfterOnLoad = true;
718     m_frontendDispatcher->domContentEventFired(timestamp());
719 }
720
721 void InspectorPageAgent::loadEventFired()
722 {
723     m_frontendDispatcher->loadEventFired(timestamp());
724 }
725
726 void InspectorPageAgent::frameNavigated(DocumentLoader* loader)
727 {
728     if (loader->frame()->isMainFrame()) {
729         m_scriptToEvaluateOnLoadOnce = m_pendingScriptToEvaluateOnLoadOnce;
730         m_pendingScriptToEvaluateOnLoadOnce = String();
731     }
732     m_frontendDispatcher->frameNavigated(buildObjectForFrame(loader->frame()));
733 }
734
735 void InspectorPageAgent::frameDetached(Frame* frame)
736 {
737     HashMap<Frame*, String>::iterator iterator = m_frameToIdentifier.find(frame);
738     if (iterator != m_frameToIdentifier.end()) {
739         m_frontendDispatcher->frameDetached(iterator->value);
740         m_identifierToFrame.remove(iterator->value);
741         m_frameToIdentifier.remove(iterator);
742     }
743 }
744
745 Frame* InspectorPageAgent::mainFrame()
746 {
747     // FIXME: This should return a Frame&
748     return &m_page->mainFrame();
749 }
750
751 Frame* InspectorPageAgent::frameForId(const String& frameId)
752 {
753     return frameId.isEmpty() ? nullptr : m_identifierToFrame.get(frameId);
754 }
755
756 String InspectorPageAgent::frameId(Frame* frame)
757 {
758     if (!frame)
759         return "";
760     String identifier = m_frameToIdentifier.get(frame);
761     if (identifier.isNull()) {
762         identifier = IdentifiersFactory::createIdentifier();
763         m_frameToIdentifier.set(frame, identifier);
764         m_identifierToFrame.set(identifier, frame);
765     }
766     return identifier;
767 }
768
769 bool InspectorPageAgent::hasIdForFrame(Frame* frame) const
770 {
771     return frame && m_frameToIdentifier.contains(frame);
772 }
773
774 String InspectorPageAgent::loaderId(DocumentLoader* loader)
775 {
776     if (!loader)
777         return "";
778     String identifier = m_loaderToIdentifier.get(loader);
779     if (identifier.isNull()) {
780         identifier = IdentifiersFactory::createIdentifier();
781         m_loaderToIdentifier.set(loader, identifier);
782     }
783     return identifier;
784 }
785
786 Frame* InspectorPageAgent::findFrameWithSecurityOrigin(const String& originRawString)
787 {
788     for (Frame* frame = &m_page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
789         RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin();
790         if (documentOrigin->toRawString() == originRawString)
791             return frame;
792     }
793     return nullptr;
794 }
795
796 Frame* InspectorPageAgent::assertFrame(ErrorString& errorString, const String& frameId)
797 {
798     Frame* frame = frameForId(frameId);
799     if (!frame)
800         errorString = ASCIILiteral("No frame for given id found");
801     return frame;
802 }
803
804 // static
805 DocumentLoader* InspectorPageAgent::assertDocumentLoader(ErrorString& errorString, Frame* frame)
806 {
807     FrameLoader& frameLoader = frame->loader();
808     DocumentLoader* documentLoader = frameLoader.documentLoader();
809     if (!documentLoader)
810         errorString = ASCIILiteral("No documentLoader for given frame found");
811     return documentLoader;
812 }
813
814 void InspectorPageAgent::loaderDetachedFromFrame(DocumentLoader* loader)
815 {
816     m_loaderToIdentifier.remove(loader);
817 }
818
819 void InspectorPageAgent::frameStartedLoading(Frame& frame)
820 {
821     m_frontendDispatcher->frameStartedLoading(frameId(&frame));
822 }
823
824 void InspectorPageAgent::frameStoppedLoading(Frame& frame)
825 {
826     m_frontendDispatcher->frameStoppedLoading(frameId(&frame));
827 }
828
829 void InspectorPageAgent::frameScheduledNavigation(Frame& frame, double delay)
830 {
831     m_frontendDispatcher->frameScheduledNavigation(frameId(&frame), delay);
832 }
833
834 void InspectorPageAgent::frameClearedScheduledNavigation(Frame& frame)
835 {
836     m_frontendDispatcher->frameClearedScheduledNavigation(frameId(&frame));
837 }
838
839 void InspectorPageAgent::willRunJavaScriptDialog(const String& message)
840 {
841     m_frontendDispatcher->javascriptDialogOpening(message);
842 }
843
844 void InspectorPageAgent::didRunJavaScriptDialog()
845 {
846     m_frontendDispatcher->javascriptDialogClosed();
847 }
848
849 void InspectorPageAgent::didPaint(RenderObject* renderer, const LayoutRect& rect)
850 {
851     if (!m_enabled || !m_showPaintRects)
852         return;
853
854     LayoutRect absoluteRect = LayoutRect(renderer->localToAbsoluteQuad(FloatRect(rect)).boundingBox());
855     FrameView* view = renderer->document().view();
856     
857     LayoutRect rootRect = absoluteRect;
858     if (!view->frame().isMainFrame()) {
859         IntRect rootViewRect = view->contentsToRootView(snappedIntRect(absoluteRect));
860         rootRect = view->frame().mainFrame().view()->rootViewToContents(rootViewRect);
861     }
862     
863     if (m_client->overridesShowPaintRects()) {
864         m_client->showPaintRect(rootRect);
865         return;
866     }
867
868     m_overlay->showPaintRect(rootRect);
869 }
870
871 void InspectorPageAgent::didLayout()
872 {
873     bool isFirstLayout = m_isFirstLayoutAfterOnLoad;
874     if (isFirstLayout)
875         m_isFirstLayoutAfterOnLoad = false;
876
877     if (!m_enabled)
878         return;
879
880     m_overlay->update();
881 }
882
883 void InspectorPageAgent::didScroll()
884 {
885     if (m_enabled)
886         m_overlay->update();
887 }
888
889 void InspectorPageAgent::didRecalculateStyle()
890 {
891     if (m_enabled)
892         m_overlay->update();
893 }
894
895 void InspectorPageAgent::scriptsEnabled(bool isEnabled)
896 {
897     if (m_ignoreScriptsEnabledNotification)
898         return;
899
900     m_frontendDispatcher->scriptsEnabled(isEnabled);
901 }
902
903 PassRefPtr<Inspector::Protocol::Page::Frame> InspectorPageAgent::buildObjectForFrame(Frame* frame)
904 {
905     RefPtr<Inspector::Protocol::Page::Frame> frameObject = Inspector::Protocol::Page::Frame::create()
906         .setId(frameId(frame))
907         .setLoaderId(loaderId(frame->loader().documentLoader()))
908         .setUrl(frame->document()->url().string())
909         .setMimeType(frame->loader().documentLoader()->responseMIMEType())
910         .setSecurityOrigin(frame->document()->securityOrigin()->toRawString());
911     if (frame->tree().parent())
912         frameObject->setParentId(frameId(frame->tree().parent()));
913     if (frame->ownerElement()) {
914         String name = frame->ownerElement()->getNameAttribute();
915         if (name.isEmpty())
916             name = frame->ownerElement()->getAttribute(HTMLNames::idAttr);
917         frameObject->setName(name);
918     }
919
920     return frameObject;
921 }
922
923 PassRefPtr<Inspector::Protocol::Page::FrameResourceTree> InspectorPageAgent::buildObjectForFrameTree(Frame* frame)
924 {
925     RefPtr<Inspector::Protocol::Page::Frame> frameObject = buildObjectForFrame(frame);
926     RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResource>> subresources = Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResource>::create();
927     RefPtr<Inspector::Protocol::Page::FrameResourceTree> result = Inspector::Protocol::Page::FrameResourceTree::create()
928          .setFrame(frameObject)
929          .setResources(subresources);
930
931     for (auto* cachedResource : cachedResourcesForFrame(frame)) {
932         RefPtr<Inspector::Protocol::Page::FrameResource> resourceObject = Inspector::Protocol::Page::FrameResource::create()
933             .setUrl(cachedResource->url())
934             .setType(cachedResourceTypeJson(*cachedResource))
935             .setMimeType(cachedResource->response().mimeType());
936         if (cachedResource->wasCanceled())
937             resourceObject->setCanceled(true);
938         else if (cachedResource->status() == CachedResource::LoadError)
939             resourceObject->setFailed(true);
940         String sourceMappingURL = InspectorPageAgent::sourceMapURLForResource(cachedResource);
941         if (!sourceMappingURL.isEmpty())
942             resourceObject->setSourceMapURL(sourceMappingURL);
943         subresources->addItem(resourceObject);
944     }
945
946     RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResourceTree>> childrenArray;
947     for (Frame* child = frame->tree().firstChild(); child; child = child->tree().nextSibling()) {
948         if (!childrenArray) {
949             childrenArray = Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResourceTree>::create();
950             result->setChildFrames(childrenArray);
951         }
952         childrenArray->addItem(buildObjectForFrameTree(child));
953     }
954     return result;
955 }
956
957 #if ENABLE(TOUCH_EVENTS)
958 void InspectorPageAgent::updateTouchEventEmulationInPage(bool enabled)
959 {
960     if (mainFrame())
961         mainFrame()->settings().setTouchEventEmulationEnabled(enabled);
962 }
963 #endif
964
965 void InspectorPageAgent::setTouchEmulationEnabled(ErrorString& error, bool enabled)
966 {
967 #if ENABLE(TOUCH_EVENTS)
968     UNUSED_PARAM(error);
969     updateTouchEventEmulationInPage(enabled);
970 #else
971     error = ASCIILiteral("Touch events emulation not supported");
972     UNUSED_PARAM(enabled);
973 #endif
974 }
975
976 void InspectorPageAgent::setEmulatedMedia(ErrorString&, const String& media)
977 {
978     if (media == m_emulatedMedia)
979         return;
980
981     m_emulatedMedia = media;
982     Document* document = m_page->mainFrame().document();
983     if (document) {
984         document->styleResolverChanged(RecalcStyleImmediately);
985         document->updateLayout();
986     }
987 }
988
989 void InspectorPageAgent::applyEmulatedMedia(String* media)
990 {
991     if (!m_emulatedMedia.isEmpty())
992         *media = m_emulatedMedia;
993 }
994
995 void InspectorPageAgent::getCompositingBordersVisible(ErrorString&, bool* outParam)
996 {
997     *outParam = m_page->settings().showDebugBorders() || m_page->settings().showRepaintCounter();
998 }
999
1000 void InspectorPageAgent::setCompositingBordersVisible(ErrorString&, bool visible)
1001 {
1002     m_page->settings().setShowDebugBorders(visible);
1003     m_page->settings().setShowRepaintCounter(visible);
1004 }
1005
1006 void InspectorPageAgent::snapshotNode(ErrorString& errorString, int nodeId, String* outDataURL)
1007 {
1008     Frame* frame = mainFrame();
1009     ASSERT(frame);
1010
1011     InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent();
1012     ASSERT(domAgent);
1013     Node* node = domAgent->assertNode(errorString, nodeId);
1014     if (!node)
1015         return;
1016
1017     std::unique_ptr<ImageBuffer> snapshot = WebCore::snapshotNode(*frame, *node);
1018     if (!snapshot) {
1019         errorString = ASCIILiteral("Could not capture snapshot");
1020         return;
1021     }
1022
1023     *outDataURL = snapshot->toDataURL(ASCIILiteral("image/png"));
1024 }
1025
1026 void InspectorPageAgent::snapshotRect(ErrorString& errorString, int x, int y, int width, int height, const String& coordinateSystem, String* outDataURL)
1027 {
1028     Frame* frame = mainFrame();
1029     ASSERT(frame);
1030
1031     SnapshotOptions options = SnapshotOptionsNone;
1032     if (coordinateSystem == "Viewport")
1033         options |= SnapshotOptionsInViewCoordinates;
1034
1035     IntRect rectangle(x, y, width, height);
1036     std::unique_ptr<ImageBuffer> snapshot = snapshotFrameRect(*frame, rectangle, options);
1037
1038     if (!snapshot) {
1039         errorString = ASCIILiteral("Could not capture snapshot");
1040         return;
1041     }
1042
1043     *outDataURL = snapshot->toDataURL(ASCIILiteral("image/png"));
1044 }
1045
1046 void InspectorPageAgent::handleJavaScriptDialog(ErrorString& errorString, bool accept, const String* promptText)
1047 {
1048     if (!m_client->handleJavaScriptDialog(accept, promptText))
1049         errorString = ASCIILiteral("Could not handle JavaScript dialog");
1050 }
1051
1052 void InspectorPageAgent::archive(ErrorString& errorString, String* data)
1053 {
1054     Frame* frame = mainFrame();
1055     if (!frame) {
1056         errorString = ASCIILiteral("No main frame");
1057         return;
1058     }
1059
1060 #if ENABLE(WEB_ARCHIVE) && USE(CF)
1061     RefPtr<LegacyWebArchive> archive = LegacyWebArchive::create(frame);
1062     if (!archive) {
1063         errorString = ASCIILiteral("Could not create web archive for main frame");
1064         return;
1065     }
1066
1067     RetainPtr<CFDataRef> buffer = archive->rawDataRepresentation();
1068     *data = base64Encode(CFDataGetBytePtr(buffer.get()), CFDataGetLength(buffer.get()));
1069 #else
1070     UNUSED_PARAM(data);
1071     errorString = ASCIILiteral("No support for creating archives");
1072 #endif
1073 }
1074
1075 } // namespace WebCore
1076
1077 #endif // ENABLE(INSPECTOR)