1/*
2 * Copyright (C) 2015 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 "WKUserContentExtensionStoreRef.h"
28
29#include "APIContentRuleList.h"
30#include "APIContentRuleListStore.h"
31#include "WKAPICast.h"
32#include <wtf/CompletionHandler.h>
33
34using namespace WebKit;
35
36WKTypeID WKUserContentExtensionStoreGetTypeID()
37{
38#if ENABLE(CONTENT_EXTENSIONS)
39 return toAPI(API::ContentRuleListStore::APIType);
40#else
41 return 0;
42#endif
43}
44
45WKUserContentExtensionStoreRef WKUserContentExtensionStoreCreate(WKStringRef path)
46{
47#if ENABLE(CONTENT_EXTENSIONS)
48 return toAPI(&API::ContentRuleListStore::storeWithPath(toWTFString(path), false).leakRef());
49#else
50 UNUSED_PARAM(path);
51 return nullptr;
52#endif
53}
54
55#if ENABLE(CONTENT_EXTENSIONS)
56static inline WKUserContentExtensionStoreResult toResult(const std::error_code& error)
57{
58 if (!error)
59 return kWKUserContentExtensionStoreSuccess;
60
61 switch (static_cast<API::ContentRuleListStore::Error>(error.value())) {
62 case API::ContentRuleListStore::Error::LookupFailed:
63 return kWKUserContentExtensionStoreLookupFailed;
64 case API::ContentRuleListStore::Error::VersionMismatch:
65 return kWKUserContentExtensionStoreVersionMismatch;
66 case API::ContentRuleListStore::Error::CompileFailed:
67 return kWKUserContentExtensionStoreCompileFailed;
68 case API::ContentRuleListStore::Error::RemoveFailed:
69 return kWKUserContentExtensionStoreRemoveFailed;
70 }
71
72 RELEASE_ASSERT_NOT_REACHED();
73}
74#endif
75
76void WKUserContentExtensionStoreCompile(WKUserContentExtensionStoreRef store, WKStringRef identifier, WKStringRef jsonSource, void* context, WKUserContentExtensionStoreFunction callback)
77{
78#if ENABLE(CONTENT_EXTENSIONS)
79 toImpl(store)->compileContentRuleList(toWTFString(identifier), toWTFString(jsonSource), [context, callback](RefPtr<API::ContentRuleList> contentRuleList, std::error_code error) {
80 callback(error ? nullptr : toAPI(contentRuleList.leakRef()), toResult(error), context);
81 });
82#else
83 UNUSED_PARAM(jsonSource);
84 callback(nullptr, kWKUserContentExtensionStoreCompileFailed, context);
85#endif
86}
87
88void WKUserContentExtensionStoreLookup(WKUserContentExtensionStoreRef store, WKStringRef identifier, void* context, WKUserContentExtensionStoreFunction callback)
89{
90#if ENABLE(CONTENT_EXTENSIONS)
91 toImpl(store)->lookupContentRuleList(toWTFString(identifier), [context, callback](RefPtr<API::ContentRuleList> contentRuleList, std::error_code error) {
92 callback(error ? nullptr : toAPI(contentRuleList.leakRef()), toResult(error), context);
93 });
94#else
95 callback(nullptr, kWKUserContentExtensionStoreLookupFailed, context);
96#endif
97}
98
99void WKUserContentExtensionStoreRemove(WKUserContentExtensionStoreRef store, WKStringRef identifier, void* context, WKUserContentExtensionStoreFunction callback)
100{
101#if ENABLE(CONTENT_EXTENSIONS)
102 toImpl(store)->removeContentRuleList(toWTFString(identifier), [context, callback](std::error_code error) {
103 callback(nullptr, toResult(error), context);
104 });
105#else
106 callback(nullptr, kWKUserContentExtensionStoreRemoveFailed, context);
107#endif
108}
109