dom/datastore/DataStore.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/datastore/DataStore.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,188 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "mozilla/dom/DataStore.h"
     1.9 +#include "mozilla/dom/DataStoreCursor.h"
    1.10 +#include "mozilla/dom/DataStoreBinding.h"
    1.11 +#include "mozilla/dom/DataStoreImplBinding.h"
    1.12 +#include "mozilla/dom/Navigator.h"
    1.13 +#include "mozilla/dom/Promise.h"
    1.14 +#include "mozilla/ErrorResult.h"
    1.15 +#include "mozilla/Preferences.h"
    1.16 +#include "AccessCheck.h"
    1.17 +
    1.18 +namespace mozilla {
    1.19 +namespace dom {
    1.20 +
    1.21 +NS_IMPL_ADDREF_INHERITED(DataStore, DOMEventTargetHelper)
    1.22 +NS_IMPL_RELEASE_INHERITED(DataStore, DOMEventTargetHelper)
    1.23 +
    1.24 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DataStore)
    1.25 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.26 +NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
    1.27 +
    1.28 +NS_IMPL_CYCLE_COLLECTION(DataStore, mStore)
    1.29 +
    1.30 +DataStore::DataStore(nsPIDOMWindow* aWindow)
    1.31 +  : DOMEventTargetHelper(aWindow)
    1.32 +{
    1.33 +}
    1.34 +
    1.35 +already_AddRefed<DataStore>
    1.36 +DataStore::Constructor(GlobalObject& aGlobal, ErrorResult& aRv)
    1.37 +{
    1.38 +  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
    1.39 +  if (!window) {
    1.40 +    aRv.Throw(NS_ERROR_FAILURE);
    1.41 +    return nullptr;
    1.42 +  }
    1.43 +
    1.44 +  nsRefPtr<DataStore> store = new DataStore(window);
    1.45 +  return store.forget();
    1.46 +}
    1.47 +
    1.48 +JSObject*
    1.49 +DataStore::WrapObject(JSContext* aCx)
    1.50 +{
    1.51 +  return DataStoreBinding::Wrap(aCx, this);
    1.52 +}
    1.53 +
    1.54 +/*static*/ bool
    1.55 +DataStore::EnabledForScope(JSContext* aCx, JS::Handle<JSObject*> aObj)
    1.56 +{
    1.57 +  // Only expose the interface when it is:
    1.58 +  // 1. enabled by the preference and
    1.59 +  // 2. accessed by the chrome codes in Gecko.
    1.60 +  return (Navigator::HasDataStoreSupport(aCx, aObj) &&
    1.61 +          nsContentUtils::ThreadsafeIsCallerChrome());
    1.62 +}
    1.63 +
    1.64 +void
    1.65 +DataStore::GetName(nsAString& aName, ErrorResult& aRv)
    1.66 +{
    1.67 +  MOZ_ASSERT(NS_IsMainThread());
    1.68 +  MOZ_ASSERT(mStore);
    1.69 +
    1.70 +  nsAutoString name;
    1.71 +  mStore->GetName(name, aRv);
    1.72 +  aName.Assign(name);
    1.73 +}
    1.74 +
    1.75 +void
    1.76 +DataStore::GetOwner(nsAString& aOwner, ErrorResult& aRv)
    1.77 +{
    1.78 +  MOZ_ASSERT(NS_IsMainThread());
    1.79 +  MOZ_ASSERT(mStore);
    1.80 +
    1.81 +  nsAutoString owner;
    1.82 +  mStore->GetOwner(owner, aRv);
    1.83 +  aOwner.Assign(owner);
    1.84 +}
    1.85 +
    1.86 +bool
    1.87 +DataStore::GetReadOnly(ErrorResult& aRv)
    1.88 +{
    1.89 +  MOZ_ASSERT(NS_IsMainThread());
    1.90 +  MOZ_ASSERT(mStore);
    1.91 +
    1.92 +  return mStore->GetReadOnly(aRv);
    1.93 +}
    1.94 +
    1.95 +already_AddRefed<Promise>
    1.96 +DataStore::Get(const Sequence<OwningStringOrUnsignedLong>& aId,
    1.97 +               ErrorResult& aRv)
    1.98 +{
    1.99 +  MOZ_ASSERT(NS_IsMainThread());
   1.100 +  MOZ_ASSERT(mStore);
   1.101 +
   1.102 +  return mStore->Get(aId, aRv);
   1.103 +}
   1.104 +
   1.105 +already_AddRefed<Promise>
   1.106 +DataStore::Put(JSContext* aCx,
   1.107 +               JS::Handle<JS::Value> aObj,
   1.108 +               const StringOrUnsignedLong& aId,
   1.109 +               const nsAString& aRevisionId,
   1.110 +               ErrorResult& aRv)
   1.111 +{
   1.112 +  MOZ_ASSERT(NS_IsMainThread());
   1.113 +  MOZ_ASSERT(mStore);
   1.114 +
   1.115 +  return mStore->Put(aObj, aId, aRevisionId, aRv);
   1.116 +}
   1.117 +
   1.118 +already_AddRefed<Promise>
   1.119 +DataStore::Add(JSContext* aCx,
   1.120 +               JS::Handle<JS::Value> aObj,
   1.121 +               const Optional<StringOrUnsignedLong>& aId,
   1.122 +               const nsAString& aRevisionId,
   1.123 +               ErrorResult& aRv)
   1.124 +{
   1.125 +  MOZ_ASSERT(NS_IsMainThread());
   1.126 +  MOZ_ASSERT(mStore);
   1.127 +
   1.128 +  return mStore->Add(aObj, aId, aRevisionId, aRv);
   1.129 +}
   1.130 +
   1.131 +already_AddRefed<Promise>
   1.132 +DataStore::Remove(const StringOrUnsignedLong& aId,
   1.133 +                  const nsAString& aRevisionId,
   1.134 +                  ErrorResult& aRv)
   1.135 +{
   1.136 +  MOZ_ASSERT(NS_IsMainThread());
   1.137 +  MOZ_ASSERT(mStore);
   1.138 +
   1.139 +  return mStore->Remove(aId, aRevisionId, aRv);
   1.140 +}
   1.141 +
   1.142 +already_AddRefed<Promise>
   1.143 +DataStore::Clear(const nsAString& aRevisionId, ErrorResult& aRv)
   1.144 +{
   1.145 +  MOZ_ASSERT(NS_IsMainThread());
   1.146 +  MOZ_ASSERT(mStore);
   1.147 +
   1.148 +  return mStore->Clear(aRevisionId, aRv);
   1.149 +}
   1.150 +
   1.151 +void
   1.152 +DataStore::GetRevisionId(nsAString& aRevisionId, ErrorResult& aRv)
   1.153 +{
   1.154 +  MOZ_ASSERT(NS_IsMainThread());
   1.155 +  MOZ_ASSERT(mStore);
   1.156 +
   1.157 +  nsAutoString revisionId;
   1.158 +  mStore->GetRevisionId(revisionId, aRv);
   1.159 +  aRevisionId.Assign(revisionId);
   1.160 +}
   1.161 +
   1.162 +already_AddRefed<Promise>
   1.163 +DataStore::GetLength(ErrorResult& aRv)
   1.164 +{
   1.165 +  MOZ_ASSERT(NS_IsMainThread());
   1.166 +  MOZ_ASSERT(mStore);
   1.167 +
   1.168 +  return mStore->GetLength(aRv);
   1.169 +}
   1.170 +
   1.171 +already_AddRefed<DataStoreCursor>
   1.172 +DataStore::Sync(const nsAString& aRevisionId, ErrorResult& aRv)
   1.173 +{
   1.174 +  MOZ_ASSERT(NS_IsMainThread());
   1.175 +  MOZ_ASSERT(mStore);
   1.176 +
   1.177 +  return mStore->Sync(aRevisionId, aRv);
   1.178 +}
   1.179 +
   1.180 +void
   1.181 +DataStore::SetDataStoreImpl(DataStoreImpl& aStore, ErrorResult& aRv)
   1.182 +{
   1.183 +  MOZ_ASSERT(NS_IsMainThread());
   1.184 +  MOZ_ASSERT(!mStore);
   1.185 +
   1.186 +  mStore = &aStore;
   1.187 +  mStore->SetEventTarget(*this, aRv);
   1.188 +}
   1.189 +
   1.190 +} //namespace dom
   1.191 +} //namespace mozilla
   1.192 \ No newline at end of file

mercurial