diff options
author | kmarshall <kmarshall@chromium.org> | 2016-03-14 13:47:46 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-14 20:49:16 +0000 |
commit | 2be305032c5e0e9ac1ae0ce29605fbaa458e2bf2 (patch) | |
tree | 567c1cc8c58f10af41d005472a911b1457513ba8 /blimp | |
parent | 4af8db87713b6acfe713762f33191d5709fc02f8 (diff) | |
download | chromium_src-2be305032c5e0e9ac1ae0ce29605fbaa458e2bf2.zip chromium_src-2be305032c5e0e9ac1ae0ce29605fbaa458e2bf2.tar.gz chromium_src-2be305032c5e0e9ac1ae0ce29605fbaa458e2bf2.tar.bz2 |
Add connection/disconnection event reporting to Blimp client UI.
C++:
* add hooks for intercepting network events and routing them out via
NetworkEventObserver methods.
* plumb NetworkEventObserver events to Java layer via JNI.
Java:
* display event details as Toast messages.
R=dtrainor@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1789713003
Cr-Commit-Position: refs/heads/master@{#381065}
Diffstat (limited to 'blimp')
7 files changed, 147 insertions, 14 deletions
diff --git a/blimp/client/app/android/blimp_client_session_android.cc b/blimp/client/app/android/blimp_client_session_android.cc index 2ab42c5..95ff54e 100644 --- a/blimp/client/app/android/blimp_client_session_android.cc +++ b/blimp/client/app/android/blimp_client_session_android.cc @@ -4,12 +4,15 @@ #include "blimp/client/app/android/blimp_client_session_android.h" +#include <string> + #include "base/android/jni_string.h" #include "base/android/scoped_java_ref.h" #include "base/thread_task_runner_handle.h" #include "blimp/client/feature/tab_control_feature.h" #include "blimp/client/session/assignment_source.h" #include "jni/BlimpClientSession_jni.h" +#include "net/base/net_errors.h" namespace blimp { namespace client { @@ -59,6 +62,18 @@ void BlimpClientSessionAndroid::Connect( BlimpClientSessionAndroid::~BlimpClientSessionAndroid() {} +void BlimpClientSessionAndroid::OnConnected() { + JNIEnv* env = base::android::AttachCurrentThread(); + Java_BlimpClientSession_onConnected(env, java_obj_.obj()); +} + +void BlimpClientSessionAndroid::OnDisconnected(int result) { + JNIEnv* env = base::android::AttachCurrentThread(); + Java_BlimpClientSession_onDisconnected( + env, java_obj_.obj(), base::android::ConvertUTF8ToJavaString( + env, net::ErrorToShortString(result)).obj()); +} + void BlimpClientSessionAndroid::Destroy(JNIEnv* env, const JavaParamRef<jobject>& jobj) { delete this; diff --git a/blimp/client/app/android/blimp_client_session_android.h b/blimp/client/app/android/blimp_client_session_android.h index 226188d..681655e 100644 --- a/blimp/client/app/android/blimp_client_session_android.h +++ b/blimp/client/app/android/blimp_client_session_android.h @@ -31,12 +31,16 @@ class BlimpClientSessionAndroid : public BlimpClientSession { void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& jobj); - // BlimpClientSession overrides. + private: + ~BlimpClientSessionAndroid() override; + + // BlimpClientSession implementation. void OnAssignmentConnectionAttempted( AssignmentSource::Result result) override; - private: - ~BlimpClientSessionAndroid() override; + // NetworkEventObserver implementation. + void OnConnected() override; + void OnDisconnected(int error_code) override; // Reference to the Java object which owns this class. base::android::ScopedJavaGlobalRef<jobject> java_obj_; diff --git a/blimp/client/app/android/java/src/org/chromium/blimp/BlimpRendererActivity.java b/blimp/client/app/android/java/src/org/chromium/blimp/BlimpRendererActivity.java index e3982c7..23ef8d9 100644 --- a/blimp/client/app/android/java/src/org/chromium/blimp/BlimpRendererActivity.java +++ b/blimp/client/app/android/java/src/org/chromium/blimp/BlimpRendererActivity.java @@ -170,4 +170,17 @@ public class BlimpRendererActivity extends Activity public void onAssignmentReceived(int result, int suggestedMessageResourceId) { Toast.makeText(this, suggestedMessageResourceId, Toast.LENGTH_LONG).show(); } + + @Override + public void onConnected() { + Toast.makeText(this, R.string.network_connected, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onDisconnected(String reason) { + Toast.makeText(this, + String.format(getResources().getString(R.string.network_disconnected), reason), + Toast.LENGTH_LONG) + .show(); + } } diff --git a/blimp/client/app/android/java/src/org/chromium/blimp/session/BlimpClientSession.java b/blimp/client/app/android/java/src/org/chromium/blimp/session/BlimpClientSession.java index 567c623..14734f1d 100644 --- a/blimp/client/app/android/java/src/org/chromium/blimp/session/BlimpClientSession.java +++ b/blimp/client/app/android/java/src/org/chromium/blimp/session/BlimpClientSession.java @@ -29,6 +29,19 @@ public class BlimpClientSession { * user if necessary. */ void onAssignmentReceived(int result, int suggestedMessageResourceId); + + /** + * Called when a connection to the engine was made successfully. + */ + void onConnected(); + + /** + * Called when the engine connection was dropped. + * @param reason The string-based error code. + * See net/base/net_errors.h for a complete list of codes + * and their explanations. + */ + void onDisconnected(String reason); } private final Callback mCallback; @@ -103,6 +116,17 @@ public class BlimpClientSession { } @CalledByNative + void onConnected() { + assert mCallback != null; + mCallback.onConnected(); + } + + @CalledByNative + void onDisconnected(String reason) { + mCallback.onDisconnected(reason); + } + + @CalledByNative private long getNativePtr() { assert mNativeBlimpClientSessionAndroidPtr != 0; return mNativeBlimpClientSessionAndroidPtr; diff --git a/blimp/client/app/android/java/strings/android_blimp_strings.grd b/blimp/client/app/android/java/strings/android_blimp_strings.grd index 6b4a42c..69b88965 100644 --- a/blimp/client/app/android/java/strings/android_blimp_strings.grd +++ b/blimp/client/app/android/java/strings/android_blimp_strings.grd @@ -136,6 +136,12 @@ <message name="IDS_ASSIGNMENT_SUCCESS" desc="Message for when getting an assignment succeeds."> Assignment succeeded. </message> + <message name="IDS_NETWORK_CONNECTED" desc="Message for when a connection is authenticated."> + Engine connected. + </message> + <message name="IDS_NETWORK_DISCONNECTED" desc="Message for when a connection terminates."> + Engine disconnected, reason: %1$s + </message> </messages> </release> -</grit>
\ No newline at end of file +</grit> diff --git a/blimp/client/session/blimp_client_session.cc b/blimp/client/session/blimp_client_session.cc index 1f05184..226b625 100644 --- a/blimp/client/session/blimp_client_session.cc +++ b/blimp/client/session/blimp_client_session.cc @@ -15,11 +15,13 @@ #include "blimp/client/feature/navigation_feature.h" #include "blimp/client/feature/render_widget_feature.h" #include "blimp/client/feature/tab_control_feature.h" +#include "blimp/net/blimp_connection.h" #include "blimp/net/blimp_message_processor.h" #include "blimp/net/blimp_message_thread_pipe.h" #include "blimp/net/browser_connection_handler.h" #include "blimp/net/client_connection_manager.h" #include "blimp/net/common.h" +#include "blimp/net/connection_handler.h" #include "blimp/net/null_blimp_message_processor.h" #include "blimp/net/ssl_client_transport.h" #include "blimp/net/tcp_client_transport.h" @@ -30,14 +32,44 @@ namespace blimp { namespace client { +namespace { + +// Posts network events to an observer across the IO/UI thread boundary. +class CrossThreadNetworkEventObserver : public NetworkEventObserver { + public: + CrossThreadNetworkEventObserver( + const base::WeakPtr<NetworkEventObserver>& target, + const scoped_refptr<base::TaskRunner>& task_runner) + : target_(target), task_runner_(task_runner) {} + + ~CrossThreadNetworkEventObserver() override {} + + void OnConnected() override { + task_runner_->PostTask( + FROM_HERE, base::Bind(&NetworkEventObserver::OnConnected, target_)); + } + + void OnDisconnected(int result) override { + task_runner_->PostTask( + FROM_HERE, + base::Bind(&NetworkEventObserver::OnDisconnected, target_, result)); + } + + private: + base::WeakPtr<NetworkEventObserver> target_; + scoped_refptr<base::TaskRunner> task_runner_; +}; + +} // namespace // This class's functions and destruction are all invoked on the IO thread by // the BlimpClientSession. -class ClientNetworkComponents { +class ClientNetworkComponents : public ConnectionHandler, + public ConnectionErrorObserver { public: // Can be created on any thread. - ClientNetworkComponents(); - ~ClientNetworkComponents(); + explicit ClientNetworkComponents(scoped_ptr<NetworkEventObserver> observer); + ~ClientNetworkComponents() override; // Sets up network components. void Initialize(); @@ -49,21 +81,29 @@ class ClientNetworkComponents { BrowserConnectionHandler* GetBrowserConnectionHandler(); private: + // ConnectionHandler implementation. + void HandleConnection(scoped_ptr<BlimpConnection> connection) override; + + // ConnectionErrorObserver implementation. + void OnConnectionError(int error) override; + scoped_ptr<BrowserConnectionHandler> connection_handler_; scoped_ptr<ClientConnectionManager> connection_manager_; + scoped_ptr<NetworkEventObserver> network_observer_; DISALLOW_COPY_AND_ASSIGN(ClientNetworkComponents); }; -ClientNetworkComponents::ClientNetworkComponents() - : connection_handler_(new BrowserConnectionHandler) {} +ClientNetworkComponents::ClientNetworkComponents( + scoped_ptr<NetworkEventObserver> network_observer) + : connection_handler_(new BrowserConnectionHandler), + network_observer_(std::move(network_observer)) {} ClientNetworkComponents::~ClientNetworkComponents() {} void ClientNetworkComponents::Initialize() { DCHECK(!connection_manager_); - connection_manager_ = - make_scoped_ptr(new ClientConnectionManager(connection_handler_.get())); + connection_manager_ = make_scoped_ptr(new ClientConnectionManager(this)); } void ClientNetworkComponents::ConnectWithAssignment( @@ -94,13 +134,27 @@ ClientNetworkComponents::GetBrowserConnectionHandler() { return connection_handler_.get(); } +void ClientNetworkComponents::HandleConnection( + scoped_ptr<BlimpConnection> connection) { + connection->AddConnectionErrorObserver(this); + network_observer_->OnConnected(); + connection_handler_->HandleConnection(std::move(connection)); +} + +void ClientNetworkComponents::OnConnectionError(int result) { + network_observer_->OnDisconnected(result); +} + BlimpClientSession::BlimpClientSession() : io_thread_("BlimpIOThread"), tab_control_feature_(new TabControlFeature), navigation_feature_(new NavigationFeature), render_widget_feature_(new RenderWidgetFeature), - net_components_(new ClientNetworkComponents), weak_factory_(this) { + net_components_.reset(new ClientNetworkComponents( + make_scoped_ptr(new CrossThreadNetworkEventObserver( + weak_factory_.GetWeakPtr(), + base::SequencedTaskRunnerHandle::Get())))); base::Thread::Options options; options.message_loop_type = base::MessageLoop::TYPE_IO; io_thread_.StartWithOptions(options); @@ -170,6 +224,10 @@ void BlimpClientSession::RegisterFeatures() { render_widget_feature_.get()); } +void BlimpClientSession::OnConnected() {} + +void BlimpClientSession::OnDisconnected(int result) {} + TabControlFeature* BlimpClientSession::GetTabControlFeature() const { return tab_control_feature_.get(); } diff --git a/blimp/client/session/blimp_client_session.h b/blimp/client/session/blimp_client_session.h index 5dc8277..d7ffa56 100644 --- a/blimp/client/session/blimp_client_session.h +++ b/blimp/client/session/blimp_client_session.h @@ -35,6 +35,15 @@ class NavigationFeature; class RenderWidgetFeature; class TabControlFeature; +class NetworkEventObserver { + public: + NetworkEventObserver() {} + virtual ~NetworkEventObserver() {} + + virtual void OnConnected() = 0; + virtual void OnDisconnected(int result) = 0; +}; + // BlimpClientSession represents a single active session of Blimp on the client // regardless of whether or not the client application is in the background or // foreground. The only time this session is invalid is during initialization @@ -43,7 +52,7 @@ class TabControlFeature; // This session glues together the feature proxy components and the network // layer. The network components must be interacted with on the IO thread. The // feature proxies must be interacted with on the UI thread. -class BLIMP_CLIENT_EXPORT BlimpClientSession { +class BLIMP_CLIENT_EXPORT BlimpClientSession : public NetworkEventObserver { public: BlimpClientSession(); @@ -64,7 +73,7 @@ class BLIMP_CLIENT_EXPORT BlimpClientSession { const Assignment& assignment); protected: - virtual ~BlimpClientSession(); + ~BlimpClientSession() override; // Notified every time the AssignmentSource returns the result of an attempted // assignment request. @@ -73,6 +82,10 @@ class BLIMP_CLIENT_EXPORT BlimpClientSession { private: void RegisterFeatures(); + // NetworkEventObserver implementation. + void OnConnected() override; + void OnDisconnected(int result) override; + base::Thread io_thread_; scoped_ptr<TabControlFeature> tab_control_feature_; scoped_ptr<NavigationFeature> navigation_feature_; |