]> git.saurik.com Git - wxWidgets.git/commitdiff
Add support for custom scheme handling and virtual file systems to the OSX WebKit...
authorSteve Lamerton <steve.lamerton@gmail.com>
Thu, 11 Aug 2011 18:56:13 +0000 (18:56 +0000)
committerSteve Lamerton <steve.lamerton@gmail.com>
Thu, 11 Aug 2011 18:56:13 +0000 (18:56 +0000)
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
src/osx/webview_webkit.mm

index 958b5695b5f8429d5de62af3f4a6b1e2042e8b0f..373439dd74c23b43f740c700ff632c69fa3d14c9 100644 (file)
@@ -114,7 +114,7 @@ public:
     void RunScript(const wxString& javascript);
     
     //Virtual Filesystem Support
-    virtual void RegisterHandler(wxSharedPtr<wxWebHandler> WXUNUSED(handler)) {};
+    virtual void RegisterHandler(wxSharedPtr<wxWebHandler> handler);
 
     // ---- methods not from the parent (common) interface
     bool  CanGetPageSource();
index 9635069f2c411d7964819c7e31346709a6abb522..f3efc4e2e542fce0d092ef3b06031d57fc3ff547 100644 (file)
@@ -26,6 +26,8 @@
 
 #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>
@@ -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<wxWebHandler>, 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<wxWebHandler> 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<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