1/*
2 * Copyright (C) 2013 Samsung Electronics. 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 "ewk_page_group.h"
28
29#include "WKAPICast.h"
30#include "WKArray.h"
31#include "WKArrayEfl.h"
32#include "WKPageGroup.h"
33#include "WKString.h"
34#include "ewk_page_group_private.h"
35#include "ewk_settings_private.h"
36#include <wtf/NeverDestroyed.h>
37
38using namespace WebKit;
39
40typedef HashMap<WKPageGroupRef, EwkPageGroup*> PageGroupMap;
41
42static inline PageGroupMap& pageGroupMap()
43{
44    static NeverDestroyed<PageGroupMap> map;
45    return map;
46}
47
48static WKTypeRef convertFromCharToWKString(void* data)
49{
50    return WKStringCreateWithUTF8CString(static_cast<char*>(data));
51}
52
53PassRefPtr<EwkPageGroup> EwkPageGroup::findOrCreateWrapper(WKPageGroupRef pageGroupRef)
54{
55    if (pageGroupMap().contains(pageGroupRef))
56        return pageGroupMap().get(pageGroupRef);
57
58    return adoptRef(new EwkPageGroup(pageGroupRef));
59}
60
61PassRefPtr<EwkPageGroup> EwkPageGroup::create(const char* identifier)
62{
63    WKRetainPtr<WKStringRef> identifierRef = adoptWK(toCopiedAPI(identifier));
64    WKRetainPtr<WKPageGroupRef> pageGroupRef = adoptWK(WKPageGroupCreateWithIdentifier(identifierRef.get()));
65
66    return adoptRef(new EwkPageGroup(pageGroupRef.get()));
67}
68
69EwkPageGroup::EwkPageGroup(WKPageGroupRef pageGroupRef)
70    : m_pageGroupRef(pageGroupRef)
71    , m_settings(std::make_unique<EwkSettings>(WKPageGroupGetPreferences(pageGroupRef)))
72{
73    PageGroupMap::AddResult result = pageGroupMap().add(pageGroupRef, this);
74    ASSERT_UNUSED(result, result.isNewEntry);
75}
76
77EwkPageGroup::~EwkPageGroup()
78{
79    ASSERT(pageGroupMap().get(m_pageGroupRef.get()) == this);
80    pageGroupMap().remove(m_pageGroupRef.get());
81}
82
83void EwkPageGroup::addUserStyleSheet(const String& source, const String& baseURL, Eina_List* whiteList, Eina_List* blackList, bool mainFrameOnly)
84{
85    ASSERT(source);
86
87    WKRetainPtr<WKStringRef> wkSource = adoptWK(toCopiedAPI(source));
88    WKRetainPtr<WKURLRef> wkBaseURL = adoptWK(toCopiedURLAPI(baseURL));
89    WKRetainPtr<WKArrayRef> wkWhitelist = adoptWK(WKArrayCreateWithEinaList(whiteList, convertFromCharToWKString));
90    WKRetainPtr<WKArrayRef> wkBlacklist = adoptWK(WKArrayCreateWithEinaList(blackList, convertFromCharToWKString));
91    WKUserContentInjectedFrames injectedFrames = mainFrameOnly ? kWKInjectInTopFrameOnly : kWKInjectInAllFrames;
92
93    WKPageGroupAddUserStyleSheet(m_pageGroupRef.get(), wkSource.get(), wkBaseURL.get(), wkWhitelist.get(), wkBlacklist.get(), injectedFrames);
94}
95
96void EwkPageGroup::removeAllUserStyleSheets()
97{
98    WKPageGroupRemoveAllUserStyleSheets(m_pageGroupRef.get());
99}
100
101Ewk_Page_Group* ewk_page_group_create(const char* identifier)
102{
103    return EwkPageGroup::create(identifier).leakRef();
104}
105
106Ewk_Settings* ewk_page_group_settings_get(const Ewk_Page_Group* ewkPageGroup)
107{
108    EWK_OBJ_GET_IMPL_OR_RETURN(const EwkPageGroup, ewkPageGroup, impl, nullptr);
109
110    return impl->settings();
111}
112
113Eina_Bool ewk_page_group_user_style_sheet_add(Ewk_Page_Group* ewkPageGroup, const char* source, const char* baseURL, Eina_List* whiteList, Eina_List* blackList, Eina_Bool mainFrameOnly)
114{
115    EWK_OBJ_GET_IMPL_OR_RETURN(EwkPageGroup, ewkPageGroup, impl, false);
116
117    impl->addUserStyleSheet(source, baseURL, whiteList, blackList, mainFrameOnly);
118
119    return true;
120}
121
122Eina_Bool ewk_page_group_user_style_sheets_remove_all(Ewk_Page_Group* ewkPageGroup)
123{
124    EWK_OBJ_GET_IMPL_OR_RETURN(EwkPageGroup, ewkPageGroup, impl, false);
125
126    impl->removeAllUserStyleSheets();
127
128    return true;
129}
130