| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
| 3 * Copyright (C) 2011 Google, Inc. All Rights Reserved. | 3 * Copyright (C) 2011 Google, Inc. All Rights Reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 : ActiveDOMObject(context) | 98 : ActiveDOMObject(context) |
| 99 , m_hasOpenDatabases(false) | 99 , m_hasOpenDatabases(false) |
| 100 , m_hasRequestedTermination(false) | 100 , m_hasRequestedTermination(false) |
| 101 { | 101 { |
| 102 // ActiveDOMObject expects this to be called to set internal flags. | 102 // ActiveDOMObject expects this to be called to set internal flags. |
| 103 suspendIfNeeded(); | 103 suspendIfNeeded(); |
| 104 | 104 |
| 105 // For debug accounting only. We must do this before we register the | 105 // For debug accounting only. We must do this before we register the |
| 106 // instance. The assertions assume this. | 106 // instance. The assertions assume this. |
| 107 DatabaseManager::manager().didConstructDatabaseContext(); | 107 DatabaseManager::manager().didConstructDatabaseContext(); |
| 108 if (context->isWorkerGlobalScope()) | |
| 109 toWorkerGlobalScope(context)->registerTerminationObserver(this); | |
| 110 } | 108 } |
| 111 | 109 |
| 112 DatabaseContext::~DatabaseContext() | 110 DatabaseContext::~DatabaseContext() |
| 113 { | 111 { |
| 114 // For debug accounting only. We must call this last. The assertions assume | 112 // For debug accounting only. We must call this last. The assertions assume |
| 115 // this. | 113 // this. |
| 116 DatabaseManager::manager().didDestructDatabaseContext(); | 114 DatabaseManager::manager().didDestructDatabaseContext(); |
| 117 } | 115 } |
| 118 | 116 |
| 119 void DatabaseContext::trace(Visitor* visitor) | 117 void DatabaseContext::trace(Visitor* visitor) |
| 120 { | 118 { |
| 121 #if ENABLE(OILPAN) | 119 #if ENABLE(OILPAN) |
| 122 visitor->trace(m_databaseThread); | 120 visitor->trace(m_databaseThread); |
| 123 visitor->trace(m_openSyncDatabases); | |
| 124 #endif | 121 #endif |
| 125 } | 122 } |
| 126 | 123 |
| 127 // This is called if the associated ExecutionContext is destructing while | 124 // This is called if the associated ExecutionContext is destructing while |
| 128 // we're still associated with it. That's our cue to disassociate and shutdown. | 125 // we're still associated with it. That's our cue to disassociate and shutdown. |
| 129 // To do this, we stop the database and let everything shutdown naturally | 126 // To do this, we stop the database and let everything shutdown naturally |
| 130 // because the database closing process may still make use of this context. | 127 // because the database closing process may still make use of this context. |
| 131 // It is not safe to just delete the context here. | 128 // It is not safe to just delete the context here. |
| 132 void DatabaseContext::contextDestroyed() | 129 void DatabaseContext::contextDestroyed() |
| 133 { | 130 { |
| 134 RefPtrWillBeRawPtr<DatabaseContext> protector(this); | 131 RefPtrWillBeRawPtr<DatabaseContext> protector(this); |
| 135 stopDatabases(); | 132 stopDatabases(); |
| 136 DatabaseManager::manager().unregisterDatabaseContext(this); | 133 DatabaseManager::manager().unregisterDatabaseContext(this); |
| 137 ActiveDOMObject::contextDestroyed(); | 134 ActiveDOMObject::contextDestroyed(); |
| 138 } | 135 } |
| 139 | 136 |
| 140 void DatabaseContext::wasRequestedToTerminate() | |
| 141 { | |
| 142 DatabaseManager::manager().interruptAllDatabasesForContext(this); | |
| 143 } | |
| 144 | |
| 145 // stop() is from stopActiveDOMObjects() which indicates that the owner LocalFra
me | 137 // stop() is from stopActiveDOMObjects() which indicates that the owner LocalFra
me |
| 146 // or WorkerThread is shutting down. Initiate the orderly shutdown by stopping | 138 // or WorkerThread is shutting down. Initiate the orderly shutdown by stopping |
| 147 // the associated databases. | 139 // the associated databases. |
| 148 void DatabaseContext::stop() | 140 void DatabaseContext::stop() |
| 149 { | 141 { |
| 150 stopDatabases(); | 142 stopDatabases(); |
| 151 } | 143 } |
| 152 | 144 |
| 153 DatabaseContext* DatabaseContext::backend() | 145 DatabaseContext* DatabaseContext::backend() |
| 154 { | 146 { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 166 | 158 |
| 167 // Create the database thread on first request - but not if at least one
database was already opened, | 159 // Create the database thread on first request - but not if at least one
database was already opened, |
| 168 // because in that case we already had a database thread and terminated
it and should not create another. | 160 // because in that case we already had a database thread and terminated
it and should not create another. |
| 169 m_databaseThread = DatabaseThread::create(); | 161 m_databaseThread = DatabaseThread::create(); |
| 170 m_databaseThread->start(); | 162 m_databaseThread->start(); |
| 171 } | 163 } |
| 172 | 164 |
| 173 return m_databaseThread.get(); | 165 return m_databaseThread.get(); |
| 174 } | 166 } |
| 175 | 167 |
| 176 void DatabaseContext::didOpenDatabase(DatabaseBackendBase& database) | |
| 177 { | |
| 178 if (!database.isSyncDatabase()) | |
| 179 return; | |
| 180 ASSERT(isContextThread()); | |
| 181 #if ENABLE(OILPAN) | |
| 182 m_openSyncDatabases.add(&database, adoptPtr(new DatabaseCloser(database))); | |
| 183 #else | |
| 184 m_openSyncDatabases.add(&database); | |
| 185 #endif | |
| 186 } | |
| 187 | |
| 188 void DatabaseContext::didCloseDatabase(DatabaseBackendBase& database) | |
| 189 { | |
| 190 #if !ENABLE(OILPAN) | |
| 191 if (!database.isSyncDatabase()) | |
| 192 return; | |
| 193 ASSERT(isContextThread()); | |
| 194 m_openSyncDatabases.remove(&database); | |
| 195 #endif | |
| 196 } | |
| 197 | |
| 198 #if ENABLE(OILPAN) | |
| 199 DatabaseContext::DatabaseCloser::~DatabaseCloser() | |
| 200 { | |
| 201 if (m_database.opened()) | |
| 202 m_database.closeDatabase(); | |
| 203 } | |
| 204 #endif | |
| 205 | |
| 206 void DatabaseContext::stopSyncDatabases() | |
| 207 { | |
| 208 // SQLite is "multi-thread safe", but each database handle can only be used | |
| 209 // on a single thread at a time. | |
| 210 // | |
| 211 // For DatabaseBackendSync, we open the SQLite database on the script | |
| 212 // context thread. And hence we should also close it on that same | |
| 213 // thread. This means that the SQLite database need to be closed here in the | |
| 214 // destructor. | |
| 215 ASSERT(isContextThread()); | |
| 216 #if ENABLE(OILPAN) | |
| 217 m_openSyncDatabases.clear(); | |
| 218 #else | |
| 219 Vector<DatabaseBackendBase*> syncDatabases; | |
| 220 copyToVector(m_openSyncDatabases, syncDatabases); | |
| 221 m_openSyncDatabases.clear(); | |
| 222 for (size_t i = 0; i < syncDatabases.size(); ++i) | |
| 223 syncDatabases[i]->closeImmediately(); | |
| 224 #endif | |
| 225 } | |
| 226 | |
| 227 void DatabaseContext::stopDatabases() | 168 void DatabaseContext::stopDatabases() |
| 228 { | 169 { |
| 229 stopSyncDatabases(); | |
| 230 | |
| 231 // Though we initiate termination of the DatabaseThread here in | 170 // Though we initiate termination of the DatabaseThread here in |
| 232 // stopDatabases(), we can't clear the m_databaseThread ref till we get to | 171 // stopDatabases(), we can't clear the m_databaseThread ref till we get to |
| 233 // the destructor. This is because the Databases that are managed by | 172 // the destructor. This is because the Databases that are managed by |
| 234 // DatabaseThread still rely on this ref between the context and the thread | 173 // DatabaseThread still rely on this ref between the context and the thread |
| 235 // to execute the task for closing the database. By the time we get to the | 174 // to execute the task for closing the database. By the time we get to the |
| 236 // destructor, we're guaranteed that the databases are destructed (which is | 175 // destructor, we're guaranteed that the databases are destructed (which is |
| 237 // why our ref count is 0 then and we're destructing). Then, the | 176 // why our ref count is 0 then and we're destructing). Then, the |
| 238 // m_databaseThread RefPtr destructor will deref and delete the | 177 // m_databaseThread RefPtr destructor will deref and delete the |
| 239 // DatabaseThread. | 178 // DatabaseThread. |
| 240 | 179 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 259 { | 198 { |
| 260 return executionContext()->securityOrigin(); | 199 return executionContext()->securityOrigin(); |
| 261 } | 200 } |
| 262 | 201 |
| 263 bool DatabaseContext::isContextThread() const | 202 bool DatabaseContext::isContextThread() const |
| 264 { | 203 { |
| 265 return executionContext()->isContextThread(); | 204 return executionContext()->isContextThread(); |
| 266 } | 205 } |
| 267 | 206 |
| 268 } // namespace blink | 207 } // namespace blink |
| OLD | NEW |