1*3f982cf4SFabien Sanglard // Copyright 2018 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 #ifndef OSP_PUBLIC_PRESENTATION_PRESENTATION_RECEIVER_H_ 6*3f982cf4SFabien Sanglard #define OSP_PUBLIC_PRESENTATION_PRESENTATION_RECEIVER_H_ 7*3f982cf4SFabien Sanglard 8*3f982cf4SFabien Sanglard #include <map> 9*3f982cf4SFabien Sanglard #include <memory> 10*3f982cf4SFabien Sanglard #include <string> 11*3f982cf4SFabien Sanglard #include <vector> 12*3f982cf4SFabien Sanglard 13*3f982cf4SFabien Sanglard #include "osp/msgs/osp_messages.h" 14*3f982cf4SFabien Sanglard #include "osp/public/message_demuxer.h" 15*3f982cf4SFabien Sanglard #include "osp/public/presentation/presentation_connection.h" 16*3f982cf4SFabien Sanglard 17*3f982cf4SFabien Sanglard namespace openscreen { 18*3f982cf4SFabien Sanglard namespace osp { 19*3f982cf4SFabien Sanglard 20*3f982cf4SFabien Sanglard enum class ResponseResult { 21*3f982cf4SFabien Sanglard kSuccess = 0, 22*3f982cf4SFabien Sanglard kInvalidUrl, 23*3f982cf4SFabien Sanglard kRequestTimedOut, 24*3f982cf4SFabien Sanglard kRequestFailedTransient, 25*3f982cf4SFabien Sanglard kRequestFailedPermanent, 26*3f982cf4SFabien Sanglard kHttpError, 27*3f982cf4SFabien Sanglard kUnknown, 28*3f982cf4SFabien Sanglard }; 29*3f982cf4SFabien Sanglard 30*3f982cf4SFabien Sanglard class ReceiverDelegate { 31*3f982cf4SFabien Sanglard public: 32*3f982cf4SFabien Sanglard virtual ~ReceiverDelegate() = default; 33*3f982cf4SFabien Sanglard 34*3f982cf4SFabien Sanglard // Called when the availability (compatible, not compatible, or invalid) 35*3f982cf4SFabien Sanglard // for specific URLs is needed to be supplied by the delegate. 36*3f982cf4SFabien Sanglard // See "#presentation-protocol" spec section. 37*3f982cf4SFabien Sanglard // Returns a list of url availabilities. 38*3f982cf4SFabien Sanglard virtual std::vector<msgs::UrlAvailability> OnUrlAvailabilityRequest( 39*3f982cf4SFabien Sanglard uint64_t watch_id, 40*3f982cf4SFabien Sanglard uint64_t watch_duration, 41*3f982cf4SFabien Sanglard std::vector<std::string> urls) = 0; 42*3f982cf4SFabien Sanglard 43*3f982cf4SFabien Sanglard // Called when a new presentation is requested by a controller. This should 44*3f982cf4SFabien Sanglard // return true if the presentation was accepted, false otherwise. 45*3f982cf4SFabien Sanglard virtual bool StartPresentation( 46*3f982cf4SFabien Sanglard const Connection::PresentationInfo& info, 47*3f982cf4SFabien Sanglard uint64_t source_id, 48*3f982cf4SFabien Sanglard const std::vector<msgs::HttpHeader>& http_headers) = 0; 49*3f982cf4SFabien Sanglard 50*3f982cf4SFabien Sanglard // Called when the receiver wants to actually connection to the presentation. 51*3f982cf4SFabien Sanglard // Should return true if the connection was successful, false otherwise. 52*3f982cf4SFabien Sanglard virtual bool ConnectToPresentation(uint64_t request_id, 53*3f982cf4SFabien Sanglard const std::string& id, 54*3f982cf4SFabien Sanglard uint64_t source_id) = 0; 55*3f982cf4SFabien Sanglard 56*3f982cf4SFabien Sanglard // Called when a presentation is requested to be terminated by a controller. 57*3f982cf4SFabien Sanglard virtual void TerminatePresentation(const std::string& id, 58*3f982cf4SFabien Sanglard TerminationReason reason) = 0; 59*3f982cf4SFabien Sanglard }; 60*3f982cf4SFabien Sanglard 61*3f982cf4SFabien Sanglard class Receiver final : public MessageDemuxer::MessageCallback, 62*3f982cf4SFabien Sanglard public Connection::ParentDelegate { 63*3f982cf4SFabien Sanglard public: 64*3f982cf4SFabien Sanglard // TODO(crbug.com/openscreen/31): Remove singletons in the embedder API and 65*3f982cf4SFabien Sanglard // protocol implementation layers. 66*3f982cf4SFabien Sanglard static Receiver* Get(); 67*3f982cf4SFabien Sanglard void Init(); 68*3f982cf4SFabien Sanglard void Deinit(); 69*3f982cf4SFabien Sanglard 70*3f982cf4SFabien Sanglard // Sets the object to call when a new receiver connection is available. 71*3f982cf4SFabien Sanglard // |delegate| must either outlive PresentationReceiver or live until a new 72*3f982cf4SFabien Sanglard // delegate (possibly nullptr) is set. Setting the delegate to nullptr will 73*3f982cf4SFabien Sanglard // automatically ignore all future receiver requests. 74*3f982cf4SFabien Sanglard void SetReceiverDelegate(ReceiverDelegate* delegate); 75*3f982cf4SFabien Sanglard 76*3f982cf4SFabien Sanglard // Called by the embedder to report its response to StartPresentation. 77*3f982cf4SFabien Sanglard Error OnPresentationStarted(const std::string& presentation_id, 78*3f982cf4SFabien Sanglard Connection* connection, 79*3f982cf4SFabien Sanglard ResponseResult result); 80*3f982cf4SFabien Sanglard 81*3f982cf4SFabien Sanglard Error OnConnectionCreated(uint64_t request_id, 82*3f982cf4SFabien Sanglard Connection* connection, 83*3f982cf4SFabien Sanglard ResponseResult result); 84*3f982cf4SFabien Sanglard 85*3f982cf4SFabien Sanglard // Connection::ParentDelegate overrides. 86*3f982cf4SFabien Sanglard Error CloseConnection(Connection* connection, 87*3f982cf4SFabien Sanglard Connection::CloseReason reason) override; 88*3f982cf4SFabien Sanglard // Also called by the embedder to report that a presentation has been 89*3f982cf4SFabien Sanglard // terminated. 90*3f982cf4SFabien Sanglard Error OnPresentationTerminated(const std::string& presentation_id, 91*3f982cf4SFabien Sanglard TerminationReason reason) override; 92*3f982cf4SFabien Sanglard void OnConnectionDestroyed(Connection* connection) override; 93*3f982cf4SFabien Sanglard 94*3f982cf4SFabien Sanglard // MessageDemuxer::MessageCallback overrides. 95*3f982cf4SFabien Sanglard ErrorOr<size_t> OnStreamMessage(uint64_t endpoint_id, 96*3f982cf4SFabien Sanglard uint64_t connection_id, 97*3f982cf4SFabien Sanglard msgs::Type message_type, 98*3f982cf4SFabien Sanglard const uint8_t* buffer, 99*3f982cf4SFabien Sanglard size_t buffer_size, 100*3f982cf4SFabien Sanglard Clock::time_point now) override; 101*3f982cf4SFabien Sanglard 102*3f982cf4SFabien Sanglard private: 103*3f982cf4SFabien Sanglard struct QueuedResponse { 104*3f982cf4SFabien Sanglard enum class Type { kInitiation, kConnection }; 105*3f982cf4SFabien Sanglard 106*3f982cf4SFabien Sanglard Type type; 107*3f982cf4SFabien Sanglard uint64_t request_id; 108*3f982cf4SFabien Sanglard uint64_t connection_id; 109*3f982cf4SFabien Sanglard uint64_t endpoint_id; 110*3f982cf4SFabien Sanglard }; 111*3f982cf4SFabien Sanglard 112*3f982cf4SFabien Sanglard struct Presentation { 113*3f982cf4SFabien Sanglard uint64_t endpoint_id; 114*3f982cf4SFabien Sanglard MessageDemuxer::MessageWatch terminate_watch; 115*3f982cf4SFabien Sanglard uint64_t terminate_request_id; 116*3f982cf4SFabien Sanglard std::vector<Connection*> connections; 117*3f982cf4SFabien Sanglard }; 118*3f982cf4SFabien Sanglard 119*3f982cf4SFabien Sanglard Receiver(); 120*3f982cf4SFabien Sanglard ~Receiver() override; 121*3f982cf4SFabien Sanglard 122*3f982cf4SFabien Sanglard using QueuedResponseIterator = std::vector<QueuedResponse>::const_iterator; 123*3f982cf4SFabien Sanglard 124*3f982cf4SFabien Sanglard void DeleteQueuedResponse(const std::string& presentation_id, 125*3f982cf4SFabien Sanglard QueuedResponseIterator response); 126*3f982cf4SFabien Sanglard ErrorOr<QueuedResponseIterator> GetQueuedResponse( 127*3f982cf4SFabien Sanglard const std::string& presentation_id, 128*3f982cf4SFabien Sanglard uint64_t request_id) const; 129*3f982cf4SFabien Sanglard 130*3f982cf4SFabien Sanglard ReceiverDelegate* delegate_ = nullptr; 131*3f982cf4SFabien Sanglard 132*3f982cf4SFabien Sanglard // TODO(jophba): scope requests by endpoint, not presentation. This doesn't 133*3f982cf4SFabien Sanglard // work properly for multiple controllers. 134*3f982cf4SFabien Sanglard std::map<std::string, std::vector<QueuedResponse>> queued_responses_; 135*3f982cf4SFabien Sanglard 136*3f982cf4SFabien Sanglard // Presentations are added when the embedder starts the presentation, 137*3f982cf4SFabien Sanglard // and ended when a new receiver delegate is set or when 138*3f982cf4SFabien Sanglard // a presentation is called to be terminated (OnPresentationTerminated). 139*3f982cf4SFabien Sanglard std::map<std::string, Presentation> started_presentations_; 140*3f982cf4SFabien Sanglard 141*3f982cf4SFabien Sanglard std::unique_ptr<ConnectionManager> connection_manager_; 142*3f982cf4SFabien Sanglard 143*3f982cf4SFabien Sanglard MessageDemuxer::MessageWatch availability_watch_; 144*3f982cf4SFabien Sanglard MessageDemuxer::MessageWatch initiation_watch_; 145*3f982cf4SFabien Sanglard MessageDemuxer::MessageWatch connection_watch_; 146*3f982cf4SFabien Sanglard 147*3f982cf4SFabien Sanglard uint64_t GetNextConnectionId(); 148*3f982cf4SFabien Sanglard }; 149*3f982cf4SFabien Sanglard 150*3f982cf4SFabien Sanglard } // namespace osp 151*3f982cf4SFabien Sanglard } // namespace openscreen 152*3f982cf4SFabien Sanglard 153*3f982cf4SFabien Sanglard #endif // OSP_PUBLIC_PRESENTATION_PRESENTATION_RECEIVER_H_ 154