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