OLD | NEW |
(Empty) | |
| 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 #include "extensions/renderer/api/display_source/wifi_display/wifi_display_sessi
on.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/timer/timer.h" |
| 9 #include "content/public/common/service_registry.h" |
| 10 #include "content/public/renderer/render_frame.h" |
| 11 |
| 12 namespace { |
| 13 const char kErrorInternal[] = "An internal error has occurred"; |
| 14 } // namespace |
| 15 |
| 16 namespace extensions { |
| 17 |
| 18 using api::display_source::ErrorType; |
| 19 |
| 20 WiFiDisplaySession::WiFiDisplaySession( |
| 21 const DisplaySourceSessionParams& params) |
| 22 : binding_(this), |
| 23 params_(params), |
| 24 weak_factory_(this) { |
| 25 DCHECK(params_.render_frame); |
| 26 params.render_frame->GetServiceRegistry()->ConnectToRemoteService( |
| 27 mojo::GetProxy(&service_)); |
| 28 service_.set_connection_error_handler(base::Bind( |
| 29 &WiFiDisplaySession::OnConnectionError, |
| 30 weak_factory_.GetWeakPtr())); |
| 31 |
| 32 WiFiDisplaySessionServiceClientPtr client_ptr; |
| 33 binding_.Bind(GetProxy(&client_ptr)); |
| 34 binding_.set_connection_error_handler(base::Bind( |
| 35 &WiFiDisplaySession::OnConnectionError, |
| 36 weak_factory_.GetWeakPtr())); |
| 37 DCHECK(client_ptr); |
| 38 service_->SetClient(std::move(client_ptr)); |
| 39 } |
| 40 |
| 41 WiFiDisplaySession::~WiFiDisplaySession() { |
| 42 } |
| 43 |
| 44 void WiFiDisplaySession::Start() { |
| 45 DCHECK(state_ == DisplaySourceSession::Idle); |
| 46 service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data); |
| 47 state_ = DisplaySourceSession::Establishing; |
| 48 } |
| 49 |
| 50 void WiFiDisplaySession::Terminate() { |
| 51 DCHECK(state_ != DisplaySourceSession::Idle); |
| 52 switch (state_) { |
| 53 case DisplaySourceSession::Idle: |
| 54 case DisplaySourceSession::Terminating: |
| 55 // Nothing to do. |
| 56 return; |
| 57 case DisplaySourceSession::Establishing: |
| 58 case DisplaySourceSession::Established: |
| 59 service_->Disconnect(); |
| 60 state_ = DisplaySourceSession::Terminating; |
| 61 break; |
| 62 default: |
| 63 NOTREACHED(); |
| 64 } |
| 65 } |
| 66 |
| 67 void WiFiDisplaySession::OnConnected( |
| 68 int32_t sink_id, const mojo::String& ip_address) { |
| 69 if (sink_id == params_.sink_id) { |
| 70 DCHECK(state_ != DisplaySourceSession::Established); |
| 71 ip_address_ = ip_address; |
| 72 state_ = DisplaySourceSession::Established; |
| 73 } |
| 74 |
| 75 if (!started_callback_.is_null()) |
| 76 started_callback_.Run(sink_id); |
| 77 } |
| 78 |
| 79 void WiFiDisplaySession::OnDisconnected(int32_t sink_id) { |
| 80 if (sink_id == params_.sink_id) { |
| 81 DCHECK(state_ == DisplaySourceSession::Established || |
| 82 state_ == DisplaySourceSession::Terminating); |
| 83 state_ = DisplaySourceSession::Idle; |
| 84 } |
| 85 |
| 86 if (!terminated_callback_.is_null()) |
| 87 terminated_callback_.Run(sink_id); |
| 88 } |
| 89 |
| 90 void WiFiDisplaySession::OnError( |
| 91 int32_t sink_id, int32_t type, const mojo::String& description) { |
| 92 DCHECK(type > api::display_source::ERROR_TYPE_NONE |
| 93 && type <= api::display_source::ERROR_TYPE_LAST); |
| 94 if (!error_callback_.is_null()) |
| 95 error_callback_.Run(sink_id, static_cast<ErrorType>(type), description); |
| 96 } |
| 97 |
| 98 void WiFiDisplaySession::OnMessage(const mojo::String& data) { |
| 99 DCHECK(state_ == DisplaySourceSession::Established); |
| 100 } |
| 101 |
| 102 void WiFiDisplaySession::OnConnectionError() { |
| 103 if (!error_callback_.is_null()) { |
| 104 error_callback_.Run(params_.sink_id, |
| 105 api::display_source::ERROR_TYPE_UNKNOWN_ERROR, |
| 106 kErrorInternal); |
| 107 } |
| 108 |
| 109 if (state_ == DisplaySourceSession::Established || |
| 110 state_ == DisplaySourceSession::Terminating) { |
| 111 // We must explicitly notify the session termination as it will never |
| 112 // arrive from browser process (IPC is broken). |
| 113 if (!terminated_callback_.is_null()) |
| 114 terminated_callback_.Run(params_.sink_id); |
| 115 } |
| 116 } |
| 117 |
| 118 } // namespace extensions |
OLD | NEW |