2013-11-11 12:04:41 +04: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: */
|
2012-05-21 15:12:37 +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/. */
|
2013-11-11 12:04:41 +04:00
|
|
|
|
2010-05-18 16:28:37 +04:00
|
|
|
#ifndef nsFrameMessageManager_h__
|
|
|
|
#define nsFrameMessageManager_h__
|
|
|
|
|
2012-08-27 18:13:02 +04:00
|
|
|
#include "nsIMessageManager.h"
|
2010-08-17 00:40:04 +04:00
|
|
|
#include "nsIObserver.h"
|
2010-05-18 16:28:37 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsTArray.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2010-05-18 16:28:37 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsTArray.h"
|
2010-08-10 21:18:26 +04:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsDataHashtable.h"
|
2013-10-26 01:55:16 +04:00
|
|
|
#include "nsClassHashtable.h"
|
2010-08-17 00:40:04 +04:00
|
|
|
#include "mozilla/Services.h"
|
2016-07-21 19:54:16 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2010-08-17 00:40:04 +04:00
|
|
|
#include "nsIObserverService.h"
|
2011-10-11 14:28:46 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2018-10-31 23:39:03 +03:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2012-06-19 06:30:09 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-08-18 02:50:18 +04:00
|
|
|
#include "js/RootingAPI.h"
|
2013-11-05 15:52:04 +04:00
|
|
|
#include "nsTObserverArray.h"
|
2017-06-14 17:44:17 +03:00
|
|
|
#include "mozilla/TypedEnumBits.h"
|
2020-04-04 00:05:29 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2017-06-20 12:09:50 +03:00
|
|
|
#include "mozilla/dom/CallbackObject.h"
|
2015-03-24 23:05:39 +03:00
|
|
|
#include "mozilla/dom/SameProcessMessageQueue.h"
|
2015-09-10 23:50:58 +03:00
|
|
|
#include "mozilla/dom/ipc/StructuredCloneData.h"
|
2010-05-18 16:28:37 +04:00
|
|
|
|
2018-03-22 05:43:17 +03:00
|
|
|
class nsFrameLoader;
|
2015-04-16 18:17:54 +03:00
|
|
|
|
2011-08-02 23:57:48 +04:00
|
|
|
namespace mozilla {
|
2018-06-23 06:35:49 +03:00
|
|
|
|
|
|
|
namespace ipc {
|
|
|
|
class FileDescriptor;
|
|
|
|
}
|
|
|
|
|
2011-08-02 23:57:48 +04:00
|
|
|
namespace dom {
|
2012-09-28 09:43:12 +04:00
|
|
|
|
2019-02-25 23:04:47 +03:00
|
|
|
class ContentParent;
|
2019-02-25 23:04:49 +03:00
|
|
|
class ContentChild;
|
2017-06-14 17:44:17 +03:00
|
|
|
class ChildProcessMessageManager;
|
2018-02-14 19:35:39 +03:00
|
|
|
class ChromeMessageBroadcaster;
|
2013-01-24 06:39:27 +04:00
|
|
|
class ClonedMessageData;
|
2018-04-16 16:18:48 +03:00
|
|
|
class MessageBroadcaster;
|
2017-06-20 12:09:50 +03:00
|
|
|
class MessageListener;
|
|
|
|
class MessageListenerManager;
|
2013-10-12 03:07:15 +04:00
|
|
|
class MessageManagerReporter;
|
2017-06-20 12:09:50 +03:00
|
|
|
template <typename T>
|
|
|
|
class Optional;
|
2018-04-16 16:18:48 +03:00
|
|
|
class ParentProcessMessageManager;
|
|
|
|
class ProcessMessageManager;
|
2012-09-28 09:43:12 +04:00
|
|
|
|
|
|
|
namespace ipc {
|
|
|
|
|
2018-06-23 06:35:49 +03:00
|
|
|
class WritableSharedMap;
|
|
|
|
|
2017-03-21 01:18:55 +03:00
|
|
|
// Note: we round the time we spend to the nearest millisecond. So a min value
|
|
|
|
// of 1 ms actually captures from 500us and above.
|
|
|
|
static const uint32_t kMinTelemetrySyncMessageManagerLatencyMs = 1;
|
|
|
|
|
2017-06-14 17:44:17 +03:00
|
|
|
enum class MessageManagerFlags {
|
|
|
|
MM_NONE = 0,
|
2012-09-28 09:43:12 +04:00
|
|
|
MM_CHROME = 1,
|
|
|
|
MM_GLOBAL = 2,
|
|
|
|
MM_PROCESSMANAGER = 4,
|
|
|
|
MM_BROADCASTER = 8,
|
|
|
|
MM_OWNSCALLBACK = 16
|
|
|
|
};
|
2017-06-14 17:44:17 +03:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(MessageManagerFlags);
|
2012-09-28 09:43:12 +04:00
|
|
|
|
|
|
|
class MessageManagerCallback {
|
|
|
|
public:
|
2020-02-21 13:41:47 +03:00
|
|
|
virtual ~MessageManagerCallback() = default;
|
2012-09-28 09:43:12 +04:00
|
|
|
|
2015-02-20 04:11:32 +03:00
|
|
|
virtual bool DoLoadMessageManagerScript(const nsAString& aURL,
|
|
|
|
bool aRunInGlobalScope) {
|
2012-09-28 09:43:12 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:07:57 +03:00
|
|
|
virtual bool DoSendBlockingMessage(const nsAString& aMessage,
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData& aData,
|
2020-04-21 17:07:57 +03:00
|
|
|
nsTArray<StructuredCloneData>* aRetVal) {
|
2012-09-28 09:43:12 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:07:57 +03:00
|
|
|
virtual nsresult DoSendAsyncMessage(const nsAString& aMessage,
|
|
|
|
StructuredCloneData& aData) {
|
2015-10-07 13:42:43 +03:00
|
|
|
return NS_OK;
|
2012-09-28 09:43:12 +04:00
|
|
|
}
|
|
|
|
|
2018-04-16 16:18:48 +03:00
|
|
|
virtual mozilla::dom::ProcessMessageManager* GetProcessMessageManager()
|
|
|
|
const {
|
2016-08-22 11:28:04 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-08 23:15:59 +03:00
|
|
|
virtual void DoGetRemoteType(nsACString& aRemoteType,
|
2018-05-17 17:42:13 +03:00
|
|
|
ErrorResult& aError) const;
|
2017-08-14 04:58:23 +03:00
|
|
|
|
2013-01-24 06:39:27 +04:00
|
|
|
protected:
|
2019-02-25 23:04:47 +03:00
|
|
|
bool BuildClonedMessageDataForParent(ContentParent* aParent,
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData& aData,
|
2013-07-11 02:05:39 +04:00
|
|
|
ClonedMessageData& aClonedData);
|
2019-02-25 23:04:49 +03:00
|
|
|
bool BuildClonedMessageDataForChild(ContentChild* aChild,
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData& aData,
|
2013-07-11 02:05:39 +04:00
|
|
|
ClonedMessageData& aClonedData);
|
2012-09-28 09:43:12 +04:00
|
|
|
};
|
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
void UnpackClonedMessageDataForParent(const ClonedMessageData& aClonedData,
|
|
|
|
StructuredCloneData& aData);
|
2015-09-02 19:20:30 +03:00
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
void UnpackClonedMessageDataForChild(const ClonedMessageData& aClonedData,
|
|
|
|
StructuredCloneData& aData);
|
2013-01-24 20:24:19 +04:00
|
|
|
|
2012-09-28 09:43:12 +04:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2011-08-02 23:57:48 +04:00
|
|
|
|
2010-05-18 16:28:37 +04:00
|
|
|
struct nsMessageListenerInfo {
|
2013-11-05 15:52:04 +04:00
|
|
|
bool operator==(const nsMessageListenerInfo& aOther) const {
|
|
|
|
return &aOther == this;
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:28:31 +03:00
|
|
|
// If mWeakListener is null then mStrongListener holds a MessageListener.
|
|
|
|
// If mWeakListener is non-null then mStrongListener contains null.
|
|
|
|
RefPtr<mozilla::dom::MessageListener> mStrongListener;
|
2013-08-08 22:36:56 +04:00
|
|
|
nsWeakPtr mWeakListener;
|
2015-02-27 08:35:26 +03:00
|
|
|
bool mListenWhenClosed;
|
2010-05-18 16:28:37 +04:00
|
|
|
};
|
|
|
|
|
2018-08-03 06:49:09 +03:00
|
|
|
class nsFrameMessageManager : public nsIMessageSender {
|
2013-10-12 03:07:15 +04:00
|
|
|
friend class mozilla::dom::MessageManagerReporter;
|
2015-09-10 23:50:58 +03:00
|
|
|
typedef mozilla::dom::ipc::StructuredCloneData StructuredCloneData;
|
2017-06-14 17:44:17 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef mozilla::dom::ipc::MessageManagerFlags MessageManagerFlags;
|
|
|
|
|
2012-09-28 09:43:12 +04:00
|
|
|
nsFrameMessageManager(mozilla::dom::ipc::MessageManagerCallback* aCallback,
|
2017-06-14 17:44:17 +03:00
|
|
|
MessageManagerFlags aFlags);
|
2010-05-18 16:28:37 +04:00
|
|
|
|
2017-06-14 17:44:17 +03:00
|
|
|
virtual ~nsFrameMessageManager();
|
2010-05-18 16:28:37 +04:00
|
|
|
|
2014-06-25 06:09:15 +04:00
|
|
|
public:
|
2017-06-14 17:44:17 +03:00
|
|
|
explicit nsFrameMessageManager(
|
|
|
|
mozilla::dom::ipc::MessageManagerCallback* aCallback)
|
|
|
|
: nsFrameMessageManager(aCallback, MessageManagerFlags::MM_NONE) {}
|
|
|
|
|
2010-05-18 16:28:37 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2018-08-03 06:49:09 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsFrameMessageManager)
|
2017-06-20 12:09:50 +03:00
|
|
|
|
2018-02-16 17:28:31 +03:00
|
|
|
void MarkForCC();
|
|
|
|
|
2017-06-20 12:09:50 +03:00
|
|
|
// MessageListenerManager
|
|
|
|
void AddMessageListener(const nsAString& aMessageName,
|
|
|
|
mozilla::dom::MessageListener& aListener,
|
|
|
|
bool aListenWhenClosed, mozilla::ErrorResult& aError);
|
|
|
|
void RemoveMessageListener(const nsAString& aMessageName,
|
|
|
|
mozilla::dom::MessageListener& aListener,
|
|
|
|
mozilla::ErrorResult& aError);
|
|
|
|
void AddWeakMessageListener(const nsAString& aMessageName,
|
|
|
|
mozilla::dom::MessageListener& aListener,
|
|
|
|
mozilla::ErrorResult& aError);
|
|
|
|
void RemoveWeakMessageListener(const nsAString& aMessageName,
|
|
|
|
mozilla::dom::MessageListener& aListener,
|
|
|
|
mozilla::ErrorResult& aError);
|
|
|
|
|
|
|
|
// MessageSender
|
|
|
|
void SendAsyncMessage(JSContext* aCx, const nsAString& aMessageName,
|
|
|
|
JS::Handle<JS::Value> aObj,
|
|
|
|
JS::Handle<JS::Value> aTransfers,
|
|
|
|
mozilla::ErrorResult& aError) {
|
2020-04-21 17:07:57 +03:00
|
|
|
DispatchAsyncMessage(aCx, aMessageName, aObj, aTransfers, aError);
|
2017-06-20 12:09:50 +03:00
|
|
|
}
|
2018-04-16 16:18:48 +03:00
|
|
|
already_AddRefed<mozilla::dom::ProcessMessageManager>
|
2017-06-20 12:09:50 +03:00
|
|
|
GetProcessMessageManager(mozilla::ErrorResult& aError);
|
2020-07-08 23:15:59 +03:00
|
|
|
void GetRemoteType(nsACString& aRemoteType,
|
2017-06-20 12:09:50 +03:00
|
|
|
mozilla::ErrorResult& aError) const;
|
|
|
|
|
|
|
|
// SyncMessageSender
|
|
|
|
void SendSyncMessage(JSContext* aCx, const nsAString& aMessageName,
|
2020-04-21 17:07:57 +03:00
|
|
|
JS::Handle<JS::Value> aObj, nsTArray<JS::Value>& aResult,
|
|
|
|
mozilla::ErrorResult& aError);
|
2017-06-20 12:09:50 +03:00
|
|
|
|
|
|
|
// GlobalProcessScriptLoader
|
|
|
|
void GetInitialProcessData(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aInitialProcessData,
|
|
|
|
mozilla::ErrorResult& aError);
|
|
|
|
|
2018-06-23 06:35:49 +03:00
|
|
|
mozilla::dom::ipc::WritableSharedMap* SharedData();
|
|
|
|
|
2012-08-27 18:13:02 +04:00
|
|
|
NS_DECL_NSIMESSAGESENDER
|
2011-08-02 23:57:48 +04:00
|
|
|
|
2018-04-16 16:18:48 +03:00
|
|
|
static mozilla::dom::ProcessMessageManager* NewProcessMessageManager(
|
|
|
|
bool aIsRemote);
|
2010-05-18 16:28:37 +04:00
|
|
|
|
2018-02-16 17:28:31 +03:00
|
|
|
void ReceiveMessage(nsISupports* aTarget, nsFrameLoader* aTargetFrameLoader,
|
|
|
|
const nsAString& aMessage, bool aIsSync,
|
|
|
|
StructuredCloneData* aCloneData,
|
|
|
|
nsTArray<StructuredCloneData>* aRetVal,
|
|
|
|
mozilla::ErrorResult& aError) {
|
|
|
|
ReceiveMessage(aTarget, aTargetFrameLoader, mClosed, aMessage, aIsSync,
|
2020-04-21 17:07:57 +03:00
|
|
|
aCloneData, aRetVal, aError);
|
2018-02-16 17:28:31 +03:00
|
|
|
}
|
2012-08-02 10:02:29 +04:00
|
|
|
|
2012-09-28 17:08:04 +04:00
|
|
|
void Disconnect(bool aRemoveFromParent = true);
|
2015-02-27 08:35:26 +03:00
|
|
|
void Close();
|
2012-09-28 09:43:12 +04:00
|
|
|
|
2014-01-31 22:45:54 +04:00
|
|
|
void SetCallback(mozilla::dom::ipc::MessageManagerCallback* aCallback);
|
2016-08-22 11:28:04 +03:00
|
|
|
|
2012-09-28 09:43:12 +04:00
|
|
|
mozilla::dom::ipc::MessageManagerCallback* GetCallback() { return mCallback; }
|
|
|
|
|
2013-07-11 02:05:39 +04:00
|
|
|
nsresult DispatchAsyncMessageInternal(JSContext* aCx,
|
|
|
|
const nsAString& aMessage,
|
2020-04-21 17:07:57 +03:00
|
|
|
StructuredCloneData& aData);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsGlobal() { return mGlobal; }
|
2014-05-23 16:52:36 +04:00
|
|
|
bool IsBroadcaster() { return mIsBroadcaster; }
|
2018-02-14 19:35:39 +03:00
|
|
|
bool IsChrome() { return mChrome; }
|
2010-08-31 22:58:35 +04:00
|
|
|
|
2018-02-14 19:35:39 +03:00
|
|
|
// GetGlobalMessageManager creates the global message manager if it hasn't
|
|
|
|
// been yet.
|
|
|
|
static already_AddRefed<mozilla::dom::ChromeMessageBroadcaster>
|
|
|
|
GetGlobalMessageManager();
|
2018-04-16 16:18:48 +03:00
|
|
|
static mozilla::dom::ParentProcessMessageManager* GetParentProcessManager() {
|
2010-08-31 22:58:35 +04:00
|
|
|
return sParentProcessManager;
|
|
|
|
}
|
2017-06-14 17:44:17 +03:00
|
|
|
static mozilla::dom::ChildProcessMessageManager* GetChildProcessManager() {
|
2010-08-31 22:58:35 +04:00
|
|
|
return sChildProcessManager;
|
|
|
|
}
|
2017-06-14 17:44:17 +03:00
|
|
|
static void SetChildProcessManager(
|
|
|
|
mozilla::dom::ChildProcessMessageManager* aManager) {
|
2015-02-20 04:12:38 +03:00
|
|
|
sChildProcessManager = aManager;
|
|
|
|
}
|
2015-05-01 04:20:51 +03:00
|
|
|
|
2019-02-08 16:02:08 +03:00
|
|
|
static bool GetParamsForMessage(JSContext* aCx, const JS::Value& aValue,
|
|
|
|
const JS::Value& aTransfer,
|
|
|
|
StructuredCloneData& aData);
|
|
|
|
|
2015-05-01 04:20:51 +03:00
|
|
|
void SetInitialProcessData(JS::HandleValue aInitialData);
|
|
|
|
|
2015-09-08 10:31:40 +03:00
|
|
|
void LoadPendingScripts();
|
|
|
|
|
2017-06-20 12:09:50 +03:00
|
|
|
protected:
|
|
|
|
friend class MMListenerRemover;
|
|
|
|
|
2018-04-16 16:18:48 +03:00
|
|
|
virtual mozilla::dom::MessageBroadcaster* GetParentManager() {
|
2017-06-14 17:44:17 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
virtual void ClearParentManager(bool aRemove) {}
|
|
|
|
|
2017-06-20 12:09:50 +03:00
|
|
|
void DispatchAsyncMessage(JSContext* aCx, const nsAString& aMessageName,
|
|
|
|
JS::Handle<JS::Value> aObj,
|
|
|
|
JS::Handle<JS::Value> aTransfers,
|
|
|
|
mozilla::ErrorResult& aError);
|
|
|
|
|
2018-02-16 17:28:31 +03:00
|
|
|
void ReceiveMessage(nsISupports* aTarget, nsFrameLoader* aTargetFrameLoader,
|
|
|
|
bool aTargetClosed, const nsAString& aMessage,
|
|
|
|
bool aIsSync, StructuredCloneData* aCloneData,
|
|
|
|
nsTArray<StructuredCloneData>* aRetVal,
|
|
|
|
mozilla::ErrorResult& aError);
|
2015-02-27 08:35:26 +03:00
|
|
|
|
2017-06-20 12:09:50 +03:00
|
|
|
void LoadScript(const nsAString& aURL, bool aAllowDelayedLoad,
|
|
|
|
bool aRunInGlobalScope, mozilla::ErrorResult& aError);
|
|
|
|
void RemoveDelayedScript(const nsAString& aURL);
|
|
|
|
void GetDelayedScripts(JSContext* aCx, nsTArray<nsTArray<JS::Value>>& aList,
|
|
|
|
mozilla::ErrorResult& aError);
|
|
|
|
|
2013-10-26 01:55:16 +04:00
|
|
|
// We keep the message listeners as arrays in a hastable indexed by the
|
|
|
|
// message name. That gives us fast lookups in ReceiveMessage().
|
2013-11-05 15:52:04 +04:00
|
|
|
nsClassHashtable<nsStringHashKey,
|
|
|
|
nsAutoTObserverArray<nsMessageListenerInfo, 1>>
|
|
|
|
mListeners;
|
2017-06-20 12:09:50 +03:00
|
|
|
nsTArray<RefPtr<mozilla::dom::MessageListenerManager>> mChildManagers;
|
2012-08-27 18:13:02 +04:00
|
|
|
bool mChrome; // true if we're in the chrome process
|
2012-09-28 09:43:12 +04:00
|
|
|
bool mGlobal; // true if we're the global frame message manager
|
2012-08-27 18:13:02 +04:00
|
|
|
bool mIsProcessManager; // true if the message manager belongs to the process
|
|
|
|
// realm
|
|
|
|
bool mIsBroadcaster; // true if the message manager is a broadcaster
|
2012-09-28 09:43:12 +04:00
|
|
|
bool mOwnsCallback;
|
2012-01-07 23:20:12 +04:00
|
|
|
bool mHandlingMessage;
|
2015-02-27 08:35:26 +03:00
|
|
|
bool mClosed; // true if we can no longer send messages
|
2012-01-07 23:20:12 +04:00
|
|
|
bool mDisconnected;
|
2012-09-28 09:43:12 +04:00
|
|
|
mozilla::dom::ipc::MessageManagerCallback* mCallback;
|
2020-04-04 00:05:29 +03:00
|
|
|
mozilla::UniquePtr<mozilla::dom::ipc::MessageManagerCallback> mOwnedCallback;
|
2010-05-18 16:28:37 +04:00
|
|
|
nsTArray<nsString> mPendingScripts;
|
2013-11-24 09:32:45 +04:00
|
|
|
nsTArray<bool> mPendingScriptsGlobalStates;
|
2015-05-01 04:20:51 +03:00
|
|
|
JS::Heap<JS::Value> mInitialProcessData;
|
2018-06-23 06:35:49 +03:00
|
|
|
RefPtr<mozilla::dom::ipc::WritableSharedMap> mSharedData;
|
2014-05-23 16:52:36 +04:00
|
|
|
|
|
|
|
void LoadPendingScripts(nsFrameMessageManager* aManager,
|
|
|
|
nsFrameMessageManager* aChildMM);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-08-31 22:58:35 +04:00
|
|
|
public:
|
2018-04-16 16:18:48 +03:00
|
|
|
static mozilla::dom::ParentProcessMessageManager* sParentProcessManager;
|
2011-10-11 14:28:46 +04:00
|
|
|
static nsFrameMessageManager* sSameProcessParentManager;
|
|
|
|
static nsTArray<nsCOMPtr<nsIRunnable>>* sPendingSameProcessAsyncMessages;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-12-22 16:00:10 +04:00
|
|
|
private:
|
2017-06-14 17:44:17 +03:00
|
|
|
static mozilla::dom::ChildProcessMessageManager* sChildProcessManager;
|
2010-05-18 16:28:37 +04:00
|
|
|
};
|
|
|
|
|
2014-03-12 18:22:00 +04:00
|
|
|
/* A helper class for taking care of many details for async message sending
|
|
|
|
within a single process. Intended to be used like so:
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class MyAsyncMessage : public nsSameProcessAsyncMessageBase, public Runnable
|
2014-03-12 18:22:00 +04:00
|
|
|
{
|
2017-11-06 06:37:28 +03:00
|
|
|
NS_IMETHOD Run() {
|
2014-03-12 18:22:00 +04:00
|
|
|
ReceiveMessage(..., ...);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
2015-10-07 13:42:43 +03:00
|
|
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsSameProcessAsyncMessageBase> ev = new MyAsyncMessage();
|
2015-10-07 13:42:43 +03:00
|
|
|
nsresult rv = ev->Init(...);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_DispatchToMainThread(ev);
|
|
|
|
}
|
|
|
|
*/
|
2014-03-12 18:22:00 +04:00
|
|
|
class nsSameProcessAsyncMessageBase {
|
|
|
|
public:
|
2015-09-10 23:50:58 +03:00
|
|
|
typedef mozilla::dom::ipc::StructuredCloneData StructuredCloneData;
|
2014-03-12 18:22:00 +04:00
|
|
|
|
2020-04-21 17:07:57 +03:00
|
|
|
nsSameProcessAsyncMessageBase();
|
2020-04-21 01:15:19 +03:00
|
|
|
nsresult Init(const nsAString& aMessage, StructuredCloneData& aData);
|
2014-03-12 18:22:00 +04:00
|
|
|
|
2018-03-22 05:43:17 +03:00
|
|
|
void ReceiveMessage(nsISupports* aTarget, nsFrameLoader* aTargetFrameLoader,
|
2015-04-16 18:17:54 +03:00
|
|
|
nsFrameMessageManager* aManager);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-03-12 18:22:00 +04:00
|
|
|
private:
|
|
|
|
nsSameProcessAsyncMessageBase(const nsSameProcessAsyncMessageBase&);
|
|
|
|
|
|
|
|
nsString mMessage;
|
2015-09-10 23:50:58 +03:00
|
|
|
StructuredCloneData mData;
|
2016-08-12 12:39:16 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool mCalledInit;
|
|
|
|
#endif
|
2014-03-12 18:22:00 +04:00
|
|
|
};
|
|
|
|
|
2010-08-17 00:40:04 +04:00
|
|
|
class nsScriptCacheCleaner;
|
|
|
|
|
2015-02-20 04:10:44 +03:00
|
|
|
struct nsMessageManagerScriptHolder {
|
|
|
|
nsMessageManagerScriptHolder(JSContext* aCx, JSScript* aScript)
|
2018-08-11 00:03:18 +03:00
|
|
|
: mScript(aCx, aScript) {
|
2015-02-20 04:10:44 +03:00
|
|
|
MOZ_COUNT_CTOR(nsMessageManagerScriptHolder);
|
|
|
|
}
|
2014-04-16 12:47:53 +04:00
|
|
|
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DTOR(nsMessageManagerScriptHolder)
|
2013-11-24 09:32:45 +04:00
|
|
|
|
2014-04-16 12:47:53 +04:00
|
|
|
JS::PersistentRooted<JSScript*> mScript;
|
2010-08-10 21:18:26 +04:00
|
|
|
};
|
|
|
|
|
2015-02-20 04:10:44 +03:00
|
|
|
class nsMessageManagerScriptExecutor {
|
2010-08-10 21:18:26 +04:00
|
|
|
public:
|
2016-04-13 21:04:11 +03:00
|
|
|
static void PurgeCache();
|
2010-08-10 21:18:26 +04:00
|
|
|
static void Shutdown();
|
2015-02-25 01:23:53 +03:00
|
|
|
|
|
|
|
void MarkScopesForCC();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-08-10 21:18:26 +04:00
|
|
|
protected:
|
2015-02-20 04:10:44 +03:00
|
|
|
friend class nsMessageManagerScriptCx;
|
|
|
|
nsMessageManagerScriptExecutor() {
|
|
|
|
MOZ_COUNT_CTOR(nsMessageManagerScriptExecutor);
|
|
|
|
}
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DTOR(nsMessageManagerScriptExecutor)
|
2014-06-13 21:56:38 +04:00
|
|
|
|
2018-08-11 00:03:18 +03:00
|
|
|
void DidCreateScriptLoader();
|
|
|
|
void LoadScriptInternal(JS::Handle<JSObject*> aMessageManager,
|
|
|
|
const nsAString& aURL, bool aRunInUniqueScope);
|
2013-11-24 09:32:45 +04:00
|
|
|
void TryCacheLoadAndCompileScript(const nsAString& aURL,
|
2018-08-11 00:03:18 +03:00
|
|
|
bool aRunInUniqueScope, bool aShouldCache,
|
|
|
|
JS::Handle<JSObject*> aMessageManager,
|
2014-06-13 21:56:38 +04:00
|
|
|
JS::MutableHandle<JSScript*> aScriptp);
|
2018-08-11 00:03:18 +03:00
|
|
|
bool Init();
|
2016-02-10 01:08:53 +03:00
|
|
|
void Trace(const TraceCallbacks& aCallbacks, void* aClosure);
|
2017-05-15 23:05:18 +03:00
|
|
|
void Unlink();
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<JS::Heap<JSObject*>, 2> mAnonymousGlobalScopes;
|
2014-06-13 21:56:38 +04:00
|
|
|
|
2018-06-30 04:07:46 +03:00
|
|
|
// Returns true if this is a process message manager. There should only be a
|
|
|
|
// single process message manager per session, so instances of this type will
|
|
|
|
// optimize their script loading to avoid unnecessary duplication.
|
|
|
|
virtual bool IsProcessScoped() const { return false; }
|
|
|
|
|
2015-02-20 04:10:44 +03:00
|
|
|
static nsDataHashtable<nsStringHashKey, nsMessageManagerScriptHolder*>*
|
|
|
|
sCachedScripts;
|
2016-07-21 19:54:16 +03:00
|
|
|
static mozilla::StaticRefPtr<nsScriptCacheCleaner> sScriptCacheCleaner;
|
2010-08-17 00:40:04 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsScriptCacheCleaner final : public nsIObserver {
|
2020-02-21 13:41:47 +03:00
|
|
|
~nsScriptCacheCleaner() = default;
|
2014-06-25 06:09:15 +04:00
|
|
|
|
2010-08-17 00:40:04 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsScriptCacheCleaner() {
|
|
|
|
nsCOMPtr<nsIObserverService> obsSvc =
|
|
|
|
mozilla::services::GetObserverService();
|
2016-04-13 21:04:11 +03:00
|
|
|
if (obsSvc) {
|
|
|
|
obsSvc->AddObserver(this, "message-manager-flush-caches", false);
|
2011-10-17 18:59:28 +04:00
|
|
|
obsSvc->AddObserver(this, "xpcom-shutdown", false);
|
2016-04-13 21:04:11 +03:00
|
|
|
}
|
2010-08-17 00:40:04 +04:00
|
|
|
}
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) override {
|
2016-04-13 21:04:11 +03:00
|
|
|
if (strcmp("message-manager-flush-caches", aTopic) == 0) {
|
|
|
|
nsMessageManagerScriptExecutor::PurgeCache();
|
|
|
|
} else if (strcmp("xpcom-shutdown", aTopic) == 0) {
|
|
|
|
nsMessageManagerScriptExecutor::Shutdown();
|
|
|
|
}
|
2010-08-17 00:40:04 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-08-10 21:18:26 +04:00
|
|
|
};
|
|
|
|
|
2012-09-11 21:59:10 +04:00
|
|
|
#endif
|