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

Side by Side Diff: components/cast_channel/cast_socket_service.h

Issue 2925053005: [cast_channel] Implement CastSocketService::OpenSocket() (Closed)
Patch Set: resolve code review comments from Mark Created 3 years, 6 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ 5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_
6 #define COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ 6 #define COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/threading/thread_checker.h" 12 #include "base/threading/thread_checker.h"
13 #include "components/cast_channel/cast_socket.h" 13 #include "components/cast_channel/cast_socket.h"
14 #include "components/keyed_service/core/refcounted_keyed_service.h" 14 #include "components/keyed_service/core/refcounted_keyed_service.h"
15 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
16 16
17 namespace cast_channel { 17 namespace cast_channel {
18 18
19 // This class adds, removes, and returns cast sockets created by CastChannelAPI 19 // This class adds, removes, and returns cast sockets created by CastChannelAPI
20 // to underlying storage. 20 // to underlying storage.
21 // Instance of this class is created on the UI thread and destroyed on the IO 21 // Instance of this class is created on the UI thread and destroyed on the IO
22 // thread. All public API must be called from the IO thread. 22 // thread. All public API must be called from the IO thread.
23 class CastSocketService : public RefcountedKeyedService { 23 class CastSocketService : public RefcountedKeyedService {
24 public: 24 public:
25 CastSocketService(); 25 CastSocketService();
26 26
27 // Adds |socket| to |sockets_| and returns the new channel_id. Takes ownership 27 // Adds |socket| to |sockets_| and returns raw pointer of |socket|. Takes
28 // of |socket|. 28 // ownership of |socket|.
29 int AddSocket(std::unique_ptr<CastSocket> socket); 29 CastSocket* AddSocket(std::unique_ptr<CastSocket> socket);
30 30
31 // Removes the CastSocket corresponding to |channel_id| from the 31 // Removes the CastSocket corresponding to |channel_id| from the
32 // CastSocketRegistry. Returns nullptr if no such CastSocket exists. 32 // CastSocketRegistry. Returns nullptr if no such CastSocket exists.
33 std::unique_ptr<CastSocket> RemoveSocket(int channel_id); 33 std::unique_ptr<CastSocket> RemoveSocket(int channel_id);
34 34
35 // Returns the socket corresponding to |channel_id| if one exists, or nullptr 35 // Returns the socket corresponding to |channel_id| if one exists, or nullptr
36 // otherwise. 36 // otherwise.
37 CastSocket* GetSocket(int channel_id) const; 37 CastSocket* GetSocket(int channel_id) const;
38 38
39 CastSocket* GetSocket(const net::IPEndPoint& ip_endpoint) const;
40
41 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening
42 // operation finishes. If cast socket with |ip_endpoint| already exists,
43 // invoke |open_cb| directly with existing socket's channel ID.
44 // Parameters:
45 // |ip_endpoint|: IP address and port of the remote host.
46 // |net_log|: Log of socket events.
47 // |connect_timeout|: Connection timeout interval.
48 // |liveness_timeout|: Liveness timeout for connect calls.
49 // |ping_interval|: Ping interval.
50 // |logger|: Log of cast channel events.
51 // |device_capabilities|: Device capabilities.
52 // |open_cb|: OnOpenCallback invoked when cast socket is opened.
53 // |observer|: Observer handles messages and errors on newly opened socket.
54 // Does not take ownership of |observer|.
55 int OpenSocket(const net::IPEndPoint& ip_endpoint,
56 net::NetLog* net_log,
57 const base::TimeDelta& connect_timeout,
58 const base::TimeDelta& liveness_timeout,
59 const base::TimeDelta& ping_interval,
60 const scoped_refptr<Logger>& logger,
61 uint64_t device_capabilities,
62 const CastSocket::OnOpenCallback& open_cb,
63 CastSocket::Observer* observer);
64
65 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening
66 // operation finishes. If cast socket with |ip_endpoint| already exists,
67 // invoke |open_cb| directly with existing socket's channel ID.
68 // |ip_endpoint|: IP address and port of the remote host.
69 // |net_log|: Log of socket events.
70 // |open_cb|: OnOpenCallback invoked when cast socket is opened.
71 // |observer|: Observer handles messages and errors on newly opened socket.
72 // Does not take ownership of |observer|.
73 int OpenSocket(const net::IPEndPoint& ip_endpoint,
74 net::NetLog* net_log,
75 const CastSocket::OnOpenCallback& open_cb,
76 CastSocket::Observer* observer);
77
39 // Returns an observer corresponding to |id|. 78 // Returns an observer corresponding to |id|.
40 CastSocket::Observer* GetObserver(const std::string& id); 79 CastSocket::Observer* GetObserver(const std::string& id);
41 80
42 // Adds |observer| to |socket_observer_map_| keyed by |id|. Return raw pointer 81 // Adds |observer| to |socket_observer_map_| keyed by |id|. Return raw pointer
43 // of the newly added observer. 82 // of the newly added observer.
44 CastSocket::Observer* AddObserver( 83 CastSocket::Observer* AddObserver(
45 const std::string& id, 84 const std::string& id,
46 std::unique_ptr<CastSocket::Observer> observer); 85 std::unique_ptr<CastSocket::Observer> observer);
47 86
87 // Allow test to inject a mock cast socket.
88 void SetSocketForTest(std::unique_ptr<CastSocket> socket_for_test);
89
48 private: 90 private:
49 ~CastSocketService() override; 91 ~CastSocketService() override;
50 92
51 // RefcountedKeyedService implementation. 93 // RefcountedKeyedService implementation.
52 void ShutdownOnUIThread() override; 94 void ShutdownOnUIThread() override;
53 95
54 // Used to generate CastSocket id. 96 // Used to generate CastSocket id.
55 static int last_channel_id_; 97 static int last_channel_id_;
56 98
57 // The collection of CastSocket keyed by channel_id. 99 // The collection of CastSocket keyed by channel_id.
58 std::map<int, std::unique_ptr<CastSocket>> sockets_; 100 std::map<int, std::unique_ptr<CastSocket>> sockets_;
59 101
60 // Map of CastSocket::Observer keyed by observer id. For extension side 102 // Map of CastSocket::Observer keyed by observer id. For extension side
61 // observers, id is extension_id; For browser side observers, id is a hard 103 // observers, id is extension_id; For browser side observers, id is a hard
62 // coded string. 104 // coded string.
63 std::map<std::string, std::unique_ptr<CastSocket::Observer>> 105 std::map<std::string, std::unique_ptr<CastSocket::Observer>>
64 socket_observer_map_; 106 socket_observer_map_;
65 107
108 scoped_refptr<Logger> logger_;
109
110 std::unique_ptr<CastSocket> socket_for_test_;
111
66 THREAD_CHECKER(thread_checker_); 112 THREAD_CHECKER(thread_checker_);
67 113
68 DISALLOW_COPY_AND_ASSIGN(CastSocketService); 114 DISALLOW_COPY_AND_ASSIGN(CastSocketService);
69 }; 115 };
70 116
71 } // namespace cast_channel 117 } // namespace cast_channel
72 118
73 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ 119 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698