| 1 | /* |
| 2 | * Copyright (C) 2019 Apple 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 |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "InspectorAuditResourcesObject.h" |
| 28 | |
| 29 | #include "CachedCSSStyleSheet.h" |
| 30 | #include "CachedFont.h" |
| 31 | #include "CachedImage.h" |
| 32 | #include "CachedRawResource.h" |
| 33 | #include "CachedResource.h" |
| 34 | #include "CachedSVGDocument.h" |
| 35 | #include "Document.h" |
| 36 | #include "InspectorPageAgent.h" |
| 37 | #include <wtf/HashMap.h> |
| 38 | #include <wtf/Vector.h> |
| 39 | #include <wtf/text/WTFString.h> |
| 40 | |
| 41 | namespace WebCore { |
| 42 | |
| 43 | using namespace Inspector; |
| 44 | |
| 45 | #define ERROR_IF_NO_ACTIVE_AUDIT() \ |
| 46 | if (!m_auditAgent.hasActiveAudit()) \ |
| 47 | return Exception { NotAllowedError, "Cannot be called outside of a Web Inspector Audit"_s }; |
| 48 | |
| 49 | InspectorAuditResourcesObject::InspectorAuditResourcesObject(InspectorAuditAgent& auditAgent) |
| 50 | : m_auditAgent(auditAgent) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | InspectorAuditResourcesObject::~InspectorAuditResourcesObject() |
| 55 | { |
| 56 | for (auto* cachedResource : m_resources.values()) |
| 57 | cachedResource->removeClient(clientForResource(*cachedResource)); |
| 58 | } |
| 59 | |
| 60 | ExceptionOr<Vector<InspectorAuditResourcesObject::Resource>> InspectorAuditResourcesObject::getResources(Document& document) |
| 61 | { |
| 62 | ERROR_IF_NO_ACTIVE_AUDIT(); |
| 63 | |
| 64 | Vector<Resource> resources; |
| 65 | |
| 66 | auto* frame = document.frame(); |
| 67 | if (!frame) |
| 68 | return Exception { NotAllowedError, "Cannot be called with a detached document"_s }; |
| 69 | |
| 70 | for (auto* cachedResource : InspectorPageAgent::cachedResourcesForFrame(frame)) { |
| 71 | Resource resource; |
| 72 | resource.url = cachedResource->url(); |
| 73 | resource.mimeType = cachedResource->mimeType(); |
| 74 | |
| 75 | bool exists = false; |
| 76 | for (const auto& entry : m_resources) { |
| 77 | if (entry.value == cachedResource) { |
| 78 | resource.id = entry.key; |
| 79 | exists = true; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | if (!exists) { |
| 84 | cachedResource->addClient(clientForResource(*cachedResource)); |
| 85 | |
| 86 | resource.id = String::number(m_resources.size() + 1); |
| 87 | m_resources.add(resource.id, cachedResource); |
| 88 | } |
| 89 | |
| 90 | resources.append(WTFMove(resource)); |
| 91 | } |
| 92 | |
| 93 | return resources; |
| 94 | } |
| 95 | |
| 96 | ExceptionOr<InspectorAuditResourcesObject::ResourceContent> InspectorAuditResourcesObject::getResourceContent(Document& document, const String& id) |
| 97 | { |
| 98 | ERROR_IF_NO_ACTIVE_AUDIT(); |
| 99 | |
| 100 | auto* frame = document.frame(); |
| 101 | if (!frame) |
| 102 | return Exception { NotAllowedError, "Cannot be called with a detached document"_s }; |
| 103 | |
| 104 | auto* cachedResource = m_resources.get(id); |
| 105 | if (!cachedResource) |
| 106 | return Exception { NotFoundError, makeString("Unknown identifier "_s , id) }; |
| 107 | |
| 108 | ErrorString errorString; |
| 109 | ResourceContent resourceContent; |
| 110 | InspectorPageAgent::resourceContent(errorString, frame, cachedResource->url(), &resourceContent.data, &resourceContent.base64Encoded); |
| 111 | if (!errorString.isEmpty()) |
| 112 | return Exception { NotFoundError, errorString }; |
| 113 | |
| 114 | return resourceContent; |
| 115 | } |
| 116 | |
| 117 | CachedResourceClient& InspectorAuditResourcesObject::clientForResource(const CachedResource& cachedResource) |
| 118 | { |
| 119 | if (is<CachedCSSStyleSheet>(cachedResource)) |
| 120 | return m_cachedStyleSheetClient; |
| 121 | |
| 122 | if (is<CachedFont>(cachedResource)) |
| 123 | return m_cachedFontClient; |
| 124 | |
| 125 | if (is<CachedImage>(cachedResource)) |
| 126 | return m_cachedImageClient; |
| 127 | |
| 128 | if (is<CachedRawResource>(cachedResource)) |
| 129 | return m_cachedRawResourceClient; |
| 130 | |
| 131 | if (is<CachedSVGDocument>(cachedResource)) |
| 132 | return m_cachedSVGDocumentClient; |
| 133 | |
| 134 | return m_cachedResourceClient; |
| 135 | } |
| 136 | |
| 137 | } // namespace WebCore |
| 138 | |