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

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

Issue 2974523002: [cast_channel] Make CastSocketService a global leaky singleton (Closed)
Patch Set: merge with master Created 3 years, 5 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 #include "components/cast_channel/cast_socket_service.h" 5 #include "components/cast_channel/cast_socket_service.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "components/cast_channel/cast_socket.h" 8 #include "components/cast_channel/cast_socket.h"
9 #include "components/cast_channel/logger.h" 9 #include "components/cast_channel/logger.h"
10 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
(...skipping 10 matching lines...) Expand all
21 // Liveness timeout for connect calls, in milliseconds. If no message is 21 // Liveness timeout for connect calls, in milliseconds. If no message is
22 // received from the receiver during kConnectLivenessTimeoutSecs, it is 22 // received from the receiver during kConnectLivenessTimeoutSecs, it is
23 // considered gone. 23 // considered gone.
24 const int kConnectLivenessTimeoutSecs = kPingIntervalInSecs * 2; 24 const int kConnectLivenessTimeoutSecs = kPingIntervalInSecs * 2;
25 } // namespace 25 } // namespace
26 26
27 namespace cast_channel { 27 namespace cast_channel {
28 28
29 int CastSocketService::last_channel_id_ = 0; 29 int CastSocketService::last_channel_id_ = 0;
30 30
31 CastSocketService::CastSocketService() 31 CastSocketService::CastSocketService() : logger_(new Logger()) {
32 : RefcountedKeyedService(
33 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)),
34 logger_(new Logger()) {
35 DETACH_FROM_THREAD(thread_checker_); 32 DETACH_FROM_THREAD(thread_checker_);
36 } 33 }
37 34
38 CastSocketService::~CastSocketService() { 35 // This is a leaky singleton and the dtor won't be called.
39 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); 36 CastSocketService::~CastSocketService() = default;
37
38 // static
39 CastSocketService* CastSocketService::GetInstance() {
40 return base::Singleton<CastSocketService,
41 base::LeakySingletonTraits<CastSocketService>>::get();
40 } 42 }
41 43
42 scoped_refptr<Logger> CastSocketService::GetLogger() { 44 scoped_refptr<Logger> CastSocketService::GetLogger() {
43 return logger_; 45 return logger_;
44 } 46 }
45 47
46 CastSocket* CastSocketService::AddSocket(std::unique_ptr<CastSocket> socket) { 48 CastSocket* CastSocketService::AddSocket(std::unique_ptr<CastSocket> socket) {
47 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); 49 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
48 DCHECK(socket); 50 DCHECK(socket);
49 int id = ++last_channel_id_; 51 int id = ++last_channel_id_;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 CastSocket::Observer* observer) { 123 CastSocket::Observer* observer) {
122 auto connect_timeout = base::TimeDelta::FromSeconds(kConnectTimeoutSecs); 124 auto connect_timeout = base::TimeDelta::FromSeconds(kConnectTimeoutSecs);
123 auto ping_interval = base::TimeDelta::FromSeconds(kPingIntervalInSecs); 125 auto ping_interval = base::TimeDelta::FromSeconds(kPingIntervalInSecs);
124 auto liveness_timeout = 126 auto liveness_timeout =
125 base::TimeDelta::FromSeconds(kConnectLivenessTimeoutSecs); 127 base::TimeDelta::FromSeconds(kConnectLivenessTimeoutSecs);
126 return OpenSocket(ip_endpoint, net_log, connect_timeout, liveness_timeout, 128 return OpenSocket(ip_endpoint, net_log, connect_timeout, liveness_timeout,
127 ping_interval, CastDeviceCapability::NONE, open_cb, 129 ping_interval, CastDeviceCapability::NONE, open_cb,
128 observer); 130 observer);
129 } 131 }
130 132
131 CastSocket::Observer* CastSocketService::GetObserver(const std::string& id) { 133 void CastSocketService::RemoveObserver(CastSocket::Observer* observer) {
132 auto it = socket_observer_map_.find(id); 134 for (auto& socket_it : sockets_)
133 return it == socket_observer_map_.end() ? nullptr : it->second.get(); 135 socket_it.second->RemoveObserver(observer);
134 }
135
136 CastSocket::Observer* CastSocketService::AddObserver(
137 const std::string& id,
138 std::unique_ptr<CastSocket::Observer> observer) {
139 CastSocket::Observer* observer_ptr = observer.get();
140 socket_observer_map_.insert(std::make_pair(id, std::move(observer)));
141 return observer_ptr;
142 } 136 }
143 137
144 void CastSocketService::SetSocketForTest( 138 void CastSocketService::SetSocketForTest(
145 std::unique_ptr<cast_channel::CastSocket> socket_for_test) { 139 std::unique_ptr<cast_channel::CastSocket> socket_for_test) {
146 socket_for_test_ = std::move(socket_for_test); 140 socket_for_test_ = std::move(socket_for_test);
147 } 141 }
148 142
149 void CastSocketService::ShutdownOnUIThread() {}
150
151 } // namespace cast_channel 143 } // namespace cast_channel
OLDNEW
« no previous file with comments | « components/cast_channel/cast_socket_service.h ('k') | components/cast_channel/cast_socket_service_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698