f46d6bffd46106deb2dbce2c95e09645b9e4ca59
[WebKit-https.git] / Source / WebCore / inspector / InspectorPageAgent.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "InspectorPageAgent.h"
34
35 #include "CachedCSSStyleSheet.h"
36 #include "CachedFont.h"
37 #include "CachedImage.h"
38 #include "CachedResource.h"
39 #include "CachedResourceLoader.h"
40 #include "CachedScript.h"
41 #include "Cookie.h"
42 #include "CookieJar.h"
43 #include "Document.h"
44 #include "DocumentLoader.h"
45 #include "Frame.h"
46 #include "FrameLoadRequest.h"
47 #include "FrameLoader.h"
48 #include "FrameSnapshotting.h"
49 #include "FrameView.h"
50 #include "HTMLFrameOwnerElement.h"
51 #include "HTMLNames.h"
52 #include "ImageBuffer.h"
53 #include "InspectorClient.h"
54 #include "InspectorDOMAgent.h"
55 #include "InspectorNetworkAgent.h"
56 #include "InspectorOverlay.h"
57 #include "InstrumentingAgents.h"
58 #include "MIMETypeRegistry.h"
59 #include "MainFrame.h"
60 #include "MemoryCache.h"
61 #include "Page.h"
62 #include "RenderObject.h"
63 #include "ScriptController.h"
64 #include "SecurityOrigin.h"
65 #include "Settings.h"
66 #include "StyleScope.h"
67 #include "TextEncoding.h"
68 #include "TextResourceDecoder.h"
69 #include "UserGestureIndicator.h"
70 #include <inspector/ContentSearchUtilities.h>
71 #include <inspector/IdentifiersFactory.h>
72 #include <inspector/InspectorValues.h>
73 #include <wtf/ListHashSet.h>
74 #include <wtf/Stopwatch.h>
75 #include <wtf/text/Base64.h>
76 #include <wtf/text/StringBuilder.h>
77 #include <yarr/RegularExpression.h>
78
79 #if ENABLE(WEB_ARCHIVE) && USE(CF)
80 #include "LegacyWebArchive.h"
81 #endif
82
83 using namespace Inspector;
84
85 namespace WebCore {
86
87 static bool decodeBuffer(const char* buffer, unsigned size, const String& textEncodingName, String* result)
88 {
89     if (buffer) {
90         TextEncoding encoding(textEncodingName);
91         if (!encoding.isValid())
92             encoding = WindowsLatin1Encoding();
93         *result = encoding.decode(buffer, size);
94         return true;
95     }
96     return false;
97 }
98
99 static bool hasTextContent(CachedResource* cachedResource)
100 {
101     // FIXME: <https://webkit.org/b/165495> Web Inspector: XHR / Fetch for non-text content should not show garbled text
102     // We should not assume XHR / Fetch have text content.
103
104     InspectorPageAgent::ResourceType type = InspectorPageAgent::cachedResourceType(*cachedResource);
105     return type == InspectorPageAgent::DocumentResource
106         || type == InspectorPageAgent::StylesheetResource
107         || type == InspectorPageAgent::ScriptResource
108         || type == InspectorPageAgent::XHRResource
109         || type == InspectorPageAgent::FetchResource;
110 }
111
112 bool InspectorPageAgent::cachedResourceContent(CachedResource* cachedResource, String* result, bool* base64Encoded)
113 {
114     if (!cachedResource)
115         return false;
116
117     *base64Encoded = !hasTextContent(cachedResource);
118
119     if (!cachedResource->encodedSize()) {
120         *result = emptyString();
121         return true;
122     }
123
124     if (*base64Encoded) {
125         if (auto* buffer = cachedResource->resourceBuffer()) {
126             *result = base64Encode(buffer->data(), buffer->size());
127             return true;
128         }
129         return false;
130     }
131
132     if (cachedResource) {
133         switch (cachedResource->type()) {
134         case CachedResource::CSSStyleSheet:
135             // This can return a null String if the MIME type is invalid.
136             *result = downcast<CachedCSSStyleSheet>(*cachedResource).sheetText();
137             return !result->isNull();
138         case CachedResource::Script:
139             *result = downcast<CachedScript>(*cachedResource).script().toString();
140             return true;
141         case CachedResource::MediaResource:
142         case CachedResource::Icon:
143         case CachedResource::RawResource: {
144             auto* buffer = cachedResource->resourceBuffer();
145             if (!buffer)
146                 return false;
147             RefPtr<TextResourceDecoder> decoder = InspectorPageAgent::createTextDecoder(cachedResource->response().mimeType(), cachedResource->response().textEncodingName());
148             // We show content for raw resources only for certain mime types (text, html and xml). Otherwise decoder will be null.
149             if (!decoder)
150                 return false;
151             *result = decoder->decodeAndFlush(buffer->data(), buffer->size());
152             return true;
153         }
154         default:
155             auto* buffer = cachedResource->resourceBuffer();
156             return decodeBuffer(buffer ? buffer->data() : nullptr, buffer ? buffer->size() : 0, cachedResource->encoding(), result);
157         }
158     }
159     return false;
160 }
161
162 bool InspectorPageAgent::mainResourceContent(Frame* frame, bool withBase64Encode, String* result)
163 {
164     RefPtr<SharedBuffer> buffer = frame->loader().documentLoader()->mainResourceData();
165     if (!buffer)
166         return false;
167     return InspectorPageAgent::dataContent(buffer->data(), buffer->size(), frame->document()->encoding(), withBase64Encode, result);
168 }
169
170 bool InspectorPageAgent::sharedBufferContent(RefPtr<SharedBuffer>&& buffer, const String& textEncodingName, bool withBase64Encode, String* result)
171 {
172     return dataContent(buffer ? buffer->data() : nullptr, buffer ? buffer->size() : 0, textEncodingName, withBase64Encode, result);
173 }
174
175 bool InspectorPageAgent::dataContent(const char* data, unsigned size, const String& textEncodingName, bool withBase64Encode, String* result)
176 {
177     if (withBase64Encode) {
178         *result = base64Encode(data, size);
179         return true;
180     }
181
182     return decodeBuffer(data, size, textEncodingName, result);
183 }
184
185 void InspectorPageAgent::resourceContent(ErrorString& errorString, Frame* frame, const URL& url, String* result, bool* base64Encoded)
186 {
187     DocumentLoader* loader = assertDocumentLoader(errorString, frame);
188     if (!loader)
189         return;
190
191     RefPtr<SharedBuffer> buffer;
192     bool success = false;
193     if (equalIgnoringFragmentIdentifier(url, loader->url())) {
194         *base64Encoded = false;
195         success = mainResourceContent(frame, *base64Encoded, result);
196     }
197
198     if (!success)
199         success = cachedResourceContent(cachedResource(frame, url), result, base64Encoded);
200
201     if (!success)
202         errorString = ASCIILiteral("No resource with given URL found");
203 }
204
205 //static
206 String InspectorPageAgent::sourceMapURLForResource(CachedResource* cachedResource)
207 {
208     static NeverDestroyed<String> sourceMapHTTPHeader(MAKE_STATIC_STRING_IMPL("SourceMap"));
209     static NeverDestroyed<String> sourceMapHTTPHeaderDeprecated(MAKE_STATIC_STRING_IMPL("X-SourceMap"));
210
211     if (!cachedResource)
212         return String();
213
214     // Scripts are handled in a separate path.
215     if (cachedResource->type() != CachedResource::CSSStyleSheet)
216         return String();
217
218     String sourceMapHeader = cachedResource->response().httpHeaderField(sourceMapHTTPHeader);
219     if (!sourceMapHeader.isEmpty())
220         return sourceMapHeader;
221
222     sourceMapHeader = cachedResource->response().httpHeaderField(sourceMapHTTPHeaderDeprecated);
223     if (!sourceMapHeader.isEmpty())
224         return sourceMapHeader;
225
226     String content;
227     bool base64Encoded;
228     if (InspectorPageAgent::cachedResourceContent(cachedResource, &content, &base64Encoded) && !base64Encoded)
229         return ContentSearchUtilities::findStylesheetSourceMapURL(content);
230
231     return String();
232 }
233
234 CachedResource* InspectorPageAgent::cachedResource(Frame* frame, const URL& url)
235 {
236     if (url.isNull())
237         return nullptr;
238
239     CachedResource* cachedResource = frame->document()->cachedResourceLoader().cachedResource(MemoryCache::removeFragmentIdentifierIfNeeded(url));
240     if (!cachedResource) {
241         ResourceRequest request(url);
242         request.setDomainForCachePartition(frame->document()->topOrigin().domainForCachePartition());
243         cachedResource = MemoryCache::singleton().resourceForRequest(request, frame->page()->sessionID());
244     }
245
246     return cachedResource;
247 }
248
249 Inspector::Protocol::Page::ResourceType InspectorPageAgent::resourceTypeJson(InspectorPageAgent::ResourceType resourceType)
250 {
251     switch (resourceType) {
252     case DocumentResource:
253         return Inspector::Protocol::Page::ResourceType::Document;
254     case ImageResource:
255         return Inspector::Protocol::Page::ResourceType::Image;
256     case FontResource:
257         return Inspector::Protocol::Page::ResourceType::Font;
258     case StylesheetResource:
259         return Inspector::Protocol::Page::ResourceType::Stylesheet;
260     case ScriptResource:
261         return Inspector::Protocol::Page::ResourceType::Script;
262     case XHRResource:
263         return Inspector::Protocol::Page::ResourceType::XHR;
264     case FetchResource:
265         return Inspector::Protocol::Page::ResourceType::Fetch;
266     case WebSocketResource:
267         return Inspector::Protocol::Page::ResourceType::WebSocket;
268     case OtherResource:
269         return Inspector::Protocol::Page::ResourceType::Other;
270     }
271     return Inspector::Protocol::Page::ResourceType::Other;
272 }
273
274 InspectorPageAgent::ResourceType InspectorPageAgent::cachedResourceType(const CachedResource& cachedResource)
275 {
276     switch (cachedResource.type()) {
277     case CachedResource::ImageResource:
278         return InspectorPageAgent::ImageResource;
279 #if ENABLE(SVG_FONTS)
280     case CachedResource::SVGFontResource:
281 #endif
282     case CachedResource::FontResource:
283         return InspectorPageAgent::FontResource;
284 #if ENABLE(XSLT)
285     case CachedResource::XSLStyleSheet:
286 #endif
287     case CachedResource::CSSStyleSheet:
288         return InspectorPageAgent::StylesheetResource;
289     case CachedResource::Script:
290         return InspectorPageAgent::ScriptResource;
291     case CachedResource::MainResource:
292         return InspectorPageAgent::DocumentResource;
293     case CachedResource::MediaResource:
294     case CachedResource::Icon:
295     case CachedResource::RawResource: {
296         switch (cachedResource.resourceRequest().requester()) {
297         case ResourceRequest::Requester::Fetch:
298             return InspectorPageAgent::FetchResource;
299         case ResourceRequest::Requester::Main:
300             return InspectorPageAgent::DocumentResource;
301         default:
302             return InspectorPageAgent::XHRResource;
303         }
304     }
305     default:
306         break;
307     }
308     return InspectorPageAgent::OtherResource;
309 }
310
311 Inspector::Protocol::Page::ResourceType InspectorPageAgent::cachedResourceTypeJson(const CachedResource& cachedResource)
312 {
313     return resourceTypeJson(cachedResourceType(cachedResource));
314 }
315
316 RefPtr<TextResourceDecoder> InspectorPageAgent::createTextDecoder(const String& mimeType, const String& textEncodingName)
317 {
318     if (!textEncodingName.isEmpty())
319         return TextResourceDecoder::create(ASCIILiteral("text/plain"), textEncodingName);
320
321     if (MIMETypeRegistry::isTextMIMEType(mimeType))
322         return TextResourceDecoder::create(mimeType, "UTF-8");
323
324     if (MIMETypeRegistry::isXMLMIMEType(mimeType)) {
325         RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create(ASCIILiteral("application/xml"));
326         decoder->useLenientXMLDecoding();
327         return decoder;
328     }
329
330     return TextResourceDecoder::create(ASCIILiteral("text/plain"), "UTF-8");
331 }
332
333 InspectorPageAgent::InspectorPageAgent(PageAgentContext& context, InspectorClient* client, InspectorOverlay* overlay)
334     : InspectorAgentBase(ASCIILiteral("Page"), context)
335     , m_frontendDispatcher(std::make_unique<Inspector::PageFrontendDispatcher>(context.frontendRouter))
336     , m_backendDispatcher(Inspector::PageBackendDispatcher::create(context.backendDispatcher, this))
337     , m_page(context.inspectedPage)
338     , m_client(client)
339     , m_overlay(overlay)
340 {
341 }
342
343 void InspectorPageAgent::didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*)
344 {
345 }
346
347 void InspectorPageAgent::willDestroyFrontendAndBackend(Inspector::DisconnectReason)
348 {
349     ErrorString unused;
350     disable(unused);
351 }
352
353 double InspectorPageAgent::timestamp()
354 {
355     return m_environment.executionStopwatch()->elapsedTime();
356 }
357
358 void InspectorPageAgent::enable(ErrorString&)
359 {
360     m_enabled = true;
361     m_instrumentingAgents.setInspectorPageAgent(this);
362
363     auto stopwatch = m_environment.executionStopwatch();
364     stopwatch->reset();
365     stopwatch->start();
366 }
367
368 void InspectorPageAgent::disable(ErrorString&)
369 {
370     m_enabled = false;
371     m_instrumentingAgents.setInspectorPageAgent(nullptr);
372
373     ErrorString unused;
374     setShowPaintRects(unused, false);
375     setEmulatedMedia(unused, emptyString());
376 }
377
378 void InspectorPageAgent::reload(ErrorString&, const bool* const optionalReloadFromOrigin, const bool* const optionalRevalidateAllResources)
379 {
380     bool reloadFromOrigin = optionalReloadFromOrigin && *optionalReloadFromOrigin;
381     bool revalidateAllResources = optionalRevalidateAllResources && *optionalRevalidateAllResources;
382
383     OptionSet<ReloadOption> reloadOptions;
384     if (reloadFromOrigin)
385         reloadOptions |= ReloadOption::FromOrigin;
386     if (!revalidateAllResources)
387         reloadOptions |= ReloadOption::ExpiredOnly;
388
389     m_page.mainFrame().loader().reload(reloadOptions);
390 }
391
392 void InspectorPageAgent::navigate(ErrorString&, const String& url)
393 {
394     UserGestureIndicator indicator { ProcessingUserGesture };
395     Frame& frame = m_page.mainFrame();
396
397     ResourceRequest resourceRequest { frame.document()->completeURL(url) };
398     FrameLoadRequest frameLoadRequest { *frame.document(), frame.document()->securityOrigin(), resourceRequest, ASCIILiteral("_self"), LockHistory::No, LockBackForwardList::No, MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, ShouldOpenExternalURLsPolicy::ShouldNotAllow, InitiatedByMainFrame::Unknown };
399     frame.loader().changeLocation(WTFMove(frameLoadRequest));
400 }
401
402 static Ref<Inspector::Protocol::Page::Cookie> buildObjectForCookie(const Cookie& cookie)
403 {
404     return Inspector::Protocol::Page::Cookie::create()
405         .setName(cookie.name)
406         .setValue(cookie.value)
407         .setDomain(cookie.domain)
408         .setPath(cookie.path)
409         .setExpires(cookie.expires)
410         .setSize((cookie.name.length() + cookie.value.length()))
411         .setHttpOnly(cookie.httpOnly)
412         .setSecure(cookie.secure)
413         .setSession(cookie.session)
414         .release();
415 }
416
417 static Ref<Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>> buildArrayForCookies(ListHashSet<Cookie>& cookiesList)
418 {
419     auto cookies = Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>::create();
420
421     for (const auto& cookie : cookiesList)
422         cookies->addItem(buildObjectForCookie(cookie));
423
424     return cookies;
425 }
426
427 static Vector<CachedResource*> cachedResourcesForFrame(Frame* frame)
428 {
429     Vector<CachedResource*> result;
430
431     for (auto& cachedResourceHandle : frame->document()->cachedResourceLoader().allCachedResources().values()) {
432         auto* cachedResource = cachedResourceHandle.get();
433         if (cachedResource->resourceRequest().hiddenFromInspector())
434             continue;
435
436         switch (cachedResource->type()) {
437         case CachedResource::ImageResource:
438             // Skip images that were not auto loaded (images disabled in the user agent).
439 #if ENABLE(SVG_FONTS)
440         case CachedResource::SVGFontResource:
441 #endif
442         case CachedResource::FontResource:
443             // Skip fonts that were referenced in CSS but never used/downloaded.
444             if (cachedResource->stillNeedsLoad())
445                 continue;
446             break;
447         default:
448             // All other CachedResource types download immediately.
449             break;
450         }
451
452         result.append(cachedResource);
453     }
454
455     return result;
456 }
457
458 static Vector<URL> allResourcesURLsForFrame(Frame* frame)
459 {
460     Vector<URL> result;
461
462     result.append(frame->loader().documentLoader()->url());
463
464     for (auto* cachedResource : cachedResourcesForFrame(frame))
465         result.append(cachedResource->url());
466
467     return result;
468 }
469
470 void InspectorPageAgent::getCookies(ErrorString&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>>& cookies)
471 {
472     // If we can get raw cookies.
473     ListHashSet<Cookie> rawCookiesList;
474
475     // If we can't get raw cookies - fall back to String representation
476     StringBuilder stringCookiesList;
477
478     // Return value to getRawCookies should be the same for every call because
479     // the return value is platform/network backend specific, and the call will
480     // always return the same true/false value.
481     bool rawCookiesImplemented = false;
482
483     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
484         Document* document = frame->document();
485         if (!document)
486             continue;
487
488         for (auto& url : allResourcesURLsForFrame(frame)) {
489             Vector<Cookie> docCookiesList;
490             rawCookiesImplemented = getRawCookies(*document, URL(ParsedURLString, url), docCookiesList);
491
492             if (!rawCookiesImplemented) {
493                 // FIXME: We need duplication checking for the String representation of cookies.
494                 // Exceptions are thrown by cookie() in sandboxed frames. That won't happen here
495                 // because "document" is the document of the main frame of the page.
496                 stringCookiesList.append(document->cookie().releaseReturnValue());
497             } else {
498                 for (auto& cookie : docCookiesList) {
499                     if (!rawCookiesList.contains(cookie))
500                         rawCookiesList.add(cookie);
501                 }
502             }
503         }
504     }
505
506     // FIXME: Do not return empty string/empty array. Make returns optional instead. https://bugs.webkit.org/show_bug.cgi?id=80855
507     if (rawCookiesImplemented)
508         cookies = buildArrayForCookies(rawCookiesList);
509     else
510         cookies = Inspector::Protocol::Array<Inspector::Protocol::Page::Cookie>::create();
511 }
512
513 void InspectorPageAgent::deleteCookie(ErrorString&, const String& cookieName, const String& url)
514 {
515     URL parsedURL(ParsedURLString, url);
516     for (Frame* frame = &m_page.mainFrame(); frame; frame = frame->tree().traverseNext()) {
517         if (auto* document = frame->document())
518             WebCore::deleteCookie(*document, parsedURL, cookieName);
519     }
520 }
521
522 void InspectorPageAgent::getResourceTree(ErrorString&, RefPtr<Inspector::Protocol::Page::FrameResourceTree>& object)
523 {
524     object = buildObjectForFrameTree(&m_page.mainFrame());
525 }
526
527 void InspectorPageAgent::getResourceContent(ErrorString& errorString, const String& frameId, const String& url, String* content, bool* base64Encoded)
528 {
529     Frame* frame = assertFrame(errorString, frameId);
530     if (!frame)
531         return;
532
533     resourceContent(errorString, frame, URL(ParsedURLString, url), content, base64Encoded);
534 }
535
536 static bool textContentForCachedResource(CachedResource* cachedResource, String* result)
537 {
538     if (hasTextContent(cachedResource)) {
539         bool base64Encoded;
540         if (InspectorPageAgent::cachedResourceContent(cachedResource, result, &base64Encoded)) {
541             ASSERT(!base64Encoded);
542             return true;
543         }
544     }
545     return false;
546 }
547
548 void InspectorPageAgent::searchInResource(ErrorString& errorString, const String& frameId, const String& url, const String& query, const bool* const optionalCaseSensitive, const bool* const optionalIsRegex, const String* optionalRequestId, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>>& results)
549 {
550     results = Inspector::Protocol::Array<Inspector::Protocol::GenericTypes::SearchMatch>::create();
551
552     bool isRegex = optionalIsRegex ? *optionalIsRegex : false;
553     bool caseSensitive = optionalCaseSensitive ? *optionalCaseSensitive : false;
554
555     if (optionalRequestId) {
556         if (InspectorNetworkAgent* networkAgent = m_instrumentingAgents.inspectorNetworkAgent()) {
557             networkAgent->searchInRequest(errorString, *optionalRequestId, query, caseSensitive, isRegex, results);
558             return;
559         }
560     }
561
562     Frame* frame = assertFrame(errorString, frameId);
563     if (!frame)
564         return;
565
566     DocumentLoader* loader = assertDocumentLoader(errorString, frame);
567     if (!loader)
568         return;
569
570     URL kurl(ParsedURLString, url);
571
572     String content;
573     bool success = false;
574     if (equalIgnoringFragmentIdentifier(kurl, loader->url()))
575         success = mainResourceContent(frame, false, &content);
576
577     if (!success) {
578         CachedResource* resource = cachedResource(frame, kurl);
579         if (resource)
580             success = textContentForCachedResource(resource, &content);
581     }
582
583     if (!success)
584         return;
585
586     results = ContentSearchUtilities::searchInTextByLines(content, query, caseSensitive, isRegex);
587 }
588
589 static Ref<Inspector::Protocol::Page::SearchResult> buildObjectForSearchResult(const String& frameId, const String& url, int matchesCount)
590 {
591     return Inspector::Protocol::Page::SearchResult::create()
592         .setUrl(url)
593         .setFrameId(frameId)
594         .setMatchesCount(matchesCount)
595         .release();
596 }
597
598 void InspectorPageAgent::searchInResources(ErrorString&, const String& text, const bool* const optionalCaseSensitive, const bool* const optionalIsRegex, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::SearchResult>>& result)
599 {
600     result = Inspector::Protocol::Array<Inspector::Protocol::Page::SearchResult>::create();
601
602     bool isRegex = optionalIsRegex ? *optionalIsRegex : false;
603     bool caseSensitive = optionalCaseSensitive ? *optionalCaseSensitive : false;
604     JSC::Yarr::RegularExpression regex = ContentSearchUtilities::createSearchRegex(text, caseSensitive, isRegex);
605
606     for (Frame* frame = &m_page.mainFrame(); frame; frame = frame->tree().traverseNext()) {
607         String content;
608
609         for (auto* cachedResource : cachedResourcesForFrame(frame)) {
610             if (textContentForCachedResource(cachedResource, &content)) {
611                 int matchesCount = ContentSearchUtilities::countRegularExpressionMatches(regex, content);
612                 if (matchesCount)
613                     result->addItem(buildObjectForSearchResult(frameId(frame), cachedResource->url(), matchesCount));
614             }
615         }
616
617         if (mainResourceContent(frame, false, &content)) {
618             int matchesCount = ContentSearchUtilities::countRegularExpressionMatches(regex, content);
619             if (matchesCount)
620                 result->addItem(buildObjectForSearchResult(frameId(frame), frame->document()->url(), matchesCount));
621         }
622     }
623
624     if (InspectorNetworkAgent* networkAgent = m_instrumentingAgents.inspectorNetworkAgent())
625         networkAgent->searchOtherRequests(regex, result);
626 }
627
628 void InspectorPageAgent::setShowPaintRects(ErrorString&, bool show)
629 {
630     m_showPaintRects = show;
631     m_client->setShowPaintRects(show);
632
633     if (m_client->overridesShowPaintRects())
634         return;
635
636     m_overlay->setShowingPaintRects(show);
637 }
638
639 void InspectorPageAgent::domContentEventFired()
640 {
641     m_isFirstLayoutAfterOnLoad = true;
642     m_frontendDispatcher->domContentEventFired(timestamp());
643 }
644
645 void InspectorPageAgent::loadEventFired()
646 {
647     m_frontendDispatcher->loadEventFired(timestamp());
648 }
649
650 void InspectorPageAgent::frameNavigated(Frame& frame)
651 {
652     m_frontendDispatcher->frameNavigated(buildObjectForFrame(&frame));
653 }
654
655 void InspectorPageAgent::frameDetached(Frame& frame)
656 {
657     HashMap<Frame*, String>::iterator iterator = m_frameToIdentifier.find(&frame);
658     if (iterator != m_frameToIdentifier.end()) {
659         m_frontendDispatcher->frameDetached(iterator->value);
660         m_identifierToFrame.remove(iterator->value);
661         m_frameToIdentifier.remove(iterator);
662     }
663 }
664
665 MainFrame& InspectorPageAgent::mainFrame()
666 {
667     return m_page.mainFrame();
668 }
669
670 Frame* InspectorPageAgent::frameForId(const String& frameId)
671 {
672     return frameId.isEmpty() ? nullptr : m_identifierToFrame.get(frameId);
673 }
674
675 String InspectorPageAgent::frameId(Frame* frame)
676 {
677     if (!frame)
678         return emptyString();
679     String identifier = m_frameToIdentifier.get(frame);
680     if (identifier.isNull()) {
681         identifier = IdentifiersFactory::createIdentifier();
682         m_frameToIdentifier.set(frame, identifier);
683         m_identifierToFrame.set(identifier, frame);
684     }
685     return identifier;
686 }
687
688 bool InspectorPageAgent::hasIdForFrame(Frame* frame) const
689 {
690     return frame && m_frameToIdentifier.contains(frame);
691 }
692
693 String InspectorPageAgent::loaderId(DocumentLoader* loader)
694 {
695     if (!loader)
696         return emptyString();
697     String identifier = m_loaderToIdentifier.get(loader);
698     if (identifier.isNull()) {
699         identifier = IdentifiersFactory::createIdentifier();
700         m_loaderToIdentifier.set(loader, identifier);
701     }
702     return identifier;
703 }
704
705 Frame* InspectorPageAgent::findFrameWithSecurityOrigin(const String& originRawString)
706 {
707     for (Frame* frame = &m_page.mainFrame(); frame; frame = frame->tree().traverseNext()) {
708         Ref<SecurityOrigin> documentOrigin = frame->document()->securityOrigin();
709         if (documentOrigin->toRawString() == originRawString)
710             return frame;
711     }
712     return nullptr;
713 }
714
715 Frame* InspectorPageAgent::assertFrame(ErrorString& errorString, const String& frameId)
716 {
717     Frame* frame = frameForId(frameId);
718     if (!frame)
719         errorString = ASCIILiteral("No frame for given id found");
720     return frame;
721 }
722
723 DocumentLoader* InspectorPageAgent::assertDocumentLoader(ErrorString& errorString, Frame* frame)
724 {
725     FrameLoader& frameLoader = frame->loader();
726     DocumentLoader* documentLoader = frameLoader.documentLoader();
727     if (!documentLoader)
728         errorString = ASCIILiteral("No documentLoader for given frame found");
729     return documentLoader;
730 }
731
732 void InspectorPageAgent::loaderDetachedFromFrame(DocumentLoader& loader)
733 {
734     m_loaderToIdentifier.remove(&loader);
735 }
736
737 void InspectorPageAgent::frameStartedLoading(Frame& frame)
738 {
739     m_frontendDispatcher->frameStartedLoading(frameId(&frame));
740 }
741
742 void InspectorPageAgent::frameStoppedLoading(Frame& frame)
743 {
744     m_frontendDispatcher->frameStoppedLoading(frameId(&frame));
745 }
746
747 void InspectorPageAgent::frameScheduledNavigation(Frame& frame, Seconds delay)
748 {
749     m_frontendDispatcher->frameScheduledNavigation(frameId(&frame), delay.value());
750 }
751
752 void InspectorPageAgent::frameClearedScheduledNavigation(Frame& frame)
753 {
754     m_frontendDispatcher->frameClearedScheduledNavigation(frameId(&frame));
755 }
756
757 void InspectorPageAgent::didPaint(RenderObject& renderer, const LayoutRect& rect)
758 {
759     if (!m_enabled || !m_showPaintRects)
760         return;
761
762     LayoutRect absoluteRect = LayoutRect(renderer.localToAbsoluteQuad(FloatRect(rect)).boundingBox());
763     FrameView* view = renderer.document().view();
764
765     LayoutRect rootRect = absoluteRect;
766     if (!view->frame().isMainFrame()) {
767         IntRect rootViewRect = view->contentsToRootView(snappedIntRect(absoluteRect));
768         rootRect = view->frame().mainFrame().view()->rootViewToContents(rootViewRect);
769     }
770
771     if (m_client->overridesShowPaintRects()) {
772         m_client->showPaintRect(rootRect);
773         return;
774     }
775
776     m_overlay->showPaintRect(rootRect);
777 }
778
779 void InspectorPageAgent::didLayout()
780 {
781     bool isFirstLayout = m_isFirstLayoutAfterOnLoad;
782     if (isFirstLayout)
783         m_isFirstLayoutAfterOnLoad = false;
784
785     if (!m_enabled)
786         return;
787
788     m_overlay->update();
789 }
790
791 void InspectorPageAgent::didScroll()
792 {
793     if (m_enabled)
794         m_overlay->update();
795 }
796
797 void InspectorPageAgent::didRecalculateStyle()
798 {
799     if (m_enabled)
800         m_overlay->update();
801 }
802
803 Ref<Inspector::Protocol::Page::Frame> InspectorPageAgent::buildObjectForFrame(Frame* frame)
804 {
805     ASSERT_ARG(frame, frame);
806
807     auto frameObject = Inspector::Protocol::Page::Frame::create()
808         .setId(frameId(frame))
809         .setLoaderId(loaderId(frame->loader().documentLoader()))
810         .setUrl(frame->document()->url().string())
811         .setMimeType(frame->loader().documentLoader()->responseMIMEType())
812         .setSecurityOrigin(frame->document()->securityOrigin().toRawString())
813         .release();
814     if (frame->tree().parent())
815         frameObject->setParentId(frameId(frame->tree().parent()));
816     if (frame->ownerElement()) {
817         String name = frame->ownerElement()->getNameAttribute();
818         if (name.isEmpty())
819             name = frame->ownerElement()->attributeWithoutSynchronization(HTMLNames::idAttr);
820         frameObject->setName(name);
821     }
822
823     return frameObject;
824 }
825
826 Ref<Inspector::Protocol::Page::FrameResourceTree> InspectorPageAgent::buildObjectForFrameTree(Frame* frame)
827 {
828     ASSERT_ARG(frame, frame);
829
830     Ref<Inspector::Protocol::Page::Frame> frameObject = buildObjectForFrame(frame);
831     auto subresources = Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResource>::create();
832     auto result = Inspector::Protocol::Page::FrameResourceTree::create()
833         .setFrame(WTFMove(frameObject))
834         .setResources(subresources.copyRef())
835         .release();
836
837     for (auto* cachedResource : cachedResourcesForFrame(frame)) {
838         auto resourceObject = Inspector::Protocol::Page::FrameResource::create()
839             .setUrl(cachedResource->url())
840             .setType(cachedResourceTypeJson(*cachedResource))
841             .setMimeType(cachedResource->response().mimeType())
842             .release();
843         if (cachedResource->wasCanceled())
844             resourceObject->setCanceled(true);
845         else if (cachedResource->status() == CachedResource::LoadError || cachedResource->status() == CachedResource::DecodeError)
846             resourceObject->setFailed(true);
847         String sourceMappingURL = InspectorPageAgent::sourceMapURLForResource(cachedResource);
848         if (!sourceMappingURL.isEmpty())
849             resourceObject->setSourceMapURL(sourceMappingURL);
850         String targetId = cachedResource->resourceRequest().initiatorIdentifier();
851         if (!targetId.isEmpty())
852             resourceObject->setTargetId(targetId);
853         subresources->addItem(WTFMove(resourceObject));
854     }
855
856     RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResourceTree>> childrenArray;
857     for (Frame* child = frame->tree().firstChild(); child; child = child->tree().nextSibling()) {
858         if (!childrenArray) {
859             childrenArray = Inspector::Protocol::Array<Inspector::Protocol::Page::FrameResourceTree>::create();
860             result->setChildFrames(childrenArray);
861         }
862         childrenArray->addItem(buildObjectForFrameTree(child));
863     }
864     return result;
865 }
866
867 void InspectorPageAgent::setEmulatedMedia(ErrorString&, const String& media)
868 {
869     if (media == m_emulatedMedia)
870         return;
871
872     m_emulatedMedia = media;
873     Document* document = m_page.mainFrame().document();
874     if (document) {
875         document->styleScope().didChangeStyleSheetEnvironment();
876         document->updateLayout();
877     }
878 }
879
880 void InspectorPageAgent::applyEmulatedMedia(String& media)
881 {
882     if (!m_emulatedMedia.isEmpty())
883         media = m_emulatedMedia;
884 }
885
886 void InspectorPageAgent::getCompositingBordersVisible(ErrorString&, bool* outParam)
887 {
888     *outParam = m_page.settings().showDebugBorders() || m_page.settings().showRepaintCounter();
889 }
890
891 void InspectorPageAgent::setCompositingBordersVisible(ErrorString&, bool visible)
892 {
893     m_page.settings().setShowDebugBorders(visible);
894     m_page.settings().setShowRepaintCounter(visible);
895 }
896
897 void InspectorPageAgent::snapshotNode(ErrorString& errorString, int nodeId, String* outDataURL)
898 {
899     Frame& frame = mainFrame();
900
901     InspectorDOMAgent* domAgent = m_instrumentingAgents.inspectorDOMAgent();
902     ASSERT(domAgent);
903     Node* node = domAgent->assertNode(errorString, nodeId);
904     if (!node)
905         return;
906
907     std::unique_ptr<ImageBuffer> snapshot = WebCore::snapshotNode(frame, *node);
908     if (!snapshot) {
909         errorString = ASCIILiteral("Could not capture snapshot");
910         return;
911     }
912
913     *outDataURL = snapshot->toDataURL(ASCIILiteral("image/png"));
914 }
915
916 void InspectorPageAgent::snapshotRect(ErrorString& errorString, int x, int y, int width, int height, const String& coordinateSystem, String* outDataURL)
917 {
918     Frame& frame = mainFrame();
919
920     SnapshotOptions options = SnapshotOptionsNone;
921     if (coordinateSystem == "Viewport")
922         options |= SnapshotOptionsInViewCoordinates;
923
924     IntRect rectangle(x, y, width, height);
925     std::unique_ptr<ImageBuffer> snapshot = snapshotFrameRect(frame, rectangle, options);
926
927     if (!snapshot) {
928         errorString = ASCIILiteral("Could not capture snapshot");
929         return;
930     }
931
932     *outDataURL = snapshot->toDataURL(ASCIILiteral("image/png"));
933 }
934
935 void InspectorPageAgent::archive(ErrorString& errorString, String* data)
936 {
937 #if ENABLE(WEB_ARCHIVE) && USE(CF)
938     Frame& frame = mainFrame();
939     RefPtr<LegacyWebArchive> archive = LegacyWebArchive::create(frame);
940     if (!archive) {
941         errorString = ASCIILiteral("Could not create web archive for main frame");
942         return;
943     }
944
945     RetainPtr<CFDataRef> buffer = archive->rawDataRepresentation();
946     *data = base64Encode(CFDataGetBytePtr(buffer.get()), CFDataGetLength(buffer.get()));
947 #else
948     UNUSED_PARAM(data);
949     errorString = ASCIILiteral("No support for creating archives");
950 #endif
951 }
952
953 } // namespace WebCore