OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ |
6 #define CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ | 6 #define CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include "base/callback_forward.h" | 9 #include "base/callback_forward.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
11 #include "content/public/browser/message_port_delegate.h" | 11 #include "content/public/browser/message_port_delegate.h" |
12 #include "content/public/browser/navigator_connect_context.h" | 12 #include "content/public/browser/navigator_connect_context.h" |
13 | 13 |
| 14 class GURL; |
| 15 |
14 namespace content { | 16 namespace content { |
15 | 17 |
16 class MessagePortMessageFilter; | 18 class MessagePortMessageFilter; |
17 class NavigatorConnectService; | 19 class NavigatorConnectService; |
18 class NavigatorConnectServiceFactory; | 20 class NavigatorConnectServiceFactory; |
19 struct NavigatorConnectClient; | 21 struct NavigatorConnectClient; |
| 22 class ServicePortServiceImpl; |
20 struct TransferredMessagePort; | 23 struct TransferredMessagePort; |
21 | 24 |
22 // Tracks all active navigator.connect connections, as well as available service | 25 // Tracks all active navigator.services connections, as well as available |
23 // factories. Delegates connection requests to the correct factory and passes | 26 // service factories. Delegates connection requests to the correct factory and |
24 // messages on to the correct service. | 27 // passes messages on to the correct service. |
25 // One instance of this class exists per StoragePartition. | 28 // One instance of this class exists per StoragePartition. |
26 // TODO(mek): Somehow clean up connections when the client side goes away. | 29 // TODO(mek): Clean up connections, fire of closed events when connections die. |
27 class NavigatorConnectContextImpl : public NavigatorConnectContext { | 30 // TODO(mek): Update service side API to be ServicePort based and get rid of |
| 31 // MessagePort dependency. |
| 32 // TODO(mek): Make ServicePorts that live in a service worker be able to survive |
| 33 // the worker being restarted. |
| 34 class NavigatorConnectContextImpl : public NavigatorConnectContext, |
| 35 public MessagePortDelegate { |
28 public: | 36 public: |
29 using ConnectCallback = | 37 using ConnectCallback = |
30 base::Callback<void(const TransferredMessagePort& message_port, | 38 base::Callback<void(int message_port_id, bool success)>; |
31 int message_port_route_id, | |
32 bool success)>; | |
33 | 39 |
34 explicit NavigatorConnectContextImpl(); | 40 explicit NavigatorConnectContextImpl(); |
35 | 41 |
36 // Called when a new connection request comes in from a client. Finds the | 42 // Called when a new connection request comes in from a client. Finds the |
37 // correct service factory and passes the connection request off to there. | 43 // correct service factory and passes the connection request off to there. |
38 // Can call the callback before this method call returns. | 44 // Can call the callback before this method call returns. |
39 void Connect(NavigatorConnectClient client, | 45 void Connect(const GURL& target_url, |
40 MessagePortMessageFilter* message_port_message_filter, | 46 const GURL& origin, |
| 47 ServicePortServiceImpl* service_port_service, |
41 const ConnectCallback& callback); | 48 const ConnectCallback& callback); |
42 | 49 |
| 50 // Called by a ServicePortServiceImpl instance when it is about to be |
| 51 // destroyed to inform this class that all its connections are no longer |
| 52 // valid. |
| 53 void ServicePortServiceDestroyed( |
| 54 ServicePortServiceImpl* service_port_service); |
| 55 |
43 // NavigatorConnectContext implementation. | 56 // NavigatorConnectContext implementation. |
44 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override; | 57 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override; |
45 | 58 |
| 59 // MessagePortDelegate implementation. |
| 60 void SendMessage( |
| 61 int route_id, |
| 62 const MessagePortMessage& message, |
| 63 const std::vector<TransferredMessagePort>& sent_message_ports) override; |
| 64 void SendMessagesAreQueued(int route_id) override; |
| 65 |
46 private: | 66 private: |
47 ~NavigatorConnectContextImpl() override; | 67 ~NavigatorConnectContextImpl() override; |
48 | 68 |
49 void AddFactoryOnIOThread(scoped_ptr<NavigatorConnectServiceFactory> factory); | 69 void AddFactoryOnIOThread(scoped_ptr<NavigatorConnectServiceFactory> factory); |
50 | 70 |
51 // Callback called by service factories when a connection succeeded or failed. | 71 // Callback called by service factories when a connection succeeded or failed. |
52 void OnConnectResult(const NavigatorConnectClient& client, | 72 void OnConnectResult(const NavigatorConnectClient& client, |
53 int client_message_port_id, | 73 int client_message_port_id, |
54 int client_port_route_id, | |
55 const ConnectCallback& callback, | 74 const ConnectCallback& callback, |
56 MessagePortDelegate* delegate, | 75 MessagePortDelegate* delegate, |
57 bool data_as_values); | 76 bool data_as_values); |
58 | 77 |
59 // List of factories to try to handle URLs. | 78 // List of factories to try to handle URLs. |
60 ScopedVector<NavigatorConnectServiceFactory> service_factories_; | 79 ScopedVector<NavigatorConnectServiceFactory> service_factories_; |
| 80 |
| 81 // List of currently active ServicePorts. |
| 82 struct Port; |
| 83 std::map<int, Port> ports_; |
61 }; | 84 }; |
62 | 85 |
63 } // namespace content | 86 } // namespace content |
64 | 87 |
65 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ | 88 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ |
OLD | NEW |