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

Side by Side Diff: extensions/browser/api/cast_channel/keep_alive_delegate.cc

Issue 2609503002: Fix cast_channel::KeepAliveDelegate DCHECK failure. (Closed)
Patch Set: We must go deeper. Created 3 years, 11 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 "extensions/browser/api/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_message_util.h"
13 #include "extensions/browser/api/cast_channel/cast_socket.h" 12 #include "extensions/browser/api/cast_channel/cast_socket.h"
14 #include "extensions/browser/api/cast_channel/logger.h" 13 #include "extensions/browser/api/cast_channel/logger.h"
15 #include "extensions/common/api/cast_channel/cast_channel.pb.h" 14 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
16 #include "extensions/common/api/cast_channel/logging.pb.h" 15 #include "extensions/common/api/cast_channel/logging.pb.h"
17 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
18 17
19 namespace extensions { 18 namespace extensions {
20 namespace api { 19 namespace api {
21 namespace cast_channel { 20 namespace cast_channel {
22 namespace { 21 namespace {
23 22
24 const char kHeartbeatNamespace[] = "urn:x-cast:com.google.cast.tp.heartbeat"; 23 const char kHeartbeatNamespace[] = "urn:x-cast:com.google.cast.tp.heartbeat";
25 const char kPingSenderId[] = "chrome"; 24 const char kPingSenderId[] = "chrome";
26 const char kPingReceiverId[] = "receiver-0"; 25 const char kPingReceiverId[] = "receiver-0";
27 const char kTypeNodeId[] = "type"; 26 const char kTypeNodeId[] = "type";
28 27
29 // Determines if the JSON-encoded payload is equivalent to 28 // Parses the JSON-encoded payload of |message| and returns the value in the
30 // { "type": |chk_type| } 29 // "type" field or the empty string if the parse fails or the field is not
31 bool NestedPayloadTypeEquals(const std::string& chk_type, 30 // found.
Wez 2017/01/13 22:37:11 Sorry, I completely missed that this was parsing J
miu 2017/01/15 02:17:08 I'm not sure. It was already doing that. I'll ment
32 const CastMessage& message) { 31 std::string ParseForPayloadType(const CastMessage& message) {
33 MessageInfo message_info; 32 std::unique_ptr<base::Value> parsed_payload(
34 CastMessageToMessageInfo(message, &message_info); 33 base::JSONReader::Read(message.payload_utf8()));
35 std::string type_json; 34 base::DictionaryValue* payload_as_dict;
36 if (!message_info.data->GetAsString(&type_json)) { 35 if (!parsed_payload || !parsed_payload->GetAsDictionary(&payload_as_dict))
37 return false; 36 return std::string();
38 }
39 std::unique_ptr<base::Value> type_value(base::JSONReader::Read(type_json));
40 if (!type_value.get()) {
41 return false;
42 }
43
44 base::DictionaryValue* type_dict;
45 if (!type_value->GetAsDictionary(&type_dict)) {
46 return false;
47 }
48
49 std::string type_string; 37 std::string type_string;
50 return (type_dict->HasKey(kTypeNodeId) && 38 if (!payload_as_dict->GetString(kTypeNodeId, &type_string))
51 type_dict->GetString(kTypeNodeId, &type_string) && 39 return std::string();
52 type_string == chk_type); 40 return type_string;
53 } 41 }
54 42
55 } // namespace 43 } // namespace
56 44
57 // static 45 // static
58 const char KeepAliveDelegate::kHeartbeatPingType[] = "PING"; 46 const char KeepAliveDelegate::kHeartbeatPingType[] = "PING";
59 47
60 // static 48 // static
61 const char KeepAliveDelegate::kHeartbeatPongType[] = "PONG"; 49 const char KeepAliveDelegate::kHeartbeatPongType[] = "PONG";
62 50
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 157
170 // CastTransport::Delegate interface. 158 // CastTransport::Delegate interface.
171 void KeepAliveDelegate::OnError(ChannelError error_state) { 159 void KeepAliveDelegate::OnError(ChannelError error_state) {
172 DCHECK(thread_checker_.CalledOnValidThread()); 160 DCHECK(thread_checker_.CalledOnValidThread());
173 VLOG(1) << "KeepAlive::OnError: " << error_state; 161 VLOG(1) << "KeepAlive::OnError: " << error_state;
174 inner_delegate_->OnError(error_state); 162 inner_delegate_->OnError(error_state);
175 Stop(); 163 Stop();
176 } 164 }
177 165
178 void KeepAliveDelegate::OnMessage(const CastMessage& message) { 166 void KeepAliveDelegate::OnMessage(const CastMessage& message) {
179 DCHECK(started_);
180 DCHECK(thread_checker_.CalledOnValidThread()); 167 DCHECK(thread_checker_.CalledOnValidThread());
181 VLOG(2) << "KeepAlive::OnMessage : " << message.payload_utf8(); 168 VLOG(2) << "KeepAlive::OnMessage : " << message.payload_utf8();
182 169
183 ResetTimers(); 170 if (started_)
171 ResetTimers();
184 172
185 if (NestedPayloadTypeEquals(kHeartbeatPingType, message)) { 173 // PING and PONG messages are intercepted and handled by KeepAliveDelegate
174 // here. All other messages are passed through to |inner_delegate_|.
175 const std::string payload_type = ParseForPayloadType(message);
176 if (payload_type == kHeartbeatPingType) {
186 VLOG(2) << "Received PING."; 177 VLOG(2) << "Received PING.";
187 SendKeepAliveMessage(pong_message_, kHeartbeatPongType); 178 if (started_)
188 } else if (NestedPayloadTypeEquals(kHeartbeatPongType, message)) { 179 SendKeepAliveMessage(pong_message_, kHeartbeatPongType);
180 return;
181 } else if (payload_type == kHeartbeatPongType) {
189 VLOG(2) << "Received PONG."; 182 VLOG(2) << "Received PONG.";
183 return;
Wez 2017/01/13 22:37:11 nit: Either remove the return's here, since they'r
miu 2017/01/15 02:17:08 Done. That was an oops on my part (left-over from
190 } else { 184 } else {
191 // PING and PONG messages are intentionally suppressed from layers above.
192 inner_delegate_->OnMessage(message); 185 inner_delegate_->OnMessage(message);
193 } 186 }
194 } 187 }
195 188
196 void KeepAliveDelegate::Stop() { 189 void KeepAliveDelegate::Stop() {
197 if (started_) { 190 if (started_) {
198 started_ = false; 191 started_ = false;
199 ping_timer_->Stop(); 192 ping_timer_->Stop();
200 liveness_timer_->Stop(); 193 liveness_timer_->Stop();
201 } 194 }
202 } 195 }
203 196
204 } // namespace cast_channel 197 } // namespace cast_channel
205 } // namespace api 198 } // namespace api
206 } // namespace extensions 199 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698