Bug 1006562 - WorkerDataStoreCursor.store should be equal to the WorkerDataStore which owns the cursor (part 2, WorkerDataStoreCursor.store == WorkerDataStore). r=baku

This commit is contained in:
Gene Lian 2014-05-07 19:55:23 +08:00
parent de94cd3210
commit 9e2b4ca60d
4 changed files with 31 additions and 71 deletions

View File

@ -38,10 +38,7 @@
function testBasicInterface() {
var cursor = gStore.sync();
ok(cursor, "Cursor is created");
// TODO This needs more love for running on workers. Tend to fire a
// follow-up for this...
// is(cursor.store, gStore, "Cursor.store is the store");
is(cursor.store, gStore, "Cursor.store is the store");
ok("next" in cursor, "Cursor.next exists");
ok("close" in cursor, "Cursor.close exists");

View File

@ -676,10 +676,14 @@ WorkerDataStore::Sync(JSContext* aCx,
MOZ_ASSERT(workerPrivate);
workerPrivate->AssertIsOnWorkerThread();
// Create a WorkerDataStoreCursor on the worker. DataStoreSyncStoreRunnable
// will point that to the DataStoreCursor created on the main thread.
nsRefPtr<WorkerDataStoreCursor> workerCursor = new WorkerDataStoreCursor();
// Create a WorkerDataStoreCursor on the worker. Note that we need to pass
// this WorkerDataStore into the WorkerDataStoreCursor, so that it can keep
// track of which WorkerDataStore owns the WorkerDataStoreCursor.
nsRefPtr<WorkerDataStoreCursor> workerCursor =
new WorkerDataStoreCursor(this);
// DataStoreSyncStoreRunnable will point the WorkerDataStoreCursor to the
// DataStoreCursor created on the main thread.
nsRefPtr<DataStoreSyncStoreRunnable> runnable =
new DataStoreSyncStoreRunnable(workerPrivate,
mBackingStore,

View File

@ -20,6 +20,17 @@
BEGIN_WORKERS_NAMESPACE
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WorkerDataStoreCursor, AddRef)
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WorkerDataStoreCursor, Release)
NS_IMPL_CYCLE_COLLECTION(WorkerDataStoreCursor, mWorkerStore)
WorkerDataStoreCursor::WorkerDataStoreCursor(WorkerDataStore* aWorkerStore)
: mWorkerStore(aWorkerStore)
{
MOZ_ASSERT(!NS_IsMainThread());
}
already_AddRefed<WorkerDataStoreCursor>
WorkerDataStoreCursor::Constructor(GlobalObject& aGlobal, ErrorResult& aRv)
{
@ -52,57 +63,6 @@ public:
}
};
// A DataStoreCursorRunnable to run DataStoreCursor::GetStore(...) on the main
// thread.
class DataStoreCursorGetStoreRunnable MOZ_FINAL : public DataStoreCursorRunnable
{
WorkerDataStore* mWorkerStore;
ErrorResult& mRv;
nsRefPtr<DataStoreChangeEventProxy> mEventProxy;
public:
DataStoreCursorGetStoreRunnable(WorkerPrivate* aWorkerPrivate,
const nsMainThreadPtrHandle<DataStoreCursor>& aBackingCursor,
WorkerDataStore* aWorkerStore,
ErrorResult& aRv)
: DataStoreCursorRunnable(aWorkerPrivate, aBackingCursor)
, mWorkerStore(aWorkerStore)
, mRv(aRv)
{
MOZ_ASSERT(aWorkerPrivate);
aWorkerPrivate->AssertIsOnWorkerThread();
// When we're on the worker thread, prepare an DataStoreChangeEventProxy.
mEventProxy = new DataStoreChangeEventProxy(aWorkerPrivate, mWorkerStore);
}
protected:
virtual bool
MainThreadRun() MOZ_OVERRIDE
{
AssertIsOnMainThread();
nsRefPtr<DataStore> store = mBackingCursor->GetStore(mRv);
// Add |mEventProxy| as an event listner to DataStore;
if (NS_FAILED(store->AddEventListener(NS_LITERAL_STRING("change"),
mEventProxy,
false,
false,
2))) {
NS_WARNING("Failed to add event listener!");
return false;
}
// Point WorkerDataStore to DataStore.
nsMainThreadPtrHandle<DataStore> backingStore =
new nsMainThreadPtrHolder<DataStore>(store);
mWorkerStore->SetBackingDataStore(backingStore);
return true;
}
};
// A DataStoreCursorRunnable to run DataStoreCursor::Next(...) on the main
// thread.
class DataStoreCursorNextRunnable MOZ_FINAL : public DataStoreCursorRunnable
@ -172,18 +132,11 @@ WorkerDataStoreCursor::GetStore(JSContext* aCx, ErrorResult& aRv)
MOZ_ASSERT(workerPrivate);
workerPrivate->AssertIsOnWorkerThread();
// Create a WorkerDataStore on the worker. DataStoreCursorGetStoreRunnable
// will point that to the DataStore created on the main thread.
nsRefPtr<WorkerDataStore> workerStore =
new WorkerDataStore(workerPrivate->GlobalScope());
nsRefPtr<DataStoreCursorGetStoreRunnable> runnable =
new DataStoreCursorGetStoreRunnable(workerPrivate,
mBackingCursor,
workerStore,
aRv);
runnable->Dispatch(aCx);
// We should direcly return the existing WorkerDataStore which owns this
// WorkerDataStoreCursor, so that the WorkerDataStoreCursor.store can be
// tested as equal to the WorkerDataStore owning this WorkerDataStoreCursor.
MOZ_ASSERT(mWorkerStore);
nsRefPtr<WorkerDataStore> workerStore = mWorkerStore;
return workerStore.forget();
}

View File

@ -25,7 +25,10 @@ class WorkerDataStore;
class WorkerDataStoreCursor MOZ_FINAL
{
public:
NS_INLINE_DECL_REFCOUNTING(WorkerDataStoreCursor)
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WorkerDataStoreCursor)
NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(WorkerDataStoreCursor)
WorkerDataStoreCursor(WorkerDataStore* aWorkerStore);
// WebIDL (internal functions)
@ -53,6 +56,9 @@ protected:
private:
nsMainThreadPtrHandle<DataStoreCursor> mBackingCursor;
// Keep track of the WorkerDataStore which owns this WorkerDataStoreCursor.
nsRefPtr<WorkerDataStore> mWorkerStore;
};
} //namespace workers