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
|
|
|
#ifndef mozilla_dom_Storage_h
|
|
|
|
#define mozilla_dom_Storage_h
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2013-06-05 20:15:48 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-07-23 09:07:12 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2016-10-03 17:10:27 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2014-07-23 09:07:12 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2018-10-31 23:39:03 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2014-07-23 09:07:12 +04:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
|
|
|
|
class nsIPrincipal;
|
2016-01-30 20:05:36 +03:00
|
|
|
class nsPIDOMWindowInner;
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-08-31 20:56:55 +03:00
|
|
|
class Storage : public nsISupports, public nsWrapperCache {
|
2014-07-23 09:07:12 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2018-08-31 20:56:55 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Storage)
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal);
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2017-11-22 00:25:33 +03:00
|
|
|
static bool StoragePrefIsEnabled();
|
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
enum StorageType {
|
|
|
|
eSessionStorage,
|
|
|
|
eLocalStorage,
|
2018-11-13 11:51:14 +03:00
|
|
|
ePartitionedLocalStorage,
|
2017-05-17 08:01:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual StorageType Type() const = 0;
|
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
virtual bool IsForkOf(const Storage* aStorage) const = 0;
|
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
virtual int64_t GetOriginQuotaUsage() const = 0;
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
nsIPrincipal* Principal() const { return mPrincipal; }
|
|
|
|
|
2014-07-23 09:07:12 +04:00
|
|
|
// WebIDL
|
2017-01-04 16:53:01 +03:00
|
|
|
JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* GetParentObject() const { return mWindow; }
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
virtual uint32_t GetLength(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) = 0;
|
2017-05-04 15:29:36 +03:00
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
virtual void Key(uint32_t aIndex, nsAString& aResult,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
virtual void GetItem(const nsAString& aKey, nsAString& aResult,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
virtual void GetSupportedNames(nsTArray<nsString>& aKeys) = 0;
|
2014-07-23 09:07:12 +04:00
|
|
|
|
|
|
|
void NamedGetter(const nsAString& aKey, bool& aFound, nsAString& aResult,
|
2016-10-11 04:07:48 +03:00
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
2016-10-03 17:10:27 +03:00
|
|
|
GetItem(aKey, aResult, aSubjectPrincipal, aRv);
|
2014-07-23 09:07:12 +04:00
|
|
|
aFound = !aResult.IsVoid();
|
|
|
|
}
|
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
virtual void SetItem(const nsAString& aKey, const nsAString& aValue,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
|
2014-07-23 09:07:12 +04:00
|
|
|
|
|
|
|
void NamedSetter(const nsAString& aKey, const nsAString& aValue,
|
2016-10-11 04:07:48 +03:00
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
2016-10-03 17:10:27 +03:00
|
|
|
SetItem(aKey, aValue, aSubjectPrincipal, aRv);
|
2014-07-23 09:07:12 +04:00
|
|
|
}
|
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
virtual void RemoveItem(const nsAString& aKey,
|
|
|
|
nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) = 0;
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2016-10-03 17:10:27 +03:00
|
|
|
void NamedDeleter(const nsAString& aKey, bool& aFound,
|
2016-10-11 04:07:48 +03:00
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
2016-10-03 17:10:27 +03:00
|
|
|
RemoveItem(aKey, aSubjectPrincipal, aRv);
|
2014-07-23 09:07:12 +04:00
|
|
|
|
2015-04-27 16:18:52 +03:00
|
|
|
aFound = !aRv.ErrorCodeIs(NS_SUCCESS_DOM_NO_OPERATION);
|
2014-07-23 09:07:12 +04:00
|
|
|
}
|
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
virtual void Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
|
|
|
|
|
2017-05-17 08:01:15 +03:00
|
|
|
bool IsSessionOnly() const { return mIsSessionOnly; }
|
2017-05-17 08:01:12 +03:00
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Testing Methods:
|
|
|
|
//
|
|
|
|
// These methods are exposed on the `Storage` WebIDL interface behind a
|
|
|
|
// preference for the benefit of automated-tests. They are not exposed to
|
|
|
|
// content. See `Storage.webidl` for more details.
|
|
|
|
|
2018-11-29 23:48:11 +03:00
|
|
|
virtual void Open(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
|
|
|
|
|
|
|
|
virtual void Close(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
|
|
|
|
|
2018-11-29 23:48:54 +03:00
|
|
|
virtual void BeginExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {}
|
|
|
|
|
|
|
|
virtual void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {}
|
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Dispatch storage notification events on all impacted pages in the current
|
|
|
|
// process as well as for consumption by devtools. Pages receive the
|
|
|
|
// notification via StorageNotifierService (not observers like in the past),
|
|
|
|
// while devtools does receive the notification via the observer service.
|
|
|
|
//
|
2018-07-14 09:34:14 +03:00
|
|
|
// aStorage can be null if this method is called by LocalStorageCacheChild.
|
|
|
|
//
|
|
|
|
// aImmediateDispatch is for use by child IPC code (LocalStorageCacheChild)
|
|
|
|
// so that PBackground ordering can be maintained. Without this, the event
|
2018-11-05 22:04:39 +03:00
|
|
|
// would be enqueued and run in a future turn of the event loop, potentially
|
2018-07-14 09:34:14 +03:00
|
|
|
// allowing other PBackground Recv* methods to trigger script that wants to
|
|
|
|
// assume our localstorage changes have already been applied. This is the
|
|
|
|
// case for message manager messages which are used by ContentTask testing
|
|
|
|
// logic and webextensions.
|
2017-05-17 08:01:14 +03:00
|
|
|
static void 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);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
protected:
|
|
|
|
virtual ~Storage();
|
|
|
|
|
2017-05-17 08:01:15 +03:00
|
|
|
// The method checks whether the caller can use a storage.
|
|
|
|
// CanUseStorage is called before any DOM initiated operation
|
|
|
|
// on a storage is about to happen and ensures that the storage's
|
|
|
|
// session-only flag is properly set according the current settings.
|
|
|
|
// It is an optimization since the privileges check and session only
|
|
|
|
// state determination are complex and share the code (comes hand in
|
|
|
|
// hand together).
|
|
|
|
bool CanUseStorage(nsIPrincipal& aSubjectPrincipal);
|
|
|
|
|
2018-11-29 23:47:24 +03:00
|
|
|
virtual void LastRelease() {}
|
|
|
|
|
2017-05-17 08:01:12 +03:00
|
|
|
private:
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> mWindow;
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2017-05-17 08:01:15 +03:00
|
|
|
|
|
|
|
// Whether storage is set to persist data only per session, may change
|
|
|
|
// dynamically and is set by CanUseStorage function that is called
|
|
|
|
// before any operation on the storage.
|
|
|
|
bool mIsSessionOnly : 1;
|
2017-05-17 08:01:12 +03:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
#endif // mozilla_dom_Storage_h
|