| 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 // Windows headers will redefine SendMessage. |
| 15 #ifdef SendMessage |
| 16 #undef SendMessage |
| 17 #endif |
| 18 |
| 19 class GURL; |
| 20 |
| 14 namespace content { | 21 namespace content { |
| 15 | 22 |
| 16 class MessagePortMessageFilter; | 23 class MessagePortMessageFilter; |
| 17 class NavigatorConnectService; | 24 class NavigatorConnectService; |
| 18 class NavigatorConnectServiceFactory; | 25 class NavigatorConnectServiceFactory; |
| 19 struct NavigatorConnectClient; | 26 struct NavigatorConnectClient; |
| 27 class ServicePortServiceImpl; |
| 20 struct TransferredMessagePort; | 28 struct TransferredMessagePort; |
| 21 | 29 |
| 22 // Tracks all active navigator.connect connections, as well as available service | 30 // Tracks all active navigator.services connections, as well as available |
| 23 // factories. Delegates connection requests to the correct factory and passes | 31 // service factories. Delegates connection requests to the correct factory and |
| 24 // messages on to the correct service. | 32 // passes messages on to the correct service. |
| 25 // One instance of this class exists per StoragePartition. | 33 // One instance of this class exists per StoragePartition. |
| 26 // TODO(mek): Somehow clean up connections when the client side goes away. | 34 // TODO(mek): Clean up connections, fire of closed events when connections die. |
| 27 class NavigatorConnectContextImpl : public NavigatorConnectContext { | 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 { |
| 28 public: | 41 public: |
| 29 using ConnectCallback = | 42 using ConnectCallback = |
| 30 base::Callback<void(const TransferredMessagePort& message_port, | 43 base::Callback<void(int message_port_id, bool success)>; |
| 31 int message_port_route_id, | |
| 32 bool success)>; | |
| 33 | 44 |
| 34 explicit NavigatorConnectContextImpl(); | 45 explicit NavigatorConnectContextImpl(); |
| 35 | 46 |
| 36 // 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 |
| 37 // correct service factory and passes the connection request off to there. | 48 // correct service factory and passes the connection request off to there. |
| 38 // Can call the callback before this method call returns. | 49 // Can call the callback before this method call returns. |
| 39 void Connect(NavigatorConnectClient client, | 50 void Connect(const GURL& target_url, |
| 40 MessagePortMessageFilter* message_port_message_filter, | 51 const GURL& origin, |
| 52 ServicePortServiceImpl* service_port_service, |
| 41 const ConnectCallback& callback); | 53 const ConnectCallback& callback); |
| 42 | 54 |
| 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 |
| 57 // valid. |
| 58 void ServicePortServiceDestroyed( |
| 59 ServicePortServiceImpl* service_port_service); |
| 60 |
| 43 // NavigatorConnectContext implementation. | 61 // NavigatorConnectContext implementation. |
| 44 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override; | 62 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override; |
| 45 | 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 |
| 46 private: | 71 private: |
| 47 ~NavigatorConnectContextImpl() override; | 72 ~NavigatorConnectContextImpl() override; |
| 48 | 73 |
| 49 void AddFactoryOnIOThread(scoped_ptr<NavigatorConnectServiceFactory> factory); | 74 void AddFactoryOnIOThread(scoped_ptr<NavigatorConnectServiceFactory> factory); |
| 50 | 75 |
| 51 // Callback called by service factories when a connection succeeded or failed. | 76 // Callback called by service factories when a connection succeeded or failed. |
| 52 void OnConnectResult(const NavigatorConnectClient& client, | 77 void OnConnectResult(const NavigatorConnectClient& client, |
| 53 int client_message_port_id, | 78 int client_message_port_id, |
| 54 int client_port_route_id, | |
| 55 const ConnectCallback& callback, | 79 const ConnectCallback& callback, |
| 56 MessagePortDelegate* delegate, | 80 MessagePortDelegate* delegate, |
| 57 bool data_as_values); | 81 bool data_as_values); |
| 58 | 82 |
| 59 // List of factories to try to handle URLs. | 83 // List of factories to try to handle URLs. |
| 60 ScopedVector<NavigatorConnectServiceFactory> service_factories_; | 84 ScopedVector<NavigatorConnectServiceFactory> service_factories_; |
| 85 |
| 86 // List of currently active ServicePorts. |
| 87 struct Port; |
| 88 std::map<int, Port> ports_; |
| 61 }; | 89 }; |
| 62 | 90 |
| 63 } // namespace content | 91 } // namespace content |
| 64 | 92 |
| 65 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ | 93 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ |
| OLD | NEW |