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