From 5e3e62bc6e77477573d3c6b36b71e96d2601a27c Mon Sep 17 00:00:00 2001 From: Steve Lamerton Date: Thu, 11 Aug 2011 18:56:13 +0000 Subject: [PATCH] Add support for custom scheme handling and virtual file systems to the OSX WebKit implementation. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/SOC2011_WEBVIEW@68644 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/webview_webkit.h | 2 +- src/osx/webview_webkit.mm | 87 +++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/include/wx/osx/webview_webkit.h b/include/wx/osx/webview_webkit.h index 958b5695b5..373439dd74 100644 --- a/include/wx/osx/webview_webkit.h +++ b/include/wx/osx/webview_webkit.h @@ -114,7 +114,7 @@ public: void RunScript(const wxString& javascript); //Virtual Filesystem Support - virtual void RegisterHandler(wxSharedPtr WXUNUSED(handler)) {}; + virtual void RegisterHandler(wxSharedPtr handler); // ---- methods not from the parent (common) interface bool CanGetPageSource(); diff --git a/src/osx/webview_webkit.mm b/src/osx/webview_webkit.mm index 9635069f2c..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 // ---------------------------------------------------------------------------- @@ -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 -- 2.45.2