Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / XWalkResourceClientInternal.java
1 // Copyright (c) 2014 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.internal;
6
7 import android.app.AlertDialog;
8 import android.content.Context;
9 import android.content.DialogInterface;
10 import android.view.View;
11 import android.webkit.WebResourceResponse;
12
13 /**
14  * This class notifies the embedder resource events/callbacks.
15  */
16 @XWalkAPI(createExternally = true)
17 public class XWalkResourceClientInternal {
18     /**
19      * Success
20      * @since 1.0
21      */
22     @XWalkAPI
23     public static final int ERROR_OK = 0;
24     /**
25      * Generic error
26      * @since 1.0
27      */
28     @XWalkAPI
29     public static final int ERROR_UNKNOWN = -1;
30     /**
31      * Server or proxy hostname lookup failed
32      * @since 1.0
33      */
34     @XWalkAPI
35     public static final int ERROR_HOST_LOOKUP = -2;
36     /**
37      * Unsupported authentication scheme (not basic or digest)
38      * @since 1.0
39      */
40     @XWalkAPI
41     public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3;
42     /**
43      * User authentication failed on server
44      * @since 1.0
45      */
46     @XWalkAPI
47     public static final int ERROR_AUTHENTICATION = -4;
48     /**
49      * User authentication failed on proxy
50      * @since 1.0
51      */
52     @XWalkAPI
53     public static final int ERROR_PROXY_AUTHENTICATION = -5;
54     /**
55      * Failed to connect to the server
56      * @since 1.0
57      */
58     @XWalkAPI
59     public static final int ERROR_CONNECT = -6;
60     /**
61      * Failed to read or write to the server
62      * @since 1.0
63      */
64     @XWalkAPI
65     public static final int ERROR_IO = -7;
66     /**
67      * Connection timed out
68      * @since 1.0
69      */
70     @XWalkAPI
71     public static final int ERROR_TIMEOUT = -8;
72     /**
73      * Too many redirects
74      * @since 1.0
75      */
76     @XWalkAPI
77     public static final int ERROR_REDIRECT_LOOP = -9;
78     /**
79      * Unsupported URI scheme
80      * @since 1.0
81      */
82     @XWalkAPI
83     public static final int ERROR_UNSUPPORTED_SCHEME = -10;
84     /**
85      * Failed to perform SSL handshake
86      * @since 1.0
87      */
88     @XWalkAPI
89     public static final int ERROR_FAILED_SSL_HANDSHAKE = -11;
90     /**
91      * Malformed URL
92      * @since 1.0
93      */
94     @XWalkAPI
95     public static final int ERROR_BAD_URL = -12;
96     /**
97      * Generic file error
98      * @since 1.0
99      */
100     @XWalkAPI
101     public static final int ERROR_FILE = -13;
102     /**
103      * File not found
104      * @since 1.0
105      */
106     @XWalkAPI
107     public static final int ERROR_FILE_NOT_FOUND = -14;
108     /**
109      * Too many requests during this load
110      * @since 1.0
111      */
112     @XWalkAPI
113     public static final int ERROR_TOO_MANY_REQUESTS = -15;
114
115     /**
116      * Constructor.
117      * @param view the owner XWalkViewInternal instance.
118      * @since 1.0
119      */
120     @XWalkAPI
121     public XWalkResourceClientInternal(XWalkViewInternal view) {
122         // Keep the above parameter for future use.
123     }
124
125     /**
126      * Notify the client that the XWalkViewInternal will load the resource specified
127      * by the given url.
128      * @param view the owner XWalkViewInternal instance.
129      * @param url the url for the resource to be loaded.
130      * @since 1.0
131      */
132     @XWalkAPI
133     public void onLoadStarted(XWalkViewInternal view, String url) {
134     }
135
136     /**
137      * Notify the client that the XWalkViewInternal completes to load the resource
138      * specified by the given url.
139      * @param view the owner XWalkViewInternal instance.
140      * @param url the url for the resource done for loading.
141      * @since 1.0
142      */
143     @XWalkAPI
144     public void onLoadFinished(XWalkViewInternal view, String url) {
145     }
146
147     /**
148      * Notify the client the progress info of loading a specific url.
149      * @param view the owner XWalkViewInternal instance.
150      * @param progressInPercent the loading process in percent.
151      * @since 1.0
152      */
153     @XWalkAPI
154     public void onProgressChanged(XWalkViewInternal view, int progressInPercent) {
155     }
156
157     /**
158      * Notify the client of a resource request and allow the client to return
159      * the data.  If the return value is null, the XWalkViewInternal
160      * will continue to load the resource as usual.  Otherwise, the return
161      * response and data will be used.  NOTE: This method is called by the
162      * network thread so clients should exercise caution when accessing private
163      * data.
164      * @param view The owner XWalkViewInternal instance that is requesting the
165      *             resource.
166      * @param url The raw url of the resource.
167      * @return A {@link android.webkit.WebResourceResponse} containing the
168      *         response information or null if the XWalkViewInternal should load the
169      *         resource itself.
170      * @since 1.0
171      */
172     @XWalkAPI
173     public WebResourceResponse shouldInterceptLoadRequest(XWalkViewInternal view, String url) {
174         return null;
175     }
176
177     /**
178      * Report an error to the client.
179      * @param view the owner XWalkViewInternal instance.
180      * @param errorCode the error id.
181      * @param description A String describing the error.
182      * @param failingUrl The url that failed to load.
183      * @since 1.0
184      */
185     @XWalkAPI
186     public void onReceivedLoadError(XWalkViewInternal view, int errorCode, String description,
187             String failingUrl) {
188         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(view.getContext());
189         dialogBuilder.setTitle(android.R.string.dialog_alert_title)
190                 .setMessage(description)
191                 .setCancelable(false)
192                 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
193                     @Override
194                     public void onClick(DialogInterface dialog, int which) {
195                         dialog.dismiss();
196                     }
197                 });
198         AlertDialog dialog = dialogBuilder.create();
199         dialog.show();
200     }
201
202     /**
203      * Give the host application a chance to take over the control when a new
204      * url is about to be loaded in the current XWalkViewInternal. If XWalkClient is not
205      * provided, by default XWalkViewInternal will ask Activity Manager to choose the
206      * proper handler for the url. If XWalkClient is provided, return true
207      * means the host application handles the url, while return false means the
208      * current XWalkViewInternal handles the url.
209      *
210      * @param view The XWalkViewInternal that is initiating the callback.
211      * @param url The url to be loaded.
212      * @return True if the host application wants to leave the current XWalkViewInternal
213      *         and handle the url itself, otherwise return false.
214      *
215      * @since 2.1
216      */
217     @XWalkAPI
218     public boolean shouldOverrideUrlLoading(XWalkViewInternal view, String url) {
219         return false;
220     }
221 }