1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_CAST_RECEIVER_BROWSER_BINDINGS_MESSAGE_PORT_CONNECTOR_H_
#define COMPONENTS_CAST_RECEIVER_BROWSER_BINDINGS_MESSAGE_PORT_CONNECTOR_H_
#include <string>
#include <string_view>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
#include "components/cast/named_message_port_connector/named_message_port_connector.h"
#include "components/cast_receiver/browser/page_state_observer.h"
#include "components/on_load_script_injector/browser/on_load_script_injector_host.h"
#include "third_party/blink/public/common/messaging/message_port_descriptor.h"
namespace cast_api_bindings {
class MessagePort;
} // namespace cast_api_bindings
namespace content {
class NavigationHandle;
class WebContents;
} // namespace content
namespace cast_receiver {
// Injects and connects NamedMessagePortConnector services into documents
// hosted by |web_contents|.
class BindingsMessagePortConnector
: public cast_api_bindings::NamedMessagePortConnector,
public content::WebContentsObserver,
public PageStateObserver {
public:
class Client {
public:
struct ApiBinding {
// Script to execute before the load of a web document. |script| uses
// UTF-8 encoding.
std::string script;
};
virtual ~Client();
// Gets the list of bindings to early-inject into the page at load time.
// The returned ApiBindings must be evaluated in order.
virtual std::vector<ApiBinding> GetAllBindings() = 0;
// Should be invoked when a connecting a named MessagePort to a native
// bindings backend.
// |port_name| is a string-based ID. It is used to locate corresponding
// native bindings backend.
// |port| is one end of a paired message channel. It can be deserilized
// to bind a blink::WebMessagePort to perform bi-directional communication.
virtual void Connect(const std::string& port_name,
blink::MessagePortDescriptor port) = 0;
// Called when a non-recoverable error has occurred.
virtual void OnError() = 0;
};
BindingsMessagePortConnector(content::WebContents* web_contents,
Client& client);
~BindingsMessagePortConnector() override;
BindingsMessagePortConnector(const BindingsMessagePortConnector&) = delete;
void operator=(const BindingsMessagePortConnector&) = delete;
// Connect to the underlying bindings service and apply all bindings of which
// the client is aware.
void ConnectToBindingsService();
private:
// Adds a new binding.
void AddBeforeLoadJavaScript(uint64_t id, std::string_view script);
// Callback for RegisterPortHandler().
void OnPortConnected(std::string_view port_name,
std::unique_ptr<cast_api_bindings::MessagePort> port);
// PageStateObserver implementation:
void OnPageLoadComplete() override;
// content::WebContentsObserver implementation:
void DidStartNavigation(
content::NavigationHandle* navigation_handle) override;
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
void ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) override;
raw_ref<Client> const client_;
on_load_script_injector::OnLoadScriptInjectorHost<uint64_t> script_injector_;
raw_ptr<content::NavigationHandle> active_navigation_ = nullptr;
base::WeakPtrFactory<BindingsMessagePortConnector> weak_factory_;
};
} // namespace cast_receiver
#endif // COMPONENTS_CAST_RECEIVER_BROWSER_BINDINGS_MESSAGE_PORT_CONNECTOR_H_
|