Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timob 9361 - Android/iOS parity re webview error event message and errorCode. #2509

Merged
merged 8 commits into from
Jul 9, 2012
14 changes: 14 additions & 0 deletions android/modules/ui/src/java/ti/modules/titanium/ui/UIModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebViewClient;
import android.widget.Toast;

@Kroll.module
Expand Down Expand Up @@ -117,6 +118,19 @@ public class UIModule extends KrollModule implements Handler.Callback
@Kroll.constant public static final String UNIT_IN = TiDimension.UNIT_IN;
@Kroll.constant public static final String UNIT_DIP = TiDimension.UNIT_DIP;

// TiWebViewClient onReceivedError error codes.
@Kroll.constant public static final int URL_ERROR_AUTHENTICATION = WebViewClient.ERROR_AUTHENTICATION;
@Kroll.constant public static final int URL_ERROR_BAD_URL = WebViewClient.ERROR_BAD_URL;
@Kroll.constant public static final int URL_ERROR_CONNECT = WebViewClient.ERROR_CONNECT;
@Kroll.constant public static final int URL_ERROR_SSL_FAILED = WebViewClient.ERROR_FAILED_SSL_HANDSHAKE;
@Kroll.constant public static final int URL_ERROR_FILE = WebViewClient.ERROR_FILE;
@Kroll.constant public static final int URL_ERROR_FILE_NOT_FOUND = WebViewClient.ERROR_FILE_NOT_FOUND;
@Kroll.constant public static final int URL_ERROR_HOST_LOOKUP = WebViewClient.ERROR_HOST_LOOKUP;
@Kroll.constant public static final int URL_ERROR_REDIRECT_LOOP = WebViewClient.ERROR_REDIRECT_LOOP;
@Kroll.constant public static final int URL_ERROR_TIMEOUT = WebViewClient.ERROR_TIMEOUT;
@Kroll.constant public static final int URL_ERROR_UNKNOWN = WebViewClient.ERROR_UNKNOWN;
@Kroll.constant public static final int URL_ERROR_UNSUPPORTED_SCHEME = WebViewClient.ERROR_UNSUPPORTED_SCHEME;

protected static final int MSG_SET_BACKGROUND_COLOR = KrollProxy.MSG_LAST_ID + 100;
protected static final int MSG_SET_BACKGROUND_IMAGE = KrollProxy.MSG_LAST_ID + 101;
protected static final int MSG_SET_ORIENTATION = KrollProxy.MSG_LAST_ID + 102;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import org.appcelerator.titanium.TiC;

