1 // Copyright 2018 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 
5 #ifndef COMPONENTS_REMOTE_COCOA_APP_SHIM_APPLICATION_BRIDGE_H_
6 #define COMPONENTS_REMOTE_COCOA_APP_SHIM_APPLICATION_BRIDGE_H_
7 
8 #include "base/no_destructor.h"
9 #include "components/remote_cocoa/app_shim/remote_cocoa_app_shim_export.h"
10 #include "components/remote_cocoa/common/alert.mojom.h"
11 #include "components/remote_cocoa/common/application.mojom.h"
12 #include "components/remote_cocoa/common/native_widget_ns_window.mojom.h"
13 #include "components/remote_cocoa/common/native_widget_ns_window_host.mojom.h"
14 #include "mojo/public/cpp/bindings/associated_receiver.h"
15 #include "mojo/public/cpp/bindings/pending_associated_receiver.h"
16 #include "mojo/public/cpp/bindings/pending_associated_remote.h"
17 #include "mojo/public/cpp/bindings/pending_receiver.h"
18 #include "mojo/public/cpp/bindings/pending_remote.h"
19 
20 namespace remote_cocoa {
21 
22 // This class implements the remote_cocoa::mojom::Application interface, and
23 // bridges that C++ interface to the Objective-C NSApplication. This class is
24 // the root of all other remote_cocoa classes (e.g, NSAlerts, NSWindows,
25 // NSViews).
26 class REMOTE_COCOA_APP_SHIM_EXPORT ApplicationBridge
27     : public mojom::Application {
28  public:
29   static ApplicationBridge* Get();
30   void BindReceiver(
31       mojo::PendingAssociatedReceiver<mojom::Application> receiver);
32 
33   // Set callbacks to create content types (content types cannot be created
34   // in remote_cocoa).
35   // TODO(https://crbug.com/888290): Move these types from content to
36   // remote_cocoa.
37   using RenderWidgetHostNSViewCreateCallback = base::RepeatingCallback<void(
38       mojo::ScopedInterfaceEndpointHandle host_handle,
39       mojo::ScopedInterfaceEndpointHandle view_request_handle)>;
40   using WebContentsNSViewCreateCallback = base::RepeatingCallback<void(
41       uint64_t view_id,
42       mojo::ScopedInterfaceEndpointHandle host_handle,
43       mojo::ScopedInterfaceEndpointHandle view_request_handle)>;
44   void SetContentNSViewCreateCallbacks(
45       RenderWidgetHostNSViewCreateCallback render_widget_host_create_callback,
46       WebContentsNSViewCreateCallback web_conents_create_callback);
47 
48   // mojom::Application:
49   void CreateAlert(
50       mojo::PendingReceiver<mojom::AlertBridge> bridge_receiver) override;
51   void ShowColorPanel(mojo::PendingReceiver<mojom::ColorPanel> receiver,
52                       mojo::PendingRemote<mojom::ColorPanelHost> host) override;
53   void CreateNativeWidgetNSWindow(
54       uint64_t bridge_id,
55       mojo::PendingAssociatedReceiver<mojom::NativeWidgetNSWindow>
56           bridge_receiver,
57       mojo::PendingAssociatedRemote<mojom::NativeWidgetNSWindowHost> host,
58       mojo::PendingAssociatedRemote<mojom::TextInputHost> text_input_host)
59       override;
60   void CreateRenderWidgetHostNSView(
61       mojo::PendingAssociatedRemote<mojom::StubInterface> host,
62       mojo::PendingAssociatedReceiver<mojom::StubInterface> view_receiver)
63       override;
64   void CreateWebContentsNSView(
65       uint64_t view_id,
66       mojo::PendingAssociatedRemote<mojom::StubInterface> host,
67       mojo::PendingAssociatedReceiver<mojom::StubInterface> view_receiver)
68       override;
69 
70  private:
71   friend class base::NoDestructor<ApplicationBridge>;
72   ApplicationBridge();
73   ~ApplicationBridge() override;
74 
75   RenderWidgetHostNSViewCreateCallback render_widget_host_create_callback_;
76   WebContentsNSViewCreateCallback web_conents_create_callback_;
77 
78   mojo::AssociatedReceiver<mojom::Application> receiver_{this};
79 };
80 
81 }  // namespace remote_cocoa
82 
83 #endif  // COMPONENTS_REMOTE_COCOA_APP_SHIM_APPLICATION_BRIDGE_H_
84