[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / navigator_connect / navigator_connect_context_impl.h
blobe57c2e804a7b257521b9bf226c93fd9e2e86495e
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.
5 #ifndef CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_
6 #define CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_
8 #include <map>
9 #include "base/callback_forward.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/strings/string16.h"
12 #include "content/common/service_worker/service_worker_status_code.h"
13 #include "content/public/browser/navigator_connect_context.h"
15 class GURL;
17 namespace content {
19 struct MessagePortMessage;
20 class NavigatorConnectService;
21 class NavigatorConnectServiceFactory;
22 struct NavigatorConnectClient;
23 class ServicePortServiceImpl;
24 class ServiceWorkerContextWrapper;
25 class ServiceWorkerRegistration;
26 struct TransferredMessagePort;
28 // Tracks all active navigator.services connections, as well as available
29 // service factories. Delegates connection requests to the correct factory and
30 // passes messages on to the correct service.
31 // One instance of this class exists per StoragePartition.
32 // TODO(mek): Clean up connections, fire of closed events when connections die.
33 // TODO(mek): Update service side API to be fully ServicePort based.
34 // TODO(mek): Make ServicePorts that live in a service worker be able to survive
35 // the worker being restarted.
36 // TODO(mek): Add back ability for service ports to be backed by native code.
37 class NavigatorConnectContextImpl : public NavigatorConnectContext {
38 public:
39 using ConnectCallback =
40 base::Callback<void(int message_port_id, bool success)>;
42 explicit NavigatorConnectContextImpl(
43 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
45 // 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.
47 // Can call the callback before this method call returns.
48 void Connect(const GURL& target_url,
49 const GURL& origin,
50 ServicePortServiceImpl* service_port_service,
51 const ConnectCallback& callback);
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);
61 // Called by a ServicePortServiceImpl instance when it is about to be
62 // destroyed to inform this class that all its connections are no longer
63 // valid.
64 void ServicePortServiceDestroyed(
65 ServicePortServiceImpl* service_port_service);
67 // NavigatorConnectContext implementation.
68 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override;
70 private:
71 ~NavigatorConnectContextImpl() override;
73 void AddFactoryOnIOThread(scoped_ptr<NavigatorConnectServiceFactory> factory);
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);
84 // Callback called by service factories when a connection succeeded or failed.
85 void OnConnectResult(const ConnectCallback& callback,
86 int client_port_id,
87 int service_port_id,
88 const scoped_refptr<ServiceWorkerRegistration>&
89 service_worker_registration,
90 ServiceWorkerStatusCode status,
91 bool accept_connection,
92 const base::string16& name,
93 const base::string16& data);
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);
104 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
106 // List of factories to try to handle URLs.
107 ScopedVector<NavigatorConnectServiceFactory> service_factories_;
109 // List of currently active ServicePorts.
110 struct Port;
111 std::map<int, Port> ports_;
112 int next_port_id_;
115 } // namespace content
117 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_