Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "mozilla/dom/DataStore.h" |
michael@0 | 6 | #include "mozilla/dom/DataStoreCursor.h" |
michael@0 | 7 | #include "mozilla/dom/DataStoreBinding.h" |
michael@0 | 8 | #include "mozilla/dom/DataStoreImplBinding.h" |
michael@0 | 9 | #include "mozilla/dom/Navigator.h" |
michael@0 | 10 | #include "mozilla/dom/Promise.h" |
michael@0 | 11 | #include "mozilla/ErrorResult.h" |
michael@0 | 12 | #include "mozilla/Preferences.h" |
michael@0 | 13 | #include "AccessCheck.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | namespace dom { |
michael@0 | 17 | |
michael@0 | 18 | NS_IMPL_ADDREF_INHERITED(DataStore, DOMEventTargetHelper) |
michael@0 | 19 | NS_IMPL_RELEASE_INHERITED(DataStore, DOMEventTargetHelper) |
michael@0 | 20 | |
michael@0 | 21 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DataStore) |
michael@0 | 22 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 23 | NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) |
michael@0 | 24 | |
michael@0 | 25 | NS_IMPL_CYCLE_COLLECTION(DataStore, mStore) |
michael@0 | 26 | |
michael@0 | 27 | DataStore::DataStore(nsPIDOMWindow* aWindow) |
michael@0 | 28 | : DOMEventTargetHelper(aWindow) |
michael@0 | 29 | { |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | already_AddRefed<DataStore> |
michael@0 | 33 | DataStore::Constructor(GlobalObject& aGlobal, ErrorResult& aRv) |
michael@0 | 34 | { |
michael@0 | 35 | nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports()); |
michael@0 | 36 | if (!window) { |
michael@0 | 37 | aRv.Throw(NS_ERROR_FAILURE); |
michael@0 | 38 | return nullptr; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | nsRefPtr<DataStore> store = new DataStore(window); |
michael@0 | 42 | return store.forget(); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | JSObject* |
michael@0 | 46 | DataStore::WrapObject(JSContext* aCx) |
michael@0 | 47 | { |
michael@0 | 48 | return DataStoreBinding::Wrap(aCx, this); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | /*static*/ bool |
michael@0 | 52 | DataStore::EnabledForScope(JSContext* aCx, JS::Handle<JSObject*> aObj) |
michael@0 | 53 | { |
michael@0 | 54 | // Only expose the interface when it is: |
michael@0 | 55 | // 1. enabled by the preference and |
michael@0 | 56 | // 2. accessed by the chrome codes in Gecko. |
michael@0 | 57 | return (Navigator::HasDataStoreSupport(aCx, aObj) && |
michael@0 | 58 | nsContentUtils::ThreadsafeIsCallerChrome()); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | void |
michael@0 | 62 | DataStore::GetName(nsAString& aName, ErrorResult& aRv) |
michael@0 | 63 | { |
michael@0 | 64 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 65 | MOZ_ASSERT(mStore); |
michael@0 | 66 | |
michael@0 | 67 | nsAutoString name; |
michael@0 | 68 | mStore->GetName(name, aRv); |
michael@0 | 69 | aName.Assign(name); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | void |
michael@0 | 73 | DataStore::GetOwner(nsAString& aOwner, ErrorResult& aRv) |
michael@0 | 74 | { |
michael@0 | 75 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 76 | MOZ_ASSERT(mStore); |
michael@0 | 77 | |
michael@0 | 78 | nsAutoString owner; |
michael@0 | 79 | mStore->GetOwner(owner, aRv); |
michael@0 | 80 | aOwner.Assign(owner); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | bool |
michael@0 | 84 | DataStore::GetReadOnly(ErrorResult& aRv) |
michael@0 | 85 | { |
michael@0 | 86 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 87 | MOZ_ASSERT(mStore); |
michael@0 | 88 | |
michael@0 | 89 | return mStore->GetReadOnly(aRv); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | already_AddRefed<Promise> |
michael@0 | 93 | DataStore::Get(const Sequence<OwningStringOrUnsignedLong>& aId, |
michael@0 | 94 | ErrorResult& aRv) |
michael@0 | 95 | { |
michael@0 | 96 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 97 | MOZ_ASSERT(mStore); |
michael@0 | 98 | |
michael@0 | 99 | return mStore->Get(aId, aRv); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | already_AddRefed<Promise> |
michael@0 | 103 | DataStore::Put(JSContext* aCx, |
michael@0 | 104 | JS::Handle<JS::Value> aObj, |
michael@0 | 105 | const StringOrUnsignedLong& aId, |
michael@0 | 106 | const nsAString& aRevisionId, |
michael@0 | 107 | ErrorResult& aRv) |
michael@0 | 108 | { |
michael@0 | 109 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 110 | MOZ_ASSERT(mStore); |
michael@0 | 111 | |
michael@0 | 112 | return mStore->Put(aObj, aId, aRevisionId, aRv); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | already_AddRefed<Promise> |
michael@0 | 116 | DataStore::Add(JSContext* aCx, |
michael@0 | 117 | JS::Handle<JS::Value> aObj, |
michael@0 | 118 | const Optional<StringOrUnsignedLong>& aId, |
michael@0 | 119 | const nsAString& aRevisionId, |
michael@0 | 120 | ErrorResult& aRv) |
michael@0 | 121 | { |
michael@0 | 122 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 123 | MOZ_ASSERT(mStore); |
michael@0 | 124 | |
michael@0 | 125 | return mStore->Add(aObj, aId, aRevisionId, aRv); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | already_AddRefed<Promise> |
michael@0 | 129 | DataStore::Remove(const StringOrUnsignedLong& aId, |
michael@0 | 130 | const nsAString& aRevisionId, |
michael@0 | 131 | ErrorResult& aRv) |
michael@0 | 132 | { |
michael@0 | 133 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 134 | MOZ_ASSERT(mStore); |
michael@0 | 135 | |
michael@0 | 136 | return mStore->Remove(aId, aRevisionId, aRv); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | already_AddRefed<Promise> |
michael@0 | 140 | DataStore::Clear(const nsAString& aRevisionId, ErrorResult& aRv) |
michael@0 | 141 | { |
michael@0 | 142 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 143 | MOZ_ASSERT(mStore); |
michael@0 | 144 | |
michael@0 | 145 | return mStore->Clear(aRevisionId, aRv); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | void |
michael@0 | 149 | DataStore::GetRevisionId(nsAString& aRevisionId, ErrorResult& aRv) |
michael@0 | 150 | { |
michael@0 | 151 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 152 | MOZ_ASSERT(mStore); |
michael@0 | 153 | |
michael@0 | 154 | nsAutoString revisionId; |
michael@0 | 155 | mStore->GetRevisionId(revisionId, aRv); |
michael@0 | 156 | aRevisionId.Assign(revisionId); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | already_AddRefed<Promise> |
michael@0 | 160 | DataStore::GetLength(ErrorResult& aRv) |
michael@0 | 161 | { |
michael@0 | 162 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 163 | MOZ_ASSERT(mStore); |
michael@0 | 164 | |
michael@0 | 165 | return mStore->GetLength(aRv); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | already_AddRefed<DataStoreCursor> |
michael@0 | 169 | DataStore::Sync(const nsAString& aRevisionId, ErrorResult& aRv) |
michael@0 | 170 | { |
michael@0 | 171 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 172 | MOZ_ASSERT(mStore); |
michael@0 | 173 | |
michael@0 | 174 | return mStore->Sync(aRevisionId, aRv); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | void |
michael@0 | 178 | DataStore::SetDataStoreImpl(DataStoreImpl& aStore, ErrorResult& aRv) |
michael@0 | 179 | { |
michael@0 | 180 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 181 | MOZ_ASSERT(!mStore); |
michael@0 | 182 | |
michael@0 | 183 | mStore = &aStore; |
michael@0 | 184 | mStore->SetEventTarget(*this, aRv); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | } //namespace dom |
michael@0 | 188 | } //namespace mozilla |