assistant_bindings.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2021 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 CHROMECAST_RENDERER_ASSISTANT_BINDINGS_H_
  5. #define CHROMECAST_RENDERER_ASSISTANT_BINDINGS_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/timer/timer.h"
  10. #include "chromecast/common/mojom/assistant_messenger.mojom.h"
  11. #include "chromecast/renderer/native_bindings_helper.h"
  12. #include "mojo/public/cpp/bindings/receiver.h"
  13. #include "mojo/public/cpp/bindings/remote.h"
  14. namespace chromecast {
  15. namespace shell {
  16. // When enabled, these bindings can be used to open a message channel with the
  17. // Assistant. These bindings are only enabled for a small set of first-party
  18. // apps.
  19. class AssistantBindings : public CastBinding,
  20. public chromecast::mojom::AssistantMessageClient {
  21. public:
  22. AssistantBindings(content::RenderFrame* frame,
  23. const base::Value& feature_config);
  24. ~AssistantBindings() override;
  25. AssistantBindings(const AssistantBindings&) = delete;
  26. AssistantBindings& operator=(const AssistantBindings&) = delete;
  27. private:
  28. friend class ::chromecast::CastBinding;
  29. // chromecast::mojom::AssistantMessageClient implementation:
  30. void OnMessage(base::Value message) override;
  31. // CastBinding implementation:
  32. void Install(v8::Local<v8::Object> cast_platform,
  33. v8::Isolate* isolate) override;
  34. // Binding methods
  35. void SetAssistantMessageHandler(
  36. v8::Local<v8::Function> assistant_message_handler);
  37. void SendAssistantRequest(const std::string& request);
  38. void ReconnectMessagePipe();
  39. void OnAssistantConnectionError();
  40. void FlushV8ToAssistantQueue();
  41. const mojo::Remote<chromecast::mojom::AssistantMessageService>&
  42. GetMojoInterface();
  43. base::RepeatingTimer reconnect_assistant_timer_;
  44. mojo::Remote<chromecast::mojom::AssistantMessageService> assistant_;
  45. base::Value feature_config_;
  46. mojo::Receiver<chromecast::mojom::AssistantMessageClient>
  47. message_client_binding_;
  48. mojo::Remote<chromecast::mojom::AssistantMessagePipe> message_pipe_;
  49. std::vector<std::string> v8_to_assistant_queue_;
  50. v8::UniquePersistent<v8::Function> assistant_message_handler_;
  51. base::WeakPtr<AssistantBindings> weak_this_;
  52. base::WeakPtrFactory<AssistantBindings> weak_factory_;
  53. };
  54. } // namespace shell
  55. } // namespace chromecast
  56. #endif // CHROMECAST_RENDERER_ASSISTANT_BINDINGS_H_