api_bindings_client.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef FUCHSIA_WEB_RUNNERS_CAST_API_BINDINGS_CLIENT_H_
  5. #define FUCHSIA_WEB_RUNNERS_CAST_API_BINDINGS_CLIENT_H_
  6. #include <fuchsia/web/cpp/fidl.h>
  7. #include <vector>
  8. #include "components/cast/message_port/message_port.h"
  9. #include "components/cast/named_message_port_connector/named_message_port_connector.h"
  10. #include "fuchsia_web/runners/cast/fidl/fidl/chromium/cast/cpp/fidl.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. // Injects scripts received from the ApiBindings service, and provides connected
  13. // ports to the Agent.
  14. class ApiBindingsClient {
  15. public:
  16. // Reads bindings definitions from |bindings_service_| at construction time.
  17. // |on_initialization_complete| is invoked when either the initial bindings
  18. // have been received, or on failure. The caller should use HasBindings()
  19. // to verify that bindings were received, and may then use AttachToFrame().
  20. ApiBindingsClient(
  21. fidl::InterfaceHandle<chromium::cast::ApiBindings> bindings_service,
  22. base::OnceClosure on_initialization_complete);
  23. ApiBindingsClient(const ApiBindingsClient&) = delete;
  24. ApiBindingsClient& operator=(const ApiBindingsClient&) = delete;
  25. ~ApiBindingsClient();
  26. // Injects APIs and handles channel connections on |frame|.
  27. // |on_error_callback| is invoked in the event of an unrecoverable error (e.g.
  28. // lost connection to the Agent). The callback must remain valid for the
  29. // entire lifetime of |this|.
  30. void AttachToFrame(fuchsia::web::Frame* frame,
  31. cast_api_bindings::NamedMessagePortConnector* connector,
  32. base::OnceClosure on_error_callback);
  33. // Indicates that the Frame is no longer live, preventing the API bindings
  34. // client from attempting to remove injected bindings from it.
  35. void DetachFromFrame(fuchsia::web::Frame* frame);
  36. // Indicates that bindings were successfully received from
  37. // |bindings_service_|.
  38. bool HasBindings() const;
  39. // TODO(crbug.com/1082821): Move this method back to private once the Cast
  40. // Streaming Receiver component has been implemented.
  41. // Called when |connector_| has connected a port.
  42. bool OnPortConnected(base::StringPiece port_name,
  43. std::unique_ptr<cast_api_bindings::MessagePort> port);
  44. private:
  45. // Called when ApiBindings::GetAll() has responded.
  46. void OnBindingsReceived(std::vector<chromium::cast::ApiBinding> bindings);
  47. absl::optional<std::vector<chromium::cast::ApiBinding>> bindings_;
  48. fuchsia::web::Frame* frame_ = nullptr;
  49. cast_api_bindings::NamedMessagePortConnector* connector_ = nullptr;
  50. chromium::cast::ApiBindingsPtr bindings_service_;
  51. base::OnceClosure on_initialization_complete_;
  52. };
  53. #endif // FUCHSIA_WEB_RUNNERS_CAST_API_BINDINGS_CLIENT_H_