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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // ======================================== | 43 // ======================================== |
44 // ... in other words, who's keeping the DatabaseContext alive and how long does | 44 // ... in other words, who's keeping the DatabaseContext alive and how long does |
45 // it need to stay alive? | 45 // it need to stay alive? |
46 // | 46 // |
47 // The DatabaseContext is referenced from: | 47 // The DatabaseContext is referenced from: |
48 // 1. DatabaseManager | 48 // 1. DatabaseManager |
49 // 2. Database | 49 // 2. Database |
50 // | 50 // |
51 // At Birth: | 51 // At Birth: |
52 // ======== | 52 // ======== |
53 // We create a DatabaseContext only when there is a need i.e. the script tries t
o | 53 // We create a DatabaseContext only when there is a need i.e. the script tries |
54 // open a Database via DatabaseManager::openDatabase(). | 54 // to open a Database via DatabaseManager::openDatabase(). |
55 // | 55 // |
56 // The DatabaseContext constructor will register itself to DatabaseManager. This | 56 // The DatabaseContext constructor will register itself to DatabaseManager. This |
57 // lets DatabaseContext keep itself alive until it is unregisterd in | 57 // lets DatabaseContext keep itself alive until it is unregisterd in |
58 // contextDestroyed(). | 58 // contextDestroyed(). |
59 // | 59 // |
60 // Once a DatabaseContext is associated with a ExecutionContext, it will | 60 // Once a DatabaseContext is associated with a ExecutionContext, it will |
61 // live until after the ExecutionContext destructs. This is true even if | 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 | 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 t
he | 70 // - This is needed because the DatabaseContext needs to remove itself from |
| 71 // the |
71 // ExecutionContext's ActiveDOMObject list and ContextLifecycleObserver | 72 // ExecutionContext's ActiveDOMObject list and ContextLifecycleObserver |
72 // list. This removal needs to be executed on the script's thread. Hence, w
e | 73 // list. This removal needs to be executed on the script's thread. Hence, |
| 74 // we |
73 // rely on the ExecutionContext's shutdown process to call | 75 // rely on the ExecutionContext's shutdown process to call |
74 // stop() and contextDestroyed() to give us a chance to clean these up from | 76 // stop() and contextDestroyed() to give us a chance to clean these up from |
75 // the script thread. | 77 // the script thread. |
76 // | 78 // |
77 // 2. "outlive" the Databases. | 79 // 2. "outlive" the Databases. |
78 // - This is because they may make use of the DatabaseContext to execute a cl
ose | 80 // - This is because they may make use of the DatabaseContext to execute a |
79 // task and shutdown in an orderly manner. When the Databases are destructe
d, | 81 // close task and shutdown in an orderly manner. When the Databases are |
80 // they will release the DatabaseContext reference from the DatabaseThread. | 82 // destructed, they will release the DatabaseContext reference from the |
| 83 // DatabaseThread. |
81 // | 84 // |
82 // During shutdown, the ExecutionContext is shutting down on the script thread | 85 // 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 | 86 // while the Databases are shutting down on the DatabaseThread. Hence, there can |
84 // a race condition as to whether the ExecutionContext or the Databases | 87 // be a race condition as to whether the ExecutionContext or the Databases |
85 // destruct first. | 88 // destruct first. |
86 // | 89 // |
87 // The Members in the Databases and DatabaseManager will ensure that the | 90 // The Members in the Databases and DatabaseManager will ensure that the |
88 // DatabaseContext will outlive Database and ExecutionContext regardless of | 91 // DatabaseContext will outlive Database and ExecutionContext regardless of |
89 // which of the 2 destructs first. | 92 // which of the 2 destructs first. |
90 | 93 |
91 DatabaseContext* DatabaseContext::create(ExecutionContext* context) { | 94 DatabaseContext* DatabaseContext::create(ExecutionContext* context) { |
92 DatabaseContext* self = new DatabaseContext(context); | 95 DatabaseContext* self = new DatabaseContext(context); |
93 DatabaseManager::manager().registerDatabaseContext(self); | 96 DatabaseManager::manager().registerDatabaseContext(self); |
94 return self; | 97 return self; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 } | 145 } |
143 | 146 |
144 DatabaseThread* DatabaseContext::databaseThread() { | 147 DatabaseThread* DatabaseContext::databaseThread() { |
145 if (!m_databaseThread && !m_hasOpenDatabases) { | 148 if (!m_databaseThread && !m_hasOpenDatabases) { |
146 // It's OK to ask for the m_databaseThread after we've requested | 149 // It's OK to ask for the m_databaseThread after we've requested |
147 // termination because we're still using it to execute the closing | 150 // termination because we're still using it to execute the closing |
148 // of the database. However, it is NOT OK to create a new thread | 151 // of the database. However, it is NOT OK to create a new thread |
149 // after we've requested termination. | 152 // after we've requested termination. |
150 ASSERT(!m_hasRequestedTermination); | 153 ASSERT(!m_hasRequestedTermination); |
151 | 154 |
152 // Create the database thread on first request - but not if at least one dat
abase was already opened, | 155 // Create the database thread on first request - but not if at least one |
153 // because in that case we already had a database thread and terminated it a
nd should not create another. | 156 // database was already opened, because in that case we already had a |
| 157 // database thread and terminated it and should not create another. |
154 m_databaseThread = DatabaseThread::create(); | 158 m_databaseThread = DatabaseThread::create(); |
155 m_databaseThread->start(); | 159 m_databaseThread->start(); |
156 } | 160 } |
157 | 161 |
158 return m_databaseThread.get(); | 162 return m_databaseThread.get(); |
159 } | 163 } |
160 | 164 |
161 bool DatabaseContext::databaseThreadAvailable() { | 165 bool DatabaseContext::databaseThreadAvailable() { |
162 return databaseThread() && !m_hasRequestedTermination; | 166 return databaseThread() && !m_hasRequestedTermination; |
163 } | 167 } |
(...skipping 22 matching lines...) Expand all Loading... |
186 | 190 |
187 SecurityOrigin* DatabaseContext::getSecurityOrigin() const { | 191 SecurityOrigin* DatabaseContext::getSecurityOrigin() const { |
188 return getExecutionContext()->getSecurityOrigin(); | 192 return getExecutionContext()->getSecurityOrigin(); |
189 } | 193 } |
190 | 194 |
191 bool DatabaseContext::isContextThread() const { | 195 bool DatabaseContext::isContextThread() const { |
192 return getExecutionContext()->isContextThread(); | 196 return getExecutionContext()->isContextThread(); |
193 } | 197 } |
194 | 198 |
195 } // namespace blink | 199 } // namespace blink |
OLD | NEW |