Bumping manifests a=b2g-bump
[gecko.git] / dom / datastore / DataStoreRevision.cpp
blob1078df678b89e94d52560c47c0b810f2df0036cf
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"
17 namespace mozilla {
18 namespace dom {
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
25 // in.
26 nsresult
27 DataStoreRevision::AddRevision(JSContext* aCx,
28 IDBObjectStore* aStore,
29 uint32_t aObjectId,
30 RevisionType aRevisionType,
31 DataStoreRevisionCallback* aCallback)
33 MOZ_ASSERT(aStore);
34 MOZ_ASSERT(aCallback);
36 nsRefPtr<DataStoreService> service = DataStoreService::Get();
37 if (!service) {
38 return NS_ERROR_FAILURE;
41 nsString id;
42 nsresult rv = service->GenerateUUID(mRevisionID);
43 if (NS_WARN_IF(NS_FAILED(rv))) {
44 return rv;
47 DataStoreRevisionData data;
48 data.mRevisionId = mRevisionID;
49 data.mObjectId = aObjectId;
51 switch (aRevisionType) {
52 case RevisionVoid:
53 data.mOperation = NS_LITERAL_STRING("void");
54 break;
56 default:
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;
65 ErrorResult error;
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"),
72 this, false);
73 if (NS_WARN_IF(NS_FAILED(rv))) {
74 return rv;
77 mCallback = aCallback;
78 return NS_OK;
81 NS_IMETHODIMP
82 DataStoreRevision::HandleEvent(nsIDOMEvent* aEvent)
84 nsString type;
85 nsresult rv = aEvent->GetType(type);
86 if (NS_WARN_IF(NS_FAILED(rv))) {
87 return rv;
90 if (!type.EqualsASCII("success")) {
91 MOZ_CRASH("This should not happen");
94 mRequest->RemoveEventListener(NS_LITERAL_STRING("success"), this, false);
95 mRequest = nullptr;
97 mCallback->Run(mRevisionID);
98 return NS_OK;
101 } // dom namespace
102 } // mozilla namespace