1 // Copyright 2014 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_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_
6 #define CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_
9 #include "base/callback_forward.h"
10 #include "base/memory/scoped_vector.h"
11 #include "content/public/browser/message_port_delegate.h"
12 #include "content/public/browser/navigator_connect_context.h"
14 // Windows headers will redefine SendMessage.
23 class MessagePortMessageFilter
;
24 class NavigatorConnectService
;
25 class NavigatorConnectServiceFactory
;
26 struct NavigatorConnectClient
;
27 class ServicePortServiceImpl
;
28 struct TransferredMessagePort
;
30 // Tracks all active navigator.services connections, as well as available
31 // service factories. Delegates connection requests to the correct factory and
32 // passes messages on to the correct service.
33 // One instance of this class exists per StoragePartition.
34 // TODO(mek): Clean up connections, fire of closed events when connections die.
35 // TODO(mek): Update service side API to be ServicePort based and get rid of
36 // MessagePort dependency.
37 // TODO(mek): Make ServicePorts that live in a service worker be able to survive
38 // the worker being restarted.
39 class NavigatorConnectContextImpl
: public NavigatorConnectContext
,
40 public MessagePortDelegate
{
42 using ConnectCallback
=
43 base::Callback
<void(int message_port_id
, bool success
)>;
45 explicit NavigatorConnectContextImpl();
47 // Called when a new connection request comes in from a client. Finds the
48 // correct service factory and passes the connection request off to there.
49 // Can call the callback before this method call returns.
50 void Connect(const GURL
& target_url
,
52 ServicePortServiceImpl
* service_port_service
,
53 const ConnectCallback
& callback
);
55 // Called by a ServicePortServiceImpl instance when it is about to be
56 // destroyed to inform this class that all its connections are no longer
58 void ServicePortServiceDestroyed(
59 ServicePortServiceImpl
* service_port_service
);
61 // NavigatorConnectContext implementation.
62 void AddFactory(scoped_ptr
<NavigatorConnectServiceFactory
> factory
) override
;
64 // MessagePortDelegate implementation.
67 const MessagePortMessage
& message
,
68 const std::vector
<TransferredMessagePort
>& sent_message_ports
) override
;
69 void SendMessagesAreQueued(int route_id
) override
;
72 ~NavigatorConnectContextImpl() override
;
74 void AddFactoryOnIOThread(scoped_ptr
<NavigatorConnectServiceFactory
> factory
);
76 // Callback called by service factories when a connection succeeded or failed.
77 void OnConnectResult(const NavigatorConnectClient
& client
,
78 int client_message_port_id
,
79 const ConnectCallback
& callback
,
80 MessagePortDelegate
* delegate
,
83 // List of factories to try to handle URLs.
84 ScopedVector
<NavigatorConnectServiceFactory
> service_factories_
;
86 // List of currently active ServicePorts.
88 std::map
<int, Port
> ports_
;
91 } // namespace content
93 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_