1/*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2011 Google, Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#include "config.h"
29#include "modules/webdatabase/DatabaseContext.h"
30
31#include "core/dom/Document.h"
32#include "core/dom/ExecutionContext.h"
33#include "modules/webdatabase/Database.h"
34#include "modules/webdatabase/DatabaseManager.h"
35#include "modules/webdatabase/DatabaseTask.h"
36#include "modules/webdatabase/DatabaseThread.h"
37#include "platform/weborigin/SchemeRegistry.h"
38#include "platform/weborigin/SecurityOrigin.h"
39#include "wtf/Assertions.h"
40
41namespace WebCore {
42
43// How the DatabaseContext Life-Cycle works?
44// ========================================
45// ... in other words, who's keeping the DatabaseContext alive and how long does
46// it need to stay alive?
47//
48// The DatabaseContext is referenced from RefPtrs in:
49// 1. ExecutionContext
50// 2. Database
51//
52// At Birth:
53// ========
54// We create a DatabaseContext only when there is a need i.e. the script tries to
55// open a Database via DatabaseManager::openDatabase().
56//
57// The DatabaseContext constructor will call ref(). This lets DatabaseContext keep itself alive.
58// Note that paired deref() is called from contextDestroyed().
59//
60// Once a DatabaseContext is associated with a ExecutionContext, it will
61// live until after the ExecutionContext destructs. This is true even if
62// we don't succeed in opening any Databases for that context. When we do
63// succeed in opening Databases for this ExecutionContext, the Database
64// will re-use the same DatabaseContext.
65//
66// At Shutdown:
67// ===========
68// During shutdown, the DatabaseContext needs to:
69// 1. "outlive" the ExecutionContext.
70//    - This is needed because the DatabaseContext needs to remove itself from the
71//      ExecutionContext's ActiveDOMObject list and ContextLifecycleObserver
72//      list. This removal needs to be executed on the script's thread. Hence, we
73//      rely on the ExecutionContext's shutdown process to call
74//      stop() and contextDestroyed() to give us a chance to clean these up from
75//      the script thread.
76//
77// 2. "outlive" the Databases.
78//    - This is because they may make use of the DatabaseContext to execute a close
79//      task and shutdown in an orderly manner. When the Databases are destructed,
80//      they will deref the DatabaseContext from the DatabaseThread.
81//
82// During shutdown, the ExecutionContext is shutting down on the script thread
83// while the Databases are shutting down on the DatabaseThread. Hence, there can be
84// a race condition as to whether the ExecutionContext or the Databases
85// destruct first.
86//
87// The RefPtrs in the Databases and ExecutionContext will ensure that the
88// DatabaseContext will outlive both regardless of which of the 2 destructs first.
89
90PassRefPtr<DatabaseContext> DatabaseContext::create(ExecutionContext* context)
91{
92    RefPtr<DatabaseContext> self = adoptRef(new DatabaseContext(context));
93    self->ref(); // Is deref()-ed on contextDestroyed().
94    return self.release();
95}
96
97DatabaseContext::DatabaseContext(ExecutionContext* context)
98    : ActiveDOMObject(context)
99    , m_hasOpenDatabases(false)
100    , m_isRegistered(true) // will register on construction below.
101    , m_hasRequestedTermination(false)
102{
103    // ActiveDOMObject expects this to be called to set internal flags.
104    suspendIfNeeded();
105
106    // For debug accounting only. We must do this before we register the
107    // instance. The assertions assume this.
108    DatabaseManager::manager().didConstructDatabaseContext();
109
110    DatabaseManager::manager().registerDatabaseContext(this);
111}
112
113DatabaseContext::~DatabaseContext()
114{
115    stopDatabases();
116    ASSERT(!m_databaseThread || m_databaseThread->terminationRequested());
117
118    // For debug accounting only. We must call this last. The assertions assume
119    // this.
120    DatabaseManager::manager().didDestructDatabaseContext();
121}
122
123// This is called if the associated ExecutionContext is destructing while
124// we're still associated with it. That's our cue to disassociate and shutdown.
125// To do this, we stop the database and let everything shutdown naturally
126// because the database closing process may still make use of this context.
127// It is not safe to just delete the context here.
128void DatabaseContext::contextDestroyed()
129{
130    stopDatabases();
131    ActiveDOMObject::contextDestroyed();
132    deref(); // paired with the ref() call on create().
133}
134
135// stop() is from stopActiveDOMObjects() which indicates that the owner Frame
136// or WorkerThread is shutting down. Initiate the orderly shutdown by stopping
137// the associated databases.
138void DatabaseContext::stop()
139{
140    stopDatabases();
141}
142
143PassRefPtr<DatabaseContext> DatabaseContext::backend()
144{
145    DatabaseContext* backend = static_cast<DatabaseContext*>(this);
146    return backend;
147}
148
149DatabaseThread* DatabaseContext::databaseThread()
150{
151    if (!m_databaseThread && !m_hasOpenDatabases) {
152        // It's OK to ask for the m_databaseThread after we've requested
153        // termination because we're still using it to execute the closing
154        // of the database. However, it is NOT OK to create a new thread
155        // after we've requested termination.
156        ASSERT(!m_hasRequestedTermination);
157
158        // Create the database thread on first request - but not if at least one database was already opened,
159        // because in that case we already had a database thread and terminated it and should not create another.
160        m_databaseThread = DatabaseThread::create();
161        m_databaseThread->start();
162    }
163
164    return m_databaseThread.get();
165}
166
167bool DatabaseContext::stopDatabases(DatabaseTaskSynchronizer* cleanupSync)
168{
169    if (m_isRegistered) {
170        DatabaseManager::manager().unregisterDatabaseContext(this);
171        m_isRegistered = false;
172    }
173
174    // Though we initiate termination of the DatabaseThread here in
175    // stopDatabases(), we can't clear the m_databaseThread ref till we get to
176    // the destructor. This is because the Databases that are managed by
177    // DatabaseThread still rely on this ref between the context and the thread
178    // to execute the task for closing the database. By the time we get to the
179    // destructor, we're guaranteed that the databases are destructed (which is
180    // why our ref count is 0 then and we're destructing). Then, the
181    // m_databaseThread RefPtr destructor will deref and delete the
182    // DatabaseThread.
183
184    if (m_databaseThread && !m_hasRequestedTermination) {
185        m_databaseThread->requestTermination(cleanupSync);
186        m_hasRequestedTermination = true;
187        return true;
188    }
189    return false;
190}
191
192bool DatabaseContext::allowDatabaseAccess() const
193{
194    if (executionContext()->isDocument())
195        return toDocument(executionContext())->isActive();
196    ASSERT(executionContext()->isWorkerGlobalScope());
197    // allowDatabaseAccess is not yet implemented for workers.
198    return true;
199}
200
201SecurityOrigin* DatabaseContext::securityOrigin() const
202{
203    return executionContext()->securityOrigin();
204}
205
206bool DatabaseContext::isContextThread() const
207{
208    return executionContext()->isContextThread();
209}
210
211} // namespace WebCore
212