summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.cc
blob: 0cad6403ce3425230c8be90d3cd010fafb606daa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api/display_source/wifi_display/wifi_display_session_service_impl.h"

#include "base/bind.h"
#include "content/public/browser/browser_context.h"
#include "extensions/browser/api/display_source/display_source_connection_delegate_factory.h"

namespace {
const char kErrorCannotHaveMultipleSessions[] =
    "Multiple Wi-Fi Display sessions are not supported";
const char kErrorSinkNotAvailable[] = "The sink is not available";
}  // namespace

namespace extensions {

using namespace api::display_source;

WiFiDisplaySessionServiceImpl::WiFiDisplaySessionServiceImpl(
    DisplaySourceConnectionDelegate* delegate,
    mojo::InterfaceRequest<WiFiDisplaySessionService> request)
    : binding_(this, std::move(request)),
      delegate_(delegate),
      sink_state_(SINK_STATE_NONE),
      sink_id_(DisplaySourceConnectionDelegate::kInvalidSinkId),
      weak_factory_(this) {
  delegate_->AddObserver(this);
}

WiFiDisplaySessionServiceImpl::~WiFiDisplaySessionServiceImpl() {
  delegate_->RemoveObserver(this);
  Disconnect();
}

// static
void WiFiDisplaySessionServiceImpl::BindToRequest(
    content::BrowserContext* browser_context,
    mojo::InterfaceRequest<WiFiDisplaySessionService> request) {
  DisplaySourceConnectionDelegate* delegate =
      DisplaySourceConnectionDelegateFactory::GetForBrowserContext(
          browser_context);
  CHECK(delegate);

  new WiFiDisplaySessionServiceImpl(delegate, std::move(request));
}

void WiFiDisplaySessionServiceImpl::SetClient(
    WiFiDisplaySessionServiceClientPtr client) {
  DCHECK(client);
  DCHECK(!client_);
  client_ = std::move(client);
  client_.set_connection_error_handler(
      base::Bind(&WiFiDisplaySessionServiceImpl::OnClientConnectionError,
                 weak_factory_.GetWeakPtr()));
}

void WiFiDisplaySessionServiceImpl::Connect(int32_t sink_id,
                                            int32_t auth_method,
                                            const mojo::String& auth_data) {
  DCHECK(client_);
  // We support only one Wi-Fi Display session at a time.
  if (delegate_->connection()) {
    client_->OnConnectRequestHandled(false, kErrorCannotHaveMultipleSessions);
    return;
  }

  const DisplaySourceSinkInfoList& sinks = delegate_->last_found_sinks();
  auto found = std::find_if(
      sinks.begin(), sinks.end(),
      [sink_id](DisplaySourceSinkInfoPtr ptr) { return ptr->id == sink_id; });
  if (found == sinks.end() || (*found)->state != SINK_STATE_DISCONNECTED) {
    client_->OnConnectRequestHandled(false, kErrorSinkNotAvailable);
    return;
  }
  AuthenticationInfo auth_info;
  if (auth_method != AUTHENTICATION_METHOD_NONE) {
    DCHECK(auth_method <= AUTHENTICATION_METHOD_LAST);
    auth_info.method = static_cast<AuthenticationMethod>(auth_method);
    auth_info.data = scoped_ptr<std::string>(new std::string(auth_data));
  }
  auto on_error = base::Bind(&WiFiDisplaySessionServiceImpl::OnConnectFailed,
                             weak_factory_.GetWeakPtr(), sink_id);
  delegate_->Connect(sink_id, auth_info, on_error);
  sink_id_ = sink_id;
  sink_state_ = (*found)->state;
  DCHECK(sink_state_ == SINK_STATE_CONNECTING);
  client_->OnConnectRequestHandled(true, "");
}

void WiFiDisplaySessionServiceImpl::Disconnect() {
  if (sink_id_ == DisplaySourceConnectionDelegate::kInvalidSinkId) {
    // The connection might drop before this call has arrived.
    // Renderer should have been notified already.
    return;
  }

  const DisplaySourceSinkInfoList& sinks = delegate_->last_found_sinks();
  auto found = std::find_if(
      sinks.begin(), sinks.end(),
      [this](DisplaySourceSinkInfoPtr ptr) { return ptr->id == sink_id_; });
  DCHECK(found != sinks.end());
  DCHECK((*found)->state == SINK_STATE_CONNECTED ||
         (*found)->state == SINK_STATE_CONNECTING);

  auto on_error = base::Bind(&WiFiDisplaySessionServiceImpl::OnDisconnectFailed,
                             weak_factory_.GetWeakPtr(), sink_id_);
  delegate_->Disconnect(on_error);
}

void WiFiDisplaySessionServiceImpl::SendMessage(const mojo::String& message) {
  if (sink_id_ == DisplaySourceConnectionDelegate::kInvalidSinkId) {
    // The connection might drop before this call has arrived.
    return;
  }
  auto connection = delegate_->connection();
  DCHECK(connection);
  DCHECK_EQ(sink_id_, connection->GetConnectedSink()->id);
  connection->SendMessage(message);
}

void WiFiDisplaySessionServiceImpl::OnSinkMessage(const std::string& message) {
  DCHECK(delegate_->connection());
  DCHECK_NE(sink_id_, DisplaySourceConnectionDelegate::kInvalidSinkId);
  DCHECK(client_);
  client_->OnMessage(message);
}

void WiFiDisplaySessionServiceImpl::OnSinksUpdated(
    const DisplaySourceSinkInfoList& sinks) {
  if (sink_id_ == DisplaySourceConnectionDelegate::kInvalidSinkId)
    return;
  // The initialized sink id means that the client should have
  // been initialized as well.
  DCHECK(client_);
  auto found = std::find_if(
      sinks.begin(), sinks.end(),
      [this](DisplaySourceSinkInfoPtr ptr) { return ptr->id == sink_id_; });
  if (found == sinks.end()) {
    client_->OnError(ERROR_TYPE_CONNECTION_ERROR, "The sink has disappeared");
    client_->OnTerminated();
    sink_id_ = DisplaySourceConnectionDelegate::kInvalidSinkId;
  }

  SinkState actual_state = (*found)->state;

  if (actual_state == sink_state_)
    return;

  if (actual_state == SINK_STATE_CONNECTED) {
    auto connection = delegate_->connection();
    DCHECK(connection);
    auto on_message = base::Bind(&WiFiDisplaySessionServiceImpl::OnSinkMessage,
                                 weak_factory_.GetWeakPtr());
    connection->SetMessageReceivedCallback(on_message);
    client_->OnConnected(connection->GetLocalAddress());
  }

  if (actual_state == SINK_STATE_DISCONNECTED) {
    client_->OnDisconnectRequestHandled(true, "");
    client_->OnTerminated();
    sink_id_ = DisplaySourceConnectionDelegate::kInvalidSinkId;
  }

  sink_state_ = actual_state;
}

void WiFiDisplaySessionServiceImpl::OnConnectionError(
    int sink_id,
    DisplaySourceErrorType type,
    const std::string& description) {
  if (sink_id != sink_id_)
    return;
  DCHECK(client_);
  client_->OnError(type, description);
}

void WiFiDisplaySessionServiceImpl::OnConnectFailed(
    int sink_id,
    const std::string& message) {
  if (sink_id != sink_id_)
    return;
  DCHECK(client_);
  client_->OnError(ERROR_TYPE_CONNECTION_ERROR, message);
}

void WiFiDisplaySessionServiceImpl::OnDisconnectFailed(
    int sink_id,
    const std::string& message) {
  if (sink_id != sink_id_)
    return;
  DCHECK(client_);
  client_->OnDisconnectRequestHandled(false, message);
}

void WiFiDisplaySessionServiceImpl::OnClientConnectionError() {
  DLOG(ERROR) << "IPC connection error";
  delete this;
}

}  // namespace extensions