| 1 | /* |
| 2 | * Copyright (C) 2013-2016 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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(CONTENT_FILTERING) |
| 29 | |
| 30 | #include "CachedResourceHandle.h" |
| 31 | #include "PlatformContentFilter.h" |
| 32 | #include "ResourceError.h" |
| 33 | #include <functional> |
| 34 | #include <wtf/Forward.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | class CachedRawResource; |
| 39 | class DocumentLoader; |
| 40 | class ResourceRequest; |
| 41 | class ResourceResponse; |
| 42 | class SubstituteData; |
| 43 | |
| 44 | class ContentFilter { |
| 45 | WTF_MAKE_FAST_ALLOCATED; |
| 46 | WTF_MAKE_NONCOPYABLE(ContentFilter); |
| 47 | |
| 48 | public: |
| 49 | template <typename T> static void addType() { types().append(type<T>()); } |
| 50 | |
| 51 | static std::unique_ptr<ContentFilter> create(DocumentLoader&); |
| 52 | ~ContentFilter(); |
| 53 | |
| 54 | static const char* urlScheme() { return "x-apple-content-filter" ; } |
| 55 | |
| 56 | void startFilteringMainResource(CachedRawResource&); |
| 57 | void stopFilteringMainResource(); |
| 58 | |
| 59 | bool continueAfterWillSendRequest(ResourceRequest&, const ResourceResponse&); |
| 60 | bool continueAfterResponseReceived(const ResourceResponse&); |
| 61 | bool continueAfterDataReceived(const char* data, int length); |
| 62 | bool continueAfterNotifyFinished(CachedResource&); |
| 63 | |
| 64 | static bool continueAfterSubstituteDataRequest(const DocumentLoader& activeLoader, const SubstituteData&); |
| 65 | void handleProvisionalLoadFailure(const ResourceError&); |
| 66 | |
| 67 | private: |
| 68 | using State = PlatformContentFilter::State; |
| 69 | |
| 70 | struct Type { |
| 71 | const std::function<std::unique_ptr<PlatformContentFilter>()> create; |
| 72 | }; |
| 73 | template <typename T> static Type type(); |
| 74 | WEBCORE_EXPORT static Vector<Type>& types(); |
| 75 | |
| 76 | using Container = Vector<std::unique_ptr<PlatformContentFilter>>; |
| 77 | friend std::unique_ptr<ContentFilter> std::make_unique<ContentFilter>(Container&&, DocumentLoader&); |
| 78 | ContentFilter(Container&&, DocumentLoader&); |
| 79 | |
| 80 | template <typename Function> void forEachContentFilterUntilBlocked(Function&&); |
| 81 | void didDecide(State); |
| 82 | void deliverResourceData(CachedResource&); |
| 83 | |
| 84 | const Container m_contentFilters; |
| 85 | DocumentLoader& m_documentLoader; |
| 86 | CachedResourceHandle<CachedRawResource> m_mainResource; |
| 87 | PlatformContentFilter* m_blockingContentFilter { nullptr }; |
| 88 | State m_state { State::Stopped }; |
| 89 | ResourceError m_blockedError; |
| 90 | bool m_isLoadingBlockedPage { false }; |
| 91 | }; |
| 92 | |
| 93 | template <typename T> |
| 94 | ContentFilter::Type ContentFilter::type() |
| 95 | { |
| 96 | static_assert(std::is_base_of<PlatformContentFilter, T>::value, "Type must be a PlatformContentFilter." ); |
| 97 | return { T::create }; |
| 98 | } |
| 99 | |
| 100 | } // namespace WebCore |
| 101 | |
| 102 | #endif // ENABLE(CONTENT_FILTERING) |
| 103 | |