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

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

Issue 2913033003: [cast_channel] Move cast_channel related files from //extensions to //components (Closed)
Patch Set: fix buildbot compile errors 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "extensions/browser/api/cast_channel/keep_alive_delegate.h" 5 #include "components/cast_channel/keep_alive_delegate.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "extensions/browser/api/cast_channel/cast_socket.h" 12 #include "base/values.h"
13 #include "extensions/browser/api/cast_channel/logger.h" 13 #include "components/cast_channel/cast_socket.h"
14 #include "extensions/common/api/cast_channel/cast_channel.pb.h" 14 #include "components/cast_channel/logger.h"
15 #include "extensions/common/api/cast_channel/logging.pb.h" 15 #include "components/cast_channel/proto/cast_channel.pb.h"
16 #include "components/cast_channel/proto/logging.pb.h"
16 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
17 18
18 namespace extensions {
19 namespace api {
20 namespace cast_channel { 19 namespace cast_channel {
21 namespace { 20 namespace {
22 21
23 const char kHeartbeatNamespace[] = "urn:x-cast:com.google.cast.tp.heartbeat"; 22 const char kHeartbeatNamespace[] = "urn:x-cast:com.google.cast.tp.heartbeat";
24 const char kPingSenderId[] = "chrome"; 23 const char kPingSenderId[] = "chrome";
25 const char kPingReceiverId[] = "receiver-0"; 24 const char kPingReceiverId[] = "receiver-0";
26 const char kTypeNodeId[] = "type"; 25 const char kTypeNodeId[] = "type";
27 26
28 // Parses the JSON-encoded payload of |message| and returns the value in the 27 // Parses the JSON-encoded payload of |message| and returns the value in the
29 // "type" field or the empty string if the parse fails or the field is not 28 // "type" field or the empty string if the parse fails or the field is not
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 inner_delegate_(std::move(inner_delegate)), 80 inner_delegate_(std::move(inner_delegate)),
82 liveness_timeout_(liveness_timeout), 81 liveness_timeout_(liveness_timeout),
83 ping_interval_(ping_interval) { 82 ping_interval_(ping_interval) {
84 DCHECK(ping_interval_ < liveness_timeout_); 83 DCHECK(ping_interval_ < liveness_timeout_);
85 DCHECK(inner_delegate_); 84 DCHECK(inner_delegate_);
86 DCHECK(socket_); 85 DCHECK(socket_);
87 ping_message_ = CreateKeepAliveMessage(kHeartbeatPingType); 86 ping_message_ = CreateKeepAliveMessage(kHeartbeatPingType);
88 pong_message_ = CreateKeepAliveMessage(kHeartbeatPongType); 87 pong_message_ = CreateKeepAliveMessage(kHeartbeatPongType);
89 } 88 }
90 89
91 KeepAliveDelegate::~KeepAliveDelegate() { 90 KeepAliveDelegate::~KeepAliveDelegate() {}
92 }
93 91
94 void KeepAliveDelegate::SetTimersForTest( 92 void KeepAliveDelegate::SetTimersForTest(
95 std::unique_ptr<base::Timer> injected_ping_timer, 93 std::unique_ptr<base::Timer> injected_ping_timer,
96 std::unique_ptr<base::Timer> injected_liveness_timer) { 94 std::unique_ptr<base::Timer> injected_liveness_timer) {
97 ping_timer_ = std::move(injected_ping_timer); 95 ping_timer_ = std::move(injected_ping_timer);
98 liveness_timer_ = std::move(injected_liveness_timer); 96 liveness_timer_ = std::move(injected_liveness_timer);
99 } 97 }
100 98
101 void KeepAliveDelegate::Start() { 99 void KeepAliveDelegate::Start() {
102 DCHECK(thread_checker_.CalledOnValidThread()); 100 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
103 DCHECK(!started_); 101 DCHECK(!started_);
104 102
105 VLOG(1) << "Starting keep-alive timers."; 103 VLOG(1) << "Starting keep-alive timers.";
106 VLOG(1) << "Ping timeout: " << ping_interval_; 104 VLOG(1) << "Ping timeout: " << ping_interval_;
107 VLOG(1) << "Liveness timeout: " << liveness_timeout_; 105 VLOG(1) << "Liveness timeout: " << liveness_timeout_;
108 106
109 // Use injected mock timers, if provided. 107 // Use injected mock timers, if provided.
110 if (!ping_timer_) { 108 if (!ping_timer_) {
111 ping_timer_.reset(new base::Timer(true, false)); 109 ping_timer_.reset(new base::Timer(true, false));
112 } 110 }
(...skipping 14 matching lines...) Expand all
127 } 125 }
128 126
129 void KeepAliveDelegate::ResetTimers() { 127 void KeepAliveDelegate::ResetTimers() {
130 DCHECK(started_); 128 DCHECK(started_);
131 ping_timer_->Reset(); 129 ping_timer_->Reset();
132 liveness_timer_->Reset(); 130 liveness_timer_->Reset();
133 } 131 }
134 132
135 void KeepAliveDelegate::SendKeepAliveMessage(const CastMessage& message, 133 void KeepAliveDelegate::SendKeepAliveMessage(const CastMessage& message,
136 const char* message_type) { 134 const char* message_type) {
137 DCHECK(thread_checker_.CalledOnValidThread()); 135 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
138 VLOG(2) << "Sending " << message_type; 136 VLOG(2) << "Sending " << message_type;
139 socket_->transport()->SendMessage( 137 socket_->transport()->SendMessage(
140 message, base::Bind(&KeepAliveDelegate::SendKeepAliveMessageComplete, 138 message, base::Bind(&KeepAliveDelegate::SendKeepAliveMessageComplete,
141 base::Unretained(this), message_type)); 139 base::Unretained(this), message_type));
142 } 140 }
143 141
144 void KeepAliveDelegate::SendKeepAliveMessageComplete(const char* message_type, 142 void KeepAliveDelegate::SendKeepAliveMessageComplete(const char* message_type,
145 int rv) { 143 int rv) {
146 VLOG(2) << "Sending " << message_type << " complete, rv=" << rv; 144 VLOG(2) << "Sending " << message_type << " complete, rv=" << rv;
147 if (rv != net::OK) { 145 if (rv != net::OK) {
148 // An error occurred while sending the ping response. 146 // An error occurred while sending the ping response.
149 VLOG(1) << "Error sending " << message_type; 147 VLOG(1) << "Error sending " << message_type;
150 logger_->LogSocketEventWithRv(socket_->id(), proto::PING_WRITE_ERROR, rv); 148 logger_->LogSocketEventWithRv(socket_->id(), proto::PING_WRITE_ERROR, rv);
151 OnError(ChannelError::CAST_SOCKET_ERROR); 149 OnError(ChannelError::CAST_SOCKET_ERROR);
152 } 150 }
153 } 151 }
154 152
155 void KeepAliveDelegate::LivenessTimeout() { 153 void KeepAliveDelegate::LivenessTimeout() {
156 OnError(ChannelError::PING_TIMEOUT); 154 OnError(ChannelError::PING_TIMEOUT);
157 Stop(); 155 Stop();
158 } 156 }
159 157
160 // CastTransport::Delegate interface. 158 // CastTransport::Delegate interface.
161 void KeepAliveDelegate::OnError(ChannelError error_state) { 159 void KeepAliveDelegate::OnError(ChannelError error_state) {
162 DCHECK(thread_checker_.CalledOnValidThread()); 160 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
163 VLOG(1) << "KeepAlive::OnError: " 161 VLOG(1) << "KeepAlive::OnError: "
164 << ::cast_channel::ChannelErrorToString(error_state); 162 << ::cast_channel::ChannelErrorToString(error_state);
165 inner_delegate_->OnError(error_state); 163 inner_delegate_->OnError(error_state);
166 Stop(); 164 Stop();
167 } 165 }
168 166
169 void KeepAliveDelegate::OnMessage(const CastMessage& message) { 167 void KeepAliveDelegate::OnMessage(const CastMessage& message) {
170 DCHECK(thread_checker_.CalledOnValidThread()); 168 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
171 VLOG(2) << "KeepAlive::OnMessage : " << message.payload_utf8(); 169 VLOG(2) << "KeepAlive::OnMessage : " << message.payload_utf8();
172 170
173 if (started_) 171 if (started_)
174 ResetTimers(); 172 ResetTimers();
175 173
176 // PING and PONG messages are intercepted and handled by KeepAliveDelegate 174 // PING and PONG messages are intercepted and handled by KeepAliveDelegate
177 // here. All other messages are passed through to |inner_delegate_|. 175 // here. All other messages are passed through to |inner_delegate_|.
178 const std::string payload_type = ParseForPayloadType(message); 176 const std::string payload_type = ParseForPayloadType(message);
179 if (payload_type == kHeartbeatPingType) { 177 if (payload_type == kHeartbeatPingType) {
180 VLOG(2) << "Received PING."; 178 VLOG(2) << "Received PING.";
181 if (started_) 179 if (started_)
182 SendKeepAliveMessage(pong_message_, kHeartbeatPongType); 180 SendKeepAliveMessage(pong_message_, kHeartbeatPongType);
183 } else if (payload_type == kHeartbeatPongType) { 181 } else if (payload_type == kHeartbeatPongType) {
184 VLOG(2) << "Received PONG."; 182 VLOG(2) << "Received PONG.";
185 } else { 183 } else {
186 inner_delegate_->OnMessage(message); 184 inner_delegate_->OnMessage(message);
187 } 185 }
188 } 186 }
189 187
190 void KeepAliveDelegate::Stop() { 188 void KeepAliveDelegate::Stop() {
191 if (started_) { 189 if (started_) {
192 started_ = false; 190 started_ = false;
193 ping_timer_->Stop(); 191 ping_timer_->Stop();
194 liveness_timer_->Stop(); 192 liveness_timer_->Stop();
195 } 193 }
196 } 194 }
197 195
198 } // namespace cast_channel 196 } // namespace cast_channel
199 } // namespace api
200 } // namespace extensions
OLDNEW
« no previous file with comments | « components/cast_channel/keep_alive_delegate.h ('k') | components/cast_channel/keep_alive_delegate_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698