weolar / miniblink49

a lighter, faster browser kernel of blink to integrate HTML UI in your app. 一个小巧、轻量的浏览器内核,用来取代wke和libcef
Apache License 2.0
7.09k stars 1.06k forks source link

How implement DownloadByPath? #526

Closed ronaldcamacho closed 2 years ago

ronaldcamacho commented 2 years ago

I put path but dont works properly What is the path formar thanks very much

weolar commented 2 years ago
struct DownInfo {
    std::string url;
    size_t recvSize;
    size_t allSize;
};

void WKE_CALL_TYPE onNetJobDataRecvCallback(void* ptr, wkeNetJob job, const char* data, int length)
{
    DownInfo* info = (DownInfo*)ptr;
    info->recvSize += length;

    char* output = (char*)malloc(0x1000);
    sprintf_s(output, 0x990, "onNetJobDataRecvCallback: %d %d, %f\n", info->allSize, info->recvSize, info->recvSize / (info->allSize + 1.0));
    OutputDebugStringA(output);
    free(output);
}

unsigned int __stdcall msgBoxThread(void* param)
{
    std::function<void(void)>* callback = (std::function<void(void)>*)param;
    (*callback)();
    delete callback;
    return 0;
}

void WKE_CALL_TYPE onNetJobDataFinishCallback(void* ptr, wkeNetJob job, wkeLoadingResult result)
{
    OutputDebugStringA("onNetJobDataFinishCallback\n");

    DownInfo* info = (DownInfo*)ptr;
    std::string url = info->url;

    wchar_t temp[20] = { 0 };
    wsprintf(temp, L"%d", info->recvSize);

    std::wstring* title = new std::wstring(utf8ToUtf16(url));
    *title += L" 下载完成:";
    *title += temp;

    delete info;

    std::function<void(void)>* callback = new std::function<void(void)>([title, result] {
        LPCWSTR lpCaption = (result == WKE_LOADING_SUCCEEDED ? L"下载成功" : L"下载失败");
        MessageBoxW(nullptr, title->c_str(), lpCaption, MB_OK);
        delete title;
        });

    unsigned int threadId = 0; // 为了不卡blink线程,messagbox放到另外个线程弹出
    HANDLE threadHandle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0, msgBoxThread, callback, 0, &threadId));
    ::CloseHandle(threadHandle);
}

static wkeDownloadOpt WKE_CALL_TYPE onDownloadCallback(wkeWebView webView,
    void* param,
    size_t expectedContentLength,
    const char* url,
    const char* mime,
    const char* disposition,
    wkeNetJob job,
    wkeNetJobDataBind* dataBind)
{
    char* output = (char*)malloc(0x8000);
    sprintf_s(output, 0x7990, "onDownloadCallback: %d %s\n", expectedContentLength, url);
    OutputDebugStringA(output);
    free(output);

    DownInfo* info = new DownInfo();
    info->url = url;
    info->recvSize = 0;
    info->allSize = expectedContentLength;

    wkeDownloadBind bind = { 0 };
    bind.param = info;
    bind.recvCallback = onNetJobDataRecvCallback;
    bind.finishCallback = onNetJobDataFinishCallback;
    bind.saveNameCallback = nullptr;

    return wkePopupDialogAndDownload(webView, param, expectedContentLength, url, mime, disposition, job, dataBind, &bind);
    //return mbDownloadByPath(webView, param, L"P:\\", expectedContentLength, url, mime, disposition, job, dataBind, &bind);
}

wkeOnDownload2(webWindow, onDownloadCallback, nullptr);

ronaldcamacho commented 2 years ago

Thanks very much

ronaldcamacho commented 2 years ago

Hi again. Currently, I'm using this code within mbOnDownloadInBlinkThread. I execute a few things from Visual Basic 6. When I try to download from the IDE it works fine. However, once I complie the project and try to download from the executable version my application crash. My guessing is that probably another thread is created additionally to the main one. Could you help try to figure out how it is working under the hood?

weolar commented 2 years ago

mbOnDownloadInBlinkThread is vip api, callback on blink(js) thread. You should use wkeOnDownload2

ronaldcamacho commented 2 years ago

'mbOnDownloadInBlinkThread is vip api' On VB6 IDE works properly, how i can implement this in build version, wkeInitializeEx is not stdcall library, show this error bad dll calling convention. image

ronaldcamacho commented 2 years ago

if i buy vip can works on compiled app, how much this, send me info please.

ronaldcamacho commented 2 years ago

Hi again , Thank very much i can implement your wke code, but only one thing, when file is downloaded web view is frozen, i solve destroy an recreate this, Do Exists other solution? Thanks very much