1 // Copyright 2015 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 EXTENSIONS_BROWSER_API_DISPLAY_SOURCE_DISPLAY_SOURCE_CONNECTION_DELEGATE_H_ 6 #define EXTENSIONS_BROWSER_API_DISPLAY_SOURCE_DISPLAY_SOURCE_CONNECTION_DELEGATE_H_ 7 8 #include "base/callback.h" 9 #include "base/observer_list.h" 10 #include "components/keyed_service/core/keyed_service.h" 11 #include "extensions/common/api/display_source.h" 12 #include "net/base/ip_address.h" 13 14 namespace extensions { 15 16 using DisplaySourceSinkInfo = api::display_source::SinkInfo; 17 using DisplaySourceSinkInfoList = std::vector<DisplaySourceSinkInfo>; 18 using DisplaySourceAuthInfo = api::display_source::AuthenticationInfo; 19 using DisplaySourceErrorType = api::display_source::ErrorType; 20 // The DisplaySourceConnectionDelegate interface should be implemented 21 // to provide sinks search and connection functionality for 22 // 'chrome.displaySource' API. 23 class DisplaySourceConnectionDelegate : public KeyedService { 24 public: 25 using AuthInfoCallback = base::Callback<void(const DisplaySourceAuthInfo&)>; 26 using StringCallback = base::Callback<void(const std::string&)>; 27 using SinkInfoListCallback = 28 base::Callback<void(const DisplaySourceSinkInfoList&)>; 29 30 const static int kInvalidSinkId = -1; 31 32 class Connection { 33 public: 34 // Returns the connected sink object. 35 virtual const DisplaySourceSinkInfo& GetConnectedSink() const = 0; 36 37 // Returns the local address of the source. 38 virtual net::IPAddress GetLocalAddress() const = 0; 39 40 // Returns the address of the connected sink. 41 virtual net::IPAddress GetSinkAddress() const = 0; 42 43 // Sends a control message to the connected sink. 44 // If an error occurs 'Observer::OnConnectionError' is invoked. 45 virtual void SendMessage(const std::string& message) = 0; 46 47 // Sets a callback to receive control messages from the connected sink. 48 // This method should only be called once in the lifetime of each 49 // Connection object. 50 // If an error occurs 'Observer::OnConnectionError' is invoked. 51 virtual void SetMessageReceivedCallback( 52 const StringCallback& callback) = 0; 53 54 protected: 55 Connection(); 56 virtual ~Connection(); 57 }; 58 59 class Observer { 60 public: 61 // This method is called each time the list of available 62 // sinks is updated whether after 'GetAvailableSinks' call 63 // or while the implementation is constantly watching the 64 // available sinks (after 'StartWatchingAvailableSinks' was called). 65 // Also this method is called to reflect current connection updates. 66 virtual void OnSinksUpdated(const DisplaySourceSinkInfoList& sinks) = 0; 67 68 // This method is called during the established connection to report 69 // a transport layer fatal error (which implies that the connection 70 // becomes broken/disconnected). 71 virtual void OnConnectionError(int sink_id, 72 DisplaySourceErrorType type, 73 const std::string& description) = 0; 74 75 protected: ~Observer()76 virtual ~Observer() {} 77 }; 78 79 DisplaySourceConnectionDelegate(); 80 ~DisplaySourceConnectionDelegate() override; 81 82 virtual void AddObserver(Observer* observer); 83 virtual void RemoveObserver(Observer* observer); 84 85 // Returns the list of last found available sinks 86 // this list may contain outdated data if the delegate 87 // is not watching the sinks (via 'StartWatchingSinks' 88 // method). The list is refreshed after 'GetAvailableSinks' 89 // call. 90 virtual const DisplaySourceSinkInfoList& last_found_sinks() const = 0; 91 92 // Returns the Connection object representing the current 93 // connection to the sink or NULL if there is no current connection. 94 virtual Connection* connection() = 0; 95 96 // Queries the list of currently available sinks. 97 virtual void GetAvailableSinks(const SinkInfoListCallback& sinks_callback, 98 const StringCallback& failure_callback) = 0; 99 100 // Queries the authentication method required by the sink for connection. 101 // If the used authentication method requires authentication data to be 102 // visible on the sink's display (e.g. PIN) the implementation should 103 // request the sink to show it. 104 virtual void RequestAuthentication( 105 int sink_id, 106 const AuthInfoCallback& auth_info_callback, 107 const StringCallback& failure_callback) = 0; 108 109 // Connects to a sink by given id and auth info. 110 virtual void Connect(int sink_id, 111 const DisplaySourceAuthInfo& auth_info, 112 const StringCallback& failure_callback) = 0; 113 114 // Disconnects the current connection to sink, the 'failure_callback' 115 // is called if an error has occurred or if there is no established 116 // connection. 117 virtual void Disconnect(const StringCallback& failure_callback) = 0; 118 119 // Implementation should start watching the available sinks updates. 120 virtual void StartWatchingAvailableSinks() = 0; 121 122 // Implementation should stop watching the available sinks updates. 123 virtual void StopWatchingAvailableSinks() = 0; 124 125 protected: 126 base::ObserverList<Observer>::Unchecked observers_; 127 }; 128 129 } // namespace extensions 130 131 #endif // EXTENSIONS_BROWSER_API_DISPLAY_SOURCE_DISPLAY_SOURCE_CONNECTION_DELEGATE_H_ 132