OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
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 * | 10 * |
(...skipping 23 matching lines...) Expand all Loading... | |
34 #include "modules/indexeddb/IDBMetadata.h" | 34 #include "modules/indexeddb/IDBMetadata.h" |
35 #include "modules/indexeddb/IDBRequest.h" | 35 #include "modules/indexeddb/IDBRequest.h" |
36 #include "modules/indexeddb/IDBValue.h" | 36 #include "modules/indexeddb/IDBValue.h" |
37 #include "platform/SharedBuffer.h" | 37 #include "platform/SharedBuffer.h" |
38 #include "public/platform/modules/indexeddb/WebIDBCursor.h" | 38 #include "public/platform/modules/indexeddb/WebIDBCursor.h" |
39 #include "public/platform/modules/indexeddb/WebIDBDatabase.h" | 39 #include "public/platform/modules/indexeddb/WebIDBDatabase.h" |
40 #include "public/platform/modules/indexeddb/WebIDBDatabaseError.h" | 40 #include "public/platform/modules/indexeddb/WebIDBDatabaseError.h" |
41 #include "public/platform/modules/indexeddb/WebIDBKey.h" | 41 #include "public/platform/modules/indexeddb/WebIDBKey.h" |
42 #include "public/platform/modules/indexeddb/WebIDBValue.h" | 42 #include "public/platform/modules/indexeddb/WebIDBValue.h" |
43 #include "wtf/PtrUtil.h" | 43 #include "wtf/PtrUtil.h" |
44 #include "wtf/ThreadSpecific.h" | |
44 #include <memory> | 45 #include <memory> |
45 | 46 |
46 using blink::WebIDBCursor; | 47 using blink::WebIDBCursor; |
47 using blink::WebIDBDatabase; | 48 using blink::WebIDBDatabase; |
48 using blink::WebIDBDatabaseError; | 49 using blink::WebIDBDatabaseError; |
49 using blink::WebIDBKey; | 50 using blink::WebIDBKey; |
50 using blink::WebIDBKeyPath; | 51 using blink::WebIDBKeyPath; |
51 using blink::WebIDBMetadata; | 52 using blink::WebIDBMetadata; |
52 using blink::WebIDBValue; | 53 using blink::WebIDBValue; |
53 using blink::WebVector; | 54 using blink::WebVector; |
54 | 55 |
55 namespace blink { | 56 namespace blink { |
56 | 57 |
58 // This thread-specific list keeps track of instances of WebIDBCallbacksImpl | |
59 // created by each thread. If a thread exits before they are destroyed then they | |
60 // would otherwise be leaked because the IO thread can no longer post a task to | |
61 // the thread on which they were created. | |
62 typedef std::vector<std::unique_ptr<WebIDBCallbacksImpl>> CallbacksList; | |
dcheng
2016/10/05 08:00:03
Nit: Prefer type aliases (using NewType = Definiti
Reilly Grant (use Gerrit)
2016/10/05 09:16:26
Done.
| |
63 static ThreadSpecific<CallbacksList>& outstandingCallbacks() { | |
64 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<CallbacksList>, callbacks, | |
65 new ThreadSpecific<CallbacksList>); | |
66 return callbacks; | |
67 } | |
68 | |
57 // static | 69 // static |
58 std::unique_ptr<WebIDBCallbacksImpl> WebIDBCallbacksImpl::create( | 70 std::unique_ptr<WebIDBCallbacksImpl> WebIDBCallbacksImpl::create( |
59 IDBRequest* request) { | 71 IDBRequest* request) { |
60 return wrapUnique(new WebIDBCallbacksImpl(request)); | 72 return wrapUnique(new WebIDBCallbacksImpl(request)); |
61 } | 73 } |
62 | 74 |
63 WebIDBCallbacksImpl::WebIDBCallbacksImpl(IDBRequest* request) | 75 WebIDBCallbacksImpl::WebIDBCallbacksImpl(IDBRequest* request) |
64 : m_request(request) { | 76 : m_request(request) { |
65 InspectorInstrumentation::asyncTaskScheduled( | 77 InspectorInstrumentation::asyncTaskScheduled( |
66 m_request->getExecutionContext(), IndexedDBNames::IndexedDB, this, true); | 78 m_request->getExecutionContext(), IndexedDBNames::IndexedDB, this, true); |
79 outstandingCallbacks()->push_back(wrapUnique(this)); | |
67 } | 80 } |
68 | 81 |
69 WebIDBCallbacksImpl::~WebIDBCallbacksImpl() { | 82 WebIDBCallbacksImpl::~WebIDBCallbacksImpl() { |
70 InspectorInstrumentation::asyncTaskCanceled(m_request->getExecutionContext(), | 83 if (m_request) { |
71 this); | 84 InspectorInstrumentation::asyncTaskCanceled( |
85 m_request->getExecutionContext(), this); | |
86 m_request->webCallbacksDestroyed(); | |
87 } | |
88 | |
89 CallbacksList& callbacks = *outstandingCallbacks(); | |
90 auto it = | |
91 std::find_if(callbacks.begin(), callbacks.end(), | |
92 [this](const std::unique_ptr<WebIDBCallbacksImpl>& element) { | |
93 return element.get() == this; | |
94 }); | |
95 if (it != callbacks.end()) { | |
96 it->release(); | |
97 callbacks.erase(it); | |
98 } | |
72 } | 99 } |
73 | 100 |
74 void WebIDBCallbacksImpl::onError(const WebIDBDatabaseError& error) { | 101 void WebIDBCallbacksImpl::onError(const WebIDBDatabaseError& error) { |
75 InspectorInstrumentation::AsyncTask asyncTask( | 102 if (m_request) { |
76 m_request->getExecutionContext(), this); | 103 InspectorInstrumentation::AsyncTask asyncTask( |
77 m_request->onError(DOMException::create(error.code(), error.message())); | 104 m_request->getExecutionContext(), this); |
105 m_request->onError(DOMException::create(error.code(), error.message())); | |
106 } | |
78 } | 107 } |
79 | 108 |
80 void WebIDBCallbacksImpl::onSuccess(const WebVector<WebString>& webStringList) { | 109 void WebIDBCallbacksImpl::onSuccess(const WebVector<WebString>& webStringList) { |
81 Vector<String> stringList; | 110 if (m_request) { |
82 for (size_t i = 0; i < webStringList.size(); ++i) | 111 Vector<String> stringList; |
83 stringList.append(webStringList[i]); | 112 for (size_t i = 0; i < webStringList.size(); ++i) |
84 InspectorInstrumentation::AsyncTask asyncTask( | 113 stringList.append(webStringList[i]); |
85 m_request->getExecutionContext(), this); | 114 InspectorInstrumentation::AsyncTask asyncTask( |
86 m_request->onSuccess(stringList); | 115 m_request->getExecutionContext(), this); |
116 m_request->onSuccess(stringList); | |
117 } | |
87 } | 118 } |
88 | 119 |
89 void WebIDBCallbacksImpl::onSuccess(WebIDBCursor* cursor, | 120 void WebIDBCallbacksImpl::onSuccess(WebIDBCursor* cursor, |
90 const WebIDBKey& key, | 121 const WebIDBKey& key, |
91 const WebIDBKey& primaryKey, | 122 const WebIDBKey& primaryKey, |
92 const WebIDBValue& value) { | 123 const WebIDBValue& value) { |
93 InspectorInstrumentation::AsyncTask asyncTask( | 124 if (m_request) { |
94 m_request->getExecutionContext(), this); | 125 InspectorInstrumentation::AsyncTask asyncTask( |
95 m_request->onSuccess(wrapUnique(cursor), key, primaryKey, | 126 m_request->getExecutionContext(), this); |
96 IDBValue::create(value)); | 127 m_request->onSuccess(wrapUnique(cursor), key, primaryKey, |
128 IDBValue::create(value)); | |
129 } | |
97 } | 130 } |
98 | 131 |
99 void WebIDBCallbacksImpl::onSuccess(WebIDBDatabase* backend, | 132 void WebIDBCallbacksImpl::onSuccess(WebIDBDatabase* backend, |
100 const WebIDBMetadata& metadata) { | 133 const WebIDBMetadata& metadata) { |
101 InspectorInstrumentation::AsyncTask asyncTask( | 134 std::unique_ptr<WebIDBDatabase> db = wrapUnique(backend); |
102 m_request->getExecutionContext(), this); | 135 if (m_request) { |
103 m_request->onSuccess(wrapUnique(backend), IDBDatabaseMetadata(metadata)); | 136 InspectorInstrumentation::AsyncTask asyncTask( |
137 m_request->getExecutionContext(), this); | |
138 m_request->onSuccess(std::move(db), IDBDatabaseMetadata(metadata)); | |
139 } else if (db) { | |
140 db->close(); | |
141 } | |
104 } | 142 } |
105 | 143 |
106 void WebIDBCallbacksImpl::onSuccess(const WebIDBKey& key) { | 144 void WebIDBCallbacksImpl::onSuccess(const WebIDBKey& key) { |
107 InspectorInstrumentation::AsyncTask asyncTask( | 145 if (m_request) { |
108 m_request->getExecutionContext(), this); | 146 InspectorInstrumentation::AsyncTask asyncTask( |
109 m_request->onSuccess(key); | 147 m_request->getExecutionContext(), this); |
148 m_request->onSuccess(key); | |
149 } | |
110 } | 150 } |
111 | 151 |
112 void WebIDBCallbacksImpl::onSuccess(const WebIDBValue& value) { | 152 void WebIDBCallbacksImpl::onSuccess(const WebIDBValue& value) { |
113 InspectorInstrumentation::AsyncTask asyncTask( | 153 if (m_request) { |
114 m_request->getExecutionContext(), this); | 154 InspectorInstrumentation::AsyncTask asyncTask( |
115 m_request->onSuccess(IDBValue::create(value)); | 155 m_request->getExecutionContext(), this); |
156 m_request->onSuccess(IDBValue::create(value)); | |
157 } | |
116 } | 158 } |
117 | 159 |
118 void WebIDBCallbacksImpl::onSuccess(const WebVector<WebIDBValue>& values) { | 160 void WebIDBCallbacksImpl::onSuccess(const WebVector<WebIDBValue>& values) { |
119 InspectorInstrumentation::AsyncTask asyncTask( | 161 if (m_request) { |
120 m_request->getExecutionContext(), this); | 162 InspectorInstrumentation::AsyncTask asyncTask( |
121 Vector<RefPtr<IDBValue>> idbValues(values.size()); | 163 m_request->getExecutionContext(), this); |
122 for (size_t i = 0; i < values.size(); ++i) | 164 Vector<RefPtr<IDBValue>> idbValues(values.size()); |
123 idbValues[i] = IDBValue::create(values[i]); | 165 for (size_t i = 0; i < values.size(); ++i) |
124 m_request->onSuccess(idbValues); | 166 idbValues[i] = IDBValue::create(values[i]); |
167 m_request->onSuccess(idbValues); | |
168 } | |
125 } | 169 } |
126 | 170 |
127 void WebIDBCallbacksImpl::onSuccess(long long value) { | 171 void WebIDBCallbacksImpl::onSuccess(long long value) { |
128 InspectorInstrumentation::AsyncTask asyncTask( | 172 if (m_request) { |
129 m_request->getExecutionContext(), this); | 173 InspectorInstrumentation::AsyncTask asyncTask( |
130 m_request->onSuccess(value); | 174 m_request->getExecutionContext(), this); |
175 m_request->onSuccess(value); | |
176 } | |
131 } | 177 } |
132 | 178 |
133 void WebIDBCallbacksImpl::onSuccess() { | 179 void WebIDBCallbacksImpl::onSuccess() { |
134 InspectorInstrumentation::AsyncTask asyncTask( | 180 if (m_request) { |
135 m_request->getExecutionContext(), this); | 181 InspectorInstrumentation::AsyncTask asyncTask( |
136 m_request->onSuccess(); | 182 m_request->getExecutionContext(), this); |
183 m_request->onSuccess(); | |
184 } | |
137 } | 185 } |
138 | 186 |
139 void WebIDBCallbacksImpl::onSuccess(const WebIDBKey& key, | 187 void WebIDBCallbacksImpl::onSuccess(const WebIDBKey& key, |
140 const WebIDBKey& primaryKey, | 188 const WebIDBKey& primaryKey, |
141 const WebIDBValue& value) { | 189 const WebIDBValue& value) { |
142 InspectorInstrumentation::AsyncTask asyncTask( | 190 if (m_request) { |
143 m_request->getExecutionContext(), this); | 191 InspectorInstrumentation::AsyncTask asyncTask( |
144 m_request->onSuccess(key, primaryKey, IDBValue::create(value)); | 192 m_request->getExecutionContext(), this); |
193 m_request->onSuccess(key, primaryKey, IDBValue::create(value)); | |
194 } | |
145 } | 195 } |
146 | 196 |
147 void WebIDBCallbacksImpl::onBlocked(long long oldVersion) { | 197 void WebIDBCallbacksImpl::onBlocked(long long oldVersion) { |
148 InspectorInstrumentation::AsyncTask asyncTask( | 198 if (m_request) { |
149 m_request->getExecutionContext(), this); | 199 InspectorInstrumentation::AsyncTask asyncTask( |
150 m_request->onBlocked(oldVersion); | 200 m_request->getExecutionContext(), this); |
201 m_request->onBlocked(oldVersion); | |
202 } | |
151 } | 203 } |
152 | 204 |
153 void WebIDBCallbacksImpl::onUpgradeNeeded(long long oldVersion, | 205 void WebIDBCallbacksImpl::onUpgradeNeeded(long long oldVersion, |
154 WebIDBDatabase* database, | 206 WebIDBDatabase* database, |
155 const WebIDBMetadata& metadata, | 207 const WebIDBMetadata& metadata, |
156 unsigned short dataLoss, | 208 unsigned short dataLoss, |
157 WebString dataLossMessage) { | 209 WebString dataLossMessage) { |
158 InspectorInstrumentation::AsyncTask asyncTask( | 210 std::unique_ptr<WebIDBDatabase> db = wrapUnique(database); |
159 m_request->getExecutionContext(), this); | 211 if (m_request) { |
160 m_request->onUpgradeNeeded( | 212 InspectorInstrumentation::AsyncTask asyncTask( |
161 oldVersion, wrapUnique(database), IDBDatabaseMetadata(metadata), | 213 m_request->getExecutionContext(), this); |
162 static_cast<WebIDBDataLoss>(dataLoss), dataLossMessage); | 214 m_request->onUpgradeNeeded( |
215 oldVersion, std::move(db), IDBDatabaseMetadata(metadata), | |
216 static_cast<WebIDBDataLoss>(dataLoss), dataLossMessage); | |
217 } else { | |
218 db->close(); | |
219 } | |
220 } | |
221 | |
222 void WebIDBCallbacksImpl::detach() { | |
223 m_request.clear(); | |
163 } | 224 } |
164 | 225 |
165 } // namespace blink | 226 } // namespace blink |
OLD | NEW |