import ti.modules.titanium.media.TiVideoActivity;
import android.content.Intent;
Expand Down Expand Up @@ -68,20 +69,12 @@ public void onReceivedError(WebView view, int errorCode, String description, Str
{
super.onReceivedError(view, errorCode, description, failingUrl);

//TODO report this to the user
String text = "Javascript Error("+errorCode+"): " + description;
Log.e(LCAT, "Received on error" + text);

// Kosso : Dec 10, 2011
// fire the error event!!!
KrollDict data = new KrollDict();
data.put("url", failingUrl);
data.put("description", description);
data.put("errorCode", errorCode);
data.put("message", description);
data.put("type", errorCode);
webView.getProxy().fireEvent("error", data);

}

@Override
Expand All @@ -93,7 +86,7 @@ public boolean shouldOverrideUrlLoading(final WebView view, String url)

if (URLUtil.isAssetUrl(url) || URLUtil.isContentUrl(url) || URLUtil.isFileUrl(url)) {
// go through the proxy to ensure we're on the UI thread
webView.getProxy().setProperty("url", url, true);
webView.getProxy().setPropertyAndFire(TiC.PROPERTY_URL, url);
return true;
} else if(url.startsWith(WebView.SCHEME_TEL)) {
Log.i(LCAT, "Launching dialer for " + url);
Expand Down
66 changes: 66 additions & 0 deletions apidoc/Titanium/UI/UI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,73 @@ properties:
type: Number
permission: read-only
platforms: [android, iphone, ipad]

- name: URL_ERROR_AUTHENTICATION
summary: Authentication error code reported via <Titanium.UI.WebView.error>.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_BAD_URL
summary: Bad url error code reported via <Titanium.UI.WebView.error>.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_CONNECT
summary: Error code reported via <Titanium.UI.WebView.error> for a failure to connect to host.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_SSL_FAILED
summary: Error code reported via <Titanium.UI.WebView.error> for an SSL failure.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_FILE
summary: Error code reported via <Titanium.UI.WebView.error> for a failure to access a file resource on a host, except "file not found", which has its own constant.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_FILE_NOT_FOUND
summary: Error code reported via <Titanium.UI.WebView.error> when a requested file does not exist on the host.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_HOST_LOOKUP
summary: Error code reported via <Titanium.UI.WebView.error> when a host name cannot be resolved, such as via a DNS lookup error.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_REDIRECT_LOOP
summary: Error code reported via <Titanium.UI.WebView.error> when a redirect loop is detected.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_TIMEOUT
summary: Error code reported via <Titanium.UI.WebView.error> when a timeout occurs.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_UNKNOWN
summary: Error code reported via <Titanium.UI.WebView.error> when an unknown error occurs.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: URL_ERROR_UNSUPPORTED_SCHEME
summary: Error code reported via <Titanium.UI.WebView.error> when a url contains an unsupported scheme.
type: Number
platforms: [android, iphone, ipad]
since: "2.2.0"

- name: backgroundColor
summary: |
Sets the background color of the master view (when there are no windows or other top-level
Expand Down
14 changes: 14 additions & 0 deletions apidoc/Titanium/UI/WebView.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,23 @@ events:
summary: Fired when the web view cannot load the content.
description: |
For Mobile Web, generally this event will **not** fire, even for 404, 500, and invalid URLs.
The `errorCode` value refers to one of the <Titanium.UI> URL_ERROR constants or, if it does not
match one of those constants, it refers to a platform-specific constant. The platform-specific
values are underlying [iOS NSURLError*](https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/uid/TP40003793-CH3g-SW40)
or [Android WebViewClient ERROR_*](http://developer.android.com/reference/android/webkit/WebViewClient.html) constants.
properties:
- name: url
summary: URL of the web document.
type: String
platforms: [iphone, ipad, mobileweb, android]
- name: message
summary: Error message.
type: String
platforms: [iphone, ipad, mobileweb, android]
- name: errorCode
summary: A <Titanium.UI> URL_ERROR_* constant or underlying platform specific error code.
type: Number
platforms: [iphone, ipad, android]

- name: load
summary: Fired when the web view content is loaded.
Expand Down
46 changes: 36 additions & 10 deletions iphone/Classes/TiUIWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -700,19 +700,45 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// this means the pending request has been cancelled and should be
// safely squashed
if ([[error domain] isEqual:NSURLErrorDomain] && [error code]==-999)
NSString *offendingUrl = [self url];

if ([[error domain] isEqual:NSURLErrorDomain])
{
return;
offendingUrl = [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey];

// this means the pending request has been cancelled and should be
// safely squashed
if ([error code]==-999)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be NSURLErrorCancelled constant instead of magic number.

{
return;
}
}
NSLog(@"[ERROR] Error loading: %@, Error: %@",[self url],error);

NSLog(@"[ERROR] Error loading: %@, Error: %@",offendingUrl,error);

if ([self.proxy _hasListeners:@"error"])
{
NSMutableDictionary *event = [NSMutableDictionary dictionaryWithObject:[self url] forKey:@"url"];
[event setObject:[error description] forKey:@"message"];
NSMutableDictionary *event = [NSMutableDictionary dictionaryWithObject:[error description] forKey:@"message"];

// We combine some error codes into a single one which we share with Android.
NSInteger rawErrorCode = [error code];
NSInteger returnErrorCode = rawErrorCode;

if (rawErrorCode == NSURLErrorUserCancelledAuthentication)
{
returnErrorCode = NSURLErrorUserAuthenticationRequired; // URL_ERROR_AUTHENTICATION
}
else if (rawErrorCode == NSURLErrorNoPermissionsToReadFile || rawErrorCode == NSURLErrorCannotCreateFile || rawErrorCode == NSURLErrorFileIsDirectory || rawErrorCode == NSURLErrorCannotCloseFile || rawErrorCode == NSURLErrorCannotWriteToFile || rawErrorCode == NSURLErrorCannotRemoveFile || rawErrorCode == NSURLErrorCannotMoveFile)
{
returnErrorCode = NSURLErrorCannotOpenFile; // URL_ERROR_FILE
}
else if (rawErrorCode == NSURLErrorDNSLookupFailed)
{
returnErrorCode = NSURLErrorCannotFindHost; // URL_ERROR_HOST_LOOKUP
}

[event setObject:[NSNumber numberWithInteger:returnErrorCode] forKey:@"errorCode"];
[event setObject:offendingUrl forKey:@"url"];
[self.proxy fireEvent:@"error" withObject:event];
}
}
Expand Down Expand Up @@ -747,4 +773,4 @@ - (void)fireEvent:(id)listener withObject:(id)obj remove:(BOOL)yn thisObject:(id

@end

#endif
#endif
12 changes: 12 additions & 0 deletions iphone/Classes/UIModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@
@property(nonatomic,readonly) NSString *UNIT_IN;
@property(nonatomic,readonly) NSString *UNIT_DIP;

@property(nonatomic,readonly) NSNumber *URL_ERROR_AUTHENTICATION;
@property(nonatomic,readonly) NSNumber *URL_ERROR_BAD_URL;
@property(nonatomic,readonly) NSNumber *URL_ERROR_CONNECT;
@property(nonatomic,readonly) NSNumber *URL_ERROR_SSL_FAILED;
@property(nonatomic,readonly) NSNumber *URL_ERROR_FILE;
@property(nonatomic,readonly) NSNumber *URL_ERROR_FILE_NOT_FOUND;
@property(nonatomic,readonly) NSNumber *URL_ERROR_HOST_LOOKUP;
@property(nonatomic,readonly) NSNumber *URL_ERROR_REDIRECT_LOOP;
@property(nonatomic,readonly) NSNumber *URL_ERROR_TIMEOUT;
@property(nonatomic,readonly) NSNumber *URL_ERROR_UNKNOWN;
@property(nonatomic,readonly) NSNumber *URL_ERROR_UNSUPPORTED_SCHEME;

#ifdef USE_TI_UI2DMATRIX
-(id)create2DMatrix:(id)args;
#endif
Expand Down
12 changes: 12 additions & 0 deletions iphone/Classes/UIModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ -(NSNumber*)KEYBOARD_DECIMAL_PAD
MAKE_SYSTEM_PROP(PICKER_TYPE_TIME,UIDatePickerModeTime);
MAKE_SYSTEM_PROP(PICKER_TYPE_COUNT_DOWN_TIMER,UIDatePickerModeCountDownTimer);

MAKE_SYSTEM_PROP(URL_ERROR_AUTHENTICATION,NSURLErrorUserAuthenticationRequired);
MAKE_SYSTEM_PROP(URL_ERROR_BAD_URL,NSURLErrorBadURL);
MAKE_SYSTEM_PROP(URL_ERROR_CONNECT,NSURLErrorCannotConnectToHost);
MAKE_SYSTEM_PROP(URL_ERROR_SSL_FAILED,NSURLErrorSecureConnectionFailed);
MAKE_SYSTEM_PROP(URL_ERROR_FILE,NSURLErrorCannotOpenFile);
MAKE_SYSTEM_PROP(URL_ERROR_FILE_NOT_FOUND,NSURLErrorFileDoesNotExist);
MAKE_SYSTEM_PROP(URL_ERROR_HOST_LOOKUP,NSURLErrorCannotFindHost);
MAKE_SYSTEM_PROP(URL_ERROR_REDIRECT_LOOP,NSURLErrorHTTPTooManyRedirects);
MAKE_SYSTEM_PROP(URL_ERROR_TIMEOUT,NSURLErrorTimedOut);
MAKE_SYSTEM_PROP(URL_ERROR_UNKNOWN,NSURLErrorUnknown);
MAKE_SYSTEM_PROP(URL_ERROR_UNSUPPORTED_SCHEME,NSURLErrorUnsupportedURL);

MAKE_SYSTEM_PROP_DEPRECATED_REPLACED(BLEND_MODE_NORMAL,kCGBlendModeNormal, @"UI.BLEND_MODE_NORMAL", @"1.8.0", @"Ti.UI.iOS.BLEND_MODE_NORMAL");
MAKE_SYSTEM_PROP_DEPRECATED_REPLACED(BLEND_MODE_MULTIPLY,kCGBlendModeMultiply, @"UI.BLEND_MODE_MULTIPLY", @"1.8.0", @"Ti.UI.iOS.BLEND_MODE_MULTIPLY");
MAKE_SYSTEM_PROP_DEPRECATED_REPLACED(BLEND_MODE_SCREEN,kCGBlendModeScreen, @"UI.BLEND_MODE_SCREEN", @"1.8.0", @"Ti.UI.iOS.BLEND_MODE_SCREEN");
Expand Down