void WebFrameLoaderClient::dispatchWillSendRequest(DocumentLoader* loader, unsigned long identifier, ResourceRequest& request, const ResourceResponse& redirectResponse)
{
#if ENABLE (ORIGYNSUITE)
    OrigynServer::get()->redirectRequest(request);
#else
    WebView* webView = m_webFrame->webView();
    SharedPtr<WebResourceLoadDelegate> resourceLoadDelegate = webView->webResourceLoadDelegate();
    if (!resourceLoadDelegate)
        return;

    WebMutableURLRequest* webURLRequest = WebMutableURLRequest::createInstance(request);
    WebURLResponse* webURLRedirectResponse = WebURLResponse::createInstance(redirectResponse);

    WebMutableURLRequest* newWebURLRequest = resourceLoadDelegate->willSendRequest(webView, identifier, webURLRequest, webURLRedirectResponse, getWebDataSource(loader));
    delete webURLRedirectResponse; 

    if (webURLRequest == newWebURLRequest) {
        delete webURLRequest;
        return;
    }

    if (!newWebURLRequest) {
        request = ResourceRequest();
        delete webURLRequest;
        return;
    }

    delete webURLRequest;

    request = newWebURLRequest->resourceRequest();
#endif
}
void WebFrameLoaderClient::dispatchDidFinishLoading(DocumentLoader* loader, unsigned long identifier)
{
    WebView* webView = m_webFrame->webView();
    SharedPtr<WebResourceLoadDelegate> resourceLoadDelegate = webView->webResourceLoadDelegate();
    if (!resourceLoadDelegate)
        return;

    resourceLoadDelegate->didFinishLoadingFromDataSource(webView, identifier, getWebDataSource(loader));
}
void WebFrameLoaderClient::dispatchDidReceiveContentLength(DocumentLoader* loader, unsigned long identifier, int length)
{
    WebView* webView = m_webFrame->webView();
    SharedPtr<WebResourceLoadDelegate> resourceLoadDelegate = webView->webResourceLoadDelegate();
    if (!resourceLoadDelegate)
        return;

    resourceLoadDelegate->didReceiveContentLength(webView, identifier, length, getWebDataSource(loader));
}
void WebFrameLoaderClient::dispatchDidFailLoading(DocumentLoader* loader, unsigned long identifier, const ResourceError& error)
{
    WebView* webView = m_webFrame->webView();
    SharedPtr<WebResourceLoadDelegate> resourceLoadDelegate = webView->webResourceLoadDelegate();
    if (!resourceLoadDelegate)
        return;

    WebError* webError = WebError::createInstance(error);
    resourceLoadDelegate->didFailLoadingWithError(webView, identifier, webError, getWebDataSource(loader));
    delete webError;
}
void WebFrameLoaderClient::dispatchDidCancelAuthenticationChallenge(DocumentLoader* loader, unsigned long identifier, const AuthenticationChallenge& challenge)
{
    WebView* webView = m_webFrame->webView();
    SharedPtr<WebResourceLoadDelegate> resourceLoadDelegate = webView->webResourceLoadDelegate();
    if (!resourceLoadDelegate)
        return;

    WebURLAuthenticationChallenge* webChallenge = WebURLAuthenticationChallenge::createInstance(challenge);
    resourceLoadDelegate->didCancelAuthenticationChallenge(webView, identifier, webChallenge, getWebDataSource(loader));
    delete webChallenge;
}
void WebFrameLoaderClient::assignIdentifierToInitialRequest(unsigned long identifier, DocumentLoader* loader, const ResourceRequest& request)
{
    WebView* webView = m_webFrame->webView();
    SharedPtr<WebResourceLoadDelegate> resourceLoadDelegate = webView->webResourceLoadDelegate();
    if (!resourceLoadDelegate)
        return;

    WebMutableURLRequest* webURLRequest = WebMutableURLRequest::createInstance(request);
    resourceLoadDelegate->identifierForInitialRequest(webView, webURLRequest, getWebDataSource(loader), identifier);
    delete webURLRequest;
}
void WebFrameLoaderClient::dispatchDidReceiveAuthenticationChallenge(DocumentLoader* loader, unsigned long identifier, const AuthenticationChallenge& challenge)
{
#if USE(CURL)
    ASSERT(challenge.authenticationClient());
#endif

    WebView* webView = m_webFrame->webView();
    SharedPtr<WebResourceLoadDelegate> resourceLoadDelegate = webView->webResourceLoadDelegate();
    if (resourceLoadDelegate) {
        WebURLAuthenticationChallenge* webChallenge = WebURLAuthenticationChallenge::createInstance(challenge);
        resourceLoadDelegate->didReceiveAuthenticationChallenge(webView, identifier, webChallenge, getWebDataSource(loader));
        delete webChallenge;
    }

#if USE(CURL)
    // If the ResourceLoadDelegate doesn't exist or fails to handle the call, we tell the ResourceHandle
    // to continue without credential - this is the best approximation of Mac behavior
    challenge.authenticationClient()->receivedRequestToContinueWithoutCredential(challenge);
#endif
}
void WebFrameLoaderClient::dispatchDidReceiveResponse(DocumentLoader* loader, unsigned long identifier, const ResourceResponse& response)
{
#if ENABLE(WIDGET_ENGINE)
    SharedPtr<WebWidgetEngineDelegate> widgetEngineDelegate = m_webFrame->webView()->webWidgetEngineDelegate();
    if (widgetEngineDelegate && loader->responseMIMEType() == "application/widget") {
        const char* url = widgetEngineDelegate->receiveWidget(strdup(loader->responseURL().string().utf8().data()), m_webFrame);
        loader->stopLoading();
        if (url)
            m_webFrame->loadURL(url);

        return;
    }
#endif

    WebView* webView = m_webFrame->webView();
    SharedPtr<WebResourceLoadDelegate> resourceLoadDelegate = webView->webResourceLoadDelegate();
    if (!resourceLoadDelegate)
        return;

    WebURLResponse* webURLResponse = WebURLResponse::createInstance(response);
    resourceLoadDelegate->didReceiveResponse(webView, identifier, webURLResponse, getWebDataSource(loader));
    delete webURLResponse;
}