Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: content/browser/navigator_connect/navigator_connect_context_impl.h

Issue 861373002: Refactor navigator.connect code to make it more flexible. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: lower similarity threshold Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/memory/scoped_vector.h"
10 #include "content/browser/message_port_delegate.h" 11 #include "content/browser/message_port_delegate.h"
11 #include "content/common/service_worker/service_worker_status_code.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 MessagePortDelegate {
26 public base::RefCountedThreadSafe<NavigatorConnectContext> {
27 public: 27 public:
28 explicit NavigatorConnectContext( 28 using ConnectCallback = base::Callback<void(bool success)>;
29 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
30 29
31 // Registers an accepted connection and updates MessagePortService with the 30 explicit NavigatorConnectContextImpl();
32 // new route/delegate for the message port associated with this connection. 31
33 void RegisterConnection(const CrossOriginServiceWorkerClient& client, 32 // Called when a new connection request comes in from a client. Finds the
34 const scoped_refptr<ServiceWorkerRegistration>& 33 // correct service factory and passes the connection request of to there.
scheib 2015/01/23 22:03:58 of->off
Marijn Kruisselbrink 2015/01/26 21:34:26 Done.
35 service_worker_registration); 34 // Can call the callback before this method call returns.
35 void Connect(const NavigatorConnectClient& client,
36 const ConnectCallback& callback);
37
38 // NavigatorConnectContext implementation.
39 void AddFactory(scoped_ptr<NavigatorConnectServiceFactory> factory) override;
36 40
37 // MessagePortDelegate implementation. For the implementation of this class, 41 // MessagePortDelegate implementation. For the implementation of this class,
38 // the route_id of a message port is always equal to the message_port_id. 42 // the route_id of a message port is always equal to the message_port_id.
39 void SendMessage(int route_id, 43 void SendMessageToPort(
40 const base::string16& message, 44 int route_id,
41 const std::vector<int>& sent_message_port_ids) override; 45 const base::string16& message,
46 const std::vector<int>& sent_message_port_ids) override;
42 void SendMessagesAreQueued(int route_id) override; 47 void SendMessagesAreQueued(int route_id) override;
43 48
44 private: 49 private:
45 friend class base::RefCountedThreadSafe<NavigatorConnectContext>; 50 ~NavigatorConnectContextImpl() override;
46 ~NavigatorConnectContext() override;
47 51
48 // Callback called by SendMessage when a ServiceWorkerRegistration for the 52 // Callback called by service factories when a connection succeeded or failed.
49 // given message port has been located. 53 void OnConnectResult(const NavigatorConnectClient& client,
50 void DoSendMessage(const CrossOriginServiceWorkerClient& client, 54 const ConnectCallback& callback,
51 const base::string16& message, 55 scoped_ptr<NavigatorConnectService> service);
52 const std::vector<int>& sent_message_port_ids,
53 ServiceWorkerStatusCode service_worker_status,
54 const scoped_refptr<ServiceWorkerRegistration>&
55 service_worker_registration);
56 56
57 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; 57 // List of factories to try to handle URLs.
58 struct Connection; 58 ScopedVector<NavigatorConnectServiceFactory> service_factories_;
59
59 // Maps route ids to connections. For the purpose of this class, the route id 60 // 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 // of a connection is the same as its message_port_id.
61 std::map<int, Connection> connections_; 62 std::map<int, NavigatorConnectService*> connected_services_;
62 }; 63 };
63 64
64 } // namespace content 65 } // namespace content
65 66
66 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_H_ 67 #endif // CONTENT_BROWSER_NAVIGATOR_CONNECT_NAVIGATOR_CONNECT_CONTEXT_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698