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 const DialSinkExtraData& dial_data) | |
14 : sink_(sink), sink_type_(SinkType::DIAL) { | |
15 dial_data_.Init(dial_data); | |
16 } | |
17 | |
18 MediaSinkInternal::MediaSinkInternal(const MediaSink& sink, | |
19 const CastSinkExtraData& cast_data) | |
20 : sink_(sink), sink_type_(SinkType::CAST) { | |
21 cast_data_.Init(cast_data); | |
22 } | |
23 | |
24 MediaSinkInternal::MediaSinkInternal() : sink_type_(SinkType::GENERIC) {} | |
25 | |
26 MediaSinkInternal::MediaSinkInternal(const MediaSinkInternal& other) { | |
27 InternalCopyConstructFrom(other); | |
28 } | |
29 | |
30 MediaSinkInternal::~MediaSinkInternal() { | |
31 InternalCleanup(); | |
32 } | |
33 | |
34 MediaSinkInternal& MediaSinkInternal::operator=( | |
35 const MediaSinkInternal& other) { | |
36 if (this != &other) { | |
37 if (sink_type_ == other.sink_type_) { | |
38 InternalCopyAssignFrom(other); | |
39 } else { | |
40 InternalCleanup(); | |
41 InternalCopyConstructFrom(other); | |
42 } | |
43 } | |
44 | |
45 return *this; | |
46 } | |
47 | |
48 bool MediaSinkInternal::operator==(const MediaSinkInternal& other) const { | |
49 if (sink_type_ != other.sink_type_) | |
50 return false; | |
51 | |
52 if (sink_ != other.sink_) | |
53 return false; | |
54 | |
55 switch (sink_type_) { | |
56 case SinkType::DIAL: | |
57 return *dial_data_ == *(other.dial_data_); | |
58 case SinkType::CAST: | |
59 return *cast_data_ == *(other.cast_data_); | |
60 case SinkType::GENERIC: | |
61 return true; | |
62 } | |
63 NOTREACHED(); | |
64 return false; | |
65 } | |
66 | |
67 void MediaSinkInternal::set_sink(const MediaSink& sink) { | |
68 sink_ = sink; | |
69 } | |
70 | |
71 void MediaSinkInternal::set_dial_data(const DialSinkExtraData& dial_data) { | |
72 DCHECK(sink_type_ != SinkType::CAST); | |
73 InternalCleanup(); | |
74 | |
75 sink_type_ = SinkType::DIAL; | |
76 dial_data_.Init(dial_data); | |
77 } | |
78 | |
79 const DialSinkExtraData& MediaSinkInternal::dial_data() const { | |
80 DCHECK(is_dial_sink()); | |
81 return *dial_data_; | |
82 } | |
83 | |
84 void MediaSinkInternal::set_cast_data(const CastSinkExtraData& cast_data) { | |
85 DCHECK(sink_type_ != SinkType::DIAL); | |
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 |