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

Side by Side Diff: chrome/browser/media/router/mojo/media_router_struct_traits.h

Issue 2176613003: [Media Router] Clean up issues related code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: IssueObserver init behavior and use StructTraits Created 4 years, 2 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #ifndef CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTER_STRUCT_TRAITS_H_ 5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTER_STRUCT_TRAITS_H_
6 #define CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTER_STRUCT_TRAITS_H_ 6 #define CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTER_STRUCT_TRAITS_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "chrome/browser/media/router/issue.h"
10 #include "chrome/browser/media/router/mojo/media_router.mojom.h" 11 #include "chrome/browser/media/router/mojo/media_router.mojom.h"
11 #include "mojo/common/common_custom_types_struct_traits.h" 12 #include "mojo/common/common_custom_types_struct_traits.h"
12 13
13 namespace mojo { 14 namespace mojo {
14 15
16 namespace {
17
18 bool IsValidIssueAction(media_router::mojom::Issue::ActionType action_type) {
19 switch (action_type) {
20 case media_router::mojom::Issue::ActionType::DISMISS:
21 case media_router::mojom::Issue::ActionType::LEARN_MORE:
22 return true;
23 default:
24 return false;
mark a. foltz 2016/10/18 21:13:44 Would this only happen if we forgot to update the
imcheng 2016/11/18 23:45:13 Yeah, also media_router_bindings.js converts unkno
25 }
26 }
27
28 media_router::IssueInfo::Action IssueActionFromMojo(
29 media_router::mojom::Issue::ActionType action_type) {
30 switch (action_type) {
31 case media_router::mojom::Issue::ActionType::DISMISS:
32 return media_router::IssueInfo::Action::DISMISS;
33 case media_router::mojom::Issue::ActionType::LEARN_MORE:
34 return media_router::IssueInfo::Action::LEARN_MORE;
35 default:
36 NOTREACHED() << "Unknown issue action type " << action_type;
37 return media_router::IssueInfo::Action::DISMISS;
38 }
39 }
40
41 media_router::mojom::Issue::ActionType IssueActionToMojo(
42 media_router::IssueInfo::Action action) {
43 switch (action) {
44 case media_router::IssueInfo::Action::DISMISS:
45 return media_router::mojom::Issue::ActionType::DISMISS;
46 case media_router::IssueInfo::Action::LEARN_MORE:
47 return media_router::mojom::Issue::ActionType::LEARN_MORE;
48 default:
49 NOTREACHED() << "Unknown issue action type " << static_cast<int>(action);
50 return media_router::mojom::Issue::ActionType::DISMISS;
51 }
52 }
53
54 bool IsValidIssueSeverity(media_router::mojom::Issue::Severity severity) {
55 switch (severity) {
56 case media_router::mojom::Issue::Severity::FATAL:
57 case media_router::mojom::Issue::Severity::WARNING:
58 case media_router::mojom::Issue::Severity::NOTIFICATION:
59 return true;
60 default:
61 return false;
62 }
63 }
64
65 media_router::IssueInfo::Severity IssueSeverityFromMojo(
66 media_router::mojom::Issue::Severity severity) {
67 switch (severity) {
68 case media_router::mojom::Issue::Severity::FATAL:
69 return media_router::IssueInfo::Severity::FATAL;
70 case media_router::mojom::Issue::Severity::WARNING:
71 return media_router::IssueInfo::Severity::WARNING;
72 case media_router::mojom::Issue::Severity::NOTIFICATION:
73 return media_router::IssueInfo::Severity::NOTIFICATION;
74 default:
75 NOTREACHED() << "Unknown issue severity " << severity;
76 return media_router::IssueInfo::Severity::WARNING;
77 }
78 }
79
80 } // namespace
81
15 template <> 82 template <>
16 struct StructTraits<media_router::mojom::RouteMessageDataView, 83 struct StructTraits<media_router::mojom::RouteMessageDataView,
17 media_router::RouteMessage> { 84 media_router::RouteMessage> {
18 static media_router::mojom::RouteMessage::Type type( 85 static media_router::mojom::RouteMessage::Type type(
19 const media_router::RouteMessage& msg) { 86 const media_router::RouteMessage& msg) {
20 switch (msg.type) { 87 switch (msg.type) {
21 case media_router::RouteMessage::TEXT: 88 case media_router::RouteMessage::TEXT:
22 return media_router::mojom::RouteMessage::Type::TEXT; 89 return media_router::mojom::RouteMessage::Type::TEXT;
23 case media_router::RouteMessage::BINARY: 90 case media_router::RouteMessage::BINARY:
24 return media_router::mojom::RouteMessage::Type::BINARY; 91 return media_router::mojom::RouteMessage::Type::BINARY;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 out->binary = std::move(binary); 126 out->binary = std::move(binary);
60 break; 127 break;
61 } 128 }
62 default: 129 default:
63 return false; 130 return false;
64 } 131 }
65 return true; 132 return true;
66 } 133 }
67 }; 134 };
68 135
136 template <>
137 struct StructTraits<media_router::mojom::IssueDataView,
138 media_router::IssueInfo> {
139 static bool Read(media_router::mojom::IssueDataView data,
140 media_router::IssueInfo* out) {
141 if (!data.ReadTitle(&out->title))
142 return false;
143
144 media_router::mojom::Issue::ActionType default_action;
145 if (!data.ReadDefaultAction(&default_action) ||
146 !IsValidIssueAction(default_action))
147 return false;
148
149 out->default_action = IssueActionFromMojo(default_action);
150
151 media_router::mojom::Issue::Severity severity;
152 if (!data.ReadSeverity(&severity) || !IsValidIssueSeverity(severity))
153 return false;
154
155 out->severity = IssueSeverityFromMojo(severity);
156
157 base::Optional<std::string> message;
158 if (!data.ReadMessage(&message))
159 return false;
160
161 out->message = message.value_or(std::string());
162
163 base::Optional<std::vector<media_router::mojom::Issue::ActionType>>
164 secondary_actions;
165 if (!data.ReadSecondaryActions(&secondary_actions))
166 return false;
167
168 if (secondary_actions.has_value()) {
169 for (const auto action : secondary_actions.value())
170 out->secondary_actions.push_back(IssueActionFromMojo(action));
171 }
172
173 base::Optional<std::string> route_id;
174 if (!data.ReadRouteId(&route_id))
175 return false;
176
177 out->route_id = route_id.value_or(std::string());
178
179 out->is_blocking = data.is_blocking();
180 out->help_page_id = data.help_page_id();
181
182 return true;
183 }
184
185 static base::Optional<std::string> route_id(
186 const media_router::IssueInfo& issue) {
187 return issue.route_id.empty() ? base::Optional<std::string>()
188 : base::make_optional(issue.route_id);
189 }
190
191 static media_router::mojom::Issue::Severity severity(
192 const media_router::IssueInfo& issue) {
193 switch (issue.severity) {
194 case media_router::IssueInfo::Severity::FATAL:
195 return media_router::mojom::Issue::Severity::FATAL;
196 case media_router::IssueInfo::Severity::WARNING:
197 return media_router::mojom::Issue::Severity::WARNING;
198 case media_router::IssueInfo::Severity::NOTIFICATION:
199 return media_router::mojom::Issue::Severity::NOTIFICATION;
200 default:
201 NOTREACHED() << "Unknown issue severity "
202 << static_cast<int>(issue.severity);
203 return media_router::mojom::Issue::Severity::WARNING;
204 }
205 }
206
207 static bool is_blocking(const media_router::IssueInfo& issue) {
208 return issue.is_blocking;
209 }
210
211 static std::string title(const media_router::IssueInfo& issue) {
212 return issue.title;
213 }
214
215 static base::Optional<std::string> message(
216 const media_router::IssueInfo& issue) {
217 return issue.message.empty() ? base::Optional<std::string>()
218 : base::make_optional(issue.message);
219 }
220
221 static media_router::mojom::Issue::ActionType default_action(
222 const media_router::IssueInfo& issue) {
223 return IssueActionToMojo(issue.default_action);
224 }
225
226 static base::Optional<std::vector<media_router::mojom::Issue::ActionType>>
227 secondary_actions(const media_router::IssueInfo& issue) {
228 if (issue.secondary_actions.empty())
229 return base::Optional<
230 std::vector<media_router::mojom::Issue::ActionType>>();
231
232 std::vector<media_router::mojom::Issue::ActionType> mojo_action_types;
233 for (const auto& action : issue.secondary_actions)
234 mojo_action_types.push_back(IssueActionToMojo(action));
235
236 return base::make_optional(std::move(mojo_action_types));
237 }
238
239 static int32_t help_page_id(const media_router::IssueInfo& issue) {
240 return issue.help_page_id;
241 }
242 };
243
69 } // namespace mojo 244 } // namespace mojo
70 245
71 #endif // CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTER_STRUCT_TRAITS_H_ 246 #endif // CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTER_STRUCT_TRAITS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698