2018-11-29 23:47:20 +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: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_localstorage_LSObject_h
|
|
|
|
#define mozilla_dom_localstorage_LSObject_h
|
|
|
|
|
|
|
|
#include "mozilla/dom/Storage.h"
|
|
|
|
|
2018-11-29 23:47:45 +03:00
|
|
|
class nsGlobalWindowInner;
|
2018-11-29 23:47:20 +03:00
|
|
|
class nsIPrincipal;
|
|
|
|
class nsPIDOMWindowInner;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class ErrorResult;
|
|
|
|
|
2018-11-29 23:47:30 +03:00
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
class PrincipalInfo;
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
|
2018-11-29 23:47:20 +03:00
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class LSDatabase;
|
2018-11-29 23:47:45 +03:00
|
|
|
class LSObjectChild;
|
|
|
|
class LSObserver;
|
2018-11-29 23:47:20 +03:00
|
|
|
class LSRequestChild;
|
|
|
|
class LSRequestChildCallback;
|
|
|
|
class LSRequestParams;
|
2018-11-29 23:47:45 +03:00
|
|
|
class LSRequestResponse;
|
2018-11-29 23:47:20 +03:00
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* Backs the WebIDL `Storage` binding; all content LocalStorage calls are
|
|
|
|
* handled by this class.
|
|
|
|
*
|
|
|
|
* ## Semantics under e10s / multi-process ##
|
|
|
|
*
|
|
|
|
* A snapshot mechanism used in conjuction with stable points ensures that JS
|
|
|
|
* run-to-completion semantics are experienced even if the same origin is
|
|
|
|
* concurrently accessing LocalStorage across multiple content processes.
|
|
|
|
*
|
|
|
|
* ### Snapshot Consistency ###
|
|
|
|
*
|
|
|
|
* An LSSnapshot is created locally whenever the contents of LocalStorage are
|
|
|
|
* about to be read or written (including length). This synchronously
|
|
|
|
* establishes a corresponding Snapshot in PBackground in the parent process.
|
|
|
|
* An effort is made to send as much data from the parent process as possible,
|
|
|
|
* so sites using a small/reasonable amount of LocalStorage data will have it
|
|
|
|
* sent to the content process for immediate access. Sites with greater
|
|
|
|
* LocalStorage usage may only have some of the information relayed. In that
|
|
|
|
* case, the parent Snapshot will ensure that it retains the exact state of the
|
|
|
|
* parent Datastore at the moment the Snapshot was created.
|
|
|
|
*/
|
2018-11-29 23:47:20 +03:00
|
|
|
class LSObject final : public Storage {
|
2018-11-29 23:47:30 +03:00
|
|
|
typedef mozilla::ipc::PrincipalInfo PrincipalInfo;
|
|
|
|
|
2018-11-29 23:47:45 +03:00
|
|
|
friend nsGlobalWindowInner;
|
|
|
|
|
2018-11-29 23:47:30 +03:00
|
|
|
nsAutoPtr<PrincipalInfo> mPrincipalInfo;
|
2019-05-14 08:49:46 +03:00
|
|
|
nsAutoPtr<PrincipalInfo> mStoragePrincipalInfo;
|
2018-11-29 23:47:20 +03:00
|
|
|
|
|
|
|
RefPtr<LSDatabase> mDatabase;
|
2018-11-29 23:47:45 +03:00
|
|
|
RefPtr<LSObserver> mObserver;
|
|
|
|
|
|
|
|
uint32_t mPrivateBrowsingId;
|
2019-02-08 23:02:14 +03:00
|
|
|
Maybe<nsID> mClientId;
|
2018-11-29 23:47:45 +03:00
|
|
|
nsCString mOrigin;
|
2019-02-08 23:01:31 +03:00
|
|
|
nsCString mOriginKey;
|
2018-11-29 23:47:45 +03:00
|
|
|
nsString mDocumentURI;
|
2018-11-29 23:47:20 +03:00
|
|
|
|
2018-11-29 23:48:54 +03:00
|
|
|
bool mInExplicitSnapshot;
|
|
|
|
|
2018-11-29 23:47:20 +03:00
|
|
|
public:
|
2019-03-11 17:35:04 +03:00
|
|
|
static void Initialize();
|
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* The normal creation path invoked by nsGlobalWindowInner.
|
|
|
|
*/
|
2018-11-29 23:48:05 +03:00
|
|
|
static nsresult CreateForWindow(nsPIDOMWindowInner* aWindow,
|
|
|
|
Storage** aStorage);
|
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* nsIDOMStorageManager creation path for use in testing logic. Supports the
|
|
|
|
* system principal where CreateForWindow does not. This is also why aPrivate
|
|
|
|
* exists separate from the principal; because the system principal can never
|
|
|
|
* be mutated to have a private browsing id even though it can be used in a
|
|
|
|
* window/document marked as private browsing. That's a legacy issue that is
|
|
|
|
* being dealt with, but it's why it exists here.
|
|
|
|
*/
|
2018-11-29 23:48:05 +03:00
|
|
|
static nsresult CreateForPrincipal(nsPIDOMWindowInner* aWindow,
|
|
|
|
nsIPrincipal* aPrincipal,
|
2019-05-14 08:49:46 +03:00
|
|
|
nsIPrincipal* aStoragePrincipal,
|
2018-11-29 23:48:05 +03:00
|
|
|
const nsAString& aDocumentURI,
|
|
|
|
bool aPrivate, LSObject** aObject);
|
2018-11-29 23:47:20 +03:00
|
|
|
|
2018-11-29 23:47:30 +03:00
|
|
|
/**
|
|
|
|
* Used for requests from the parent process to the parent process; in that
|
|
|
|
* case we want ActorsParent to know our event-target and this is better than
|
|
|
|
* trying to tunnel the pointer through IPC.
|
|
|
|
*/
|
2018-12-19 14:38:23 +03:00
|
|
|
static already_AddRefed<nsISerialEventTarget> GetSyncLoopEventTarget();
|
2018-11-29 23:47:30 +03:00
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* Helper invoked by ContentChild::OnChannelReceivedMessage when a sync IPC
|
2018-12-19 14:38:23 +03:00
|
|
|
* message is received. This will be invoked on the IPC I/O thread and it
|
|
|
|
* will set the gPendingSyncMessage flag to true. It will also force the sync
|
|
|
|
* loop (if it's active) to check the gPendingSyncMessage flag which will
|
|
|
|
* result in premature finish of the loop.
|
2018-11-05 22:04:39 +03:00
|
|
|
*
|
2018-12-19 14:38:23 +03:00
|
|
|
* This is necessary to unblock the main thread when a sync IPC message is
|
|
|
|
* received to avoid the potential for browser deadlock. This should only
|
|
|
|
* occur in (ugly) testing scenarios where CPOWs are in use.
|
|
|
|
*
|
|
|
|
* Aborted sync loop will result in the underlying LSRequest being explicitly
|
2018-11-05 22:04:39 +03:00
|
|
|
* canceled, resulting in the parent sending an NS_ERROR_FAILURE result.
|
|
|
|
*/
|
2018-12-19 14:38:23 +03:00
|
|
|
static void OnSyncMessageReceived();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper invoked by ContentChild::OnMessageReceived when a sync IPC message
|
|
|
|
* has been handled. This will be invoked on the main thread and it will
|
|
|
|
* set the gPendingSyncMessage flag to false.
|
|
|
|
*/
|
|
|
|
static void OnSyncMessageHandled();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-29 23:47:20 +03:00
|
|
|
void AssertIsOnOwningThread() const { NS_ASSERT_OWNINGTHREAD(LSObject); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-29 23:48:47 +03:00
|
|
|
const nsString& DocumentURI() const { return mDocumentURI; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-29 23:47:20 +03:00
|
|
|
LSRequestChild* StartRequest(nsIEventTarget* aMainEventTarget,
|
|
|
|
const LSRequestParams& aParams,
|
|
|
|
LSRequestChildCallback* aCallback);
|
|
|
|
|
|
|
|
// Storage overrides.
|
|
|
|
StorageType Type() const override;
|
|
|
|
|
|
|
|
bool IsForkOf(const Storage* aStorage) const override;
|
|
|
|
|
|
|
|
int64_t GetOriginQuotaUsage() const override;
|
|
|
|
|
|
|
|
uint32_t GetLength(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aError) override;
|
|
|
|
|
|
|
|
void Key(uint32_t aIndex, nsAString& aResult, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aError) override;
|
|
|
|
|
|
|
|
void GetItem(const nsAString& aKey, nsAString& aResult,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aError) override;
|
|
|
|
|
|
|
|
void GetSupportedNames(nsTArray<nsString>& aNames) override;
|
|
|
|
|
|
|
|
void SetItem(const nsAString& aKey, const nsAString& aValue,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aError) override;
|
|
|
|
|
|
|
|
void RemoveItem(const nsAString& aKey, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aError) override;
|
|
|
|
|
|
|
|
void Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aError) override;
|
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Testing Methods: See Storage.h
|
2018-11-29 23:48:11 +03:00
|
|
|
void Open(nsIPrincipal& aSubjectPrincipal, ErrorResult& aError) override;
|
|
|
|
|
|
|
|
void Close(nsIPrincipal& aSubjectPrincipal, ErrorResult& aError) override;
|
|
|
|
|
2018-11-29 23:48:54 +03:00
|
|
|
void BeginExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aError) override;
|
|
|
|
|
|
|
|
void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aError) override;
|
|
|
|
|
2019-06-02 09:00:25 +03:00
|
|
|
bool GetHasActiveSnapshot(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aError) override;
|
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-11-29 23:47:24 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(LSObject, Storage)
|
|
|
|
|
2018-11-29 23:47:20 +03:00
|
|
|
private:
|
2019-05-14 08:49:46 +03:00
|
|
|
LSObject(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
|
|
|
|
nsIPrincipal* aStoragePrincipal);
|
2018-11-29 23:47:20 +03:00
|
|
|
|
|
|
|
~LSObject();
|
|
|
|
|
2018-11-29 23:47:45 +03:00
|
|
|
nsresult DoRequestSynchronously(const LSRequestParams& aParams,
|
|
|
|
LSRequestResponse& aResponse);
|
|
|
|
|
2018-11-29 23:47:20 +03:00
|
|
|
nsresult EnsureDatabase();
|
2018-11-29 23:47:24 +03:00
|
|
|
|
|
|
|
void DropDatabase();
|
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* Invoked by nsGlobalWindowInner whenever a new "storage" event listener is
|
|
|
|
* added to the window in order to ensure that "storage" events are received
|
|
|
|
* from other processes. (`LSObject::OnChange` directly invokes
|
|
|
|
* `Storage::NotifyChange` to notify in-process listeners.)
|
|
|
|
*
|
|
|
|
* If this is the first request in the process for an observer for this
|
|
|
|
* origin, this will trigger a RequestHelper-mediated synchronous LSRequest
|
|
|
|
* to prepare a new observer in the parent process and also construction of
|
|
|
|
* corresponding actors, which will result in the observer being fully
|
|
|
|
* registered in the parent process.
|
|
|
|
*/
|
2018-11-29 23:47:45 +03:00
|
|
|
nsresult EnsureObserver();
|
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* Invoked by nsGlobalWindowInner whenever its last "storage" event listener
|
|
|
|
* is removed.
|
|
|
|
*/
|
2018-11-29 23:47:45 +03:00
|
|
|
void DropObserver();
|
|
|
|
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* Internal helper method used by mutation methods that wraps the call to
|
|
|
|
* Storage::NotifyChange to generate same-process "storage" events.
|
|
|
|
*/
|
2018-11-29 23:47:45 +03:00
|
|
|
void OnChange(const nsAString& aKey, const nsAString& aOldValue,
|
|
|
|
const nsAString& aNewValue);
|
|
|
|
|
2018-11-29 23:48:54 +03:00
|
|
|
nsresult EndExplicitSnapshotInternal();
|
|
|
|
|
2018-11-29 23:47:24 +03:00
|
|
|
// Storage overrides.
|
|
|
|
void LastRelease() override;
|
2018-11-29 23:47:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_localstorage_LSObject_h
|