blob: 89d5e4cbdb532ff7685788e62982efe1ac9ef883 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FUCHSIA_RUNNERS_CAST_NAMED_MESSAGE_PORT_CONNECTOR_H_
#define FUCHSIA_RUNNERS_CAST_NAMED_MESSAGE_PORT_CONNECTOR_H_
#include <fuchsia/web/cpp/fidl.h>
#include <deque>
#include <map>
#include <string>
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
// The implementation actively creates a MessagePort for each registered Id,
// passing them all to the web container every time a page-load occurs. It then
// waits for the page to acknowledge each MessagePort, before invoking its
// registered |handler| to notify the called that it is ready for use.
class NamedMessagePortConnector {
public:
using PortConnectedCallback =
base::RepeatingCallback<void(fuchsia::web::MessagePortPtr)>;
NamedMessagePortConnector();
~NamedMessagePortConnector();
// Registers a |handler| which will be passed a new MessagePort after every
// page load.
//
// The first call to Register() will ensure that the supporting JavaScript
// bundle for the NamedMessagePortConnector is injected into |frame|.
// Therefore, any JavaScript bundles which depend on the Connector must be
// injected after Register is called.
void Register(const std::string& id,
PortConnectedCallback handler,
fuchsia::web::Frame* frame);
// Unregisters a handler for |frame|. Should be called before |frame| is
// deleted.
void Unregister(fuchsia::web::Frame* frame, const std::string& id);
// Client should call this every time a page is loaded.
void NotifyPageLoad(fuchsia::web::Frame* frame);
private:
struct RegistrationEntry {
RegistrationEntry();
~RegistrationEntry();
RegistrationEntry(const RegistrationEntry& other);
std::string id;
PortConnectedCallback handler;
};
void InjectBindings(fuchsia::web::Frame* frame);
std::multimap<fuchsia::web::Frame*, RegistrationEntry> registrations_;
fuchsia::mem::Buffer bindings_script_;
DISALLOW_COPY_AND_ASSIGN(NamedMessagePortConnector);
};
#endif // FUCHSIA_RUNNERS_CAST_NAMED_MESSAGE_PORT_CONNECTOR_H_