Clean up FrameTree::traverseNext() traversals of main frames
[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 RefPtr<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 WTF::move(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 Ref<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 Ref<Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>> buildArrayForCookies(ListHashSet<Cookie>& cookiesList)
439 {
440     auto cookies = Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>::create();
441
442     for (const auto& cookie : cookiesList)
443         cookies->addItem(buildObjectForCookie(cookie));
444
445     return WTF::move(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()) {
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())
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 Ref<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>>& result)
611 {
612     result = 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()) {
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                     result->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                 result->addItem(buildObjectForSearchResult(frameId(frame), frame->document()->url(), matchesCount));
633         }
634     }
635 }
636
637 void InspectorPageAgent::setDocumentContent(ErrorString& errorString, const String& frameId, const String& html)
638 {
639     Frame* frame = assertFrame(errorString, frameId);
640     if (!frame)
641         return;
642
643     Document* document = frame->document();
644     if (!document) {
645         errorString = ASCIILiteral("No Document instance to set HTML for");
646         return;
647     }
648     DOMPatchSupport::patchDocument(document, html);
649 }
650
651 void InspectorPageAgent::setShowPaintRects(ErrorString&, bool show)
652 {
653     m_showPaintRects = show;
654     m_client->setShowPaintRects(show);
655
656     if (m_client->overridesShowPaintRects())
657         return;
658
659     m_overlay->setShowingPaintRects(show);
660 }
661
662 void InspectorPageAgent::getScriptExecutionStatus(ErrorString&, InspectorPageBackendDispatcherHandler::Result* status)
663 {
664     bool disabledByScriptController = false;
665     bool disabledInSettings = false;
666     Frame* frame = mainFrame();
667     if (frame) {
668         disabledByScriptController = !frame->script().canExecuteScripts(NotAboutToExecuteScript);
669         disabledInSettings = !frame->settings().isScriptEnabled();
670     }
671
672     if (!disabledByScriptController) {
673         *status = InspectorPageBackendDispatcherHandler::Result::Allowed;
674         return;
675     }
676
677     if (disabledInSettings)
678         *status = InspectorPageBackendDispatcherHandler::Result::Disabled;
679     else
680         *status = InspectorPageBackendDispatcherHandler::Result::Forbidden;
681 }
682
683 void InspectorPageAgent::setScriptExecutionDisabled(ErrorString&, bool value)
684 {
685     if (!mainFrame())
686         return;
687
688     m_ignoreScriptsEnabledNotification = true;
689     mainFrame()->settings().setScriptEnabled(!value);
690     m_ignoreScriptsEnabledNotification = false;
691 }
692
693 void InspectorPageAgent::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld& world)
694 {
695     if (&world != &mainThreadNormalWorld())
696         return;
697
698     if (!m_frontendDispatcher)
699         return;
700
701     if (m_scriptsToEvaluateOnLoad) {
702         for (auto& keyValuePair : *m_scriptsToEvaluateOnLoad) {
703             String scriptText;
704             if (keyValuePair.value->asString(scriptText))
705                 frame->script().executeScript(scriptText);
706         }
707     }
708
709     if (!m_scriptToEvaluateOnLoadOnce.isEmpty())
710         frame->script().executeScript(m_scriptToEvaluateOnLoadOnce);
711 }
712
713 void InspectorPageAgent::domContentEventFired()
714 {
715     m_isFirstLayoutAfterOnLoad = true;
716     m_frontendDispatcher->domContentEventFired(timestamp());
717 }
718
719 void InspectorPageAgent::loadEventFired()
720 {
721     m_frontendDispatcher->loadEventFired(timestamp());
722 }
723
724 void InspectorPageAgent::frameNavigated(DocumentLoader* loader)
725 {
726     if (loader->frame()->isMainFrame()) {
727         m_scriptToEvaluateOnLoadOnce = m_pendingScriptToEvaluateOnLoadOnce;
728         m_pendingScriptToEvaluateOnLoadOnce = String();
729     }
730     m_frontendDispatcher->frameNavigated(buildObjectForFrame(loader->frame()));
731 }
732
733 void InspectorPageAgent::frameDetached(Frame& frame)
734 {
735     HashMap<Frame*, String>::iterator iterator = m_frameToIdentifier.find(&frame);
736     if (iterator != m_frameToIdentifier.end()) {
737         m_frontendDispatcher->frameDetached(iterator->value);
738         m_identifierToFrame.remove(iterator->value);
739         m_frameToIdentifier.remove(iterator);
740     }
741 }
742
743 Frame* InspectorPageAgent::mainFrame()
744 {
745     // FIXME: This should return a Frame&
746     return &m_page->mainFrame();
747 }
748
749 Frame* InspectorPageAgent::frameForId(const String& frameId)
750 {
751     return frameId.isEmpty() ? nullptr : m_identifierToFrame.get(frameId);
752 }
753
754 String InspectorPageAgent::frameId(Frame* frame)
755 {
756     if (!frame)
757         return "";
758     String identifier = m_frameToIdentifier.get(frame);
759     if (identifier.isNull()) {
760         identifier = IdentifiersFactory::createIdentifier();
761         m_frameToIdentifier.set(frame, identifier);
762         m_identifierToFrame.set(identifier, frame);
763     }
764     return identifier;
765 }
766
767 bool InspectorPageAgent::hasIdForFrame(Frame* frame) const
768 {
769     return frame && m_frameToIdentifier.contains(frame);
770 }
771
772 String InspectorPageAgent::loaderId(DocumentLoader* loader)
773 {
774     if (!loader)
775         return "";
776     String identifier = m_loaderToIdentifier.get(loader);
777     if (identifier.isNull()) {
778         identifier = IdentifiersFactory::createIdentifier();
779         m_loaderToIdentifier.set(loader, identifier);
780     }
781     return identifier;
782 }
783
784 Frame* InspectorPageAgent::findFrameWithSecurityOrigin(const String& originRawString)
785 {
786     for (Frame* frame = &m_page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
787         RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin();
788         if (documentOrigin->toRawString() == originRawString)
789             return frame;
790     }
791     return nullptr;
792 }
793
794 Frame* InspectorPageAgent::assertFrame(ErrorString& errorString, const String& frameId)
795 {
796     Frame* frame = frameForId(frameId);
797     if (!frame)
798         errorString = ASCIILiteral("No frame for given id found");
799     return frame;
800 }
801
802 // static
803 DocumentLoader* InspectorPageAgent::assertDocumentLoader(ErrorString& errorString, Frame* frame)
804 {
805     FrameLoader& frameLoader = frame->loader();
806     DocumentLoader* documentLoader = frameLoader.documentLoader();
807     if (!documentLoader)
808         errorString = ASCIILiteral("No documentLoader for given frame found");
809     return documentLoader;
810 }
811
812 void InspectorPageAgent::loaderDetachedFromFrame(DocumentLoader& loader)
813 {
814     m_loaderToIdentifier.remove(&loader);
815 }
816
817 void InspectorPageAgent::frameStartedLoading(Frame& frame)
818 {
819     m_frontendDispatcher->frameStartedLoading(frameId(&frame));
820 }
821
822 void InspectorPageAgent::frameStoppedLoading(Frame& frame)
823 {
824     m_frontendDispatcher->frameStoppedLoading(frameId(&frame));
825 }
826
827 void InspectorPageAgent::frameScheduledNavigation(Frame& frame, double delay)
828 {
829     m_frontendDispatcher->frameScheduledNavigation(frameId(&frame), delay);
830 }
831
832 void InspectorPageAgent::frameClearedScheduledNavigation(Frame& frame)
833 {
834     m_frontendDispatcher->frameClearedScheduledNavigation(frameId(&frame));
835 }
836
837 void InspectorPageAgent::willRunJavaScriptDialog(const String& message)
838 {
839     m_frontendDispatcher->javascriptDialogOpening(message);
840 }
841
842 void InspectorPageAgent::didRunJavaScriptDialog()
843 {
844     m_frontendDispatcher->javascriptDialogClosed();
845 }
846
847 void InspectorPageAgent::didPaint(RenderObject* renderer, const LayoutRect& rect)
848 {
849     if (!m_enabled || !m_showPaintRects)
850         return;
851
852     LayoutRect absoluteRect = LayoutRect(renderer->localToAbsoluteQuad(FloatRect(rect)).boundingBox());
853     FrameView* view = renderer->document().view();
854
855     LayoutRect rootRect = absoluteRect;
856     if (!view->frame().isMainFrame()) {
857         IntRect rootViewRect = view->contentsToRootView(snappedIntRect(absoluteRect));
858         rootRect = view->frame().mainFrame().view()->rootViewToContents(rootViewRect);
859     }
860
861     if (m_client->overridesShowPaintRects()) {
862         m_client->showPaintRect(rootRect);
863         return;
864     }
865
866     m_overlay->showPaintRect(rootRect);
867 }
868
869 void InspectorPageAgent::didLayout()
870 {
871     bool isFirstLayout = m_isFirstLayoutAfterOnLoad;
872     if (isFirstLayout)
873         m_isFirstLayoutAfterOnLoad = false;
874
875     if (!m_enabled)
876         return;
877
878     m_overlay->update();
879 }
880
881 void InspectorPageAgent::didScroll()
882 {
883     if (m_enabled)
884         m_overlay->update();
885 }
886
887 void InspectorPageAgent::didRecalculateStyle()
888 {
889     if (m_enabled)
890         m_overlay->update();
891 }
892
893 void InspectorPageAgent::scriptsEnabled(bool isEnabled)
894 {
895     if (m_ignoreScriptsEnabledNotification)
896         return;
897
898     m_frontendDispatcher->scriptsEnabled(isEnabled);
899 }
900
901 Ref<Inspector::Protocol::Page::Frame> InspectorPageAgent::buildObjectForFrame(Frame* frame)
902 {
903     auto frameObject = Inspector::Protocol::Page::Frame::create()
904         .setId(frameId(frame))
905         .setLoaderId(loaderId(frame->loader().documentLoader()))
906         .setUrl(frame->document()->url().string())
907         .setMimeType(frame->loader().documentLoader()->responseMIMEType())
908         .setSecurityOrigin(frame->document()->securityOrigin()->toRawString())
909         .release();
910     if (frame->tree().parent())
911         frameObject->setParentId(frameId(frame->tree().parent()));
912     if (frame->ownerElement()) {
913         String name = frame->ownerElement()->getNameAttribute();
914         if (name.isEmpty())
915             name = frame->ownerElement()->getAttribute(HTMLNames::idAttr);
916         frameObject->setName(name);
917     }
918
919     return WTF::move(frameObject);
920 }
921
922 Ref<Inspector::Protocol::Page::FrameResourceTree> InspectorPageAgent::buildObjectForFrameTree(Frame* frame)
923 {
924     Ref<Inspector::Protocol::Page::Frame> frameObject = buildObjectForFrame(frame);
925     auto subresources = Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResource>::create();
926     auto result = Inspector::Protocol::Page::FrameResourceTree::create()
927         .setFrame(WTF::move(frameObject))
928         .setResources(subresources.copyRef())
929         .release();
930
931     for (auto* cachedResource : cachedResourcesForFrame(frame)) {
932         auto resourceObject = Inspector::Protocol::Page::FrameResource::create()
933             .setUrl(cachedResource->url())
934             .setType(cachedResourceTypeJson(*cachedResource))
935             .setMimeType(cachedResource->response().mimeType())
936             .release();
937         if (cachedResource->wasCanceled())
938             resourceObject->setCanceled(true);
939         else if (cachedResource->status() == CachedResource::LoadError)
940             resourceObject->setFailed(true);
941         String sourceMappingURL = InspectorPageAgent::sourceMapURLForResource(cachedResource);
942         if (!sourceMappingURL.isEmpty())
943             resourceObject->setSourceMapURL(sourceMappingURL);
944         subresources->addItem(WTF::move(resourceObject));
945     }
946
947     RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResourceTree>> childrenArray;
948     for (Frame* child = frame->tree().firstChild(); child; child = child->tree().nextSibling()) {
949         if (!childrenArray) {
950             childrenArray = Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResourceTree>::create();
951             result->setChildFrames(childrenArray);
952         }
953         childrenArray->addItem(buildObjectForFrameTree(child));
954     }
955     return result;
956 }
957
958 #if ENABLE(TOUCH_EVENTS)
959 void InspectorPageAgent::updateTouchEventEmulationInPage(bool enabled)
960 {
961     if (mainFrame())
962         mainFrame()->settings().setTouchEventEmulationEnabled(enabled);
963 }
964 #endif
965
966 void InspectorPageAgent::setTouchEmulationEnabled(ErrorString& error, bool enabled)
967 {
968 #if ENABLE(TOUCH_EVENTS)
969     UNUSED_PARAM(error);
970     updateTouchEventEmulationInPage(enabled);
971 #else
972     error = ASCIILiteral("Touch events emulation not supported");
973     UNUSED_PARAM(enabled);
974 #endif
975 }
976
977 void InspectorPageAgent::setEmulatedMedia(ErrorString&, const String& media)
978 {
979     if (media == m_emulatedMedia)
980         return;
981
982     m_emulatedMedia = media;
983     Document* document = m_page->mainFrame().document();
984     if (document) {
985         document->styleResolverChanged(RecalcStyleImmediately);
986         document->updateLayout();
987     }
988 }
989
990 void InspectorPageAgent::applyEmulatedMedia(String& media)
991 {
992     if (!m_emulatedMedia.isEmpty())
993         media = m_emulatedMedia;
994 }
995
996 void InspectorPageAgent::getCompositingBordersVisible(ErrorString&, bool* outParam)
997 {
998     *outParam = m_page->settings().showDebugBorders() || m_page->settings().showRepaintCounter();
999 }
1000
1001 void InspectorPageAgent::setCompositingBordersVisible(ErrorString&, bool visible)
1002 {
1003     m_page->settings().setShowDebugBorders(visible);
1004     m_page->settings().setShowRepaintCounter(visible);
1005 }
1006
1007 void InspectorPageAgent::snapshotNode(ErrorString& errorString, int nodeId, String* outDataURL)
1008 {
1009     Frame* frame = mainFrame();
1010     ASSERT(frame);
1011
1012     InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent();
1013     ASSERT(domAgent);
1014     Node* node = domAgent->assertNode(errorString, nodeId);
1015     if (!node)
1016         return;
1017
1018     std::unique_ptr<ImageBuffer> snapshot = WebCore::snapshotNode(*frame, *node);
1019     if (!snapshot) {
1020         errorString = ASCIILiteral("Could not capture snapshot");
1021         return;
1022     }
1023
1024     *outDataURL = snapshot->toDataURL(ASCIILiteral("image/png"));
1025 }
1026
1027 void InspectorPageAgent::snapshotRect(ErrorString& errorString, int x, int y, int width, int height, const String& coordinateSystem, String* outDataURL)
1028 {
1029     Frame* frame = mainFrame();
1030     ASSERT(frame);
1031
1032     SnapshotOptions options = SnapshotOptionsNone;
1033     if (coordinateSystem == "Viewport")
1034         options |= SnapshotOptionsInViewCoordinates;
1035
1036     IntRect rectangle(x, y, width, height);
1037     std::unique_ptr<ImageBuffer> snapshot = snapshotFrameRect(*frame, rectangle, options);
1038
1039     if (!snapshot) {
1040         errorString = ASCIILiteral("Could not capture snapshot");
1041         return;
1042     }
1043
1044     *outDataURL = snapshot->toDataURL(ASCIILiteral("image/png"));
1045 }
1046
1047 void InspectorPageAgent::handleJavaScriptDialog(ErrorString& errorString, bool accept, const String* promptText)
1048 {
1049     if (!m_client->handleJavaScriptDialog(accept, promptText))
1050         errorString = ASCIILiteral("Could not handle JavaScript dialog");
1051 }
1052
1053 void InspectorPageAgent::archive(ErrorString& errorString, String* data)
1054 {
1055     Frame* frame = mainFrame();
1056     if (!frame) {
1057         errorString = ASCIILiteral("No main frame");
1058         return;
1059     }
1060
1061 #if ENABLE(WEB_ARCHIVE) && USE(CF)
1062     RefPtr<LegacyWebArchive> archive = LegacyWebArchive::create(frame);
1063     if (!archive) {
1064         errorString = ASCIILiteral("Could not create web archive for main frame");
1065         return;
1066     }
1067
1068     RetainPtr<CFDataRef> buffer = archive->rawDataRepresentation();
1069     *data = base64Encode(CFDataGetBytePtr(buffer.get()), CFDataGetLength(buffer.get()));
1070 #else
1071     UNUSED_PARAM(data);
1072     errorString = ASCIILiteral("No support for creating archives");
1073 #endif
1074 }
1075
1076 } // namespace WebCore
1077
1078 #endif // ENABLE(INSPECTOR)