X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d2e66707deb10dea6f92e3e4092f8a43ef942a5d..eea4d01c65f9b29baa1193db762b4c6b8144af24:/src/osx/webview_webkit.mm?ds=sidebyside diff --git a/src/osx/webview_webkit.mm b/src/osx/webview_webkit.mm index e15efad411..f3efc4e2e5 100644 --- a/src/osx/webview_webkit.mm +++ b/src/osx/webview_webkit.mm @@ -26,6 +26,8 @@ #include "wx/osx/private.h" #include "wx/cocoa/string.h" +#include "wx/hashmap.h" +#include "wx/filesys.h" #include #include @@ -313,6 +315,16 @@ DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebViewWebKitEventHandler ) @end +//We use a hash to map scheme names to wxWebHandlers +WX_DECLARE_STRING_HASH_MAP(wxSharedPtr, wxStringToWebHandlerMap); + +static wxStringToWebHandlerMap g_stringHandlerMap; + +@interface WebViewCustomProtocol : NSURLProtocol +{ +} +@end + // ---------------------------------------------------------------------------- // creation/destruction // ---------------------------------------------------------------------------- @@ -364,7 +376,7 @@ bool wxWebViewWebKit::Create(wxWindow *parent, // Register event listener interfaces WebViewLoadDelegate* loadDelegate = - [[WebViewWebKitLoadDelegate alloc] initWithWxWindow: this]; + [[WebViewLoadDelegate alloc] initWithWxWindow: this]; [m_webView setFrameLoadDelegate:loadDelegate]; @@ -374,6 +386,9 @@ bool wxWebViewWebKit::Create(wxWindow *parent, [m_webView setPolicyDelegate:policyDelegate]; + //Register our own class for custom scheme handling + [NSURLProtocol registerClass:[WebViewCustomProtocol class]]; + LoadUrl(strURL); return true; } @@ -967,6 +982,11 @@ void wxWebViewWebKit::Redo() [[m_webView undoManager] redo]; } +void wxWebViewWebKit::RegisterHandler(wxSharedPtr handler) +{ + g_stringHandlerMap[handler->GetName()] = handler; +} + //------------------------------------------------------------ // Listener interfaces //------------------------------------------------------------ @@ -1222,4 +1242,71 @@ wxString nsErrorToWxHtmlError(NSError* error, wxWebNavigationError* out) } @end +@implementation WebViewCustomProtocol + ++ (BOOL)canInitWithRequest:(NSURLRequest *)request +{ + NSString *scheme = [[request URL] scheme]; + + wxStringToWebHandlerMap::const_iterator it; + for( it = g_stringHandlerMap.begin(); it != g_stringHandlerMap.end(); ++it ) + { + if(it->first.IsSameAs(wxStringWithNSString(scheme))) + { + return YES; + } + } + + return NO; +} + ++ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request +{ + //We don't do any processing here as the wxWebHandler classes do it + return request; +} + +- (void)startLoading +{ + NSURLRequest *request = [self request]; + NSString* path = [[request URL] absoluteString]; + + wxString wxpath = wxStringWithNSString(path); + wxString scheme = wxStringWithNSString([[request URL] scheme]); + wxFSFile* file = g_stringHandlerMap[scheme]->GetFile(wxpath); + size_t length = file->GetStream()->GetLength(); + + + NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[request URL] + MIMEType:wxNSStringWithWxString(file->GetMimeType()) + expectedContentLength:length + textEncodingName:nil]; + + //Load the data, we malloc it so it is tidied up properly + void* buffer = malloc(length); + file->GetStream()->Read(buffer, length); + NSData *data = [[NSData alloc] initWithBytesNoCopy:buffer length:length]; + + id client = [self client]; + + //We do not support caching anything yet + [client URLProtocol:self didReceiveResponse:response + cacheStoragePolicy:NSURLCacheStorageNotAllowed]; + + //Set the data + [client URLProtocol:self didLoadData:data]; + + //Notify that we have finished + [client URLProtocolDidFinishLoading:self]; + + [response release]; +} + +- (void)stopLoading +{ + +} + +@end + #endif //wxUSE_WEBVIEW_WEBKIT