| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // while the Databases are shutting down on the DatabaseThread. Hence, there can
be | 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 | 84 // a race condition as to whether the ExecutionContext or the Databases |
| 85 // destruct first. | 85 // destruct first. |
| 86 // | 86 // |
| 87 // The RefPtrs in the Databases and ExecutionContext will ensure that the | 87 // The RefPtrs in the Databases and ExecutionContext will ensure that the |
| 88 // DatabaseContext will outlive both regardless of which of the 2 destructs firs
t. | 88 // DatabaseContext will outlive both regardless of which of the 2 destructs firs
t. |
| 89 | 89 |
| 90 PassRefPtr<DatabaseContext> DatabaseContext::create(ExecutionContext* context) | 90 PassRefPtr<DatabaseContext> DatabaseContext::create(ExecutionContext* context) |
| 91 { | 91 { |
| 92 RefPtr<DatabaseContext> self = adoptRef(new DatabaseContext(context)); | 92 RefPtr<DatabaseContext> self = adoptRef(new DatabaseContext(context)); |
| 93 self->ref(); // Is deref()-ed on contextDestroyed(). | 93 DatabaseManager::manager().registerDatabaseContext(self.get()); |
| 94 return self.release(); | 94 return self.release(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 DatabaseContext::DatabaseContext(ExecutionContext* context) | 97 DatabaseContext::DatabaseContext(ExecutionContext* context) |
| 98 : ActiveDOMObject(context) | 98 : ActiveDOMObject(context) |
| 99 , m_hasOpenDatabases(false) | 99 , m_hasOpenDatabases(false) |
| 100 , m_isRegistered(true) // will register on construction below. | |
| 101 , m_hasRequestedTermination(false) | 100 , m_hasRequestedTermination(false) |
| 102 { | 101 { |
| 103 // ActiveDOMObject expects this to be called to set internal flags. | 102 // ActiveDOMObject expects this to be called to set internal flags. |
| 104 suspendIfNeeded(); | 103 suspendIfNeeded(); |
| 105 | 104 |
| 106 // 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 |
| 107 // instance. The assertions assume this. | 106 // instance. The assertions assume this. |
| 108 DatabaseManager::manager().didConstructDatabaseContext(); | 107 DatabaseManager::manager().didConstructDatabaseContext(); |
| 109 | |
| 110 DatabaseManager::manager().registerDatabaseContext(this); | |
| 111 } | 108 } |
| 112 | 109 |
| 113 DatabaseContext::~DatabaseContext() | 110 DatabaseContext::~DatabaseContext() |
| 114 { | 111 { |
| 115 ASSERT(!m_databaseThread || m_databaseThread->terminationRequested()); | 112 ASSERT(!m_databaseThread || m_databaseThread->terminationRequested()); |
| 116 | 113 |
| 117 // For debug accounting only. We must call this last. The assertions assume | 114 // For debug accounting only. We must call this last. The assertions assume |
| 118 // this. | 115 // this. |
| 119 DatabaseManager::manager().didDestructDatabaseContext(); | 116 DatabaseManager::manager().didDestructDatabaseContext(); |
| 120 } | 117 } |
| 121 | 118 |
| 122 // This is called if the associated ExecutionContext is destructing while | 119 // This is called if the associated ExecutionContext is destructing while |
| 123 // we're still associated with it. That's our cue to disassociate and shutdown. | 120 // we're still associated with it. That's our cue to disassociate and shutdown. |
| 124 // To do this, we stop the database and let everything shutdown naturally | 121 // To do this, we stop the database and let everything shutdown naturally |
| 125 // because the database closing process may still make use of this context. | 122 // because the database closing process may still make use of this context. |
| 126 // It is not safe to just delete the context here. | 123 // It is not safe to just delete the context here. |
| 127 void DatabaseContext::contextDestroyed() | 124 void DatabaseContext::contextDestroyed() |
| 128 { | 125 { |
| 126 RefPtr<DatabaseContext> protector(this); |
| 129 stopDatabases(); | 127 stopDatabases(); |
| 128 DatabaseManager::manager().unregisterDatabaseContext(this); |
| 130 ActiveDOMObject::contextDestroyed(); | 129 ActiveDOMObject::contextDestroyed(); |
| 131 deref(); // paired with the ref() call on create(). | |
| 132 } | 130 } |
| 133 | 131 |
| 134 void DatabaseContext::willStop() | 132 void DatabaseContext::willStop() |
| 135 { | 133 { |
| 136 DatabaseManager::manager().interruptAllDatabasesForContext(this); | 134 DatabaseManager::manager().interruptAllDatabasesForContext(this); |
| 137 } | 135 } |
| 138 | 136 |
| 139 // stop() is from stopActiveDOMObjects() which indicates that the owner LocalFra
me | 137 // stop() is from stopActiveDOMObjects() which indicates that the owner LocalFra
me |
| 140 // or WorkerThread is shutting down. Initiate the orderly shutdown by stopping | 138 // or WorkerThread is shutting down. Initiate the orderly shutdown by stopping |
| 141 // the associated databases. | 139 // the associated databases. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 Vector<DatabaseBackendBase*> syncDatabases; | 194 Vector<DatabaseBackendBase*> syncDatabases; |
| 197 copyToVector(m_openSyncDatabases, syncDatabases); | 195 copyToVector(m_openSyncDatabases, syncDatabases); |
| 198 m_openSyncDatabases.clear(); | 196 m_openSyncDatabases.clear(); |
| 199 for (size_t i = 0; i < syncDatabases.size(); ++i) | 197 for (size_t i = 0; i < syncDatabases.size(); ++i) |
| 200 syncDatabases[i]->closeImmediately(); | 198 syncDatabases[i]->closeImmediately(); |
| 201 } | 199 } |
| 202 | 200 |
| 203 void DatabaseContext::stopDatabases() | 201 void DatabaseContext::stopDatabases() |
| 204 { | 202 { |
| 205 stopSyncDatabases(); | 203 stopSyncDatabases(); |
| 206 if (m_isRegistered) { | |
| 207 DatabaseManager::manager().unregisterDatabaseContext(this); | |
| 208 m_isRegistered = false; | |
| 209 } | |
| 210 | 204 |
| 211 // Though we initiate termination of the DatabaseThread here in | 205 // Though we initiate termination of the DatabaseThread here in |
| 212 // stopDatabases(), we can't clear the m_databaseThread ref till we get to | 206 // stopDatabases(), we can't clear the m_databaseThread ref till we get to |
| 213 // the destructor. This is because the Databases that are managed by | 207 // the destructor. This is because the Databases that are managed by |
| 214 // DatabaseThread still rely on this ref between the context and the thread | 208 // DatabaseThread still rely on this ref between the context and the thread |
| 215 // to execute the task for closing the database. By the time we get to the | 209 // to execute the task for closing the database. By the time we get to the |
| 216 // destructor, we're guaranteed that the databases are destructed (which is | 210 // destructor, we're guaranteed that the databases are destructed (which is |
| 217 // why our ref count is 0 then and we're destructing). Then, the | 211 // why our ref count is 0 then and we're destructing). Then, the |
| 218 // m_databaseThread RefPtr destructor will deref and delete the | 212 // m_databaseThread RefPtr destructor will deref and delete the |
| 219 // DatabaseThread. | 213 // DatabaseThread. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 239 { | 233 { |
| 240 return executionContext()->securityOrigin(); | 234 return executionContext()->securityOrigin(); |
| 241 } | 235 } |
| 242 | 236 |
| 243 bool DatabaseContext::isContextThread() const | 237 bool DatabaseContext::isContextThread() const |
| 244 { | 238 { |
| 245 return executionContext()->isContextThread(); | 239 return executionContext()->isContextThread(); |
| 246 } | 240 } |
| 247 | 241 |
| 248 } // namespace WebCore | 242 } // namespace WebCore |
| OLD | NEW |