diff options
author | mikhail.pozdnyakov <mikhail.pozdnyakov@intel.com> | 2016-02-26 02:48:10 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-26 10:49:03 +0000 |
commit | ef88b05837e968daf0389fce203c81dba16c17ed (patch) | |
tree | b7c3b092eae814e263f999556c4957f4060abcf5 /extensions/renderer/api | |
parent | 9c7c38629b18307b8d03e3cdeca0ebb37e2b978c (diff) | |
download | chromium_src-ef88b05837e968daf0389fce203c81dba16c17ed.zip chromium_src-ef88b05837e968daf0389fce203c81dba16c17ed.tar.gz chromium_src-ef88b05837e968daf0389fce203c81dba16c17ed.tar.bz2 |
[chrome.displaySource] further implementation of call completion callbacks
The API implementation determines when and with which
arguments the 'startSession'/'terminateSession' completion
callbacks are invoked.
The 'exceeded_session_limit_error' session error type is
eliminated and substituted with 'startSession' completion
callback call (which is more natural since this problem is
detected before a new session is started).
JS bindings code is simplified.
BUG=242107
Review URL: https://codereview.chromium.org/1730583002
Cr-Commit-Position: refs/heads/master@{#377864}
Diffstat (limited to 'extensions/renderer/api')
4 files changed, 101 insertions, 64 deletions
diff --git a/extensions/renderer/api/display_source/display_source_session.cc b/extensions/renderer/api/display_source/display_source_session.cc index df83007..20b1442 100644 --- a/extensions/renderer/api/display_source/display_source_session.cc +++ b/extensions/renderer/api/display_source/display_source_session.cc @@ -22,15 +22,12 @@ DisplaySourceSession::DisplaySourceSession() DisplaySourceSession::~DisplaySourceSession() = default; -void DisplaySourceSession::SetCallbacks( - const SinkIdCallback& started_callback, - const SinkIdCallback& terminated_callback, +void DisplaySourceSession::SetNotificationCallbacks( + const base::Closure& terminated_callback, const ErrorCallback& error_callback) { - DCHECK(started_callback_.is_null()); DCHECK(terminated_callback_.is_null()); DCHECK(error_callback_.is_null()); - started_callback_ = started_callback; terminated_callback_ = terminated_callback; error_callback_ = error_callback; } diff --git a/extensions/renderer/api/display_source/display_source_session.h b/extensions/renderer/api/display_source/display_source_session.h index 2c4053c..548958e 100644 --- a/extensions/renderer/api/display_source/display_source_session.h +++ b/extensions/renderer/api/display_source/display_source_session.h @@ -23,10 +23,10 @@ using DisplaySourceErrorType = api::display_source::ErrorType; // This class represents a generic display source session interface. class DisplaySourceSession { public: - using SinkIdCallback = base::Callback<void(int sink_id)>; + using CompletionCallback = + base::Callback<void(bool success, const std::string& error_description)>; using ErrorCallback = - base::Callback<void(int sink_id, - DisplaySourceErrorType error_type, + base::Callback<void(DisplaySourceErrorType error_type, const std::string& error_description)>; // State flow is ether: @@ -47,36 +47,43 @@ class DisplaySourceSession { // Starts the session. // The session state should be set to 'Establishing' immediately after this // method is called. - virtual void Start() = 0; + // |callback| : Called with 'success' flag set to 'true' if the session is + // successfully started (state should be set to 'Established') + // + // Called with 'success' flag set to 'false' if the session + // has failed to start (state should be set back to 'Idle'). + // The 'error_description' argument contains description of + // an error that had caused the call falure. + virtual void Start(const CompletionCallback& callback) = 0; // Terminates the session. // The session state should be set to 'Terminating' immediately after this // method is called. - virtual void Terminate() = 0; + // |callback| : Called with 'success' flag set to 'true' if the session is + // successfully terminated (state should be set to 'Idle') + // + // Called with 'success' flag set to 'false' if the session + // could not terminate (state should not be modified). + // The 'error_description' argument contains description of + // an error that had caused the call falure. + virtual void Terminate(const CompletionCallback& callback) = 0; State state() const { return state_; } // Sets the callbacks invoked to inform about the session's state changes. // It is required to set the callbacks before the session is started. - // |started_callback| : Called when the session was actually started (state - // should be set to 'Established') - // |terminated_callback| : Called when the session was actually started (state + // |terminated_callback| : Called when session was terminated (state // should be set to 'Idle') // |error_callback| : Called if a fatal error has occured and the session - // either cannot be started (if was invoked in - // 'Establishing' state) or will be terminated soon for - // emergency reasons (if was invoked in 'Established' - // state). - void SetCallbacks(const SinkIdCallback& started_callback, - const SinkIdCallback& terminated_callback, - const ErrorCallback& error_callback); + // will be terminated soon for emergency reasons. + void SetNotificationCallbacks(const base::Closure& terminated_callback, + const ErrorCallback& error_callback); protected: DisplaySourceSession(); State state_; - SinkIdCallback started_callback_; - SinkIdCallback terminated_callback_; + base::Closure terminated_callback_; ErrorCallback error_callback_; private: diff --git a/extensions/renderer/api/display_source/wifi_display/wifi_display_session.cc b/extensions/renderer/api/display_source/wifi_display/wifi_display_session.cc index f6d1104..78fd285 100644 --- a/extensions/renderer/api/display_source/wifi_display/wifi_display_session.cc +++ b/extensions/renderer/api/display_source/wifi_display/wifi_display_session.cc @@ -38,70 +38,93 @@ WiFiDisplaySession::WiFiDisplaySession( WiFiDisplaySession::~WiFiDisplaySession() { } -void WiFiDisplaySession::Start() { - DCHECK(state_ == DisplaySourceSession::Idle); +void WiFiDisplaySession::Start(const CompletionCallback& callback) { + DCHECK_EQ(DisplaySourceSession::Idle, state_); + DCHECK(!terminated_callback_.is_null()) + << "Should be set with 'SetNotificationCallbacks'"; + DCHECK(!error_callback_.is_null()) + << "Should be set with 'SetNotificationCallbacks'"; + service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data); state_ = DisplaySourceSession::Establishing; - if (!started_callback_.is_null()) - started_callback_.Run(params_.sink_id); + start_completion_callback_ = callback; } -void WiFiDisplaySession::Terminate() { - DCHECK(state_ != DisplaySourceSession::Idle); - switch (state_) { - case DisplaySourceSession::Idle: - case DisplaySourceSession::Terminating: - // Nothing to do. - return; - case DisplaySourceSession::Establishing: - case DisplaySourceSession::Established: - service_->Disconnect(); - state_ = DisplaySourceSession::Terminating; - break; - default: - NOTREACHED(); - } +void WiFiDisplaySession::Terminate(const CompletionCallback& callback) { + DCHECK_EQ(DisplaySourceSession::Established, state_); + service_->Disconnect(); + state_ = DisplaySourceSession::Terminating; + teminate_completion_callback_ = callback; } -void WiFiDisplaySession::OnEstablished(const mojo::String& ip_address) { - DCHECK(state_ != DisplaySourceSession::Established); +void WiFiDisplaySession::OnConnected(const mojo::String& ip_address) { + DCHECK_EQ(DisplaySourceSession::Established, state_); ip_address_ = ip_address; - state_ = DisplaySourceSession::Established; + // TODO(Mikhail): Start Wi-Fi Display session control message exchange. +} + +void WiFiDisplaySession::OnConnectRequestHandled(bool success, + const mojo::String& error) { + DCHECK_EQ(DisplaySourceSession::Establishing, state_); + state_ = + success ? DisplaySourceSession::Established : DisplaySourceSession::Idle; + RunStartCallback(success, error); } void WiFiDisplaySession::OnTerminated() { - DCHECK(state_ != DisplaySourceSession::Idle); + DCHECK_NE(DisplaySourceSession::Idle, state_); state_ = DisplaySourceSession::Idle; - if (!terminated_callback_.is_null()) - terminated_callback_.Run(params_.sink_id); + terminated_callback_.Run(); +} + +void WiFiDisplaySession::OnDisconnectRequestHandled(bool success, + const mojo::String& error) { + RunTerminateCallback(success, error); } void WiFiDisplaySession::OnError(int32_t type, const mojo::String& description) { DCHECK(type > api::display_source::ERROR_TYPE_NONE && type <= api::display_source::ERROR_TYPE_LAST); - if (!error_callback_.is_null()) - error_callback_.Run(params_.sink_id, static_cast<ErrorType>(type), - description); + DCHECK_EQ(DisplaySourceSession::Established, state_); + error_callback_.Run(static_cast<ErrorType>(type), description); } void WiFiDisplaySession::OnMessage(const mojo::String& data) { - DCHECK(state_ == DisplaySourceSession::Established); + DCHECK_EQ(DisplaySourceSession::Established, state_); } void WiFiDisplaySession::OnConnectionError() { - if (!error_callback_.is_null()) { - error_callback_.Run(params_.sink_id, - api::display_source::ERROR_TYPE_UNKNOWN_ERROR, - kErrorInternal); + // We must explicitly notify the session termination as it will never + // arrive from browser process (IPC is broken). + switch (state_) { + case DisplaySourceSession::Idle: + case DisplaySourceSession::Establishing: + RunStartCallback(false, kErrorInternal); + break; + case DisplaySourceSession::Terminating: + case DisplaySourceSession::Established: + error_callback_.Run(api::display_source::ERROR_TYPE_UNKNOWN_ERROR, + kErrorInternal); + state_ = DisplaySourceSession::Idle; + terminated_callback_.Run(); + break; + default: + NOTREACHED(); } +} - if (state_ != DisplaySourceSession::Idle) { - // We must explicitly notify the session termination as it will never - // arrive from browser process (IPC is broken). - if (!terminated_callback_.is_null()) - terminated_callback_.Run(params_.sink_id); - } +void WiFiDisplaySession::RunStartCallback(bool success, + const std::string& error_message) { + if (!start_completion_callback_.is_null()) + start_completion_callback_.Run(success, error_message); +} + +void WiFiDisplaySession::RunTerminateCallback( + bool success, + const std::string& error_message) { + if (!teminate_completion_callback_.is_null()) + teminate_completion_callback_.Run(success, error_message); } } // namespace extensions diff --git a/extensions/renderer/api/display_source/wifi_display/wifi_display_session.h b/extensions/renderer/api/display_source/wifi_display/wifi_display_session.h index 5d10308..7eeb987 100644 --- a/extensions/renderer/api/display_source/wifi_display/wifi_display_session.h +++ b/extensions/renderer/api/display_source/wifi_display/wifi_display_session.h @@ -21,24 +21,34 @@ class WiFiDisplaySession: public DisplaySourceSession, ~WiFiDisplaySession() override; private: + using DisplaySourceSession::CompletionCallback; // DisplaySourceSession overrides. - void Start() override; - void Terminate() override; + void Start(const CompletionCallback& callback) override; + void Terminate(const CompletionCallback& callback) override; // WiFiDisplaySessionServiceClient overrides. - void OnEstablished(const mojo::String& ip_address) override; + void OnConnected(const mojo::String& ip_address) override; + void OnConnectRequestHandled(bool success, + const mojo::String& error) override; void OnTerminated() override; + void OnDisconnectRequestHandled(bool success, + const mojo::String& error) override; void OnError(int32_t type, const mojo::String& description) override; void OnMessage(const mojo::String& data) override; // A connection error handler for the mojo objects used in this class. void OnConnectionError(); + void RunStartCallback(bool success, const std::string& error = ""); + void RunTerminateCallback(bool success, const std::string& error = ""); + private: WiFiDisplaySessionServicePtr service_; mojo::Binding<WiFiDisplaySessionServiceClient> binding_; std::string ip_address_; DisplaySourceSessionParams params_; + CompletionCallback start_completion_callback_; + CompletionCallback teminate_completion_callback_; base::WeakPtrFactory<WiFiDisplaySession> weak_factory_; DISALLOW_COPY_AND_ASSIGN(WiFiDisplaySession); |