Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / examples / android / src / org / appspot / apprtc / GAEChannelClient.java
1 /*
2  * libjingle
3  * Copyright 2013, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 package org.appspot.apprtc;
29
30 import android.annotation.SuppressLint;
31 import android.app.Activity;
32 import android.util.Log;
33 import android.webkit.ConsoleMessage;
34 import android.webkit.JavascriptInterface;
35 import android.webkit.WebChromeClient;
36 import android.webkit.WebView;
37 import android.webkit.WebViewClient;
38
39 /**
40  * Java-land version of Google AppEngine's JavaScript Channel API:
41  * https://developers.google.com/appengine/docs/python/channel/javascript
42  *
43  * Requires a hosted HTML page that opens the desired channel and dispatches JS
44  * on{Open,Message,Close,Error}() events to a global object named
45  * "androidMessageHandler".
46  */
47 public class GAEChannelClient {
48   private static final String TAG = "GAERTCClient";
49   private WebView webView;
50   private final ProxyingMessageHandler proxyingMessageHandler;
51
52   /**
53    * Callback interface for messages delivered on the Google AppEngine channel.
54    */
55   public interface GAEMessageHandler {
56     public void onOpen();
57     public void onMessage(final String data);
58     public void onClose();
59     public void onError(final int code, final String description);
60   }
61
62   /** Asynchronously open an AppEngine channel. */
63   @SuppressLint("SetJavaScriptEnabled")
64   public GAEChannelClient(
65       Activity activity, String token, GAEMessageHandler handler) {
66     webView = new WebView(activity);
67     webView.getSettings().setJavaScriptEnabled(true);
68     webView.setWebChromeClient(new WebChromeClient() {  // Purely for debugging.
69         public boolean onConsoleMessage (ConsoleMessage msg) {
70           Log.d(TAG, "console: " + msg.message() + " at " +
71               msg.sourceId() + ":" + msg.lineNumber());
72           return false;
73         }
74       });
75     webView.setWebViewClient(new WebViewClient() {  // Purely for debugging.
76         public void onReceivedError(
77             WebView view, int errorCode, String description,
78             String failingUrl) {
79           Log.e(TAG, "JS error: " + errorCode + " in " + failingUrl +
80               ", desc: " + description);
81         }
82       });
83     proxyingMessageHandler = new ProxyingMessageHandler(handler, token);
84     webView.addJavascriptInterface(
85         proxyingMessageHandler, "androidMessageHandler");
86     webView.loadUrl("file:///android_asset/channel.html");
87   }
88
89   /** Close the connection to the AppEngine channel. */
90   public void close() {
91     if (webView == null) {
92       return;
93     }
94     proxyingMessageHandler.disconnect();
95     webView.removeJavascriptInterface("androidMessageHandler");
96     webView.loadUrl("about:blank");
97     webView = null;
98   }
99
100   // Helper class for proxying callbacks from the Java<->JS interaction
101   // (private, background) thread.
102   private static class ProxyingMessageHandler {
103     private final GAEMessageHandler handler;
104     private boolean disconnected = false;
105     private final String token;
106
107     public ProxyingMessageHandler(GAEMessageHandler handler, String token) {
108       this.handler = handler;
109       this.token = token;
110     }
111
112     public void disconnect() {
113       disconnected = true;
114     }
115
116     @JavascriptInterface
117     public String getToken() {
118       return token;
119     }
120
121     @JavascriptInterface
122     public void onOpen() {
123       if (!disconnected) {
124         handler.onOpen();
125       }
126     }
127
128     @JavascriptInterface
129     public void onMessage(final String data) {
130       if (!disconnected) {
131         handler.onMessage(data);
132       }
133     }
134
135     @JavascriptInterface
136     public void onClose() {
137       if (!disconnected) {
138         handler.onClose();
139       }
140     }
141
142     @JavascriptInterface
143     public void onError(final int code, final String description) {
144       if (!disconnected) {
145         handler.onError(code, description);
146       }
147     }
148   }
149 }