summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api
diff options
context:
space:
mode:
authormikhail.pozdnyakov <mikhail.pozdnyakov@intel.com>2016-01-21 02:27:43 -0800
committerCommit bot <commit-bot@chromium.org>2016-01-21 10:28:37 +0000
commit8f13e03e223400dc7e7807470eb16f3c1e9bc4da (patch)
treed3d3f628475b7b8264458a1dbc774cfdc92bb882 /extensions/browser/api
parent3560eab7b023bd3e0e282d8509cf2d75d0d89264 (diff)
downloadchromium_src-8f13e03e223400dc7e7807470eb16f3c1e9bc4da.zip
chromium_src-8f13e03e223400dc7e7807470eb16f3c1e9bc4da.tar.gz
chromium_src-8f13e03e223400dc7e7807470eb16f3c1e9bc4da.tar.bz2
[chrome.displaySource] DisplaySourceConnectionDelegate::Connection interface
Extend DisplaySourceConnectionDelegate::Connection interface to support control message exchange with the connected sink. Also connection error notification method is added. BUG=242107 Review URL: https://codereview.chromium.org/1603013002 Cr-Commit-Position: refs/heads/master@{#370661}
Diffstat (limited to 'extensions/browser/api')
-rw-r--r--extensions/browser/api/display_source/display_source_apitest.cc8
-rw-r--r--extensions/browser/api/display_source/display_source_connection_delegate.h49
-rw-r--r--extensions/browser/api/display_source/display_source_event_router.h6
-rw-r--r--extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.cc43
-rw-r--r--extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.h12
5 files changed, 95 insertions, 23 deletions
diff --git a/extensions/browser/api/display_source/display_source_apitest.cc b/extensions/browser/api/display_source/display_source_apitest.cc
index 6a949a5..dc7e9ef 100644
--- a/extensions/browser/api/display_source/display_source_apitest.cc
+++ b/extensions/browser/api/display_source/display_source_apitest.cc
@@ -36,14 +36,14 @@ class MockDisplaySourceConnectionDelegate
DisplaySourceSinkInfoList last_found_sinks() const override { return sinks_; }
const Connection* connection() const override { return nullptr; }
void GetAvailableSinks(const SinkInfoListCallback& sinks_callback,
- const FailureCallback& failure_callback) override {
+ const StringCallback& failure_callback) override {
AddSink(CreateSinkInfoPtr(1, "sink 1", SINK_STATE_DISCONNECTED));
sinks_callback.Run(sinks_);
}
void RequestAuthentication(int sink_id,
const AuthInfoCallback& auth_info_callback,
- const FailureCallback& failure_callback) override {
+ const StringCallback& failure_callback) override {
DisplaySourceAuthInfo info;
info.method = AUTHENTICATION_METHOD_PBC;
auth_info_callback.Run(info);
@@ -51,9 +51,9 @@ class MockDisplaySourceConnectionDelegate
void Connect(int sink_id,
const DisplaySourceAuthInfo& auth_info,
- const FailureCallback& failure_callback) override {}
+ const StringCallback& failure_callback) override {}
- void Disconnect(const FailureCallback& failure_callback) override {}
+ void Disconnect(const StringCallback& failure_callback) override {}
void StartWatchingAvailableSinks() override {
AddSink(CreateSinkInfoPtr(2, "sink 2", SINK_STATE_DISCONNECTED));
diff --git a/extensions/browser/api/display_source/display_source_connection_delegate.h b/extensions/browser/api/display_source/display_source_connection_delegate.h
index dbd4553..1133cdf 100644
--- a/extensions/browser/api/display_source/display_source_connection_delegate.h
+++ b/extensions/browser/api/display_source/display_source_connection_delegate.h
@@ -15,25 +15,45 @@ namespace extensions {
using DisplaySourceSinkInfoPtr = linked_ptr<api::display_source::SinkInfo>;
using DisplaySourceSinkInfoList = std::vector<DisplaySourceSinkInfoPtr>;
using DisplaySourceAuthInfo = api::display_source::AuthenticationInfo;
-
+using DisplaySourceErrorType = api::display_source::ErrorType;
// The DisplaySourceConnectionDelegate interface should be implemented
// to provide sinks search and connection functionality for
// 'chrome.displaySource' API.
class DisplaySourceConnectionDelegate : public KeyedService {
public:
using AuthInfoCallback = base::Callback<void(const DisplaySourceAuthInfo&)>;
- using FailureCallback = base::Callback<void(const std::string&)>;
+ using StringCallback = base::Callback<void(const std::string&)>;
using SinkInfoListCallback =
base::Callback<void(const DisplaySourceSinkInfoList&)>;
const static int kInvalidSinkId = -1;
- struct Connection {
+ class Connection {
+ public:
+ // Returns a pointer to the connected sink object. The result is
+ // guaranteed not to be NULL.
+ virtual DisplaySourceSinkInfoPtr GetConnectedSink() const = 0;
+
+ // Returns the local address of the source.
+ virtual std::string GetLocalAddress() const = 0;
+
+ // Returns the address of the connected sink.
+ virtual std::string GetSinkAddress() const = 0;
+
+ // Sends a control message to the connected sink.
+ // If an error occurs 'Observer::OnConnectionError' is invoked.
+ virtual void SendMessage(const std::string& message) const = 0;
+
+ // Sets a callback to receive control messages from the connected sink.
+ // This method should only be called once in the lifetime of each
+ // Connection object.
+ // If an error occurs 'Observer::OnConnectionError' is invoked.
+ virtual void SetMessageReceivedCallback(
+ const StringCallback& callback) const = 0;
+
+ protected:
Connection();
- ~Connection();
- DisplaySourceSinkInfoPtr connected_sink;
- std::string local_ip;
- std::string sink_ip;
+ virtual ~Connection();
};
class Observer {
@@ -45,6 +65,13 @@ class DisplaySourceConnectionDelegate : public KeyedService {
// Also this method is called to reflect current connection updates.
virtual void OnSinksUpdated(const DisplaySourceSinkInfoList& sinks) = 0;
+ // This method is called during the established connection to report
+ // a transport layer fatal error (which implies that the connection
+ // becomes broken/disconnected).
+ virtual void OnConnectionError(int sink_id,
+ DisplaySourceErrorType type,
+ const std::string& description) = 0;
+
protected:
virtual ~Observer() {}
};
@@ -68,7 +95,7 @@ class DisplaySourceConnectionDelegate : public KeyedService {
// Queries the list of currently available sinks.
virtual void GetAvailableSinks(const SinkInfoListCallback& sinks_callback,
- const FailureCallback& failure_callback) = 0;
+ const StringCallback& failure_callback) = 0;
// Queries the authentication method required by the sink for connection.
// If the used authentication method requires authentication data to be
@@ -77,17 +104,17 @@ class DisplaySourceConnectionDelegate : public KeyedService {
virtual void RequestAuthentication(
int sink_id,
const AuthInfoCallback& auth_info_callback,
- const FailureCallback& failure_callback) = 0;
+ const StringCallback& failure_callback) = 0;
// Connects to a sink by given id and auth info.
virtual void Connect(int sink_id,
const DisplaySourceAuthInfo& auth_info,
- const FailureCallback& failure_callback) = 0;
+ const StringCallback& failure_callback) = 0;
// Disconnects the current connection to sink, the 'failure_callback'
// is called if an error has occurred or if there is no established
// connection.
- virtual void Disconnect(const FailureCallback& failure_callback) = 0;
+ virtual void Disconnect(const StringCallback& failure_callback) = 0;
// Implementation should start watching the available sinks updates.
virtual void StartWatchingAvailableSinks() = 0;
diff --git a/extensions/browser/api/display_source/display_source_event_router.h b/extensions/browser/api/display_source/display_source_event_router.h
index 6697fe8..97e2e6f 100644
--- a/extensions/browser/api/display_source/display_source_event_router.h
+++ b/extensions/browser/api/display_source/display_source_event_router.h
@@ -17,6 +17,9 @@ class BrowserContext;
namespace extensions {
// Observe listeners to 'onSinksUpdated' events.
+// This class initiates/stops listening for the available sinks
+// by the DisplaySourceConnectionDelegate, depending on whether
+// there are existing listeners to 'onSinksUpdated' event.
class DisplaySourceEventRouter
: public KeyedService,
public EventRouter::Observer,
@@ -39,6 +42,9 @@ class DisplaySourceEventRouter
// DisplaySourceConnectionDelegate::Observer overrides:
void OnSinksUpdated(const DisplaySourceSinkInfoList& sinks) override;
+ void OnConnectionError(int sink_id,
+ DisplaySourceErrorType type,
+ const std::string& description) override {}
private:
void StartOrStopListeningForSinksChanges();
diff --git a/extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.cc b/extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.cc
index ae3713d..59dfda1 100644
--- a/extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.cc
+++ b/extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.cc
@@ -30,7 +30,7 @@ WiFiDisplaySessionServiceImpl::WiFiDisplaySessionServiceImpl(
auto connection = delegate_->connection();
if (connection)
- last_connected_sink_ = connection->connected_sink->id;
+ last_connected_sink_ = connection->GetConnectedSink()->id;
}
WiFiDisplaySessionServiceImpl::~WiFiDisplaySessionServiceImpl() {
@@ -100,13 +100,29 @@ void WiFiDisplaySessionServiceImpl::Disconnect() {
return;
}
DCHECK(delegate_->connection());
- DCHECK_EQ(own_sink_, delegate_->connection()->connected_sink->id);
+ DCHECK_EQ(own_sink_, delegate_->connection()->GetConnectedSink()->id);
auto on_error = base::Bind(&WiFiDisplaySessionServiceImpl::OnDisconnectFailed,
weak_factory_.GetWeakPtr(), own_sink_);
delegate_->Disconnect(on_error);
}
-void WiFiDisplaySessionServiceImpl::SendMessage(const mojo::String& message) {}
+void WiFiDisplaySessionServiceImpl::SendMessage(const mojo::String& message) {
+ if (own_sink_ == DisplaySourceConnectionDelegate::kInvalidSinkId) {
+ // The connection might drop before this call has arrived.
+ return;
+ }
+ auto connection = delegate_->connection();
+ DCHECK(connection);
+ DCHECK_EQ(own_sink_, connection->GetConnectedSink()->id);
+ connection->SendMessage(message);
+}
+
+void WiFiDisplaySessionServiceImpl::OnSinkMessage(const std::string& message) {
+ DCHECK(delegate_->connection());
+ DCHECK_NE(own_sink_, DisplaySourceConnectionDelegate::kInvalidSinkId);
+ DCHECK(client_);
+ client_->OnMessage(message);
+}
void WiFiDisplaySessionServiceImpl::OnSinksUpdated(
const DisplaySourceSinkInfoList& sinks) {
@@ -119,13 +135,28 @@ void WiFiDisplaySessionServiceImpl::OnSinksUpdated(
client_->OnDisconnected(last_connected_sink_);
last_connected_sink_ = DisplaySourceConnectionDelegate::kInvalidSinkId;
}
- if (connection && last_connected_sink_ != connection->connected_sink->id) {
- last_connected_sink_ = connection->connected_sink->id;
+ if (connection &&
+ last_connected_sink_ != connection->GetConnectedSink()->id) {
+ last_connected_sink_ = connection->GetConnectedSink()->id;
if (client_)
- client_->OnConnected(last_connected_sink_, connection->local_ip);
+ client_->OnConnected(last_connected_sink_, connection->GetLocalAddress());
+ if (last_connected_sink_ == own_sink_) {
+ auto on_message =
+ base::Bind(&WiFiDisplaySessionServiceImpl::OnSinkMessage,
+ weak_factory_.GetWeakPtr());
+ connection->SetMessageReceivedCallback(on_message);
+ }
}
}
+void WiFiDisplaySessionServiceImpl::OnConnectionError(
+ int sink_id,
+ DisplaySourceErrorType type,
+ const std::string& description) {
+ DCHECK(client_);
+ client_->OnError(sink_id, type, description);
+}
+
void WiFiDisplaySessionServiceImpl::OnConnectFailed(
int sink_id,
const std::string& message) {
diff --git a/extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.h b/extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.h
index 6348b12..e50237c 100644
--- a/extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.h
+++ b/extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.h
@@ -39,14 +39,22 @@ class WiFiDisplaySessionServiceImpl
// DisplaySourceConnectionDelegate::Observer overrides.
void OnSinksUpdated(const DisplaySourceSinkInfoList& sinks) override;
+ void OnConnectionError(int sink_id,
+ DisplaySourceErrorType type,
+ const std::string& description) override;
explicit WiFiDisplaySessionServiceImpl(
DisplaySourceConnectionDelegate* delegate,
mojo::InterfaceRequest<WiFiDisplaySessionService> request);
- void OnConnectFailed(int sink_id, const std::string& message);
- void OnDisconnectFailed(int sink_id, const std::string& message);
+ // Called if a message is received from the connected sink.
+ void OnSinkMessage(const std::string& message);
+ // Failure callbacks for Connect and Disconnect methods.
+ void OnConnectFailed(int sink_id, const std::string& reason);
+ void OnDisconnectFailed(int sink_id, const std::string& reason);
+
+ // Mojo error callback.
void OnClientConnectionError();
mojo::StrongBinding<WiFiDisplaySessionService> binding_;