#ifndef EXTENSIONS_BROWSER_API_MESSAGING_MESSAGE_SERVICE_H_
#define EXTENSIONS_BROWSER_API_MESSAGING_MESSAGE_SERVICE_H_
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/compiler_specific.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "extensions/browser/api/messaging/message_port.h"
#include "extensions/browser/api/messaging/native_message_host.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/lazy_context_id.h"
#include "extensions/browser/lazy_context_task_queue.h"
#include "extensions/common/api/messaging/message.h"
#include "extensions/common/api/messaging/port_id.h"
#include "extensions/common/extension_id.h"
class GURL;
namespace content {
class BrowserContext;
}
namespace extensions {
namespace mojom {
enum class ChannelType;
}
class ChannelEndpoint;
class Extension;
class ExtensionHost;
class MessagingDelegate;
struct MessagingEndpoint;
struct PortContext;
class MessageService : public BrowserContextKeyedAPI,
public MessagePort::ChannelDelegate {
public:
struct MessageChannel;
explicit MessageService(content::BrowserContext* context);
MessageService(const MessageService&) = delete;
MessageService& operator=(const MessageService&) = delete;
~MessageService() override;
static BrowserContextKeyedAPIFactory<MessageService>* GetFactoryInstance();
void CloseChannel(const PortId& port_id,
const std::string& error_message) override;
void ClosePort(const PortId& port_id,
int process_id,
const PortContext& port_context,
bool close_channel,
const std::string& error_message) override;
void PostMessage(const PortId& port_id, const Message& message) override;
void NotifyResponsePending(const PortId& port_id) override;
static MessageService* Get(content::BrowserContext* context);
void OpenChannelToExtension(const ChannelEndpoint& source,
const PortId& source_port_id,
const MessagingEndpoint& source_endpoint,
std::unique_ptr<MessagePort> opener_port,
const std::string& target_extension_id,
const GURL& source_url,
mojom::ChannelType channel_type,
const std::string& channel_name);
using ExternalConnectionInfo = mojom::ExternalConnectionInfo;
void OpenChannelToExtension(
const ChannelEndpoint& source,
const PortId& source_port_id,
const ExternalConnectionInfo& info,
mojom::ChannelType channel_type,
const std::string& channel_name,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host);
void OpenChannelToNativeApp(
const ChannelEndpoint& source,
const PortId& source_port_id,
const std::string& native_app_name,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host);
void OpenChannelToTab(
const ChannelEndpoint& source,
const PortId& source_port_id,
int tab_id,
int frame_id,
const std::string& document_id,
mojom::ChannelType channel_type,
const std::string& channel_name,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host);
void OpenPort(content::RenderProcessHost* process,
const PortId& port_id,
const PortContext& port_context);
void ClosePort(content::RenderProcessHost* process,
const PortId& port_id,
const PortContext& port_context,
bool force_close);
void NotifyResponsePending(content::RenderProcessHost* process,
const PortId& port_id,
const PortContext& port_context);
size_t GetChannelCountForTest() { return channels_.size(); }
bool HasPendingLazyContextChannelsForExtension(
const ExtensionId& extension_id) const;
base::WeakPtr<MessagePort::ChannelDelegate> GetChannelDelegate() {
return weak_factory_.GetWeakPtr();
}
private:
friend class MockMessageService;
friend class BrowserContextKeyedAPIFactory<MessageService>;
struct OpenChannelParams;
void OpenChannelToTabImpl(
const ChannelEndpoint& source,
const PortId& source_port_id,
int tab_id,
int frame_id,
const std::string& document_id,
const ExtensionId& extension_id,
mojom::ChannelType channel_type,
const std::string& channel_name,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host);
std::unique_ptr<MessagePort> CreateReceiverForTab(
const ExtensionId& extension_id,
const PortId& receiver_port_id,
content::WebContents* receiver_contents,
int receiver_frame_id,
const std::string& receiver_document_id);
void OpenChannelToNativeAppImpl(
const ChannelEndpoint& source,
const PortId& source_port_id,
const std::string& native_app_name,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host);
void OpenPortImpl(const PortId& port_id,
int process_id,
const PortContext& port_context);
using MessageChannelMap =
std::map<ChannelId, std::unique_ptr<MessageChannel>>;
using PendingMessage = std::pair<PortId, Message>;
using PendingMessagesQueue = std::vector<PendingMessage>;
using PendingChannelMap = std::map<ChannelId, PendingMessagesQueue>;
using PendingLazyContextChannelMap = std::map<ChannelId, LazyContextId>;
void OpenChannelImpl(content::BrowserContext* browser_context,
std::unique_ptr<OpenChannelParams> params,
const Extension* target_extension,
bool did_enqueue);
void ClosePortImpl(const PortId& port_id,
int process_id,
int routing_id,
int worker_thread_id,
bool force_close,
const std::string& error_message);
void CloseChannelImpl(MessageChannelMap::iterator channel_iter,
const PortId& port_id,
const std::string& error_message,
bool notify_other_port);
void AddChannel(std::unique_ptr<MessageChannel> channel,
const PortId& receiver_port_id);
void OnOpenChannelAllowed(std::unique_ptr<OpenChannelParams> params,
bool allowed);
void EnqueuePendingMessage(const PortId& port_id,
const ChannelId& channel_id,
const Message& message);
void EnqueuePendingMessageForLazyBackgroundLoad(const PortId& port_id,
const ChannelId& channel_id,
const Message& message);
void DispatchMessage(const PortId& port_id,
MessageChannel* channel,
const Message& message);
bool MaybeAddPendingLazyContextOpenChannelTask(
content::BrowserContext* context,
const Extension* extension,
std::unique_ptr<OpenChannelParams>* params,
const PendingMessagesQueue& pending_messages);
void PendingLazyContextOpenChannel(
std::unique_ptr<OpenChannelParams> params,
const base::UnguessableToken& open_channel_wakeup_context_tracking_id,
std::unique_ptr<LazyContextTaskQueue::ContextInfo> context_info);
void PendingLazyContextClosePort(
const PortId& port_id,
int process_id,
int routing_id,
int worker_thread_id,
bool force_close,
const std::string& error_message,
std::unique_ptr<LazyContextTaskQueue::ContextInfo> context_info) {
if (context_info) {
ClosePortImpl(port_id, process_id, routing_id, worker_thread_id,
force_close, error_message);
}
}
void PendingLazyContextPostMessage(
const PortId& port_id,
const Message& message,
std::unique_ptr<LazyContextTaskQueue::ContextInfo> context_info) {
if (context_info) {
PostMessage(port_id, message);
}
}
void DispatchPendingMessages(const PendingMessagesQueue& queue,
const ChannelId& channel_id);
static const char* service_name() { return "MessageService"; }
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsCreatedWithBrowserContext = false;
static const bool kServiceIsNULLWhileTesting = true;
const raw_ptr<content::BrowserContext> context_;
raw_ptr<MessagingDelegate> messaging_delegate_;
MessageChannelMap channels_;
PendingChannelMap pending_incognito_channels_;
PendingLazyContextChannelMap pending_lazy_context_channels_;
base::WeakPtrFactory<MessageService> weak_factory_{this};
};
}
#endif