Changeset 242860 in webkit
- Timestamp:
- Mar 13, 2019, 1:24:51 AM (7 years ago)
- Location:
- branches/safari-607-branch
- Files:
-
- 23 edited
-
Source/WebKit/ChangeLog (modified) (1 diff)
-
Source/WebKit/NetworkProcess/NetworkContentRuleListManager.cpp (modified) (2 diffs)
-
Source/WebKit/NetworkProcess/NetworkContentRuleListManager.h (modified) (1 diff)
-
Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.cpp (modified) (1 diff)
-
Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.h (modified) (1 diff)
-
Source/WebKit/Shared/WebCompiledContentRuleList.cpp (modified) (4 diffs)
-
Source/WebKit/Shared/WebCompiledContentRuleList.h (modified) (1 diff)
-
Source/WebKit/Shared/WebCompiledContentRuleListData.cpp (modified) (2 diffs)
-
Source/WebKit/Shared/WebCompiledContentRuleListData.h (modified) (3 diffs)
-
Source/WebKit/UIProcess/API/APIContentRuleList.cpp (modified) (1 diff)
-
Source/WebKit/UIProcess/API/APIContentRuleList.h (modified) (1 diff)
-
Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp (modified) (13 diffs)
-
Source/WebKit/UIProcess/API/APIContentRuleListStore.h (modified) (2 diffs)
-
Source/WebKit/UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm (modified) (2 diffs)
-
Source/WebKit/UIProcess/API/Cocoa/WKContentRuleListStore.mm (modified) (2 diffs)
-
Source/WebKit/UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h (modified) (1 diff)
-
Source/WebKit/UIProcess/API/Cocoa/_WKUserContentFilter.mm (modified) (1 diff)
-
Source/WebKit/UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h (modified) (1 diff)
-
Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp (modified) (1 diff)
-
Source/WebKit/WebProcess/UserContent/WebUserContentController.h (modified) (1 diff)
-
Source/WebKit/WebProcess/WebPage/WebPage.cpp (modified) (1 diff)
-
Tools/ChangeLog (modified) (1 diff)
-
Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/safari-607-branch/Source/WebKit/ChangeLog
r242858 r242860 1 2019-03-13 Babak Shafiei <bshafiei@apple.com> 2 3 Cherry-pick r242735. rdar://problem/48780112 4 5 Add a WKContentRuleList variant that uses copied memory instead of mmap'd shared memory for class A containerized apps 6 https://bugs.webkit.org/show_bug.cgi?id=195511 7 <rdar://problem/44873269> 8 9 Patch by Alex Christensen <achristensen@webkit.org> on 2019-03-11 10 Reviewed by Darin Adler. 11 12 Source/WebKit: 13 14 * NetworkProcess/NetworkContentRuleListManager.cpp: 15 (WebKit::NetworkContentRuleListManager::addContentRuleLists): 16 * NetworkProcess/NetworkContentRuleListManager.h: 17 * NetworkProcess/cache/NetworkCacheFileSystem.cpp: 18 (WebKit::NetworkCache::pathRegisteredAsUnsafeToMemoryMapForTesting): 19 (WebKit::NetworkCache::registerPathAsUnsafeToMemoryMapForTesting): 20 (WebKit::NetworkCache::isSafeToUseMemoryMapForPath): 21 * NetworkProcess/cache/NetworkCacheFileSystem.h: 22 * Shared/WebCompiledContentRuleList.cpp: 23 (WebKit::WebCompiledContentRuleList::usesCopiedMemory const): 24 (WebKit::WebCompiledContentRuleList::conditionsApplyOnlyToDomain const): 25 (WebKit::WebCompiledContentRuleList::filtersWithoutConditionsBytecode const): 26 (WebKit::WebCompiledContentRuleList::filtersWithConditionsBytecode const): 27 (WebKit::WebCompiledContentRuleList::topURLFiltersBytecode const): 28 (WebKit::WebCompiledContentRuleList::actions const): 29 * Shared/WebCompiledContentRuleList.h: 30 * Shared/WebCompiledContentRuleListData.cpp: 31 (WebKit::WebCompiledContentRuleListData::size const): 32 (WebKit::WebCompiledContentRuleListData::dataPointer const): 33 (WebKit::WebCompiledContentRuleListData::encode const): 34 (WebKit::WebCompiledContentRuleListData::decode): 35 * Shared/WebCompiledContentRuleListData.h: 36 (WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData): 37 * UIProcess/API/APIContentRuleList.cpp: 38 (API::ContentRuleList::usesCopiedMemory const): 39 * UIProcess/API/APIContentRuleList.h: 40 * UIProcess/API/APIContentRuleListStore.cpp: 41 (API::getData): 42 (API::decodeContentRuleListMetaData): 43 (API::ContentRuleListStore::readContentsOfFile): 44 (API::MappedOrCopiedData::dataPointer const): 45 (API::openAndMapOrCopyContentRuleList): 46 (API::compiledToFile): 47 (API::createExtension): 48 (API::ContentRuleListStore::lookupContentRuleList): 49 (API::ContentRuleListStore::compileContentRuleList): 50 (API::ContentRuleListStore::getContentRuleListSource): 51 (API::openAndMapContentRuleList): Deleted. 52 * UIProcess/API/APIContentRuleListStore.h: 53 * UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm: 54 (API::ContentRuleListStore::readContentsOfFile): 55 * UIProcess/API/Cocoa/WKContentRuleListStore.mm: 56 (+[WKContentRuleListStore _registerPathAsUnsafeToMemoryMapForTesting:]): 57 * UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h: 58 * UIProcess/API/Cocoa/_WKUserContentFilter.mm: 59 (-[_WKUserContentFilter usesCopiedMemory]): 60 * UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h: 61 * WebProcess/UserContent/WebUserContentController.cpp: 62 (WebKit::WebUserContentController::addContentRuleLists): 63 * WebProcess/UserContent/WebUserContentController.h: 64 * WebProcess/WebPage/WebPage.cpp: 65 (WebKit::m_hostFileDescriptor): 66 67 Tools: 68 69 * TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm: 70 (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:startURLSchemeTask:]): 71 (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:stopURLSchemeTask:]): 72 (TEST_F): 73 74 git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242735 268f45cc-cd09-0410-ab3c-d52691b4dbfc 75 76 2019-03-11 Alex Christensen <achristensen@webkit.org> 77 78 Add a WKContentRuleList variant that uses copied memory instead of mmap'd shared memory for class A containerized apps 79 https://bugs.webkit.org/show_bug.cgi?id=195511 80 <rdar://problem/44873269> 81 82 Reviewed by Darin Adler. 83 84 * NetworkProcess/NetworkContentRuleListManager.cpp: 85 (WebKit::NetworkContentRuleListManager::addContentRuleLists): 86 * NetworkProcess/NetworkContentRuleListManager.h: 87 * NetworkProcess/cache/NetworkCacheFileSystem.cpp: 88 (WebKit::NetworkCache::pathRegisteredAsUnsafeToMemoryMapForTesting): 89 (WebKit::NetworkCache::registerPathAsUnsafeToMemoryMapForTesting): 90 (WebKit::NetworkCache::isSafeToUseMemoryMapForPath): 91 * NetworkProcess/cache/NetworkCacheFileSystem.h: 92 * Shared/WebCompiledContentRuleList.cpp: 93 (WebKit::WebCompiledContentRuleList::usesCopiedMemory const): 94 (WebKit::WebCompiledContentRuleList::conditionsApplyOnlyToDomain const): 95 (WebKit::WebCompiledContentRuleList::filtersWithoutConditionsBytecode const): 96 (WebKit::WebCompiledContentRuleList::filtersWithConditionsBytecode const): 97 (WebKit::WebCompiledContentRuleList::topURLFiltersBytecode const): 98 (WebKit::WebCompiledContentRuleList::actions const): 99 * Shared/WebCompiledContentRuleList.h: 100 * Shared/WebCompiledContentRuleListData.cpp: 101 (WebKit::WebCompiledContentRuleListData::size const): 102 (WebKit::WebCompiledContentRuleListData::dataPointer const): 103 (WebKit::WebCompiledContentRuleListData::encode const): 104 (WebKit::WebCompiledContentRuleListData::decode): 105 * Shared/WebCompiledContentRuleListData.h: 106 (WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData): 107 * UIProcess/API/APIContentRuleList.cpp: 108 (API::ContentRuleList::usesCopiedMemory const): 109 * UIProcess/API/APIContentRuleList.h: 110 * UIProcess/API/APIContentRuleListStore.cpp: 111 (API::getData): 112 (API::decodeContentRuleListMetaData): 113 (API::ContentRuleListStore::readContentsOfFile): 114 (API::MappedOrCopiedData::dataPointer const): 115 (API::openAndMapOrCopyContentRuleList): 116 (API::compiledToFile): 117 (API::createExtension): 118 (API::ContentRuleListStore::lookupContentRuleList): 119 (API::ContentRuleListStore::compileContentRuleList): 120 (API::ContentRuleListStore::getContentRuleListSource): 121 (API::openAndMapContentRuleList): Deleted. 122 * UIProcess/API/APIContentRuleListStore.h: 123 * UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm: 124 (API::ContentRuleListStore::readContentsOfFile): 125 * UIProcess/API/Cocoa/WKContentRuleListStore.mm: 126 (+[WKContentRuleListStore _registerPathAsUnsafeToMemoryMapForTesting:]): 127 * UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h: 128 * UIProcess/API/Cocoa/_WKUserContentFilter.mm: 129 (-[_WKUserContentFilter usesCopiedMemory]): 130 * UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h: 131 * WebProcess/UserContent/WebUserContentController.cpp: 132 (WebKit::WebUserContentController::addContentRuleLists): 133 * WebProcess/UserContent/WebUserContentController.h: 134 * WebProcess/WebPage/WebPage.cpp: 135 (WebKit::m_hostFileDescriptor): 136 1 137 2019-03-13 Babak Shafiei <bshafiei@apple.com> 2 138 -
branches/safari-607-branch/Source/WebKit/NetworkProcess/NetworkContentRuleListManager.cpp
r235101 r242860 63 63 } 64 64 65 void NetworkContentRuleListManager::addContentRuleLists(UserContentControllerIdentifier identifier, const Vector<std::pair<String, WebCompiledContentRuleListData>>& contentRuleLists)65 void NetworkContentRuleListManager::addContentRuleLists(UserContentControllerIdentifier identifier, Vector<std::pair<String, WebCompiledContentRuleListData>>&& contentRuleLists) 66 66 { 67 67 auto& backend = *m_contentExtensionBackends.ensure(identifier, [] { … … 69 69 }).iterator->value; 70 70 71 for (const auto& contentRuleList : contentRuleLists) { 72 WebCompiledContentRuleListData contentRuleListData = contentRuleList.second; 73 auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleListData)); 71 for (auto&& contentRuleList : contentRuleLists) { 72 auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleList.second)); 74 73 backend.addContentExtension(contentRuleList.first, WTFMove(compiledContentRuleList), ContentExtensions::ContentExtension::ShouldCompileCSS::No); 75 74 } -
branches/safari-607-branch/Source/WebKit/NetworkProcess/NetworkContentRuleListManager.h
r239007 r242860 50 50 51 51 private: 52 void addContentRuleLists(UserContentControllerIdentifier, const Vector<std::pair<String, WebCompiledContentRuleListData>>&);52 void addContentRuleLists(UserContentControllerIdentifier, Vector<std::pair<String, WebCompiledContentRuleListData>>&&); 53 53 void removeContentRuleList(UserContentControllerIdentifier, const String& name); 54 54 void removeAllContentRuleLists(UserContentControllerIdentifier); -
branches/safari-607-branch/Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.cpp
r239366 r242860 146 146 } 147 147 148 static String& pathRegisteredAsUnsafeToMemoryMapForTesting() 149 { 150 static NeverDestroyed<String> path; 151 return path.get(); 152 } 153 154 void registerPathAsUnsafeToMemoryMapForTesting(const String& path) 155 { 156 pathRegisteredAsUnsafeToMemoryMapForTesting() = path; 157 } 158 159 148 160 bool isSafeToUseMemoryMapForPath(const String& path) 149 161 { 162 if (path == pathRegisteredAsUnsafeToMemoryMapForTesting()) 163 return false; 164 150 165 #if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR) 151 166 struct { -
branches/safari-607-branch/Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.h
r231484 r242860 44 44 45 45 bool isSafeToUseMemoryMapForPath(const String& path); 46 void registerPathAsUnsafeToMemoryMapForTesting(const String&); 46 47 47 48 } -
branches/safari-607-branch/Source/WebKit/Shared/WebCompiledContentRuleList.cpp
r216809 r242860 45 45 } 46 46 47 bool WebCompiledContentRuleList::usesCopiedMemory() const 48 { 49 return WTF::holds_alternative<RefPtr<WebCore::SharedBuffer>>(m_data.data); 50 } 51 47 52 bool WebCompiledContentRuleList::conditionsApplyOnlyToDomain() const 48 53 { 49 return *reinterpret_cast< uint32_t*>(reinterpret_cast<uint8_t*>(m_data.data->data()) + m_data.conditionsApplyOnlyToDomainOffset);54 return *reinterpret_cast<const uint32_t*>(reinterpret_cast<const uint8_t*>(m_data.dataPointer()) + m_data.conditionsApplyOnlyToDomainOffset); 50 55 } 51 56 52 57 const WebCore::ContentExtensions::DFABytecode* WebCompiledContentRuleList::filtersWithoutConditionsBytecode() const 53 58 { 54 return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.data ->data()) + m_data.filtersWithoutConditionsBytecodeOffset;59 return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.dataPointer()) + m_data.filtersWithoutConditionsBytecodeOffset; 55 60 } 56 61 … … 62 67 const WebCore::ContentExtensions::DFABytecode* WebCompiledContentRuleList::filtersWithConditionsBytecode() const 63 68 { 64 return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.data ->data()) + m_data.filtersWithConditionsBytecodeOffset;69 return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.dataPointer()) + m_data.filtersWithConditionsBytecodeOffset; 65 70 } 66 71 … … 72 77 const WebCore::ContentExtensions::DFABytecode* WebCompiledContentRuleList::topURLFiltersBytecode() const 73 78 { 74 return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.data ->data()) + m_data.topURLFiltersBytecodeOffset;79 return static_cast<const WebCore::ContentExtensions::DFABytecode*>(m_data.dataPointer()) + m_data.topURLFiltersBytecodeOffset; 75 80 } 76 81 … … 82 87 const WebCore::ContentExtensions::SerializedActionByte* WebCompiledContentRuleList::actions() const 83 88 { 84 return static_cast<const WebCore::ContentExtensions::SerializedActionByte*>(m_data.data ->data()) + m_data.actionsOffset;89 return static_cast<const WebCore::ContentExtensions::SerializedActionByte*>(m_data.dataPointer()) + m_data.actionsOffset; 85 90 } 86 91 -
branches/safari-607-branch/Source/WebKit/Shared/WebCompiledContentRuleList.h
r216809 r242860 39 39 virtual ~WebCompiledContentRuleList(); 40 40 41 WebCompiledContentRuleListDatadata() const { return m_data; }41 const WebCompiledContentRuleListData& data() const { return m_data; } 42 42 43 bool usesCopiedMemory() const; 44 43 45 private: 44 46 WebCompiledContentRuleList(WebCompiledContentRuleListData&&); -
branches/safari-607-branch/Source/WebKit/Shared/WebCompiledContentRuleListData.cpp
r239427 r242860 30 30 31 31 #include "ArgumentCoders.h" 32 #include "SharedBufferDataReference.h" 32 33 33 34 namespace WebKit { 34 35 36 size_t WebCompiledContentRuleListData::size() const 37 { 38 return WTF::switchOn(data, [] (const auto& sharedMemoryOrBuffer) { 39 return sharedMemoryOrBuffer->size(); 40 }); 41 } 42 43 const void* WebCompiledContentRuleListData::dataPointer() const 44 { 45 return WTF::switchOn(data, [] (const auto& sharedMemoryOrBuffer) -> const void* { 46 return sharedMemoryOrBuffer->data(); 47 }); 48 } 49 35 50 void WebCompiledContentRuleListData::encode(IPC::Encoder& encoder) const 36 51 { 37 SharedMemory::Handle handle; 38 data->createHandle(handle, SharedMemory::Protection::ReadOnly); 39 encoder << handle; 52 if (auto sharedMemory = WTF::get_if<RefPtr<SharedMemory>>(data)) { 53 encoder << true; 54 SharedMemory::Handle handle; 55 sharedMemory->get()->createHandle(handle, SharedMemory::Protection::ReadOnly); 56 encoder << handle; 57 } else { 58 encoder << false; 59 encoder << IPC::SharedBufferDataReference { *WTF::get<RefPtr<WebCore::SharedBuffer>>(data) }; 60 } 61 62 // fileData needs to be kept in the UIProcess, but it does not need to be serialized. 63 // FIXME: Move it to API::ContentRuleList 40 64 41 65 encoder << conditionsApplyOnlyToDomainOffset; … … 53 77 { 54 78 WebCompiledContentRuleListData compiledContentRuleListData; 55 SharedMemory::Handle handle; 56 if (!decoder.decode(handle)) 79 80 Optional<bool> hasSharedMemory; 81 decoder >> hasSharedMemory; 82 if (!hasSharedMemory) 57 83 return WTF::nullopt; 58 compiledContentRuleListData.data = SharedMemory::map(handle, SharedMemory::Protection::ReadOnly); 84 if (*hasSharedMemory) { 85 SharedMemory::Handle handle; 86 if (!decoder.decode(handle)) 87 return WTF::nullopt; 88 compiledContentRuleListData.data = { SharedMemory::map(handle, SharedMemory::Protection::ReadOnly) }; 89 } else { 90 IPC::DataReference dataReference; 91 if (!decoder.decode(dataReference)) 92 return WTF::nullopt; 93 compiledContentRuleListData.data = { RefPtr<WebCore::SharedBuffer>(WebCore::SharedBuffer::create(dataReference.data(), dataReference.size())) }; 94 } 59 95 60 96 if (!decoder.decode(compiledContentRuleListData.conditionsApplyOnlyToDomainOffset)) -
branches/safari-607-branch/Source/WebKit/Shared/WebCompiledContentRuleListData.h
r239427 r242860 30 30 #include "NetworkCacheData.h" 31 31 #include "SharedMemory.h" 32 #include <WebCore/SharedBuffer.h> 32 33 #include <wtf/RefPtr.h> 34 #include <wtf/Variant.h> 33 35 34 36 namespace IPC { … … 43 45 WebCompiledContentRuleListData() = default; 44 46 45 WebCompiledContentRuleListData( RefPtr<SharedMemory>&& data, NetworkCache::Data fileData, unsigned conditionsApplyOnlyToDomainOffset, unsigned actionsOffset, unsigned actionsSize, unsigned filtersWithoutConditionsBytecodeOffset, unsigned filtersWithoutConditionsBytecodeSize, unsigned filtersWithConditionsBytecodeOffset, unsigned filtersWithConditionsBytecodeSize, unsigned topURLFiltersBytecodeOffset, unsigned topURLFiltersBytecodeSize)47 WebCompiledContentRuleListData(Variant<RefPtr<SharedMemory>, RefPtr<WebCore::SharedBuffer>>&& data, NetworkCache::Data fileData, unsigned conditionsApplyOnlyToDomainOffset, unsigned actionsOffset, unsigned actionsSize, unsigned filtersWithoutConditionsBytecodeOffset, unsigned filtersWithoutConditionsBytecodeSize, unsigned filtersWithConditionsBytecodeOffset, unsigned filtersWithConditionsBytecodeSize, unsigned topURLFiltersBytecodeOffset, unsigned topURLFiltersBytecodeSize) 46 48 : data(WTFMove(data)) 47 49 , fileData(fileData) … … 61 63 static Optional<WebCompiledContentRuleListData> decode(IPC::Decoder&); 62 64 63 RefPtr<SharedMemory> data; 65 size_t size() const; 66 const void* dataPointer() const; 67 68 Variant<RefPtr<SharedMemory>, RefPtr<WebCore::SharedBuffer>> data; 64 69 NetworkCache::Data fileData; 65 70 unsigned conditionsApplyOnlyToDomainOffset { 0 }; -
branches/safari-607-branch/Source/WebKit/UIProcess/API/APIContentRuleList.cpp
r216809 r242860 43 43 } 44 44 45 bool ContentRuleList::usesCopiedMemory() const 46 { 47 return m_compiledRuleList->usesCopiedMemory(); 48 } 49 45 50 } // namespace API 46 51 -
branches/safari-607-branch/Source/WebKit/UIProcess/API/APIContentRuleList.h
r216809 r242860 49 49 const WebKit::WebCompiledContentRuleList& compiledRuleList() const { return m_compiledRuleList.get(); } 50 50 51 bool usesCopiedMemory() const; 52 51 53 private: 52 54 WTF::String m_name; -
branches/safari-607-branch/Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp
r239260 r242860 38 38 #include <WebCore/ContentExtensionParser.h> 39 39 #include <WebCore/QualifiedName.h> 40 #include <WebCore/SharedBuffer.h> 40 41 #include <string> 41 42 #include <wtf/CompletionHandler.h> … … 156 157 } 157 158 158 static bool decodeContentRuleListMetaData(ContentRuleListMetaData& metaData, const WebKit::NetworkCache::Data& fileData) 159 template<typename T> void getData(const T&, const Function<bool(const uint8_t*, size_t)>&); 160 template<> void getData(const WebKit::NetworkCache::Data& data, const Function<bool(const uint8_t*, size_t)>& function) 161 { 162 data.apply(function); 163 } 164 template<> void getData(const WebCore::SharedBuffer& data, const Function<bool(const uint8_t*, size_t)>& function) 165 { 166 function(reinterpret_cast<const uint8_t*>(data.data()), data.size()); 167 } 168 169 template<typename T> 170 static Optional<ContentRuleListMetaData> decodeContentRuleListMetaData(const T& fileData) 159 171 { 160 172 bool success = false; 161 fileData.apply([&metaData, &success, &fileData](const uint8_t* data, size_t size) { 173 ContentRuleListMetaData metaData; 174 getData(fileData, [&metaData, &success, &fileData](const uint8_t* data, size_t size) { 162 175 // The file data should be mapped into one continuous memory segment so the size 163 176 // passed to the applier should always equal the data size. … … 183 196 return false; 184 197 }); 185 return success; 186 } 187 188 static bool openAndMapContentRuleList(const WTF::String& path, ContentRuleListMetaData& metaData, WebKit::NetworkCache::Data& fileData) 189 { 190 fileData = mapFile(fileSystemRepresentation(path).data()); 198 if (!success) 199 return WTF::nullopt; 200 return WTFMove(metaData); 201 } 202 203 #if !PLATFORM(COCOA) 204 RefPtr<WebCore::SharedBuffer> ContentRuleListStore::readContentsOfFile(const WTF::String& filePath) 205 { 206 ASSERT_NOT_REACHED(); 207 return nullptr; 208 } 209 #endif 210 211 struct MappedOrCopiedData { 212 ContentRuleListMetaData metaData; 213 Variant<WebKit::NetworkCache::Data, RefPtr<WebCore::SharedBuffer>> data; 214 215 const uint8_t* dataPointer() const 216 { 217 return WTF::switchOn(data, [] (const WebKit::NetworkCache::Data& data) { 218 return data.data(); 219 }, [] (const RefPtr<WebCore::SharedBuffer>& sharedBuffer) { 220 return reinterpret_cast<const uint8_t*>(sharedBuffer->data()); 221 }); 222 } 223 }; 224 225 static Optional<MappedOrCopiedData> openAndMapOrCopyContentRuleList(const WTF::String& path) 226 { 227 if (!WebKit::NetworkCache::isSafeToUseMemoryMapForPath(path)) { 228 RefPtr<WebCore::SharedBuffer> buffer = ContentRuleListStore::readContentsOfFile(path); 229 if (!buffer) 230 return WTF::nullopt; 231 auto metaData = decodeContentRuleListMetaData(*buffer); 232 if (!metaData) 233 return WTF::nullopt; 234 return {{ WTFMove(*metaData), { buffer.releaseNonNull() }}}; 235 } 236 237 WebKit::NetworkCache::Data fileData = mapFile(fileSystemRepresentation(path).data()); 191 238 if (fileData.isNull()) 192 return false; 193 194 if (!decodeContentRuleListMetaData(metaData, fileData)) 195 return false; 196 197 return true; 239 return WTF::nullopt; 240 auto metaData = decodeContentRuleListMetaData(fileData); 241 if (!metaData) 242 return WTF::nullopt; 243 return {{ WTFMove(*metaData), { WTFMove(fileData) }}}; 198 244 } 199 245 … … 212 258 } 213 259 214 static std::error_code compiledToFile(WTF::String&& json, Vector<WebCore::ContentExtensions::ContentExtensionRule>&& parsedRules, const WTF::String& finalFilePath, ContentRuleListMetaData& metaData, WebKit::NetworkCache::Data& mappedData)260 static Expected<MappedOrCopiedData, std::error_code> compiledToFile(WTF::String&& json, Vector<WebCore::ContentExtensions::ContentExtensionRule>&& parsedRules, const WTF::String& finalFilePath) 215 261 { 216 262 using namespace WebCore::ContentExtensions; … … 329 375 if (temporaryFileHandle == invalidPlatformFileHandle) { 330 376 WTFLogAlways("Content Rule List compiling failed: Opening temporary file failed."); 331 return ContentRuleListStore::Error::CompileFailed;377 return makeUnexpected(ContentRuleListStore::Error::CompileFailed); 332 378 } 333 379 … … 338 384 WTFLogAlways("Content Rule List compiling failed: Writing header to file failed."); 339 385 closeFile(temporaryFileHandle); 340 return ContentRuleListStore::Error::CompileFailed; 341 } 342 386 return makeUnexpected(ContentRuleListStore::Error::CompileFailed); 387 } 388 389 ContentRuleListMetaData metaData; 343 390 CompilationClient compilationClient(temporaryFileHandle, metaData); 344 391 … … 346 393 WTFLogAlways("Content Rule List compiling failed: Compiling failed."); 347 394 closeFile(temporaryFileHandle); 348 return compilerError;395 return makeUnexpected(compilerError); 349 396 } 350 397 if (compilationClient.hadErrorWhileWritingToFile()) { 351 398 WTFLogAlways("Content Rule List compiling failed: Writing to file failed."); 352 399 closeFile(temporaryFileHandle); 353 return ContentRuleListStore::Error::CompileFailed;354 } 355 356 mappedData = adoptAndMapFile(temporaryFileHandle, 0, metaData.fileSize());400 return makeUnexpected(ContentRuleListStore::Error::CompileFailed); 401 } 402 403 auto mappedData = adoptAndMapFile(temporaryFileHandle, 0, metaData.fileSize()); 357 404 if (mappedData.isNull()) { 358 405 WTFLogAlways("Content Rule List compiling failed: Mapping file failed."); 359 return ContentRuleListStore::Error::CompileFailed;406 return makeUnexpected(ContentRuleListStore::Error::CompileFailed); 360 407 } 361 408 362 409 if (!moveFile(temporaryFilePath, finalFilePath)) { 363 410 WTFLogAlways("Content Rule List compiling failed: Moving file failed."); 364 return ContentRuleListStore::Error::CompileFailed; 365 } 366 367 return { }; 368 } 369 370 static Ref<API::ContentRuleList> createExtension(const WTF::String& identifier, const ContentRuleListMetaData& metaData, const WebKit::NetworkCache::Data& fileData) 371 { 372 // Content extensions are always compiled to files, and at this point the file 373 // has been already mapped, therefore tryCreateSharedMemory() cannot fail. 374 auto sharedMemory = fileData.tryCreateSharedMemory(); 375 ASSERT(sharedMemory); 376 377 const size_t headerAndSourceSize = ContentRuleListFileHeaderSize + metaData.sourceSize; 411 return makeUnexpected(ContentRuleListStore::Error::CompileFailed); 412 } 413 414 if (!isSafeToUseMemoryMapForPath(finalFilePath)) { 415 auto contents = ContentRuleListStore::readContentsOfFile(finalFilePath); 416 if (!contents) 417 return makeUnexpected(ContentRuleListStore::Error::CompileFailed); 418 return {{ WTFMove(metaData), WTFMove(contents) }}; 419 } 420 421 return {{ WTFMove(metaData), WTFMove(mappedData) }}; 422 } 423 424 static Ref<API::ContentRuleList> createExtension(const WTF::String& identifier, MappedOrCopiedData&& data) 425 { 426 RefPtr<WebKit::SharedMemory> sharedMemory; 427 if (auto mappedFileData = WTF::get_if<WebKit::NetworkCache::Data>(data.data)) { 428 sharedMemory = mappedFileData->tryCreateSharedMemory(); 429 430 // Content extensions are always compiled to files, and at this point the file 431 // has been already mapped, therefore tryCreateSharedMemory() cannot fail. 432 ASSERT(sharedMemory); 433 } 434 auto mappedOrCopiedFileData = sharedMemory ? 435 Variant<RefPtr<WebKit::SharedMemory>, RefPtr<WebCore::SharedBuffer>> { sharedMemory } 436 : Variant<RefPtr<WebKit::SharedMemory>, RefPtr<WebCore::SharedBuffer>> { WTFMove(WTF::get<RefPtr<WebCore::SharedBuffer>>(data.data)) }; 437 438 const size_t headerAndSourceSize = ContentRuleListFileHeaderSize + data.metaData.sourceSize; 378 439 auto compiledContentRuleListData = WebKit::WebCompiledContentRuleListData( 379 WTFMove( sharedMemory),380 fileData,440 WTFMove(mappedOrCopiedFileData), 441 WTF::holds_alternative<WebKit::NetworkCache::Data>(data.data) ? WTF::get<WebKit::NetworkCache::Data>(data.data) : WebKit::NetworkCache::Data { }, 381 442 ConditionsApplyOnlyToDomainOffset, 382 443 headerAndSourceSize, 383 metaData.actionsSize,444 data.metaData.actionsSize, 384 445 headerAndSourceSize 385 + metaData.actionsSize,386 metaData.filtersWithoutConditionsBytecodeSize,446 + data.metaData.actionsSize, 447 data.metaData.filtersWithoutConditionsBytecodeSize, 387 448 headerAndSourceSize 388 + metaData.actionsSize389 + metaData.filtersWithoutConditionsBytecodeSize,390 metaData.filtersWithConditionsBytecodeSize,449 + data.metaData.actionsSize 450 + data.metaData.filtersWithoutConditionsBytecodeSize, 451 data.metaData.filtersWithConditionsBytecodeSize, 391 452 headerAndSourceSize 392 + metaData.actionsSize393 + metaData.filtersWithoutConditionsBytecodeSize394 + metaData.filtersWithConditionsBytecodeSize,395 metaData.conditionedFiltersBytecodeSize453 + data.metaData.actionsSize 454 + data.metaData.filtersWithoutConditionsBytecodeSize 455 + data.metaData.filtersWithConditionsBytecodeSize, 456 data.metaData.conditionedFiltersBytecodeSize 396 457 ); 397 458 auto compiledContentRuleList = WebKit::WebCompiledContentRuleList::create(WTFMove(compiledContentRuleListData)); … … 404 465 auto path = constructedPath(storePath, identifier, legacyFilename); 405 466 406 ContentRuleListMetaData metaData; 407 WebKit::NetworkCache::Data fileData; 408 if (!openAndMapContentRuleList(path, metaData, fileData)) { 467 auto contentRuleList = openAndMapOrCopyContentRuleList(path); 468 if (!contentRuleList) { 409 469 RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), completionHandler = WTFMove(completionHandler)] () mutable { 410 470 completionHandler(nullptr, Error::LookupFailed); … … 413 473 } 414 474 415 if ( metaData.version != ContentRuleListStore::CurrentContentRuleListFileVersion) {475 if (contentRuleList->metaData.version != ContentRuleListStore::CurrentContentRuleListFileVersion) { 416 476 RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), completionHandler = WTFMove(completionHandler)] () mutable { 417 477 completionHandler(nullptr, Error::VersionMismatch); … … 420 480 } 421 481 422 RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), identifier = identifier.isolatedCopy(), fileData = WTFMove(fileData), metaData = WTFMove(metaData), completionHandler = WTFMove(completionHandler)] () mutable {423 completionHandler(createExtension(identifier, metaData, fileData), { });482 RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), identifier = identifier.isolatedCopy(), contentRuleList = WTFMove(*contentRuleList), completionHandler = WTFMove(completionHandler)] () mutable { 483 completionHandler(createExtension(identifier, WTFMove(contentRuleList)), { }); 424 484 }); 425 485 }); … … 455 515 auto path = constructedPath(storePath, identifier, legacyFilename); 456 516 457 ContentRuleListMetaData metaData; 458 WebKit::NetworkCache::Data fileData; 459 auto error = compiledToFile(WTFMove(json), WTFMove(parsedRules), path, metaData, fileData); 460 if (error) { 461 RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), error = WTFMove(error), completionHandler = WTFMove(completionHandler)] () mutable { 517 auto result = compiledToFile(WTFMove(json), WTFMove(parsedRules), path); 518 if (!result.has_value()) { 519 RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), error = WTFMove(result.error()), completionHandler = WTFMove(completionHandler)] () mutable { 462 520 completionHandler(nullptr, error); 463 521 }); … … 465 523 } 466 524 467 RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), identifier = WTFMove(identifier), fileData = WTFMove(fileData), metaData = WTFMove(metaData), completionHandler = WTFMove(completionHandler)] () mutable {468 RefPtr<API::ContentRuleList> contentRuleList = createExtension(identifier, metaData, fileData);469 completionHandler(contentRuleList , { });525 RunLoop::main().dispatch([protectedThis = WTFMove(protectedThis), identifier = WTFMove(identifier), data = WTFMove(result.value()), completionHandler = WTFMove(completionHandler)] () mutable { 526 auto contentRuleList = createExtension(identifier, WTFMove(data)); 527 completionHandler(contentRuleList.ptr(), { }); 470 528 }); 471 529 }); … … 517 575 }); 518 576 }; 519 520 ContentRuleListMetaData metaData; 521 WebKit::NetworkCache::Data fileData; 522 if (!openAndMapContentRuleList(path, metaData, fileData)) { 577 578 auto contentRuleList = openAndMapOrCopyContentRuleList(path); 579 if (!contentRuleList) { 523 580 complete({ }); 524 581 return; 525 582 } 526 583 527 switch ( metaData.version) {584 switch (contentRuleList->metaData.version) { 528 585 case 9: 529 586 case 10: 530 if (! metaData.sourceSize) {587 if (!contentRuleList->metaData.sourceSize) { 531 588 complete({ }); 532 589 return; 533 590 } 534 bool is8Bit = fileData.data()[ContentRuleListFileHeaderSize];591 bool is8Bit = contentRuleList->dataPointer()[ContentRuleListFileHeaderSize]; 535 592 size_t start = ContentRuleListFileHeaderSize + sizeof(bool); 536 size_t length = metaData.sourceSize - sizeof(bool);593 size_t length = contentRuleList->metaData.sourceSize - sizeof(bool); 537 594 if (is8Bit) 538 complete(WTF::String( fileData.data() + start, length));595 complete(WTF::String(contentRuleList->dataPointer() + start, length)); 539 596 else { 540 597 ASSERT(!(length % sizeof(UChar))); 541 complete(WTF::String(reinterpret_cast<const UChar*>( fileData.data() + start), length / sizeof(UChar)));598 complete(WTF::String(reinterpret_cast<const UChar*>(contentRuleList->dataPointer() + start), length / sizeof(UChar))); 542 599 } 543 600 return; -
branches/safari-607-branch/Source/WebKit/UIProcess/API/APIContentRuleListStore.h
r234873 r242860 31 31 #include <system_error> 32 32 #include <wtf/text/WTFString.h> 33 34 namespace WebCore { 35 class SharedBuffer; 36 } 33 37 34 38 namespace WTF { … … 73 77 void getContentRuleListSource(const WTF::String& identifier, CompletionHandler<void(WTF::String)>); 74 78 79 static RefPtr<WebCore::SharedBuffer> readContentsOfFile(const WTF::String& path); 80 75 81 private: 76 82 WTF::String defaultStorePath(bool legacyFilename); -
branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm
r235828 r242860 30 30 31 31 #include "SandboxUtilities.h" 32 #include <WebCore/SharedBuffer.h> 32 33 33 34 namespace API { … … 64 65 } 65 66 67 RefPtr<WebCore::SharedBuffer> ContentRuleListStore::readContentsOfFile(const String& filePath) 68 { 69 ASSERT(!isMainThread()); 70 NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:filePath isDirectory:NO]]; 71 if (!data) 72 return nullptr; 73 return WebCore::SharedBuffer::create(data); 74 } 75 66 76 } // namespace API 67 77 -
branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/WKContentRuleListStore.mm
r235365 r242860 31 31 32 32 #import "APIContentRuleListStore.h" 33 #import "NetworkCacheFilesystem.h" 33 34 #import "WKErrorInternal.h" 34 35 #import <wtf/BlockPtr.h> … … 128 129 // For testing only. 129 130 131 + (void)_registerPathAsUnsafeToMemoryMapForTesting:(NSString *)filename 132 { 133 WebKit::NetworkCache::registerPathAsUnsafeToMemoryMapForTesting(filename); 134 } 135 130 136 - (void)_removeAllContentRuleLists 131 137 { -
branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h
r216809 r242860 34 34 - (void)_invalidateContentRuleListVersionForIdentifier:(NSString *)identifier; 35 35 - (void)_getContentRuleListSourceForIdentifier:(NSString *)identifier completionHandler:(void (^)(NSString*))completionHandler; 36 + (void)_registerPathAsUnsafeToMemoryMapForTesting:(NSString *)filename; 36 37 37 38 // NS_RELEASES_ARGUMENT to keep peak memory usage low. -
branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/_WKUserContentFilter.mm
r232520 r242860 59 59 } 60 60 61 - (BOOL)usesCopiedMemory 62 { 63 return _contentRuleList->_contentRuleList->usesCopiedMemory(); 64 } 65 61 66 @end 62 67 -
branches/safari-607-branch/Source/WebKit/UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h
r221930 r242860 33 33 34 34 - (id)_initWithWKContentRuleList:(WKContentRuleList*)contentRuleList WK_API_AVAILABLE(macosx(10.13), ios(11.0)); 35 @property (nonatomic, readonly) BOOL usesCopiedMemory; 35 36 36 37 @end -
branches/safari-607-branch/Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp
r240768 r242860 349 349 350 350 #if ENABLE(CONTENT_EXTENSIONS) 351 void WebUserContentController::addContentRuleLists(const Vector<std::pair<String, WebCompiledContentRuleListData>>& contentRuleLists) 352 { 353 for (const auto& contentRuleList : contentRuleLists) { 354 WebCompiledContentRuleListData contentRuleListData = contentRuleList.second; 355 auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleListData)); 351 void WebUserContentController::addContentRuleLists(Vector<std::pair<String, WebCompiledContentRuleListData>>&& contentRuleLists) 352 { 353 for (auto&& contentRuleList : contentRuleLists) { 354 auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleList.second)); 356 355 357 356 m_contentExtensionBackend.addContentExtension(contentRuleList.first, WTFMove(compiledContentRuleList)); -
branches/safari-607-branch/Source/WebKit/WebProcess/UserContent/WebUserContentController.h
r240425 r242860 70 70 void addUserScriptMessageHandlers(const Vector<WebScriptMessageHandlerData>&); 71 71 #if ENABLE(CONTENT_EXTENSIONS) 72 void addContentRuleLists( const Vector<std::pair<String, WebCompiledContentRuleListData>>&);72 void addContentRuleLists(Vector<std::pair<String, WebCompiledContentRuleListData>>&&); 73 73 #endif 74 74 -
branches/safari-607-branch/Source/WebKit/WebProcess/WebPage/WebPage.cpp
r242858 r242860 625 625 m_userContentController->addUserScriptMessageHandlers(parameters.messageHandlers); 626 626 #if ENABLE(CONTENT_EXTENSIONS) 627 m_userContentController->addContentRuleLists( parameters.contentRuleLists);627 m_userContentController->addContentRuleLists(WTFMove(parameters.contentRuleLists)); 628 628 #endif 629 629 -
branches/safari-607-branch/Tools/ChangeLog
r242388 r242860 1 2019-03-13 Babak Shafiei <bshafiei@apple.com> 2 3 Cherry-pick r242735. rdar://problem/48780112 4 5 Add a WKContentRuleList variant that uses copied memory instead of mmap'd shared memory for class A containerized apps 6 https://bugs.webkit.org/show_bug.cgi?id=195511 7 <rdar://problem/44873269> 8 9 Patch by Alex Christensen <achristensen@webkit.org> on 2019-03-11 10 Reviewed by Darin Adler. 11 12 Source/WebKit: 13 14 * NetworkProcess/NetworkContentRuleListManager.cpp: 15 (WebKit::NetworkContentRuleListManager::addContentRuleLists): 16 * NetworkProcess/NetworkContentRuleListManager.h: 17 * NetworkProcess/cache/NetworkCacheFileSystem.cpp: 18 (WebKit::NetworkCache::pathRegisteredAsUnsafeToMemoryMapForTesting): 19 (WebKit::NetworkCache::registerPathAsUnsafeToMemoryMapForTesting): 20 (WebKit::NetworkCache::isSafeToUseMemoryMapForPath): 21 * NetworkProcess/cache/NetworkCacheFileSystem.h: 22 * Shared/WebCompiledContentRuleList.cpp: 23 (WebKit::WebCompiledContentRuleList::usesCopiedMemory const): 24 (WebKit::WebCompiledContentRuleList::conditionsApplyOnlyToDomain const): 25 (WebKit::WebCompiledContentRuleList::filtersWithoutConditionsBytecode const): 26 (WebKit::WebCompiledContentRuleList::filtersWithConditionsBytecode const): 27 (WebKit::WebCompiledContentRuleList::topURLFiltersBytecode const): 28 (WebKit::WebCompiledContentRuleList::actions const): 29 * Shared/WebCompiledContentRuleList.h: 30 * Shared/WebCompiledContentRuleListData.cpp: 31 (WebKit::WebCompiledContentRuleListData::size const): 32 (WebKit::WebCompiledContentRuleListData::dataPointer const): 33 (WebKit::WebCompiledContentRuleListData::encode const): 34 (WebKit::WebCompiledContentRuleListData::decode): 35 * Shared/WebCompiledContentRuleListData.h: 36 (WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData): 37 * UIProcess/API/APIContentRuleList.cpp: 38 (API::ContentRuleList::usesCopiedMemory const): 39 * UIProcess/API/APIContentRuleList.h: 40 * UIProcess/API/APIContentRuleListStore.cpp: 41 (API::getData): 42 (API::decodeContentRuleListMetaData): 43 (API::ContentRuleListStore::readContentsOfFile): 44 (API::MappedOrCopiedData::dataPointer const): 45 (API::openAndMapOrCopyContentRuleList): 46 (API::compiledToFile): 47 (API::createExtension): 48 (API::ContentRuleListStore::lookupContentRuleList): 49 (API::ContentRuleListStore::compileContentRuleList): 50 (API::ContentRuleListStore::getContentRuleListSource): 51 (API::openAndMapContentRuleList): Deleted. 52 * UIProcess/API/APIContentRuleListStore.h: 53 * UIProcess/API/Cocoa/APIContentRuleListStoreCocoa.mm: 54 (API::ContentRuleListStore::readContentsOfFile): 55 * UIProcess/API/Cocoa/WKContentRuleListStore.mm: 56 (+[WKContentRuleListStore _registerPathAsUnsafeToMemoryMapForTesting:]): 57 * UIProcess/API/Cocoa/WKContentRuleListStorePrivate.h: 58 * UIProcess/API/Cocoa/_WKUserContentFilter.mm: 59 (-[_WKUserContentFilter usesCopiedMemory]): 60 * UIProcess/API/Cocoa/_WKUserContentFilterPrivate.h: 61 * WebProcess/UserContent/WebUserContentController.cpp: 62 (WebKit::WebUserContentController::addContentRuleLists): 63 * WebProcess/UserContent/WebUserContentController.h: 64 * WebProcess/WebPage/WebPage.cpp: 65 (WebKit::m_hostFileDescriptor): 66 67 Tools: 68 69 * TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm: 70 (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:startURLSchemeTask:]): 71 (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:stopURLSchemeTask:]): 72 (TEST_F): 73 74 git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242735 268f45cc-cd09-0410-ab3c-d52691b4dbfc 75 76 2019-03-11 Alex Christensen <achristensen@webkit.org> 77 78 Add a WKContentRuleList variant that uses copied memory instead of mmap'd shared memory for class A containerized apps 79 https://bugs.webkit.org/show_bug.cgi?id=195511 80 <rdar://problem/44873269> 81 82 Reviewed by Darin Adler. 83 84 * TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm: 85 (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:startURLSchemeTask:]): 86 (-[TestSchemeHandlerSubresourceShouldBeBlocked webView:stopURLSchemeTask:]): 87 (TEST_F): 88 1 89 2019-03-04 Kocsen Chung <kocsen_chung@apple.com> 2 90 -
branches/safari-607-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm
r216809 r242860 31 31 #import "PlatformUtilities.h" 32 32 #import "Test.h" 33 #import "TestWKWebView.h" 33 34 #import <WebKit/WKContentRuleList.h> 34 35 #import <WebKit/WKContentRuleListStorePrivate.h> 36 #import <WebKit/_WKUserContentFilterPrivate.h> 35 37 #import <wtf/RetainPtr.h> 38 #import <wtf/text/StringBuilder.h> 39 #import <wtf/text/StringConcatenate.h> 40 #import <wtf/text/StringConcatenateNumbers.h> 36 41 37 42 class WKContentRuleListStoreTest : public testing::Test { … … 381 386 } 382 387 388 @interface TestSchemeHandlerSubresourceShouldBeBlocked : NSObject <WKURLSchemeHandler> 389 @end 390 @implementation TestSchemeHandlerSubresourceShouldBeBlocked 391 - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)task 392 { 393 EXPECT_TRUE([task.request.URL.path isEqualToString:@"/shouldload"]); 394 [task didReceiveResponse:[[[NSURLResponse alloc] initWithURL:task.request.URL MIMEType:@"text/html" expectedContentLength:0 textEncodingName:nil] autorelease]]; 395 [task didFinish]; 396 } 397 - (void)webView:(WKWebView *)webView stopURLSchemeTask:(id <WKURLSchemeTask>)task 398 { 399 EXPECT_TRUE(false); 400 } 401 @end 402 403 TEST_F(WKContentRuleListStoreTest, UnsafeMMap) 404 { 405 RetainPtr<NSString> tempDir = [NSTemporaryDirectory() stringByAppendingPathComponent:@"UnsafeMMapTest"]; 406 RetainPtr<WKContentRuleListStore> store = [WKContentRuleListStore storeWithURL:[NSURL fileURLWithPath:tempDir.get() isDirectory:YES]]; 407 static NSString *identifier = @"TestRuleList"; 408 static NSString *fileName = @"ContentRuleList-TestRuleList"; 409 static NSString *ruleListSourceString = @"[{\"action\":{\"type\":\"block\"},\"trigger\":{\"url-filter\":\"blockedsubresource\"}}]"; 410 RetainPtr<NSString> filePath = [tempDir stringByAppendingPathComponent:fileName]; 411 412 auto runTest = [&] (bool shouldUseCopiedMemory) { 413 EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:filePath.get()]); 414 415 __block bool doneCompiling = false; 416 __block RetainPtr<WKContentRuleList> ruleList; 417 [store compileContentRuleListForIdentifier:identifier encodedContentRuleList:ruleListSourceString completionHandler:^(WKContentRuleList *filter, NSError *error) { 418 EXPECT_NOT_NULL(filter); 419 EXPECT_NULL(error); 420 doneCompiling = true; 421 ruleList = filter; 422 EXPECT_TRUE([[[[_WKUserContentFilter alloc] _initWithWKContentRuleList:filter] autorelease] usesCopiedMemory] == shouldUseCopiedMemory); 423 }]; 424 TestWebKitAPI::Util::run(&doneCompiling); 425 426 EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:filePath.get()]); 427 428 auto handler = adoptNS([TestSchemeHandlerSubresourceShouldBeBlocked new]); 429 auto configuration = adoptNS([WKWebViewConfiguration new]); 430 [configuration setURLSchemeHandler:handler.get() forURLScheme:@"testmmap"]; 431 [[configuration userContentController] addContentRuleList:ruleList.get()]; 432 auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]); 433 [webView synchronouslyLoadHTMLString:@"<html>main resource content</html>" baseURL:[NSURL URLWithString:@"testmmap://webkit.org/mainresource"]]; 434 435 auto loadingShouldSucceed = [&] (NSString *resourcePath, NSString *shouldSucceed) { 436 __block bool doneEvaluating = false; 437 [webView evaluateJavaScript:[NSString stringWithFormat:@"var caught = false; var xhr = new XMLHttpRequest(); xhr.open('GET', '%@', false); try{ xhr.send() } catch(e) { caught = true; }; caught != %@ ? 'success' : 'failure'", resourcePath, shouldSucceed] completionHandler:^(id result, NSError *error) { 438 EXPECT_NULL(error); 439 EXPECT_TRUE([@"success" isEqualToString:result]); 440 doneEvaluating = true; 441 }]; 442 TestWebKitAPI::Util::run(&doneEvaluating); 443 }; 444 loadingShouldSucceed(@"/shouldload", @"true"); 445 loadingShouldSucceed(@"/blockedsubresource", @"false"); 446 447 [[configuration userContentController] removeContentRuleList:ruleList.get()]; 448 449 __block bool doneLookingUp = false; 450 [store lookUpContentRuleListForIdentifier:identifier completionHandler:^(WKContentRuleList *filter, NSError *error) { 451 EXPECT_NOT_NULL(filter); 452 EXPECT_NULL(error); 453 454 doneLookingUp = true; 455 456 EXPECT_TRUE([[[[_WKUserContentFilter alloc] _initWithWKContentRuleList:filter] autorelease] usesCopiedMemory] == shouldUseCopiedMemory); 457 ruleList = filter; 458 }]; 459 TestWebKitAPI::Util::run(&doneLookingUp); 460 461 [[configuration userContentController] addContentRuleList:ruleList.get()]; 462 loadingShouldSucceed(@"/shouldload", @"true"); 463 loadingShouldSucceed(@"/blockedsubresource", @"false"); 464 465 __block bool doneCheckingSource = false; 466 [store _getContentRuleListSourceForIdentifier:identifier completionHandler:^(NSString *source) { 467 EXPECT_TRUE([source isEqualToString:ruleListSourceString]); 468 doneCheckingSource = true; 469 }]; 470 TestWebKitAPI::Util::run(&doneCheckingSource); 471 472 __block bool doneRemoving = false; 473 [store removeContentRuleListForIdentifier:identifier completionHandler:^(NSError *error) { 474 EXPECT_NULL(error); 475 doneRemoving = true; 476 }]; 477 TestWebKitAPI::Util::run(&doneRemoving); 478 479 EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:filePath.get()]); 480 }; 481 482 runTest(false); 483 [WKContentRuleListStore _registerPathAsUnsafeToMemoryMapForTesting:filePath.get()]; 484 runTest(true); 485 } 383 486 384 487 #endif
Note:
See TracChangeset
for help on using the changeset viewer.