Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkView.java
1 // Copyright (c) 2013 Intel Corporation. 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 package org.xwalk.core;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.graphics.Rect;
11 import android.os.Bundle;
12 import android.os.Looper;
13 import android.util.AttributeSet;
14 import android.view.KeyEvent;
15 import android.view.ViewGroup;
16 import android.webkit.WebSettings;
17 import android.widget.FrameLayout;
18
19 import org.xwalk.core.XWalkDefaultClient;
20 import org.xwalk.core.XWalkDefaultDownloadListener;
21 import org.xwalk.core.XWalkDefaultNavigationHandler;
22 import org.xwalk.core.XWalkDefaultNotificationService;
23 import org.xwalk.core.XWalkDefaultWebChromeClient;
24 import org.xwalk.core.extension.XWalkExtensionManager;
25
26 public class XWalkView extends FrameLayout {
27
28     private XWalkContent mContent;
29     private XWalkDevToolsServer mDevToolsServer;
30     private Activity mActivity;
31     private Context mContext;
32     private XWalkExtensionManager mExtensionManager;
33
34     public XWalkView(Context context, Activity activity) {
35         super(context, null);
36
37         checkThreadSafety();
38         // Make sure mActivity is initialized before calling 'init' method.
39         mActivity = activity;
40         mContext = context;
41         init(context, null);
42     }
43
44     public Activity getActivity() {
45         if (mActivity != null) {
46             return mActivity;
47         } else if (getContext() instanceof Activity) {
48             return (Activity)getContext();
49         }
50
51         // Never achieve here.
52         assert(false);
53         return null;
54     }
55
56     public Context getViewContext() {
57         return mContext;
58     }
59
60     /**
61      * Constructors for inflating via XML.
62      */
63     public XWalkView(Context context, AttributeSet attrs) {
64         super(context, attrs);
65
66         checkThreadSafety();
67         mContext = context;
68         init(context, attrs);
69     }
70
71     private void init(Context context, AttributeSet attrs) {
72         // Initialize chromium resources. Assign them the correct ids in
73         // xwalk core.
74         XWalkInternalResources.resetIds(context);
75
76         // Intialize library, paks and others.
77         XWalkViewDelegate.init(this);
78
79         initXWalkContent(context, attrs);
80     }
81
82     private void initXWalkContent(Context context, AttributeSet attrs) {
83         mContent = new XWalkContent(context, attrs, this);
84         addView(mContent,
85                 new FrameLayout.LayoutParams(
86                         FrameLayout.LayoutParams.MATCH_PARENT,
87                         FrameLayout.LayoutParams.MATCH_PARENT));
88
89
90         // Set default XWalkDefaultClient.
91         setXWalkClient(new XWalkDefaultClient(context, this));
92         // Set default XWalkWebChromeClient and DownloadListener. The default actions
93         // are provided via the following clients if special actions are not needed.
94         setXWalkWebChromeClient(new XWalkDefaultWebChromeClient(context, this));
95         setDownloadListener(new XWalkDefaultDownloadListener(context));
96         setNavigationHandler(new XWalkDefaultNavigationHandler(context));
97         setNotificationService(new XWalkDefaultNotificationService(context, this));
98
99         // Enable xwalk extension mechanism and start load extensions here.
100         // Note that it has to be after above initialization.
101         mExtensionManager = new XWalkExtensionManager(context, getActivity());
102         mExtensionManager.loadExtensions();
103     }
104
105     public void loadUrl(String url) {
106         checkThreadSafety();
107         mContent.loadUrl(url);
108     }
109
110     public void loadAppFromManifest(String path, String manifest) {
111         mContent.loadAppFromManifest(path, manifest);
112     }
113
114     public void reload() {
115         checkThreadSafety();
116         mContent.reload();
117     }
118
119     public void addJavascriptInterface(Object object, String name) {
120         checkThreadSafety();
121         mContent.addJavascriptInterface(object, name);
122     }
123
124     public String getUrl() {
125         checkThreadSafety();
126         return mContent.getUrl();
127     }
128
129     public String getTitle() {
130         checkThreadSafety();
131         return mContent.getTitle();
132     }
133
134     public void clearCache(boolean includeDiskFiles) {
135         checkThreadSafety();
136         mContent.clearCache(includeDiskFiles);
137     }
138
139     public void clearHistory() {
140         checkThreadSafety();
141         mContent.clearHistory();
142     }
143
144     public boolean canGoBack() {
145         checkThreadSafety();
146         return mContent.canGoBack();
147     }
148
149     public void goBack() {
150         checkThreadSafety();
151         mContent.goBack();
152     }
153
154     public boolean canGoForward() {
155         checkThreadSafety();
156         return mContent.canGoForward();
157     }
158
159     public void goForward() {
160         checkThreadSafety();
161         mContent.goForward();
162     }
163
164     public boolean isFullscreen() {
165         checkThreadSafety();
166         return mContent.isFullscreen();
167     }
168
169     public void exitFullscreen() {
170         checkThreadSafety();
171         mContent.exitFullscreen();
172     }
173
174     public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
175         checkThreadSafety();
176         return false;
177     }
178
179     public void setLayoutParams(ViewGroup.LayoutParams params) {
180         checkThreadSafety();
181         super.setLayoutParams(params);
182     }
183
184     public XWalkSettings getSettings() {
185         checkThreadSafety();
186         return mContent.getSettings();
187     }
188
189     public String getOriginalUrl() {
190         checkThreadSafety();
191         return mContent.getOriginalUrl();
192     }
193
194     public void setNetworkAvailable(boolean networkUp) {
195         checkThreadSafety();
196         mContent.setNetworkAvailable(networkUp);
197     }
198
199     public void setInitialScale(int scaleInPercent) {
200         checkThreadSafety();
201     }
202
203     public void setXWalkWebChromeClient(XWalkWebChromeClient client) {
204         checkThreadSafety();
205         mContent.setXWalkWebChromeClient(client);
206     }
207
208     public void setXWalkClient(XWalkClient client) {
209         checkThreadSafety();
210         mContent.setXWalkClient(client);
211     }
212
213     public void stopLoading() {
214         checkThreadSafety();
215         mContent.stopLoading();
216     }
217
218     public void pauseTimers() {
219         checkThreadSafety();
220         mContent.pauseTimers();
221     }
222
223     public void resumeTimers() {
224         checkThreadSafety();
225         mContent.resumeTimers();
226     }
227
228     public void setDownloadListener(DownloadListener listener) {
229         checkThreadSafety();
230         mContent.setDownloadListener(listener);
231     }
232
233     public void setNavigationHandler(XWalkNavigationHandler handler) {
234         checkThreadSafety();
235         mContent.setNavigationHandler(handler);
236     }
237
238     public void setNotificationService(XWalkNotificationService service) {
239         checkThreadSafety();
240         mContent.setNotificationService(service);
241     }
242
243     // Enables remote debugging and returns the URL at which the dev tools server is listening
244     // for commands. The allowedUid argument can be used to specify the uid of the process that is
245     // permitted to connect.
246     public String enableRemoteDebugging(int allowedUid) {
247         checkThreadSafety();
248         // Chrome looks for "devtools_remote" pattern in the name of a unix domain socket
249         // to identify a debugging page
250         final String socketName = getContext().getApplicationContext().getPackageName() + "_devtools_remote";
251         if (mDevToolsServer == null) {
252             mDevToolsServer = new XWalkDevToolsServer(socketName);
253             mDevToolsServer.allowConnectionFromUid(allowedUid);
254             mDevToolsServer.setRemoteDebuggingEnabled(true);
255         }
256         // devtools/page is hardcoded in devtools_http_handler_impl.cc (kPageUrlPrefix)
257         return "ws://" + socketName + "/devtools/page/" + mContent.devToolsAgentId();
258     }
259
260     // Enables remote debugging and returns the URL at which the dev tools server is listening
261     // for commands. Only the current process is allowed to connect to the server.
262     public String enableRemoteDebugging() {
263         return enableRemoteDebugging(mContext.getApplicationInfo().uid);
264     }
265
266     public void disableRemoteDebugging() {
267         checkThreadSafety();
268         if (mDevToolsServer ==  null) return;
269
270         if (mDevToolsServer.isRemoteDebuggingEnabled()) {
271             mDevToolsServer.setRemoteDebuggingEnabled(false);
272         }
273         mDevToolsServer.destroy();
274         mDevToolsServer = null;
275     }
276
277     public void onPause() {
278         mExtensionManager.onPause();
279         mContent.onPause();
280     }
281
282     public void onResume() {
283         mExtensionManager.onResume();
284         mContent.onResume();
285     }
286
287     public boolean onKeyUp(int keyCode, KeyEvent event) {
288         if (keyCode == KeyEvent.KEYCODE_BACK) {
289             // If there's navigation happens when app is fullscreen,
290             // the content will still be fullscreen after navigation.
291             // In such case, the back key will exit fullscreen first.
292             if (isFullscreen()) {
293                 exitFullscreen();
294                 return true;
295             } else if (canGoBack()) {
296                 goBack();
297                 return true;
298             }
299         }
300         return false;
301     }
302
303     public void onDestroy() {
304         destroy();
305     }
306
307     public void destroy() {
308         mExtensionManager.onDestroy();
309         mContent.destroy();
310         disableRemoteDebugging();
311     }
312
313     public void onActivityResult(int requestCode, int resultCode, Intent data) {
314         mExtensionManager.onActivityResult(requestCode, resultCode, data);
315         mContent.onActivityResult(requestCode, resultCode, data);
316     }
317
318     public boolean onNewIntent(Intent intent) {
319         return mContent.onNewIntent(intent);
320     }
321
322     public String getVersion() {
323         return mContent.getVersion();
324     }
325
326     public int getContentID() {
327         return mContent.getRoutingID();
328     }
329
330     // TODO(shouqun): requestFocusFromTouch, setVerticalScrollBarEnabled are
331     // from android.view.View;
332
333     // For instrumentation test.
334     public XWalkContent getXWalkViewContentForTest() {
335         checkThreadSafety();
336         return mContent;
337     }
338
339     public XWalkWebChromeClient getXWalkWebChromeClientForTest() {
340         return mContent.getXWalkWebChromeClient();
341     }
342
343     public WebBackForwardList copyBackForwardList() {
344         return mContent.copyBackForwardList();
345     }
346
347     public WebBackForwardList saveState(Bundle outState) {
348         return mContent.saveState(outState);
349     }
350
351     public WebBackForwardList restoreState(Bundle inState) {
352         return mContent.restoreState(inState);
353     }
354
355     private static void checkThreadSafety() {
356         if (Looper.myLooper() != Looper.getMainLooper()) {
357             Throwable throwable = new Throwable(
358                 "Warning: A XWalkView method was called on thread '" +
359                 Thread.currentThread().getName() + "'. " +
360                 "All XWalkView methods must be called on the UI thread. ");
361             throw new RuntimeException(throwable);
362         }
363     }
364 }