| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 // we don't succeed in opening any Databases for that context. When we do | 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 | 63 // succeed in opening Databases for this ExecutionContext, the Database |
| 64 // will re-use the same DatabaseContext. | 64 // will re-use the same DatabaseContext. |
| 65 // | 65 // |
| 66 // At Shutdown: | 66 // At Shutdown: |
| 67 // =========== | 67 // =========== |
| 68 // During shutdown, the DatabaseContext needs to: | 68 // During shutdown, the DatabaseContext needs to: |
| 69 // 1. "outlive" the ExecutionContext. | 69 // 1. "outlive" the ExecutionContext. |
| 70 // - This is needed because the DatabaseContext needs to remove itself from | 70 // - This is needed because the DatabaseContext needs to remove itself from |
| 71 // the | 71 // the |
| 72 // ExecutionContext's SuspendableObject list and ContextLifecycleObserver | 72 // ExecutionContext's ContextLifecycleObserver list and |
| 73 // ContextLifecycleObserver |
| 73 // list. This removal needs to be executed on the script's thread. Hence, | 74 // list. This removal needs to be executed on the script's thread. Hence, |
| 74 // we | 75 // we |
| 75 // rely on the ExecutionContext's shutdown process to call | 76 // rely on the ExecutionContext's shutdown process to call |
| 76 // stop() and contextDestroyed() to give us a chance to clean these up from | 77 // stop() and contextDestroyed() to give us a chance to clean these up from |
| 77 // the script thread. | 78 // the script thread. |
| 78 // | 79 // |
| 79 // 2. "outlive" the Databases. | 80 // 2. "outlive" the Databases. |
| 80 // - This is because they may make use of the DatabaseContext to execute a | 81 // - This is because they may make use of the DatabaseContext to execute a |
| 81 // close task and shutdown in an orderly manner. When the Databases are | 82 // close task and shutdown in an orderly manner. When the Databases are |
| 82 // destructed, they will release the DatabaseContext reference from the | 83 // destructed, they will release the DatabaseContext reference from the |
| 83 // DatabaseThread. | 84 // DatabaseThread. |
| 84 // | 85 // |
| 85 // During shutdown, the ExecutionContext is shutting down on the script thread | 86 // During shutdown, the ExecutionContext is shutting down on the script thread |
| 86 // while the Databases are shutting down on the DatabaseThread. Hence, there can | 87 // while the Databases are shutting down on the DatabaseThread. Hence, there can |
| 87 // be a race condition as to whether the ExecutionContext or the Databases | 88 // be a race condition as to whether the ExecutionContext or the Databases |
| 88 // destruct first. | 89 // destruct first. |
| 89 // | 90 // |
| 90 // The Members in the Databases and DatabaseManager will ensure that the | 91 // The Members in the Databases and DatabaseManager will ensure that the |
| 91 // DatabaseContext will outlive Database and ExecutionContext regardless of | 92 // DatabaseContext will outlive Database and ExecutionContext regardless of |
| 92 // which of the 2 destructs first. | 93 // which of the 2 destructs first. |
| 93 | 94 |
| 94 DatabaseContext* DatabaseContext::create(ExecutionContext* context) { | 95 DatabaseContext* DatabaseContext::create(ExecutionContext* context) { |
| 95 DatabaseContext* self = new DatabaseContext(context); | 96 DatabaseContext* self = new DatabaseContext(context); |
| 96 DatabaseManager::manager().registerDatabaseContext(self); | 97 DatabaseManager::manager().registerDatabaseContext(self); |
| 97 return self; | 98 return self; |
| 98 } | 99 } |
| 99 | 100 |
| 100 DatabaseContext::DatabaseContext(ExecutionContext* context) | 101 DatabaseContext::DatabaseContext(ExecutionContext* context) |
| 101 : SuspendableObject(context), | 102 : ContextLifecycleObserver(context), |
| 102 m_hasOpenDatabases(false), | 103 m_hasOpenDatabases(false), |
| 103 m_hasRequestedTermination(false) { | 104 m_hasRequestedTermination(false) { |
| 104 DCHECK(isMainThread()); | 105 DCHECK(isMainThread()); |
| 105 | 106 |
| 106 // SuspendableObject expects this to be called to set internal flags. | |
| 107 suspendIfNeeded(); | |
| 108 | |
| 109 // For debug accounting only. We must do this before we register the | 107 // For debug accounting only. We must do this before we register the |
| 110 // instance. The assertions assume this. | 108 // instance. The assertions assume this. |
| 111 DatabaseManager::manager().didConstructDatabaseContext(); | 109 DatabaseManager::manager().didConstructDatabaseContext(); |
| 112 } | 110 } |
| 113 | 111 |
| 114 DatabaseContext::~DatabaseContext() { | 112 DatabaseContext::~DatabaseContext() { |
| 115 // For debug accounting only. We must call this last. The assertions assume | 113 // For debug accounting only. We must call this last. The assertions assume |
| 116 // this. | 114 // this. |
| 117 DatabaseManager::manager().didDestructDatabaseContext(); | 115 DatabaseManager::manager().didDestructDatabaseContext(); |
| 118 } | 116 } |
| 119 | 117 |
| 120 DEFINE_TRACE(DatabaseContext) { | 118 DEFINE_TRACE(DatabaseContext) { |
| 121 visitor->trace(m_databaseThread); | 119 visitor->trace(m_databaseThread); |
| 122 SuspendableObject::trace(visitor); | 120 ContextLifecycleObserver::trace(visitor); |
| 123 } | 121 } |
| 124 | 122 |
| 125 // This is called if the associated ExecutionContext is destructing while | 123 // This is called if the associated ExecutionContext is destructing while |
| 126 // we're still associated with it. That's our cue to disassociate and shutdown. | 124 // we're still associated with it. That's our cue to disassociate and shutdown. |
| 127 // To do this, we stop the database and let everything shutdown naturally | 125 // To do this, we stop the database and let everything shutdown naturally |
| 128 // because the database closing process may still make use of this context. | 126 // because the database closing process may still make use of this context. |
| 129 // It is not safe to just delete the context here. | 127 // It is not safe to just delete the context here. |
| 130 void DatabaseContext::contextDestroyed() { | 128 void DatabaseContext::contextDestroyed() { |
| 131 stopDatabases(); | 129 stopDatabases(); |
| 132 DatabaseManager::manager().unregisterDatabaseContext(this); | 130 DatabaseManager::manager().unregisterDatabaseContext(this); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 180 |
| 183 SecurityOrigin* DatabaseContext::getSecurityOrigin() const { | 181 SecurityOrigin* DatabaseContext::getSecurityOrigin() const { |
| 184 return getExecutionContext()->getSecurityOrigin(); | 182 return getExecutionContext()->getSecurityOrigin(); |
| 185 } | 183 } |
| 186 | 184 |
| 187 bool DatabaseContext::isContextThread() const { | 185 bool DatabaseContext::isContextThread() const { |
| 188 return getExecutionContext()->isContextThread(); | 186 return getExecutionContext()->isContextThread(); |
| 189 } | 187 } |
| 190 | 188 |
| 191 } // namespace blink | 189 } // namespace blink |
| OLD | NEW |