1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "DataStoreRevision.h"
9 #include "DataStoreCallbacks.h"
10 #include "DataStoreService.h"
11 #include "mozilla/dom/DataStoreBinding.h"
12 #include "mozilla/dom/ToJSValue.h"
13 #include "mozilla/dom/indexedDB/IDBObjectStore.h"
14 #include "mozilla/dom/indexedDB/IDBRequest.h"
15 #include "nsIDOMEvent.h"
20 using namespace indexedDB
;
22 NS_IMPL_ISUPPORTS(DataStoreRevision
, nsIDOMEventListener
)
24 // Note: this code in it must not assume anything about the compartment cx is
27 DataStoreRevision::AddRevision(JSContext
* aCx
,
28 IDBObjectStore
* aStore
,
30 RevisionType aRevisionType
,
31 DataStoreRevisionCallback
* aCallback
)
34 MOZ_ASSERT(aCallback
);
36 nsRefPtr
<DataStoreService
> service
= DataStoreService::Get();
38 return NS_ERROR_FAILURE
;
42 nsresult rv
= service
->GenerateUUID(mRevisionID
);
43 if (NS_WARN_IF(NS_FAILED(rv
))) {
47 DataStoreRevisionData data
;
48 data
.mRevisionId
= mRevisionID
;
49 data
.mObjectId
= aObjectId
;
51 switch (aRevisionType
) {
53 data
.mOperation
= NS_LITERAL_STRING("void");
57 MOZ_CRASH("This should not happen");
60 JS::Rooted
<JS::Value
> value(aCx
);
61 if (!ToJSValue(aCx
, data
, &value
)) {
62 return NS_ERROR_FAILURE
;
66 mRequest
= aStore
->Put(aCx
, value
, JS::UndefinedHandleValue
, error
);
67 if (NS_WARN_IF(error
.Failed())) {
68 return error
.ErrorCode();
71 rv
= mRequest
->EventTarget::AddEventListener(NS_LITERAL_STRING("success"),
73 if (NS_WARN_IF(NS_FAILED(rv
))) {
77 mCallback
= aCallback
;
82 DataStoreRevision::HandleEvent(nsIDOMEvent
* aEvent
)
85 nsresult rv
= aEvent
->GetType(type
);
86 if (NS_WARN_IF(NS_FAILED(rv
))) {
90 if (!type
.EqualsASCII("success")) {
91 MOZ_CRASH("This should not happen");
94 mRequest
->RemoveEventListener(NS_LITERAL_STRING("success"), this, false);
97 mCallback
->Run(mRevisionID
);
102 } // mozilla namespace