Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 #include "chrome/browser/media/router/discovery/media_sink_internal.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/strings/string_util.h" | |
| 9 | |
| 10 namespace media_router { | |
| 11 | |
| 12 MediaSinkInternal::MediaSinkInternal(const MediaSink& sink) | |
| 13 : sink_(sink), sink_type_(SinkType::GENERIC) {} | |
| 14 | |
| 15 MediaSinkInternal::MediaSinkInternal(const MediaSink& sink, | |
| 16 const DialSinkExtraData& dial_data) | |
| 17 : sink_(sink), sink_type_(SinkType::DIAL) { | |
| 18 dial_data_.Init(dial_data); | |
| 19 } | |
| 20 | |
| 21 MediaSinkInternal::MediaSinkInternal(const MediaSink& sink, | |
| 22 const CastSinkExtraData& cast_data) | |
| 23 : sink_(sink), sink_type_(SinkType::CAST) { | |
| 24 cast_data_.Init(cast_data); | |
| 25 } | |
| 26 | |
| 27 MediaSinkInternal::MediaSinkInternal() : sink_type_(SinkType::GENERIC) {} | |
| 28 | |
| 29 MediaSinkInternal::MediaSinkInternal(const MediaSinkInternal& other) { | |
| 30 InternalCopyConstructFrom(other); | |
| 31 } | |
| 32 | |
| 33 MediaSinkInternal::~MediaSinkInternal() { | |
| 34 InternalCleanup(); | |
| 35 } | |
| 36 | |
| 37 MediaSinkInternal& MediaSinkInternal::operator=( | |
| 38 const MediaSinkInternal& other) { | |
| 39 if (this != &other) { | |
|
mark a. foltz
2017/03/13 17:47:24
The conversion to union seems to require all this
zhaobin
2017/03/14 01:23:23
dcheng@ suggests that .mojom should be consistent
| |
| 40 if (sink_type_ == other.sink_type_) { | |
| 41 InternalCopyAssignFrom(other); | |
| 42 } else { | |
| 43 InternalCleanup(); | |
| 44 InternalCopyConstructFrom(other); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 return *this; | |
| 49 } | |
| 50 | |
| 51 bool MediaSinkInternal::operator==(const MediaSinkInternal& other) const { | |
| 52 if (sink_type_ != other.sink_type_) | |
| 53 return false; | |
| 54 | |
| 55 if (!(sink_ == other.sink_)) | |
|
imcheng
2017/03/11 00:33:08
Can this just use MediaSink::Equals?
mark a. foltz
2017/03/13 17:47:24
Equals() methods are discouraged vs. operator==.
zhaobin
2017/03/14 01:23:23
Acknowledged.
zhaobin
2017/03/14 01:23:23
MediaSink::Equals only compares IDs. Here we want
| |
| 56 return false; | |
| 57 | |
| 58 switch (sink_type_) { | |
| 59 case SinkType::DIAL: | |
| 60 return *dial_data_ == *(other.dial_data_); | |
| 61 case SinkType::CAST: | |
| 62 return *cast_data_ == *(other.cast_data_); | |
| 63 case SinkType::GENERIC: | |
| 64 return true; | |
| 65 } | |
| 66 NOTREACHED(); | |
|
imcheng
2017/03/11 00:33:08
Hmm.. I thought you'd still have to have a return
zhaobin
2017/03/14 01:23:23
Done.
| |
| 67 } | |
| 68 | |
| 69 void MediaSinkInternal::set_sink(const MediaSink& sink) { | |
| 70 sink_ = sink; | |
| 71 } | |
| 72 | |
| 73 void MediaSinkInternal::set_dial_data(const DialSinkExtraData& dial_data) { | |
|
imcheng
2017/03/11 00:33:08
Is there a case set_*_data would be called more th
zhaobin
2017/03/14 01:23:23
I think it is valid to call set_dial_data() multip
| |
| 74 InternalCleanup(); | |
| 75 | |
| 76 sink_type_ = SinkType::DIAL; | |
| 77 dial_data_.Init(dial_data); | |
| 78 } | |
| 79 | |
| 80 const DialSinkExtraData& MediaSinkInternal::dial_data() const { | |
| 81 DCHECK(is_dial_sink()); | |
| 82 return *dial_data_; | |
| 83 } | |
| 84 | |
| 85 void MediaSinkInternal::set_cast_data(const CastSinkExtraData& cast_data) { | |
| 86 InternalCleanup(); | |
| 87 | |
| 88 sink_type_ = SinkType::CAST; | |
| 89 cast_data_.Init(cast_data); | |
| 90 } | |
| 91 | |
| 92 const CastSinkExtraData& MediaSinkInternal::cast_data() const { | |
| 93 DCHECK(is_cast_sink()); | |
| 94 return *cast_data_; | |
| 95 } | |
| 96 | |
| 97 // static | |
| 98 bool MediaSinkInternal::IsValidSinkId(const std::string& sink_id) { | |
| 99 if (sink_id.empty() || !base::IsStringASCII(sink_id)) { | |
| 100 DLOG(WARNING) << "Invalid [sink_id]: " << sink_id; | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 void MediaSinkInternal::InternalCopyAssignFrom(const MediaSinkInternal& other) { | |
| 108 sink_ = other.sink_; | |
| 109 sink_type_ = other.sink_type_; | |
| 110 | |
| 111 switch (sink_type_) { | |
| 112 case SinkType::DIAL: | |
| 113 *dial_data_ = *other.dial_data_; | |
| 114 return; | |
| 115 case SinkType::CAST: | |
| 116 *cast_data_ = *other.cast_data_; | |
| 117 return; | |
| 118 case SinkType::GENERIC: | |
| 119 return; | |
| 120 } | |
| 121 NOTREACHED(); | |
| 122 } | |
| 123 | |
| 124 void MediaSinkInternal::InternalCopyConstructFrom( | |
| 125 const MediaSinkInternal& other) { | |
| 126 sink_ = other.sink_; | |
| 127 sink_type_ = other.sink_type_; | |
| 128 | |
| 129 switch (sink_type_) { | |
| 130 case SinkType::DIAL: | |
| 131 dial_data_.Init(*other.dial_data_); | |
| 132 return; | |
| 133 case SinkType::CAST: | |
| 134 cast_data_.Init(*other.cast_data_); | |
| 135 return; | |
| 136 case SinkType::GENERIC: | |
| 137 return; | |
| 138 } | |
| 139 NOTREACHED(); | |
| 140 } | |
| 141 | |
| 142 void MediaSinkInternal::InternalCleanup() { | |
| 143 switch (sink_type_) { | |
| 144 case SinkType::DIAL: | |
| 145 dial_data_.Destroy(); | |
| 146 return; | |
| 147 case SinkType::CAST: | |
| 148 cast_data_.Destroy(); | |
| 149 return; | |
| 150 case SinkType::GENERIC: | |
| 151 return; | |
| 152 } | |
| 153 NOTREACHED(); | |
| 154 } | |
| 155 | |
| 156 DialSinkExtraData::DialSinkExtraData() = default; | |
| 157 DialSinkExtraData::DialSinkExtraData(const DialSinkExtraData& other) = default; | |
| 158 DialSinkExtraData::~DialSinkExtraData() = default; | |
| 159 | |
| 160 bool DialSinkExtraData::operator==(const DialSinkExtraData& other) const { | |
| 161 return ip_address == other.ip_address && model_name == other.model_name && | |
| 162 app_url == other.app_url; | |
| 163 } | |
| 164 | |
| 165 CastSinkExtraData::CastSinkExtraData() = default; | |
| 166 CastSinkExtraData::CastSinkExtraData(const CastSinkExtraData& other) = default; | |
| 167 CastSinkExtraData::~CastSinkExtraData() = default; | |
| 168 | |
| 169 bool CastSinkExtraData::operator==(const CastSinkExtraData& other) const { | |
| 170 return ip_address == other.ip_address && model_name == other.model_name && | |
| 171 capabilities == other.capabilities && | |
| 172 cast_channel_id == other.cast_channel_id; | |
| 173 } | |
| 174 | |
| 175 } // namespace media_router | |
| OLD | NEW |