2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-04-15 16:38:48 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
#include "Storage.h"
|
2017-07-06 20:35:33 +03:00
|
|
|
#include "StorageNotifierService.h"
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2014-07-23 09:07:12 +04:00
|
|
|
#include "mozilla/dom/StorageBinding.h"
|
2019-05-27 17:06:49 +03:00
|
|
|
#include "mozilla/StorageAccess.h"
|
2017-05-17 08:01:13 +03:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-09-25 11:20:53 +03:00
|
|
|
static const char kStorageEnabled[] = "dom.storage.enabled";
|
|
|
|
|
2019-05-14 08:49:46 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Storage, mWindow, mPrincipal,
|
|
|
|
mStoragePrincipal)
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Storage)
|
2018-11-29 23:47:24 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(Storage, LastRelease())
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Storage)
|
2014-07-23 09:07:12 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2018-08-31 20:56:55 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2013-04-15 16:38:48 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2019-05-14 08:49:46 +03:00
|
|
|
Storage::Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
|
|
|
|
nsIPrincipal* aStoragePrincipal)
|
|
|
|
: mWindow(aWindow),
|
|
|
|
mPrincipal(aPrincipal),
|
|
|
|
mStoragePrincipal(aStoragePrincipal),
|
|
|
|
mIsSessionOnly(false) {
|
2017-05-17 08:01:12 +03:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
2019-03-08 12:00:46 +03:00
|
|
|
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(mPrincipal)) {
|
|
|
|
mIsSessionOnly = false;
|
|
|
|
} else if (mWindow) {
|
|
|
|
uint32_t rejectedReason = 0;
|
2019-05-27 17:06:49 +03:00
|
|
|
StorageAccess access = StorageAllowedForWindow(mWindow, &rejectedReason);
|
2019-03-08 12:00:46 +03:00
|
|
|
|
2019-05-27 17:06:49 +03:00
|
|
|
MOZ_ASSERT(access != StorageAccess::eDeny ||
|
2019-03-08 12:00:46 +03:00
|
|
|
rejectedReason ==
|
|
|
|
nsIWebProgressListener::STATE_COOKIES_BLOCKED_FOREIGN);
|
|
|
|
|
2019-05-27 17:06:49 +03:00
|
|
|
mIsSessionOnly = access <= StorageAccess::eSessionScoped;
|
2019-03-08 12:00:46 +03:00
|
|
|
}
|
2013-04-15 16:38:48 +04:00
|
|
|
}
|
|
|
|
|
2019-05-14 08:49:46 +03:00
|
|
|
Storage::~Storage() = default;
|
2017-05-17 08:01:12 +03:00
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
bool Storage::StoragePrefIsEnabled() {
|
2017-11-22 00:25:33 +03:00
|
|
|
return mozilla::Preferences::GetBool(kStorageEnabled);
|
|
|
|
}
|
|
|
|
|
2017-05-17 08:01:15 +03:00
|
|
|
bool Storage::CanUseStorage(nsIPrincipal& aSubjectPrincipal) {
|
2017-11-22 00:25:33 +03:00
|
|
|
if (!StoragePrefIsEnabled()) {
|
2017-05-17 08:01:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aSubjectPrincipal.Subsumes(mPrincipal);
|
2013-04-15 16:38:48 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* virtual */
|
|
|
|
JSObject* Storage::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return Storage_Binding::Wrap(aCx, this, aGivenProto);
|
2014-07-23 09:07:12 +04:00
|
|
|
}
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class StorageNotifierRunnable : public Runnable {
|
|
|
|
public:
|
|
|
|
StorageNotifierRunnable(nsISupports* aSubject, const char16_t* aStorageType,
|
|
|
|
bool aPrivateBrowsing)
|
|
|
|
: Runnable("StorageNotifierRunnable"),
|
|
|
|
mSubject(aSubject),
|
|
|
|
mStorageType(aStorageType),
|
|
|
|
mPrivateBrowsing(aPrivateBrowsing) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->NotifyObservers(mSubject,
|
|
|
|
mPrivateBrowsing
|
|
|
|
? "dom-private-storage2-changed"
|
|
|
|
: "dom-storage2-changed",
|
|
|
|
mStorageType);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsISupports> mSubject;
|
|
|
|
const char16_t* mStorageType;
|
|
|
|
const bool mPrivateBrowsing;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void Storage::NotifyChange(Storage* aStorage, nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aKey, const nsAString& aOldValue,
|
|
|
|
const nsAString& aNewValue,
|
|
|
|
const char16_t* aStorageType,
|
|
|
|
const nsAString& aDocumentURI, bool aIsPrivate,
|
|
|
|
bool aImmediateDispatch) {
|
2017-05-17 08:01:14 +03:00
|
|
|
StorageEventInit dict;
|
|
|
|
dict.mBubbles = false;
|
|
|
|
dict.mCancelable = false;
|
|
|
|
dict.mKey = aKey;
|
|
|
|
dict.mNewValue = aNewValue;
|
|
|
|
dict.mOldValue = aOldValue;
|
|
|
|
dict.mStorageArea = aStorage;
|
|
|
|
dict.mUrl = aDocumentURI;
|
|
|
|
|
|
|
|
// Note, this DOM event should never reach JS. It is cloned later in
|
|
|
|
// nsGlobalWindow.
|
|
|
|
RefPtr<StorageEvent> event =
|
|
|
|
StorageEvent::Constructor(nullptr, NS_LITERAL_STRING("storage"), dict);
|
|
|
|
|
|
|
|
event->SetPrincipal(aPrincipal);
|
|
|
|
|
2017-07-06 20:35:33 +03:00
|
|
|
// This will send the event to any registered window.
|
|
|
|
StorageNotifierService::Broadcast(event, aStorageType, aIsPrivate,
|
|
|
|
aImmediateDispatch);
|
|
|
|
|
|
|
|
// This runnable is mainly used by devtools. Windows receive notification by
|
|
|
|
// StorageNotifierService.
|
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
RefPtr<StorageNotifierRunnable> r =
|
|
|
|
new StorageNotifierRunnable(event, aStorageType, aIsPrivate);
|
|
|
|
|
|
|
|
if (aImmediateDispatch) {
|
|
|
|
Unused << r->Run();
|
|
|
|
} else {
|
2017-07-26 11:13:35 +03:00
|
|
|
SystemGroup::Dispatch(TaskCategory::Other, r.forget());
|
2017-05-17 08:01:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|