Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / navigator_connect / service_port_service_impl.h
blobc599b8690ed603df94cd32ed24390eb13e938c59
1 // Copyright 2015 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.
5 #ifndef CONTENT_BROWSER_NAVIGATOR_CONNECT_SERVICE_PORT_SERVICE_IMPL_H_
6 #define CONTENT_BROWSER_NAVIGATOR_CONNECT_SERVICE_PORT_SERVICE_IMPL_H_
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/common/service_port_service.mojom.h"
12 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
14 namespace content {
15 struct MessagePortMessage;
16 class MessagePortMessageFilter;
17 class NavigatorConnectContextImpl;
18 struct TransferredMessagePort;
20 // Browser side class representing a ServicePortCollection in the renderer. One
21 // instance of this class is created for each ServicePortCollection, and is
22 // automatically destroyed when the ServicePortCollection in the renderer goes
23 // away.
24 class ServicePortServiceImpl : public ServicePortService {
25 public:
26 // Factory method called by mojo to create a new instance of this class to
27 // handle requests from a new ServicePortCollection.
28 static void Create(const scoped_refptr<NavigatorConnectContextImpl>&
29 navigator_connect_context,
30 const scoped_refptr<MessagePortMessageFilter>&
31 message_port_message_filter,
32 mojo::InterfaceRequest<ServicePortService> request);
33 ~ServicePortServiceImpl() override;
35 // Called by NavigatorConnectContextImpl to post a message to a port that is
36 // owned by this ServicePortCollection.
37 void PostMessageToClient(
38 int port_id,
39 const MessagePortMessage& message,
40 const std::vector<TransferredMessagePort>& sent_message_ports);
42 private:
43 static void CreateOnIOThread(
44 const scoped_refptr<NavigatorConnectContextImpl>&
45 navigator_connect_context,
46 const scoped_refptr<MessagePortMessageFilter>&
47 message_port_message_filter,
48 mojo::InterfaceRequest<ServicePortService> request);
49 ServicePortServiceImpl(const scoped_refptr<NavigatorConnectContextImpl>&
50 navigator_connect_context,
51 const scoped_refptr<MessagePortMessageFilter>&
52 message_port_message_filter,
53 mojo::InterfaceRequest<ServicePortService> request);
55 // ServicePortService methods:
56 void SetClient(ServicePortServiceClientPtr client) override;
57 void Connect(const mojo::String& target_url,
58 const mojo::String& origin,
59 const ConnectCallback& callback) override;
60 void PostMessage(int32_t port_id,
61 const mojo::String& message,
62 mojo::Array<MojoTransferredMessagePortPtr> ports) override;
63 void ClosePort(int32_t port_id) override;
65 // Callback called when a connection to a service has been establised or
66 // rejected.
67 void OnConnectResult(const ConnectCallback& callback,
68 int message_port_id,
69 bool success);
71 mojo::StrongBinding<ServicePortService> binding_;
72 scoped_refptr<NavigatorConnectContextImpl> navigator_connect_context_;
73 scoped_refptr<MessagePortMessageFilter> message_port_message_filter_;
74 ServicePortServiceClientPtr client_;
75 base::WeakPtrFactory<ServicePortServiceImpl> weak_ptr_factory_;
77 DISALLOW_COPY_AND_ASSIGN(ServicePortServiceImpl);
80 } // namespace content
82 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_SERVICE_PORT_SERVICE_IMPL_H_