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#pragma once
27
28#if ENABLE(CONTENT_EXTENSIONS)
29
30#include "APIObject.h"
31#include <system_error>
32#include <wtf/text/WTFString.h>
33
34namespace WebCore {
35class SharedBuffer;
36}
37
38namespace WTF {
39class WorkQueue;
40}
41
42namespace API {
43
44class ContentRuleList;
45
46class ContentRuleListStore final : public ObjectImpl<Object::Type::ContentRuleListStore> {
47public:
48 enum class Error {
49 LookupFailed = 1,
50 VersionMismatch,
51 CompileFailed,
52 RemoveFailed
53 };
54
55 // This should be incremented every time a functional change is made to the bytecode, file format, etc.
56 // to prevent crashing while loading old data.
57 // Also update ContentRuleListStore::getContentRuleListSource to be able to find the original JSON
58 // source from old versions.
59 // Update ContentRuleListStore::getContentRuleListSource with this.
60 const static uint32_t CurrentContentRuleListFileVersion = 10;
61
62 static ContentRuleListStore& defaultStore(bool legacyFilename);
63 static Ref<ContentRuleListStore> storeWithPath(const WTF::String& storePath, bool legacyFilename);
64
65 explicit ContentRuleListStore(bool legacyFilename);
66 explicit ContentRuleListStore(const WTF::String& storePath, bool legacyFilename);
67 virtual ~ContentRuleListStore();
68
69 void compileContentRuleList(const WTF::String& identifier, WTF::String&& json, CompletionHandler<void(RefPtr<API::ContentRuleList>, std::error_code)>);
70 void lookupContentRuleList(const WTF::String& identifier, CompletionHandler<void(RefPtr<API::ContentRuleList>, std::error_code)>);
71 void removeContentRuleList(const WTF::String& identifier, CompletionHandler<void(std::error_code)>);
72 void getAvailableContentRuleListIdentifiers(CompletionHandler<void(WTF::Vector<WTF::String>)>);
73
74 // For testing only.
75 void synchronousRemoveAllContentRuleLists();
76 void invalidateContentRuleListVersion(const WTF::String& identifier);
77 void getContentRuleListSource(const WTF::String& identifier, CompletionHandler<void(WTF::String)>);
78
79private:
80 WTF::String defaultStorePath(bool legacyFilename);
81 static ContentRuleListStore& legacyDefaultStore();
82 static ContentRuleListStore& nonLegacyDefaultStore();
83
84 const WTF::String m_storePath;
85 Ref<WTF::WorkQueue> m_compileQueue;
86 Ref<WTF::WorkQueue> m_readQueue;
87 Ref<WTF::WorkQueue> m_removeQueue;
88 bool m_legacyFilename { false };
89};
90
91const std::error_category& contentRuleListStoreErrorCategory();
92
93inline std::error_code make_error_code(ContentRuleListStore::Error error)
94{
95 return { static_cast<int>(error), contentRuleListStoreErrorCategory() };
96}
97
98} // namespace API
99
100namespace std {
101template<> struct is_error_code_enum<API::ContentRuleListStore::Error> : public true_type { };
102}
103
104#endif // ENABLE(CONTENT_EXTENSIONS)
105