Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 media_router::IssueInfo::Action IssueActionFromMojo( | |
| 19 media_router::mojom::Issue::ActionType action_type) { | |
| 20 switch (action_type) { | |
| 21 case media_router::mojom::Issue::ActionType::DISMISS: | |
| 22 return media_router::IssueInfo::Action::DISMISS; | |
| 23 case media_router::mojom::Issue::ActionType::LEARN_MORE: | |
| 24 return media_router::IssueInfo::Action::LEARN_MORE; | |
| 25 default: | |
| 26 NOTREACHED() << "Unknown issue action type " << action_type; | |
| 27 return media_router::IssueInfo::Action::DISMISS; | |
|
mark a. foltz
2016/11/29 21:27:08
The Mojo enum reader should validate enum values.
imcheng
2016/12/05 23:18:22
IMO, it's good practice to always have default cas
| |
| 28 } | |
| 29 } | |
| 30 | |
| 31 media_router::mojom::Issue::ActionType IssueActionToMojo( | |
| 32 media_router::IssueInfo::Action action) { | |
| 33 switch (action) { | |
| 34 case media_router::IssueInfo::Action::DISMISS: | |
| 35 return media_router::mojom::Issue::ActionType::DISMISS; | |
| 36 case media_router::IssueInfo::Action::LEARN_MORE: | |
| 37 return media_router::mojom::Issue::ActionType::LEARN_MORE; | |
| 38 default: | |
| 39 NOTREACHED() << "Unknown issue action type " << static_cast<int>(action); | |
| 40 return media_router::mojom::Issue::ActionType::DISMISS; | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 media_router::IssueInfo::Severity IssueSeverityFromMojo( | |
| 45 media_router::mojom::Issue::Severity severity) { | |
| 46 switch (severity) { | |
| 47 case media_router::mojom::Issue::Severity::FATAL: | |
| 48 return media_router::IssueInfo::Severity::FATAL; | |
| 49 case media_router::mojom::Issue::Severity::WARNING: | |
| 50 return media_router::IssueInfo::Severity::WARNING; | |
| 51 case media_router::mojom::Issue::Severity::NOTIFICATION: | |
| 52 return media_router::IssueInfo::Severity::NOTIFICATION; | |
| 53 default: | |
| 54 NOTREACHED() << "Unknown issue severity " << severity; | |
| 55 return media_router::IssueInfo::Severity::WARNING; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 15 template <> | 61 template <> |
| 16 struct StructTraits<media_router::mojom::RouteMessageDataView, | 62 struct StructTraits<media_router::mojom::RouteMessageDataView, |
| 17 media_router::RouteMessage> { | 63 media_router::RouteMessage> { |
| 18 static media_router::mojom::RouteMessage::Type type( | 64 static media_router::mojom::RouteMessage::Type type( |
| 19 const media_router::RouteMessage& msg) { | 65 const media_router::RouteMessage& msg) { |
| 20 switch (msg.type) { | 66 switch (msg.type) { |
| 21 case media_router::RouteMessage::TEXT: | 67 case media_router::RouteMessage::TEXT: |
| 22 return media_router::mojom::RouteMessage::Type::TEXT; | 68 return media_router::mojom::RouteMessage::Type::TEXT; |
| 23 case media_router::RouteMessage::BINARY: | 69 case media_router::RouteMessage::BINARY: |
| 24 return media_router::mojom::RouteMessage::Type::BINARY; | 70 return media_router::mojom::RouteMessage::Type::BINARY; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 out->binary = std::move(binary); | 105 out->binary = std::move(binary); |
| 60 break; | 106 break; |
| 61 } | 107 } |
| 62 default: | 108 default: |
| 63 return false; | 109 return false; |
| 64 } | 110 } |
| 65 return true; | 111 return true; |
| 66 } | 112 } |
| 67 }; | 113 }; |
| 68 | 114 |
| 115 template <> | |
| 116 struct StructTraits<media_router::mojom::IssueDataView, | |
| 117 media_router::IssueInfo> { | |
| 118 static bool Read(media_router::mojom::IssueDataView data, | |
| 119 media_router::IssueInfo* out) { | |
| 120 if (!data.ReadTitle(&out->title)) | |
| 121 return false; | |
| 122 | |
| 123 media_router::mojom::Issue::ActionType default_action; | |
| 124 if (!data.ReadDefaultAction(&default_action)) | |
| 125 return false; | |
| 126 | |
| 127 out->default_action = IssueActionFromMojo(default_action); | |
|
mark a. foltz
2016/11/29 21:27:08
Are Mojo enums zero indexed? Can you just static
imcheng
2016/12/05 23:18:19
I think so. But I would prefer a safer approach th
mark a. foltz
2016/12/05 23:24:09
Mojo must provide some strong guarantees about its
imcheng
2016/12/09 23:28:41
dcheng@ - can you advise on whether we can do a st
| |
| 128 | |
| 129 media_router::mojom::Issue::Severity severity; | |
| 130 if (!data.ReadSeverity(&severity)) | |
| 131 return false; | |
| 132 | |
| 133 out->severity = IssueSeverityFromMojo(severity); | |
|
mark a. foltz
2016/11/29 21:27:07
Similar question as above.
imcheng
2016/12/05 23:18:20
Acknowledged.
| |
| 134 | |
| 135 base::Optional<std::string> message; | |
| 136 if (!data.ReadMessage(&message)) | |
| 137 return false; | |
| 138 | |
| 139 out->message = message.value_or(std::string()); | |
| 140 | |
| 141 base::Optional<std::vector<media_router::mojom::Issue::ActionType>> | |
| 142 secondary_actions; | |
| 143 if (!data.ReadSecondaryActions(&secondary_actions)) | |
| 144 return false; | |
| 145 | |
| 146 if (secondary_actions.has_value()) { | |
| 147 for (const auto action : secondary_actions.value()) | |
| 148 out->secondary_actions.push_back(IssueActionFromMojo(action)); | |
| 149 } | |
| 150 | |
| 151 base::Optional<std::string> route_id; | |
| 152 if (!data.ReadRouteId(&route_id)) | |
| 153 return false; | |
| 154 | |
| 155 out->route_id = route_id.value_or(std::string()); | |
| 156 | |
| 157 out->is_blocking = data.is_blocking(); | |
| 158 out->help_page_id = data.help_page_id(); | |
| 159 | |
| 160 return true; | |
| 161 } | |
| 162 | |
| 163 static base::Optional<std::string> route_id( | |
| 164 const media_router::IssueInfo& issue) { | |
| 165 return issue.route_id.empty() ? base::Optional<std::string>() | |
| 166 : base::make_optional(issue.route_id); | |
| 167 } | |
| 168 | |
| 169 static media_router::mojom::Issue::Severity severity( | |
| 170 const media_router::IssueInfo& issue) { | |
| 171 switch (issue.severity) { | |
| 172 case media_router::IssueInfo::Severity::FATAL: | |
|
mark a. foltz
2016/11/29 21:27:08
Can this be a static cast?
imcheng
2016/12/05 23:18:18
See above.
| |
| 173 return media_router::mojom::Issue::Severity::FATAL; | |
| 174 case media_router::IssueInfo::Severity::WARNING: | |
| 175 return media_router::mojom::Issue::Severity::WARNING; | |
| 176 case media_router::IssueInfo::Severity::NOTIFICATION: | |
| 177 return media_router::mojom::Issue::Severity::NOTIFICATION; | |
| 178 default: | |
| 179 NOTREACHED() << "Unknown issue severity " | |
| 180 << static_cast<int>(issue.severity); | |
| 181 return media_router::mojom::Issue::Severity::WARNING; | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 static bool is_blocking(const media_router::IssueInfo& issue) { | |
| 186 return issue.is_blocking; | |
| 187 } | |
| 188 | |
| 189 static std::string title(const media_router::IssueInfo& issue) { | |
| 190 return issue.title; | |
| 191 } | |
| 192 | |
| 193 static base::Optional<std::string> message( | |
| 194 const media_router::IssueInfo& issue) { | |
| 195 return issue.message.empty() ? base::Optional<std::string>() | |
| 196 : base::make_optional(issue.message); | |
| 197 } | |
| 198 | |
| 199 static media_router::mojom::Issue::ActionType default_action( | |
| 200 const media_router::IssueInfo& issue) { | |
| 201 return IssueActionToMojo(issue.default_action); | |
| 202 } | |
| 203 | |
| 204 static base::Optional<std::vector<media_router::mojom::Issue::ActionType>> | |
| 205 secondary_actions(const media_router::IssueInfo& issue) { | |
| 206 if (issue.secondary_actions.empty()) | |
| 207 return base::Optional< | |
| 208 std::vector<media_router::mojom::Issue::ActionType>>(); | |
| 209 | |
| 210 std::vector<media_router::mojom::Issue::ActionType> mojo_action_types; | |
| 211 for (const auto& action : issue.secondary_actions) | |
| 212 mojo_action_types.push_back(IssueActionToMojo(action)); | |
| 213 | |
| 214 return base::make_optional(std::move(mojo_action_types)); | |
| 215 } | |
| 216 | |
| 217 static int32_t help_page_id(const media_router::IssueInfo& issue) { | |
| 218 return issue.help_page_id; | |
| 219 } | |
| 220 }; | |
| 221 | |
| 69 } // namespace mojo | 222 } // namespace mojo |
| 70 | 223 |
| 71 #endif // CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTER_STRUCT_TRAITS_H_ | 224 #endif // CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTER_STRUCT_TRAITS_H_ |
| OLD | NEW |