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 "base/strings/string16.h" | 11 #include "content/public/browser/message_port_delegate.h" |
12 #include "content/common/service_worker/service_worker_status_code.h" | |
13 #include "content/public/browser/navigator_connect_context.h" | 12 #include "content/public/browser/navigator_connect_context.h" |
14 | 13 |
| 14 // Windows headers will redefine SendMessage. |
| 15 #ifdef SendMessage |
| 16 #undef SendMessage |
| 17 #endif |
| 18 |
15 class GURL; | 19 class GURL; |
16 | 20 |
17 namespace content { | 21 namespace content { |
18 | 22 |
19 struct MessagePortMessage; | 23 class MessagePortMessageFilter; |
20 class NavigatorConnectService; | 24 class NavigatorConnectService; |
21 class NavigatorConnectServiceFactory; | 25 class NavigatorConnectServiceFactory; |
22 struct NavigatorConnectClient; | 26 struct NavigatorConnectClient; |
23 class ServicePortServiceImpl; | 27 class ServicePortServiceImpl; |
24 class ServiceWorkerContextWrapper; | |
25 class ServiceWorkerRegistration; | |
26 struct TransferredMessagePort; | 28 struct TransferredMessagePort; |
27 | 29 |
28 // Tracks all active navigator.services connections, as well as available | 30 // Tracks all active navigator.services connections, as well as available |
29 // service factories. Delegates connection requests to the correct factory and | 31 // service factories. Delegates connection requests to the correct factory and |
30 // passes messages on to the correct service. | 32 // passes messages on to the correct service. |
31 // One instance of this class exists per StoragePartition. | 33 // One instance of this class exists per StoragePartition. |
32 // TODO(mek): Clean up connections, fire of closed events when connections die. | 34 // TODO(mek): Clean up connections, fire of closed events when connections die. |
33 // TODO(mek): Update service side API to be fully ServicePort based. | 35 // TODO(mek): Update service side API to be ServicePort based and get rid of |
| 36 // MessagePort dependency. |
34 // TODO(mek): Make ServicePorts that live in a service worker be able to survive | 37 // TODO(mek): Make ServicePorts that live in a service worker be able to survive |
35 // the worker being restarted. | 38 // the worker being restarted. |
36 // TODO(mek): Add back ability for service ports to be backed by native code. | 39 class NavigatorConnectContextImpl : public NavigatorConnectContext, |
37 class NavigatorConnectContextImpl : public NavigatorConnectContext { | 40 public MessagePortDelegate { |
38 public: | 41 public: |
39 using ConnectCallback = | 42 using ConnectCallback = |
40 base::Callback<void(int message_port_id, bool success)>; | 43 base::Callback<void(int message_port_id, bool success)>; |
41 | 44 |
42 explicit NavigatorConnectContextImpl( | 45 explicit NavigatorConnectContextImpl(); |
43 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | |
44 | 46 |
45 // Called when a new connection request comes in from a client. Finds the | 47 // Called when a new connection request comes in from a client. Finds the |
46 // correct service factory and passes the connection request off to there. | 48 // correct service factory and passes the connection request off to there. |
47 // Can call the callback before this method call returns. | 49 // Can call the callback before this method call returns. |
48 void Connect(const GURL& target_url, | 50 void Connect(const GURL& target_url, |
49 const GURL& origin, | 51 const GURL& origin, |
50 ServicePortServiceImpl* service_port_service, | 52 ServicePortServiceImpl* service_port_service, |
51 const ConnectCallback& callback); | 53 const ConnectCallback& callback); |
52 | 54 |
53 // Called when a message is sent to a ServicePort. The |sender_port_id| is the | |
54 // id of the port the message is sent from, this will look up what other port | |
55 // the port is entangled with and deliver the message to that port. | |
56 void PostMessage( | |
57 int sender_port_id, | |
58 const MessagePortMessage& message, | |
59 const std::vector<TransferredMessagePort>& sent_message_ports); | |
60 | |
61 // Called by a ServicePortServiceImpl instance when it is about to be | 55 // Called by a ServicePortServiceImpl instance when it is about to be |
62 // destroyed to inform this class that all its connections are no longer | 56 // destroyed to inform this class that all its connections are no longer |
63 // valid. | 57 // valid. |
64 void ServicePortServiceDestroyed( | 58 void ServicePortServiceDestroyed( |
65 ServicePortServiceImpl* service_port_service); | 59 ServicePortServiceImpl* service_port_service); |
66 | 60 |
67 // NavigatorConnectContext implementation. | 61 // NavigatorConnectContext implementation. |
68 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override; | 62 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override; |
69 | 63 |
| 64 // MessagePortDelegate implementation. |
| 65 void SendMessage( |
| 66 int route_id, |
| 67 const MessagePortMessage& message, |
| 68 const std::vector<TransferredMessagePort>& sent_message_ports) override; |
| 69 void SendMessagesAreQueued(int route_id) override; |
| 70 |
70 private: | 71 private: |
71 ~NavigatorConnectContextImpl() override; | 72 ~NavigatorConnectContextImpl() override; |
72 | 73 |
73 void AddFactoryOnIOThread(scoped_ptr<NavigatorConnectServiceFactory> factory); | 74 void AddFactoryOnIOThread(scoped_ptr<NavigatorConnectServiceFactory> factory); |
74 | 75 |
75 // Callback called when a ServiceWorkerRegistration has been located (or | |
76 // has failed to be located) for a connection attempt. | |
77 void GotServiceWorkerRegistration( | |
78 const ConnectCallback& callback, | |
79 int client_port_id, | |
80 int service_port_id, | |
81 ServiceWorkerStatusCode status, | |
82 const scoped_refptr<ServiceWorkerRegistration>& registration); | |
83 | |
84 // Callback called by service factories when a connection succeeded or failed. | 76 // Callback called by service factories when a connection succeeded or failed. |
85 void OnConnectResult(const ConnectCallback& callback, | 77 void OnConnectResult(const NavigatorConnectClient& client, |
86 int client_port_id, | 78 int client_message_port_id, |
87 int service_port_id, | 79 const ConnectCallback& callback, |
88 const scoped_refptr<ServiceWorkerRegistration>& | 80 MessagePortDelegate* delegate, |
89 service_worker_registration, | 81 bool data_as_values); |
90 ServiceWorkerStatusCode status, | |
91 bool accept_connection, | |
92 const base::string16& name, | |
93 const base::string16& data); | |
94 | |
95 // Callback called when a ServiceWorkerRegistration has been located to | |
96 // deliver a message to. | |
97 void DeliverMessage( | |
98 int port_id, | |
99 const base::string16& message, | |
100 const std::vector<TransferredMessagePort>& sent_message_ports, | |
101 ServiceWorkerStatusCode status, | |
102 const scoped_refptr<ServiceWorkerRegistration>& registration); | |
103 | |
104 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | |
105 | 82 |
106 // List of factories to try to handle URLs. | 83 // List of factories to try to handle URLs. |
107 ScopedVector<NavigatorConnectServiceFactory> service_factories_; | 84 ScopedVector<NavigatorConnectServiceFactory> service_factories_; |
108 | 85 |
109 // List of currently active ServicePorts. | 86 // List of currently active ServicePorts. |
110 struct Port; | 87 struct Port; |
111 std::map<int, Port> ports_; | 88 std::map<int, Port> ports_; |
112 int next_port_id_; | |
113 }; | 89 }; |
114 | 90 |
115 } // namespace content | 91 } // namespace content |
116 | 92 |
117 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ | 93 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ |
OLD | NEW |