[M73 Dev][EFL] Disable VizDisplayCompositor for EFL port
[platform/framework/web/chromium-efl.git] / components / cast_channel / cast_socket_service.h
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.
4
5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_
6 #define COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_
7
8 #include <map>
9 #include <memory>
10
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"
17
18 namespace cast_channel {
19
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 {
24  public:
25   static CastSocketService* GetInstance();
26
27   virtual ~CastSocketService();
28
29   // Returns a pointer to the Logger member variable.
30   scoped_refptr<cast_channel::Logger> GetLogger();
31
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);
35
36   // Returns the socket corresponding to |channel_id| if one exists, or nullptr
37   // otherwise.
38   virtual CastSocket* GetSocket(int channel_id) const;
39
40   CastSocket* GetSocket(const net::IPEndPoint& ip_endpoint) const;
41
42   using NetworkContextGetter =
43       base::RepeatingCallback<network::mojom::NetworkContext*()>;
44
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);
56
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);
61
62   // Remove |observer| from each socket in |sockets_|
63   void RemoveObserver(CastSocket::Observer* observer);
64
65   // Gets the TaskRunner for accessing this instance. Can be called from any
66   // thread.
67   scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
68     return task_runner_;
69   }
70
71   void SetTaskRunnerForTest(
72       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) {
73     task_runner_ = task_runner;
74   }
75
76   // Allow test to inject a mock cast socket.
77   void SetSocketForTest(std::unique_ptr<CastSocket> socket_for_test);
78
79  private:
80   friend class CastSocketServiceTest;
81   friend class MockCastSocketService;
82
83   CastSocketService();
84
85   // Adds |socket| to |sockets_| and returns raw pointer of |socket|. Takes
86   // ownership of |socket|.
87   CastSocket* AddSocket(std::unique_ptr<CastSocket> socket);
88
89   // Used to generate CastSocket id.
90   static int last_channel_id_;
91
92   // The collection of CastSocket keyed by channel_id.
93   std::map<int, std::unique_ptr<CastSocket>> sockets_;
94
95   // List of socket observers.
96   base::ObserverList<CastSocket::Observer>::Unchecked observers_;
97
98   scoped_refptr<Logger> logger_;
99
100   std::unique_ptr<CastSocket> socket_for_test_;
101
102   // The task runner on which |this| runs.
103   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
104
105   DISALLOW_COPY_AND_ASSIGN(CastSocketService);
106 };
107
108 }  // namespace cast_channel
109
110 #endif  // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_