OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/api/display_source/wifi_display/wifi_display_sessi
on.h" | 5 #include "extensions/renderer/api/display_source/wifi_display/wifi_display_sessi
on.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/timer/timer.h" | 8 #include "base/timer/timer.h" |
9 #include "content/public/common/service_registry.h" | 9 #include "content/public/common/service_registry.h" |
10 #include "content/public/renderer/render_frame.h" | 10 #include "content/public/renderer/render_frame.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 service_->SetClient(binding_.CreateInterfacePtrAndBind()); | 32 service_->SetClient(binding_.CreateInterfacePtrAndBind()); |
33 binding_.set_connection_error_handler(base::Bind( | 33 binding_.set_connection_error_handler(base::Bind( |
34 &WiFiDisplaySession::OnConnectionError, | 34 &WiFiDisplaySession::OnConnectionError, |
35 weak_factory_.GetWeakPtr())); | 35 weak_factory_.GetWeakPtr())); |
36 } | 36 } |
37 | 37 |
38 WiFiDisplaySession::~WiFiDisplaySession() { | 38 WiFiDisplaySession::~WiFiDisplaySession() { |
39 } | 39 } |
40 | 40 |
41 void WiFiDisplaySession::Start() { | 41 void WiFiDisplaySession::Start(const CompletionCallback& callback) { |
42 DCHECK(state_ == DisplaySourceSession::Idle); | 42 DCHECK_EQ(DisplaySourceSession::Idle, state_); |
| 43 DCHECK(!terminated_callback_.is_null()) |
| 44 << "Should be set with 'SetNotificationCallbacks'"; |
| 45 DCHECK(!error_callback_.is_null()) |
| 46 << "Should be set with 'SetNotificationCallbacks'"; |
| 47 |
43 service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data); | 48 service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data); |
44 state_ = DisplaySourceSession::Establishing; | 49 state_ = DisplaySourceSession::Establishing; |
45 if (!started_callback_.is_null()) | 50 start_completion_callback_ = callback; |
46 started_callback_.Run(params_.sink_id); | |
47 } | 51 } |
48 | 52 |
49 void WiFiDisplaySession::Terminate() { | 53 void WiFiDisplaySession::Terminate(const CompletionCallback& callback) { |
50 DCHECK(state_ != DisplaySourceSession::Idle); | 54 DCHECK_EQ(DisplaySourceSession::Established, state_); |
51 switch (state_) { | 55 service_->Disconnect(); |
52 case DisplaySourceSession::Idle: | 56 state_ = DisplaySourceSession::Terminating; |
53 case DisplaySourceSession::Terminating: | 57 teminate_completion_callback_ = callback; |
54 // Nothing to do. | |
55 return; | |
56 case DisplaySourceSession::Establishing: | |
57 case DisplaySourceSession::Established: | |
58 service_->Disconnect(); | |
59 state_ = DisplaySourceSession::Terminating; | |
60 break; | |
61 default: | |
62 NOTREACHED(); | |
63 } | |
64 } | 58 } |
65 | 59 |
66 void WiFiDisplaySession::OnEstablished(const mojo::String& ip_address) { | 60 void WiFiDisplaySession::OnConnected(const mojo::String& ip_address) { |
67 DCHECK(state_ != DisplaySourceSession::Established); | 61 DCHECK_EQ(DisplaySourceSession::Established, state_); |
68 ip_address_ = ip_address; | 62 ip_address_ = ip_address; |
69 state_ = DisplaySourceSession::Established; | 63 // TODO(Mikhail): Start Wi-Fi Display session control message exchange. |
| 64 } |
| 65 |
| 66 void WiFiDisplaySession::OnConnectRequestHandled(bool success, |
| 67 const mojo::String& error) { |
| 68 DCHECK_EQ(DisplaySourceSession::Establishing, state_); |
| 69 state_ = |
| 70 success ? DisplaySourceSession::Established : DisplaySourceSession::Idle; |
| 71 RunStartCompletionCB(success, error); |
70 } | 72 } |
71 | 73 |
72 void WiFiDisplaySession::OnTerminated() { | 74 void WiFiDisplaySession::OnTerminated() { |
73 DCHECK(state_ != DisplaySourceSession::Idle); | 75 DCHECK_NE(DisplaySourceSession::Idle, state_); |
74 state_ = DisplaySourceSession::Idle; | 76 state_ = DisplaySourceSession::Idle; |
75 if (!terminated_callback_.is_null()) | 77 terminated_callback_.Run(); |
76 terminated_callback_.Run(params_.sink_id); | 78 } |
| 79 |
| 80 void WiFiDisplaySession::OnDisconnectRequestHandled(bool success, |
| 81 const mojo::String& error) { |
| 82 RunTerminateCompletionCB(success, error); |
77 } | 83 } |
78 | 84 |
79 void WiFiDisplaySession::OnError(int32_t type, | 85 void WiFiDisplaySession::OnError(int32_t type, |
80 const mojo::String& description) { | 86 const mojo::String& description) { |
81 DCHECK(type > api::display_source::ERROR_TYPE_NONE | 87 DCHECK(type > api::display_source::ERROR_TYPE_NONE |
82 && type <= api::display_source::ERROR_TYPE_LAST); | 88 && type <= api::display_source::ERROR_TYPE_LAST); |
83 if (!error_callback_.is_null()) | 89 DCHECK_EQ(DisplaySourceSession::Established, state_); |
84 error_callback_.Run(params_.sink_id, static_cast<ErrorType>(type), | 90 error_callback_.Run(static_cast<ErrorType>(type), description); |
85 description); | |
86 } | 91 } |
87 | 92 |
88 void WiFiDisplaySession::OnMessage(const mojo::String& data) { | 93 void WiFiDisplaySession::OnMessage(const mojo::String& data) { |
89 DCHECK(state_ == DisplaySourceSession::Established); | 94 DCHECK_EQ(DisplaySourceSession::Established, state_); |
90 } | 95 } |
91 | 96 |
92 void WiFiDisplaySession::OnConnectionError() { | 97 void WiFiDisplaySession::OnConnectionError() { |
93 if (!error_callback_.is_null()) { | 98 // We must explicitly notify the session termination as it will never |
94 error_callback_.Run(params_.sink_id, | 99 // arrive from browser process (IPC is broken). |
95 api::display_source::ERROR_TYPE_UNKNOWN_ERROR, | 100 switch (state_) { |
96 kErrorInternal); | 101 case DisplaySourceSession::Idle: |
97 } | 102 case DisplaySourceSession::Establishing: |
98 | 103 RunStartCompletionCB(false, kErrorInternal); |
99 if (state_ != DisplaySourceSession::Idle) { | 104 break; |
100 // We must explicitly notify the session termination as it will never | 105 case DisplaySourceSession::Terminating: |
101 // arrive from browser process (IPC is broken). | 106 case DisplaySourceSession::Established: |
102 if (!terminated_callback_.is_null()) | 107 error_callback_.Run(api::display_source::ERROR_TYPE_UNKNOWN_ERROR, |
103 terminated_callback_.Run(params_.sink_id); | 108 kErrorInternal); |
| 109 state_ = DisplaySourceSession::Idle; |
| 110 terminated_callback_.Run(); |
| 111 break; |
| 112 default: |
| 113 NOTREACHED(); |
104 } | 114 } |
105 } | 115 } |
106 | 116 |
| 117 void WiFiDisplaySession::RunStartCompletionCB( |
| 118 bool success, |
| 119 const std::string& error_message) { |
| 120 if (!start_completion_callback_.is_null()) |
| 121 start_completion_callback_.Run(success, error_message); |
| 122 } |
| 123 |
| 124 void WiFiDisplaySession::RunTerminateCompletionCB( |
| 125 bool success, |
| 126 const std::string& error_message) { |
| 127 if (!teminate_completion_callback_.is_null()) |
| 128 teminate_completion_callback_.Run(success, error_message); |
| 129 } |
| 130 |
107 } // namespace extensions | 131 } // namespace extensions |
OLD | NEW |