| 1 | /* |
| 2 | * Copyright (C) 2012 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. ``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 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 | #pragma once |
| 27 | |
| 28 | #include "DatabaseDetails.h" |
| 29 | #include "ExceptionOr.h" |
| 30 | #include <wtf/Assertions.h> |
| 31 | #include <wtf/HashSet.h> |
| 32 | #include <wtf/Lock.h> |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | class Database; |
| 37 | class DatabaseCallback; |
| 38 | class DatabaseContext; |
| 39 | class DatabaseManagerClient; |
| 40 | class DatabaseTaskSynchronizer; |
| 41 | class Exception; |
| 42 | class SecurityOrigin; |
| 43 | class ScriptExecutionContext; |
| 44 | struct SecurityOriginData; |
| 45 | |
| 46 | class DatabaseManager { |
| 47 | WTF_MAKE_NONCOPYABLE(DatabaseManager); |
| 48 | friend class WTF::NeverDestroyed<DatabaseManager>; |
| 49 | public: |
| 50 | WEBCORE_EXPORT static DatabaseManager& singleton(); |
| 51 | |
| 52 | WEBCORE_EXPORT void initialize(const String& databasePath); |
| 53 | WEBCORE_EXPORT void setClient(DatabaseManagerClient*); |
| 54 | |
| 55 | bool isAvailable(); |
| 56 | WEBCORE_EXPORT void setIsAvailable(bool); |
| 57 | |
| 58 | // This gets a DatabaseContext for the specified ScriptExecutionContext. |
| 59 | // If one doesn't already exist, it will create a new one. |
| 60 | Ref<DatabaseContext> databaseContext(ScriptExecutionContext&); |
| 61 | |
| 62 | ExceptionOr<Ref<Database>> openDatabase(ScriptExecutionContext&, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, RefPtr<DatabaseCallback>&&); |
| 63 | |
| 64 | WEBCORE_EXPORT bool hasOpenDatabases(ScriptExecutionContext&); |
| 65 | void stopDatabases(ScriptExecutionContext&, DatabaseTaskSynchronizer*); |
| 66 | |
| 67 | WEBCORE_EXPORT String fullPathForDatabase(SecurityOrigin&, const String& name, bool createIfDoesNotExist = true); |
| 68 | |
| 69 | WEBCORE_EXPORT DatabaseDetails detailsForNameAndOrigin(const String&, SecurityOrigin&); |
| 70 | |
| 71 | private: |
| 72 | DatabaseManager() = default; |
| 73 | ~DatabaseManager() = delete; |
| 74 | |
| 75 | void platformInitialize(const String& databasePath); |
| 76 | |
| 77 | enum OpenAttempt { FirstTryToOpenDatabase, RetryOpenDatabase }; |
| 78 | ExceptionOr<Ref<Database>> openDatabaseBackend(ScriptExecutionContext&, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase); |
| 79 | ExceptionOr<Ref<Database>> tryToOpenDatabaseBackend(ScriptExecutionContext&, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase, OpenAttempt); |
| 80 | |
| 81 | class ProposedDatabase; |
| 82 | void addProposedDatabase(ProposedDatabase&); |
| 83 | void removeProposedDatabase(ProposedDatabase&); |
| 84 | |
| 85 | static void logErrorMessage(ScriptExecutionContext&, const String& message); |
| 86 | |
| 87 | DatabaseManagerClient* m_client { nullptr }; |
| 88 | bool m_databaseIsAvailable { true }; |
| 89 | |
| 90 | Lock m_proposedDatabasesMutex; |
| 91 | HashSet<ProposedDatabase*> m_proposedDatabases; |
| 92 | }; |
| 93 | |
| 94 | } // namespace WebCore |
| 95 | |