OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_H_ | |
6 #define CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_H_ | |
7 | |
8 #include <map> | |
9 #include "base/memory/ref_counted.h" | |
10 #include "content/common/service_worker/service_worker_status_code.h" | |
11 #include "content/public/browser/message_port_delegate.h" | |
12 | |
13 namespace content { | |
14 | |
15 struct CrossOriginServiceWorkerClient; | |
16 class ServiceWorkerContextWrapper; | |
17 class ServiceWorkerRegistration; | |
18 | |
19 // Tracks all active navigator.connect connections, wakes up service workers | |
20 // when a message arrives, and routes the messages. | |
21 // 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 class NavigatorConnectContext | |
25 : public MessagePortDelegate, | |
26 public base::RefCountedThreadSafe<NavigatorConnectContext> { | |
27 public: | |
28 explicit NavigatorConnectContext( | |
29 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | |
30 | |
31 // Registers an accepted connection and updates MessagePortService with the | |
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 | |
37 // MessagePortDelegate implementation. For the implementation of this class, | |
38 // the route_id of a message port is always equal to the message_port_id. | |
39 void SendMessage(int route_id, | |
40 const base::string16& message, | |
41 const std::vector<int>& sent_message_port_ids) override; | |
42 void SendMessagesAreQueued(int route_id) override; | |
43 | |
44 private: | |
45 friend class base::RefCountedThreadSafe<NavigatorConnectContext>; | |
46 ~NavigatorConnectContext() override; | |
47 | |
48 // Callback called by SendMessage when a ServiceWorkerRegistration for the | |
49 // given message port has been located. | |
50 void DoSendMessage(const CrossOriginServiceWorkerClient& client, | |
51 const base::string16& message, | |
52 const std::vector<int>& sent_message_port_ids, | |
53 ServiceWorkerStatusCode service_worker_status, | |
54 const scoped_refptr<ServiceWorkerRegistration>& | |
55 service_worker_registration); | |
56 | |
57 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | |
58 struct Connection; | |
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 }; | |
63 | |
64 } // namespace content | |
65 | |
66 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_H_ | |
OLD | NEW |