1 // Copyright 2020 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 #ifndef CAST_SENDER_CAST_APP_DISCOVERY_SERVICE_IMPL_H_ 6 #define CAST_SENDER_CAST_APP_DISCOVERY_SERVICE_IMPL_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "cast/common/public/service_info.h" 13 #include "cast/sender/cast_app_availability_tracker.h" 14 #include "cast/sender/cast_platform_client.h" 15 #include "cast/sender/public/cast_app_discovery_service.h" 16 #include "platform/api/time.h" 17 #include "util/weak_ptr.h" 18 19 namespace openscreen { 20 namespace cast { 21 22 // Keeps track of availability queries, receives receiver updates, and issues 23 // app availability requests based on these signals. 24 class CastAppDiscoveryServiceImpl : public CastAppDiscoveryService { 25 public: 26 // |platform_client| must outlive |this|. 27 CastAppDiscoveryServiceImpl(CastPlatformClient* platform_client, 28 ClockNowFunctionPtr clock); 29 ~CastAppDiscoveryServiceImpl() override; 30 31 // CastAppDiscoveryService implementation. 32 Subscription StartObservingAvailability( 33 const CastMediaSource& source, 34 AvailabilityCallback callback) override; 35 36 // Reissues app availability requests for currently registered (device_id, 37 // app_id) pairs whose status is kUnavailable or kUnknown. 38 void Refresh() override; 39 40 void AddOrUpdateReceiver(const ServiceInfo& receiver); 41 void RemoveReceiver(const ServiceInfo& receiver); 42 43 private: 44 struct AvailabilityCallbackEntry { 45 uint32_t id; 46 AvailabilityCallback callback; 47 }; 48 49 // Issues an app availability request for |app_id| to the receiver given by 50 // |device_id|. 51 void RequestAppAvailability(const std::string& device_id, 52 const std::string& app_id); 53 54 // Updates the availability result for |device_id| and |app_id| with |result|, 55 // and notifies callbacks with updated availability query results. 56 void UpdateAppAvailability(const std::string& device_id, 57 const std::string& app_id, 58 AppAvailabilityResult result); 59 60 // Updates the availability query results for |sources|. 61 void UpdateAvailabilityQueries(const std::vector<CastMediaSource>& sources); 62 63 std::vector<ServiceInfo> GetReceiversByIds( 64 const std::vector<std::string>& device_ids) const; 65 66 // Returns true if an app availability request should be issued for 67 // |device_id| and |app_id|. |now| is used for checking whether previously 68 // cached results should be refreshed. 69 bool ShouldRefreshAppAvailability(const std::string& device_id, 70 const std::string& app_id, 71 Clock::time_point now) const; 72 73 void RemoveAvailabilityCallback(uint32_t id) override; 74 75 std::map<std::string, ServiceInfo> receivers_by_id_; 76 77 // Registered availability queries and their associated callbacks keyed by 78 // media source IDs. 79 std::map<std::string, std::vector<AvailabilityCallbackEntry>> avail_queries_; 80 81 // Callback ID tracking. 82 uint32_t next_avail_query_id_ = 1U; 83 84 CastPlatformClient* const platform_client_; 85 86 CastAppAvailabilityTracker availability_tracker_; 87 88 const ClockNowFunctionPtr clock_; 89 90 WeakPtrFactory<CastAppDiscoveryServiceImpl> weak_factory_; 91 }; 92 93 } // namespace cast 94 } // namespace openscreen 95 96 #endif // CAST_SENDER_CAST_APP_DISCOVERY_SERVICE_IMPL_H_ 97