Add ICU message format support
[chromium-blink-merge.git] / content / browser / navigator_connect / navigator_connect_context_impl.h
blobd5b2d047c31872c4395b2763bd4923bd22b5415b
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 "content/public/browser/message_port_delegate.h"
12 #include "content/public/browser/navigator_connect_context.h"
14 // Windows headers will redefine SendMessage.
15 #ifdef SendMessage
16 #undef SendMessage
17 #endif
19 class GURL;
21 namespace content {
23 class MessagePortMessageFilter;
24 class NavigatorConnectService;
25 class NavigatorConnectServiceFactory;
26 struct NavigatorConnectClient;
27 class ServicePortServiceImpl;
28 struct TransferredMessagePort;
30 // Tracks all active navigator.services connections, as well as available
31 // service factories. Delegates connection requests to the correct factory and
32 // passes messages on to the correct service.
33 // One instance of this class exists per StoragePartition.
34 // 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
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 {
41 public:
42 using ConnectCallback =
43 base::Callback<void(int message_port_id, bool success)>;
45 explicit NavigatorConnectContextImpl();
47 // 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.
49 // Can call the callback before this method call returns.
50 void Connect(const GURL& target_url,
51 const GURL& origin,
52 ServicePortServiceImpl* service_port_service,
53 const ConnectCallback& callback);
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);
61 // NavigatorConnectContext implementation.
62 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override;
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;
71 private:
72 ~NavigatorConnectContextImpl() override;
74 void AddFactoryOnIOThread(scoped_ptr<NavigatorConnectServiceFactory> factory);
76 // Callback called by service factories when a connection succeeded or failed.
77 void OnConnectResult(const NavigatorConnectClient& client,
78 int client_message_port_id,
79 const ConnectCallback& callback,
80 MessagePortDelegate* delegate,
81 bool data_as_values);
83 // List of factories to try to handle URLs.
84 ScopedVector<NavigatorConnectServiceFactory> service_factories_;
86 // List of currently active ServicePorts.
87 struct Port;
88 std::map<int, Port> ports_;
91 } // namespace content
93 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_