michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/DataStore.h" michael@0: #include "mozilla/dom/DataStoreCursor.h" michael@0: #include "mozilla/dom/DataStoreBinding.h" michael@0: #include "mozilla/dom/DataStoreImplBinding.h" michael@0: #include "mozilla/dom/Navigator.h" michael@0: #include "mozilla/dom/Promise.h" michael@0: #include "mozilla/ErrorResult.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "AccessCheck.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(DataStore, DOMEventTargetHelper) michael@0: NS_IMPL_RELEASE_INHERITED(DataStore, DOMEventTargetHelper) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DataStore) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION(DataStore, mStore) michael@0: michael@0: DataStore::DataStore(nsPIDOMWindow* aWindow) michael@0: : DOMEventTargetHelper(aWindow) michael@0: { michael@0: } michael@0: michael@0: already_AddRefed michael@0: DataStore::Constructor(GlobalObject& aGlobal, ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr window = do_QueryInterface(aGlobal.GetAsSupports()); michael@0: if (!window) { michael@0: aRv.Throw(NS_ERROR_FAILURE); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsRefPtr store = new DataStore(window); michael@0: return store.forget(); michael@0: } michael@0: michael@0: JSObject* michael@0: DataStore::WrapObject(JSContext* aCx) michael@0: { michael@0: return DataStoreBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: /*static*/ bool michael@0: DataStore::EnabledForScope(JSContext* aCx, JS::Handle aObj) michael@0: { michael@0: // Only expose the interface when it is: michael@0: // 1. enabled by the preference and michael@0: // 2. accessed by the chrome codes in Gecko. michael@0: return (Navigator::HasDataStoreSupport(aCx, aObj) && michael@0: nsContentUtils::ThreadsafeIsCallerChrome()); michael@0: } michael@0: michael@0: void michael@0: DataStore::GetName(nsAString& aName, ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: nsAutoString name; michael@0: mStore->GetName(name, aRv); michael@0: aName.Assign(name); michael@0: } michael@0: michael@0: void michael@0: DataStore::GetOwner(nsAString& aOwner, ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: nsAutoString owner; michael@0: mStore->GetOwner(owner, aRv); michael@0: aOwner.Assign(owner); michael@0: } michael@0: michael@0: bool michael@0: DataStore::GetReadOnly(ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: return mStore->GetReadOnly(aRv); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DataStore::Get(const Sequence& aId, michael@0: ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: return mStore->Get(aId, aRv); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DataStore::Put(JSContext* aCx, michael@0: JS::Handle aObj, michael@0: const StringOrUnsignedLong& aId, michael@0: const nsAString& aRevisionId, michael@0: ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: return mStore->Put(aObj, aId, aRevisionId, aRv); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DataStore::Add(JSContext* aCx, michael@0: JS::Handle aObj, michael@0: const Optional& aId, michael@0: const nsAString& aRevisionId, michael@0: ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: return mStore->Add(aObj, aId, aRevisionId, aRv); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DataStore::Remove(const StringOrUnsignedLong& aId, michael@0: const nsAString& aRevisionId, michael@0: ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: return mStore->Remove(aId, aRevisionId, aRv); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DataStore::Clear(const nsAString& aRevisionId, ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: return mStore->Clear(aRevisionId, aRv); michael@0: } michael@0: michael@0: void michael@0: DataStore::GetRevisionId(nsAString& aRevisionId, ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: nsAutoString revisionId; michael@0: mStore->GetRevisionId(revisionId, aRv); michael@0: aRevisionId.Assign(revisionId); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DataStore::GetLength(ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: return mStore->GetLength(aRv); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DataStore::Sync(const nsAString& aRevisionId, ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mStore); michael@0: michael@0: return mStore->Sync(aRevisionId, aRv); michael@0: } michael@0: michael@0: void michael@0: DataStore::SetDataStoreImpl(DataStoreImpl& aStore, ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(!mStore); michael@0: michael@0: mStore = &aStore; michael@0: mStore->SetEventTarget(*this, aRv); michael@0: } michael@0: michael@0: } //namespace dom michael@0: } //namespace mozilla