| 1 | /* |
| 2 | * Copyright (C) 2014 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 COMPUTER, INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "WebMediaKeyStorageManager.h" |
| 28 | |
| 29 | #include "WebProcessDataStoreParameters.h" |
| 30 | #include <WebCore/SecurityOrigin.h> |
| 31 | #include <WebCore/SecurityOriginData.h> |
| 32 | #include <wtf/FileSystem.h> |
| 33 | #include <wtf/URL.h> |
| 34 | |
| 35 | namespace WebKit { |
| 36 | using namespace WebCore; |
| 37 | |
| 38 | void WebMediaKeyStorageManager::setWebsiteDataStore(const WebProcessDataStoreParameters& parameters) |
| 39 | { |
| 40 | ASSERT(!parameters.mediaKeyStorageDirectory.isEmpty()); |
| 41 | m_mediaKeyStorageDirectory = parameters.mediaKeyStorageDirectory; |
| 42 | } |
| 43 | |
| 44 | const char* WebMediaKeyStorageManager::supplementName() |
| 45 | { |
| 46 | return "WebMediaKeyStorageManager" ; |
| 47 | } |
| 48 | |
| 49 | String WebMediaKeyStorageManager::mediaKeyStorageDirectoryForOrigin(const SecurityOriginData& originData) |
| 50 | { |
| 51 | if (!m_mediaKeyStorageDirectory) |
| 52 | return emptyString(); |
| 53 | |
| 54 | return FileSystem::pathByAppendingComponent(m_mediaKeyStorageDirectory, originData.databaseIdentifier()); |
| 55 | } |
| 56 | |
| 57 | Vector<SecurityOriginData> WebMediaKeyStorageManager::getMediaKeyOrigins() |
| 58 | { |
| 59 | Vector<SecurityOriginData> results; |
| 60 | |
| 61 | if (m_mediaKeyStorageDirectory.isEmpty()) |
| 62 | return results; |
| 63 | |
| 64 | Vector<String> originPaths = FileSystem::listDirectory(m_mediaKeyStorageDirectory, "*" ); |
| 65 | for (const auto& originPath : originPaths) { |
| 66 | URL url; |
| 67 | url.setProtocol("file"_s ); |
| 68 | url.setPath(originPath); |
| 69 | |
| 70 | String mediaKeyIdentifier = url.lastPathComponent(); |
| 71 | |
| 72 | auto securityOrigin = SecurityOriginData::fromDatabaseIdentifier(mediaKeyIdentifier); |
| 73 | if (!securityOrigin) |
| 74 | continue; |
| 75 | |
| 76 | results.append(*securityOrigin); |
| 77 | } |
| 78 | |
| 79 | return results; |
| 80 | } |
| 81 | |
| 82 | static void removeAllMediaKeyStorageForOriginPath(const String& originPath, WallTime startDate, WallTime endDate) |
| 83 | { |
| 84 | Vector<String> mediaKeyPaths = FileSystem::listDirectory(originPath, "*" ); |
| 85 | |
| 86 | for (const auto& mediaKeyPath : mediaKeyPaths) { |
| 87 | String mediaKeyFile = FileSystem::pathByAppendingComponent(mediaKeyPath, "SecureStop.plist" ); |
| 88 | |
| 89 | if (!FileSystem::fileExists(mediaKeyFile)) |
| 90 | continue; |
| 91 | |
| 92 | auto modificationTime = FileSystem::getFileModificationTime(mediaKeyFile); |
| 93 | if (!modificationTime) |
| 94 | continue; |
| 95 | if (modificationTime.value() < startDate || modificationTime.value() > endDate) |
| 96 | continue; |
| 97 | |
| 98 | FileSystem::deleteFile(mediaKeyFile); |
| 99 | FileSystem::deleteEmptyDirectory(mediaKeyPath); |
| 100 | } |
| 101 | |
| 102 | FileSystem::deleteEmptyDirectory(originPath); |
| 103 | } |
| 104 | |
| 105 | void WebMediaKeyStorageManager::deleteMediaKeyEntriesForOrigin(const SecurityOriginData& originData) |
| 106 | { |
| 107 | if (m_mediaKeyStorageDirectory.isEmpty()) |
| 108 | return; |
| 109 | |
| 110 | String originPath = mediaKeyStorageDirectoryForOrigin(originData); |
| 111 | removeAllMediaKeyStorageForOriginPath(originPath, -WallTime::infinity(), WallTime::infinity()); |
| 112 | } |
| 113 | |
| 114 | void WebMediaKeyStorageManager::deleteMediaKeyEntriesModifiedBetweenDates(WallTime startDate, WallTime endDate) |
| 115 | { |
| 116 | if (m_mediaKeyStorageDirectory.isEmpty()) |
| 117 | return; |
| 118 | |
| 119 | Vector<String> originPaths = FileSystem::listDirectory(m_mediaKeyStorageDirectory, "*" ); |
| 120 | for (auto& originPath : originPaths) |
| 121 | removeAllMediaKeyStorageForOriginPath(originPath, startDate, endDate); |
| 122 | } |
| 123 | |
| 124 | void WebMediaKeyStorageManager::deleteAllMediaKeyEntries() |
| 125 | { |
| 126 | if (m_mediaKeyStorageDirectory.isEmpty()) |
| 127 | return; |
| 128 | |
| 129 | Vector<String> originPaths = FileSystem::listDirectory(m_mediaKeyStorageDirectory, "*" ); |
| 130 | for (auto& originPath : originPaths) |
| 131 | removeAllMediaKeyStorageForOriginPath(originPath, -WallTime::infinity(), WallTime::infinity()); |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |