Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / navigator_connect / navigator_connect_context_impl.h
blobffb4647041f445d342383a458f59eea8e3df778f
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 namespace content {
16 class MessagePortMessageFilter;
17 class NavigatorConnectService;
18 class NavigatorConnectServiceFactory;
19 struct NavigatorConnectClient;
20 struct TransferredMessagePort;
22 // Tracks all active navigator.connect connections, as well as available service
23 // factories. Delegates connection requests to the correct factory and passes
24 // messages on to the correct service.
25 // One instance of this class exists per StoragePartition.
26 // TODO(mek): Somehow clean up connections when the client side goes away.
27 class NavigatorConnectContextImpl : public NavigatorConnectContext {
28 public:
29 using ConnectCallback =
30 base::Callback<void(const TransferredMessagePort& message_port,
31 int message_port_route_id,
32 bool success)>;
34 explicit NavigatorConnectContextImpl();
36 // 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.
38 // Can call the callback before this method call returns.
39 void Connect(NavigatorConnectClient client,
40 MessagePortMessageFilter* message_port_message_filter,
41 const ConnectCallback& callback);
43 // NavigatorConnectContext implementation.
44 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override;
46 private:
47 ~NavigatorConnectContextImpl() override;
49 void AddFactoryOnIOThread(scoped_ptr<NavigatorConnectServiceFactory> factory);
51 // Callback called by service factories when a connection succeeded or failed.
52 void OnConnectResult(const NavigatorConnectClient& client,
53 int client_message_port_id,
54 int client_port_route_id,
55 const ConnectCallback& callback,
56 MessagePortDelegate* delegate,
57 bool data_as_values);
59 // List of factories to try to handle URLs.
60 ScopedVector<NavigatorConnectServiceFactory> service_factories_;
63 } // namespace content
65 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_