1/*
2 * Copyright (C) 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#include "config.h"
27#include "UserContentProvider.h"
28
29#include "CustomHeaderFields.h"
30#include "Document.h"
31#include "DocumentLoader.h"
32#include "Frame.h"
33#include "FrameLoader.h"
34#include "Page.h"
35
36#if ENABLE(CONTENT_EXTENSIONS)
37#include "ContentExtensionCompiler.h"
38#include "ContentExtensionsBackend.h"
39#include "ContentRuleListResults.h"
40#endif
41
42namespace WebCore {
43
44UserContentProvider::UserContentProvider()
45{
46}
47
48UserContentProvider::~UserContentProvider()
49{
50 ASSERT(m_pages.isEmpty());
51}
52
53void UserContentProvider::addPage(Page& page)
54{
55 ASSERT(!m_pages.contains(&page));
56
57 m_pages.add(&page);
58}
59
60void UserContentProvider::removePage(Page& page)
61{
62 ASSERT(m_pages.contains(&page));
63
64 m_pages.remove(&page);
65}
66
67void UserContentProvider::registerForUserMessageHandlerInvalidation(UserContentProviderInvalidationClient& invalidationClient)
68{
69 ASSERT(!m_userMessageHandlerInvalidationClients.contains(&invalidationClient));
70
71 m_userMessageHandlerInvalidationClients.add(&invalidationClient);
72}
73
74void UserContentProvider::unregisterForUserMessageHandlerInvalidation(UserContentProviderInvalidationClient& invalidationClient)
75{
76 ASSERT(m_userMessageHandlerInvalidationClients.contains(&invalidationClient));
77
78 m_userMessageHandlerInvalidationClients.remove(&invalidationClient);
79}
80
81void UserContentProvider::invalidateAllRegisteredUserMessageHandlerInvalidationClients()
82{
83 for (auto& client : m_userMessageHandlerInvalidationClients)
84 client->didInvalidate(*this);
85}
86
87void UserContentProvider::invalidateInjectedStyleSheetCacheInAllFramesInAllPages()
88{
89 for (auto& page : m_pages)
90 page->invalidateInjectedStyleSheetCacheInAllFrames();
91}
92
93#if ENABLE(CONTENT_EXTENSIONS)
94static bool contentExtensionsEnabled(const DocumentLoader& documentLoader)
95{
96 if (auto frame = documentLoader.frame()) {
97 if (frame->isMainFrame())
98 return documentLoader.userContentExtensionsEnabled();
99 if (auto mainDocumentLoader = frame->mainFrame().loader().documentLoader())
100 return mainDocumentLoader->userContentExtensionsEnabled();
101 }
102
103 return true;
104}
105
106ContentRuleListResults UserContentProvider::processContentRuleListsForLoad(const URL& url, OptionSet<ContentExtensions::ResourceType> resourceType, DocumentLoader& initiatingDocumentLoader)
107{
108 if (!contentExtensionsEnabled(initiatingDocumentLoader))
109 return { };
110
111 return userContentExtensionBackend().processContentRuleListsForLoad(url, resourceType, initiatingDocumentLoader);
112}
113
114Vector<ContentExtensions::ActionsFromContentRuleList> UserContentProvider::actionsForResourceLoad(const ContentExtensions::ResourceLoadInfo& resourceLoadInfo, DocumentLoader& initiatingDocumentLoader)
115{
116 if (!contentExtensionsEnabled(initiatingDocumentLoader))
117 return { };
118
119 return userContentExtensionBackend().actionsForResourceLoad(resourceLoadInfo);
120}
121
122void UserContentProvider::forEachContentExtension(const WTF::Function<void(const String&, ContentExtensions::ContentExtension&)>& apply, DocumentLoader& initiatingDocumentLoader)
123{
124 if (!contentExtensionsEnabled(initiatingDocumentLoader))
125 return;
126
127 userContentExtensionBackend().forEach(apply);
128}
129
130#endif
131
132} // namespace WebCore
133