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_H_ | 5 #ifndef CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ |
6 #define CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_H_ | 6 #define CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include "base/memory/ref_counted.h" | 9 #include "base/callback_forward.h" |
10 #include "content/common/service_worker/service_worker_status_code.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 | 13 |
13 namespace content { | 14 namespace content { |
14 | 15 |
15 struct CrossOriginServiceWorkerClient; | 16 struct NavigatorConnectClient; |
16 class ServiceWorkerContextWrapper; | 17 class NavigatorConnectService; |
17 class ServiceWorkerRegistration; | 18 class NavigatorConnectServiceFactory; |
18 | 19 |
19 // Tracks all active navigator.connect connections, wakes up service workers | 20 // Tracks all active navigator.connect connections, as well as available service |
20 // when a message arrives, and routes the messages. | 21 // factories. Delegates connection requests to the correct factory and passes |
| 22 // messages on to the correct service. |
21 // One instance of this class exists per StoragePartition. | 23 // One instance of this class exists per StoragePartition. |
22 // TODO(mek): Clean up message ports when a service worker is unregistered. | |
23 // TODO(mek): Somehow clean up connections when the client side goes away. | 24 // TODO(mek): Somehow clean up connections when the client side goes away. |
24 class NavigatorConnectContext | 25 class NavigatorConnectContextImpl : public NavigatorConnectContext { |
25 : public MessagePortDelegate, | |
26 public base::RefCountedThreadSafe<NavigatorConnectContext> { | |
27 public: | 26 public: |
28 explicit NavigatorConnectContext( | 27 using ConnectCallback = base::Callback<void(bool success)>; |
29 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | |
30 | 28 |
31 // Registers an accepted connection and updates MessagePortService with the | 29 explicit NavigatorConnectContextImpl(); |
32 // new route/delegate for the message port associated with this connection. | |
33 void RegisterConnection(const CrossOriginServiceWorkerClient& client, | |
34 const scoped_refptr<ServiceWorkerRegistration>& | |
35 service_worker_registration); | |
36 | 30 |
37 // MessagePortDelegate implementation. For the implementation of this class, | 31 // Called when a new connection request comes in from a client. Finds the |
38 // the route_id of a message port is always equal to the message_port_id. | 32 // correct service factory and passes the connection request off to there. |
39 void SendMessage(int route_id, | 33 // Can call the callback before this method call returns. |
40 const base::string16& message, | 34 void Connect(const NavigatorConnectClient& client, |
41 const std::vector<int>& sent_message_port_ids) override; | 35 const ConnectCallback& callback); |
42 void SendMessagesAreQueued(int route_id) override; | 36 |
| 37 // NavigatorConnectContext implementation. |
| 38 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override; |
43 | 39 |
44 private: | 40 private: |
45 friend class base::RefCountedThreadSafe<NavigatorConnectContext>; | 41 ~NavigatorConnectContextImpl() override; |
46 ~NavigatorConnectContext() override; | |
47 | 42 |
48 // Callback called by SendMessage when a ServiceWorkerRegistration for the | 43 // Callback called by service factories when a connection succeeded or failed. |
49 // given message port has been located. | 44 void OnConnectResult(const NavigatorConnectClient& client, |
50 void DoSendMessage(const CrossOriginServiceWorkerClient& client, | 45 const ConnectCallback& callback, |
51 const base::string16& message, | 46 MessagePortDelegate* delegate); |
52 const std::vector<int>& sent_message_port_ids, | |
53 ServiceWorkerStatusCode service_worker_status, | |
54 const scoped_refptr<ServiceWorkerRegistration>& | |
55 service_worker_registration); | |
56 | 47 |
57 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | 48 // List of factories to try to handle URLs. |
58 struct Connection; | 49 ScopedVector<NavigatorConnectServiceFactory> service_factories_; |
59 // Maps route ids to connections. For the purpose of this class, the route id | |
60 // of a connection is the same as its message_port_id. | |
61 std::map<int, Connection> connections_; | |
62 }; | 50 }; |
63 | 51 |
64 } // namespace content | 52 } // namespace content |
65 | 53 |
66 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_H_ | 54 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_ |
OLD | NEW |