1*3f982cf4SFabien Sanglard // Copyright 2020 The Chromium Authors. All rights reserved.
2*3f982cf4SFabien Sanglard // Use of this source code is governed by a BSD-style license that can be
3*3f982cf4SFabien Sanglard // found in the LICENSE file.
4*3f982cf4SFabien Sanglard
5*3f982cf4SFabien Sanglard #include "cast/receiver/channel/message_util.h"
6*3f982cf4SFabien Sanglard
7*3f982cf4SFabien Sanglard #include "util/json/json_serialization.h"
8*3f982cf4SFabien Sanglard #include "util/json/json_value.h"
9*3f982cf4SFabien Sanglard #include "util/osp_logging.h"
10*3f982cf4SFabien Sanglard
11*3f982cf4SFabien Sanglard namespace openscreen {
12*3f982cf4SFabien Sanglard namespace cast {
13*3f982cf4SFabien Sanglard
14*3f982cf4SFabien Sanglard using ::cast::channel::CastMessage;
15*3f982cf4SFabien Sanglard
16*3f982cf4SFabien Sanglard namespace {
17*3f982cf4SFabien Sanglard
CreateAppAvailabilityResponse(int request_id,const std::string & sender_id,const std::string & app_id,AppAvailabilityResult availability_result)18*3f982cf4SFabien Sanglard ErrorOr<CastMessage> CreateAppAvailabilityResponse(
19*3f982cf4SFabien Sanglard int request_id,
20*3f982cf4SFabien Sanglard const std::string& sender_id,
21*3f982cf4SFabien Sanglard const std::string& app_id,
22*3f982cf4SFabien Sanglard AppAvailabilityResult availability_result) {
23*3f982cf4SFabien Sanglard CastMessage availability_response;
24*3f982cf4SFabien Sanglard Json::Value dict(Json::ValueType::objectValue);
25*3f982cf4SFabien Sanglard dict[kMessageKeyRequestId] = request_id;
26*3f982cf4SFabien Sanglard Json::Value availability(Json::ValueType::objectValue);
27*3f982cf4SFabien Sanglard availability[app_id.c_str()] =
28*3f982cf4SFabien Sanglard availability_result == AppAvailabilityResult::kAvailable
29*3f982cf4SFabien Sanglard ? kMessageValueAppAvailable
30*3f982cf4SFabien Sanglard : kMessageValueAppUnavailable;
31*3f982cf4SFabien Sanglard dict[kMessageKeyAvailability] = std::move(availability);
32*3f982cf4SFabien Sanglard ErrorOr<std::string> serialized = json::Stringify(dict);
33*3f982cf4SFabien Sanglard if (!serialized) {
34*3f982cf4SFabien Sanglard return Error::Code::kJsonWriteError;
35*3f982cf4SFabien Sanglard }
36*3f982cf4SFabien Sanglard
37*3f982cf4SFabien Sanglard availability_response.set_source_id(kPlatformReceiverId);
38*3f982cf4SFabien Sanglard availability_response.set_destination_id(sender_id);
39*3f982cf4SFabien Sanglard availability_response.set_namespace_(kReceiverNamespace);
40*3f982cf4SFabien Sanglard availability_response.set_protocol_version(
41*3f982cf4SFabien Sanglard ::cast::channel::CastMessage_ProtocolVersion_CASTV2_1_0);
42*3f982cf4SFabien Sanglard availability_response.set_payload_utf8(std::move(serialized.value()));
43*3f982cf4SFabien Sanglard availability_response.set_payload_type(
44*3f982cf4SFabien Sanglard ::cast::channel::CastMessage_PayloadType_STRING);
45*3f982cf4SFabien Sanglard return availability_response;
46*3f982cf4SFabien Sanglard }
47*3f982cf4SFabien Sanglard
48*3f982cf4SFabien Sanglard } // namespace
49*3f982cf4SFabien Sanglard
CreateAppAvailableResponse(int request_id,const std::string & sender_id,const std::string & app_id)50*3f982cf4SFabien Sanglard ErrorOr<CastMessage> CreateAppAvailableResponse(int request_id,
51*3f982cf4SFabien Sanglard const std::string& sender_id,
52*3f982cf4SFabien Sanglard const std::string& app_id) {
53*3f982cf4SFabien Sanglard return CreateAppAvailabilityResponse(request_id, sender_id, app_id,
54*3f982cf4SFabien Sanglard AppAvailabilityResult::kAvailable);
55*3f982cf4SFabien Sanglard }
56*3f982cf4SFabien Sanglard
CreateAppUnavailableResponse(int request_id,const std::string & sender_id,const std::string & app_id)57*3f982cf4SFabien Sanglard ErrorOr<CastMessage> CreateAppUnavailableResponse(int request_id,
58*3f982cf4SFabien Sanglard const std::string& sender_id,
59*3f982cf4SFabien Sanglard const std::string& app_id) {
60*3f982cf4SFabien Sanglard return CreateAppAvailabilityResponse(request_id, sender_id, app_id,
61*3f982cf4SFabien Sanglard AppAvailabilityResult::kUnavailable);
62*3f982cf4SFabien Sanglard }
63*3f982cf4SFabien Sanglard
64*3f982cf4SFabien Sanglard } // namespace cast
65*3f982cf4SFabien Sanglard } // namespace openscreen
66