]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/webview_webkit.mm
Use the data scheme to load resources in the WebKitGTK+ implementation, rather than...
[wxWidgets.git] / src / osx / webview_webkit.mm
index e15efad4115ba9aae4602af9571bf4884deff6e1..f3efc4e2e542fce0d092ef3b06031d57fc3ff547 100644 (file)
@@ -26,6 +26,8 @@
 
 #include "wx/osx/private.h"
 #include "wx/cocoa/string.h"
 
 #include "wx/osx/private.h"
 #include "wx/cocoa/string.h"
+#include "wx/hashmap.h"
+#include "wx/filesys.h"
 
 #include <WebKit/WebKit.h>
 #include <WebKit/HIWebView.h>
 
 #include <WebKit/WebKit.h>
 #include <WebKit/HIWebView.h>
@@ -313,6 +315,16 @@ DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebViewWebKitEventHandler )
 
 @end
 
 
 @end
 
+//We use a hash to map scheme names to wxWebHandlers
+WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxWebHandler>, wxStringToWebHandlerMap);
+
+static wxStringToWebHandlerMap g_stringHandlerMap;
+
+@interface WebViewCustomProtocol : NSURLProtocol
+{
+}
+@end
+
 // ----------------------------------------------------------------------------
 // creation/destruction
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // creation/destruction
 // ----------------------------------------------------------------------------
@@ -364,7 +376,7 @@ bool wxWebViewWebKit::Create(wxWindow *parent,
 
     // Register event listener interfaces
     WebViewLoadDelegate* loadDelegate =
 
     // Register event listener interfaces
     WebViewLoadDelegate* loadDelegate =
-            [[WebViewWebKitLoadDelegate alloc] initWithWxWindow: this];
+            [[WebViewLoadDelegate alloc] initWithWxWindow: this];
 
     [m_webView setFrameLoadDelegate:loadDelegate];
 
 
     [m_webView setFrameLoadDelegate:loadDelegate];
 
@@ -374,6 +386,9 @@ bool wxWebViewWebKit::Create(wxWindow *parent,
 
     [m_webView setPolicyDelegate:policyDelegate];
 
 
     [m_webView setPolicyDelegate:policyDelegate];
 
+    //Register our own class for custom scheme handling
+    [NSURLProtocol registerClass:[WebViewCustomProtocol class]];
+
     LoadUrl(strURL);
     return true;
 }
     LoadUrl(strURL);
     return true;
 }
@@ -967,6 +982,11 @@ void wxWebViewWebKit::Redo()
     [[m_webView undoManager] redo];
 }
 
     [[m_webView undoManager] redo];
 }
 
+void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebHandler> handler)
+{
+    g_stringHandlerMap[handler->GetName()] = handler;
+}
+
 //------------------------------------------------------------
 // Listener interfaces
 //------------------------------------------------------------
 //------------------------------------------------------------
 // Listener interfaces
 //------------------------------------------------------------
@@ -1222,4 +1242,71 @@ wxString nsErrorToWxHtmlError(NSError* error, wxWebNavigationError* out)
 }
 @end
 
 }
 @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<NSURLProtocolClient> 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
 #endif //wxUSE_WEBVIEW_WEBKIT