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"
|
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";
|
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Storage, mWindow, mPrincipal)
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Storage)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Storage)
|
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
|
2013-04-15 16:38:48 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMStorage)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMStorage)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
Storage::Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal)
|
2017-01-04 16:53:01 +03:00
|
|
|
: mWindow(aWindow)
|
|
|
|
, mPrincipal(aPrincipal)
|
2017-05-17 08:01:15 +03:00
|
|
|
, mIsSessionOnly(false)
|
2013-04-15 16:38:48 +04:00
|
|
|
{
|
2017-05-17 08:01:12 +03:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
2013-04-15 16:38:48 +04:00
|
|
|
}
|
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
Storage::~Storage()
|
2017-05-17 08:01:12 +03:00
|
|
|
{}
|
|
|
|
|
2017-11-22 00:25:33 +03:00
|
|
|
/* static */ bool
|
|
|
|
Storage::StoragePrefIsEnabled()
|
|
|
|
{
|
|
|
|
return mozilla::Preferences::GetBool(kStorageEnabled);
|
|
|
|
}
|
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
bool
|
2017-05-17 08:01:15 +03:00
|
|
|
Storage::CanUseStorage(nsIPrincipal& aSubjectPrincipal)
|
2013-04-15 16:38:48 +04:00
|
|
|
{
|
2017-05-17 08:01:15 +03:00
|
|
|
// This method is responsible for correct setting of mIsSessionOnly.
|
2017-11-22 00:25:33 +03:00
|
|
|
if (!StoragePrefIsEnabled()) {
|
2017-05-17 08:01:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsContentUtils::StorageAccess access =
|
|
|
|
nsContentUtils::StorageAllowedForPrincipal(Principal());
|
|
|
|
|
|
|
|
if (access == nsContentUtils::StorageAccess::eDeny) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsSessionOnly = access <= nsContentUtils::StorageAccess::eSessionScoped;
|
|
|
|
|
|
|
|
return aSubjectPrincipal.Subsumes(mPrincipal);
|
2013-04-15 16:38:48 +04:00
|
|
|
}
|
|
|
|
|
2014-07-23 09:07:12 +04:00
|
|
|
/* virtual */ JSObject*
|
2017-01-04 16:53:01 +03:00
|
|
|
Storage::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-07-23 09:07:12 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return StorageBinding::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
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
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
|