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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_
6 #define COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_
11 #include "base/macros.h"
12 #include "base/observer_list.h"
13 #include "base/sequence_checker.h"
14 #include "base/single_thread_task_runner.h"
15 #include "components/cast_channel/cast_socket.h"
16 #include "services/network/public/mojom/network_context.mojom.h"
18 namespace cast_channel {
20 // Manages, opens, and closes CastSockets.
21 // This class may be created on any thread. All methods, unless otherwise noted,
22 // must be invoked on the SequencedTaskRunner given by |task_runner_|.
23 class CastSocketService {
25 static CastSocketService* GetInstance();
27 virtual ~CastSocketService();
29 // Returns a pointer to the Logger member variable.
30 scoped_refptr<cast_channel::Logger> GetLogger();
32 // Removes the CastSocket corresponding to |channel_id| from the
33 // CastSocketRegistry. Returns nullptr if no such CastSocket exists.
34 std::unique_ptr<CastSocket> RemoveSocket(int channel_id);
36 // Returns the socket corresponding to |channel_id| if one exists, or nullptr
38 virtual CastSocket* GetSocket(int channel_id) const;
40 CastSocket* GetSocket(const net::IPEndPoint& ip_endpoint) const;
42 using NetworkContextGetter =
43 base::RepeatingCallback<network::mojom::NetworkContext*()>;
45 // Opens cast socket with |open_params| and invokes |open_cb| when opening
46 // operation finishes. If cast socket with |ip_endpoint| already exists,
47 // invoke |open_cb| directly with the existing socket.
48 // It is the caller's responsibility to ensure |open_params.ip_address| is
49 // a valid private IP address as determined by |IsValidCastIPAddress()|.
50 // |open_params|: Parameters necessary to open a Cast channel.
51 // |open_cb|: OnOpenCallback invoked when cast socket is opened.
52 // |network_context_getter| is called on UI thread only.
53 virtual void OpenSocket(NetworkContextGetter network_context_getter,
54 const CastSocketOpenParams& open_params,
55 CastSocket::OnOpenCallback open_cb);
57 // Adds |observer| to socket service. When socket service opens cast socket,
58 // it passes |observer| to opened socket.
59 // Does not take ownership of |observer|.
60 void AddObserver(CastSocket::Observer* observer);
62 // Remove |observer| from each socket in |sockets_|
63 void RemoveObserver(CastSocket::Observer* observer);
65 // Gets the TaskRunner for accessing this instance. Can be called from any
67 scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
71 void SetTaskRunnerForTest(
72 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) {
73 task_runner_ = task_runner;
76 // Allow test to inject a mock cast socket.
77 void SetSocketForTest(std::unique_ptr<CastSocket> socket_for_test);
80 friend class CastSocketServiceTest;
81 friend class MockCastSocketService;
85 // Adds |socket| to |sockets_| and returns raw pointer of |socket|. Takes
86 // ownership of |socket|.
87 CastSocket* AddSocket(std::unique_ptr<CastSocket> socket);
89 // Used to generate CastSocket id.
90 static int last_channel_id_;
92 // The collection of CastSocket keyed by channel_id.
93 std::map<int, std::unique_ptr<CastSocket>> sockets_;
95 // List of socket observers.
96 base::ObserverList<CastSocket::Observer>::Unchecked observers_;
98 scoped_refptr<Logger> logger_;
100 std::unique_ptr<CastSocket> socket_for_test_;
102 // The task runner on which |this| runs.
103 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
105 DISALLOW_COPY_AND_ASSIGN(CastSocketService);
108 } // namespace cast_channel
110 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_