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