学习cef

#include "custom_resource_handler.h"
#include "include/cef_parser.h"
#include <regex>

CustomResourceHandler::CustomResourceHandler() {

}

bool CustomResourceHandler::Open(CefRefPtr<CefRequest> request,
    bool& handle_request,
    CefRefPtr<CefCallback> callback) {
    CefString url = request->GetURL();
    cef_uri_unescape_rule_t unescape_rule = UU_SPACES;
    CefString& decoded_value = CefURIDecode(url, true, unescape_rule);
    CefURLParts urlParts;
    if (CefParseURL(url, urlParts)) {
        CefString filePath(&urlParts.path);
        std::wstring wfilepath = CefURIDecode(CefString(&urlParts.query), true, unescape_rule);
        
        cef_string_userfree_utf8_t output = cef_string_userfree_utf8_alloc();
        cef_string_utf16_to_utf8(filePath.c_str(), filePath.length(), output);
        std::string filepath(output->str, output->length);
        cef_string_userfree_utf8_free(output);

        //std::regex pattern("//resources");
        //std::string fulpath = std::regex_replace(filepath, pattern, "./resources");

        //std::wstring str = std::regex_replace(output, pattern, L"");
        //CefRefPtr<CefStreamReader> fileStream = CefStreamReader::CreateForFile(CefString(filePath));
        //fileStream->Read()

        std::string::size_type pos = filepath.rfind('.');
        extension_ = filepath.substr(pos == std::string::npos ? filepath.length() : pos + 1);

        char filename_c[256];
        snprintf(filename_c, sizeof(filename_c), "%s", ("./resources"+filepath).c_str());
        FILE* pf = fopen(filename_c, "r");
        if (pf) {
            readpos_ = 0;
            datalen_ = fread(databuf_, sizeof(char), sizeof(databuf_), pf);
            fclose(pf);
            callback->Continue();
            return true;
        }
    }

    return false;
}

void CustomResourceHandler::GetResponseHeaders(CefRefPtr<CefResponse> response,
    int64_t& response_length,
    CefString& redirectUrl) {
    
    CefString mimeType = CefGetMimeType(extension_);
    response->SetMimeType(mimeType);
    response->SetStatus(200);
    //response->SetHeaderByName("Access-Control-Allow-Origin", "*", true);
    //response->SetHeaderByName("Access-Control-Allow-Methods", "POST,PUT,GET,OPTIONS,DELETE", true);
    response_length = datalen_;
}

void CustomResourceHandler::Cancel() {
    // Cancel the response...
}

bool CustomResourceHandler::Read(void* data_out,
    int bytes_to_read,
    int& bytes_read,
    CefRefPtr<CefResourceReadCallback> callback) {
    int remain = datalen_ - readpos_;
    int readlen = bytes_to_read > remain ? remain : bytes_to_read;
    memcpy(data_out, databuf_ + readpos_, readlen);
    readpos_ += readlen;
    bytes_read = readlen;
    return remain > 0 ? true : false;
}

serve content in CEF without http server
https://bbonczek.github.io/jekyll/update/2018/04/24/serve-content-in-cef-without-http-server.html

在cef中使用自定义协议(scheme)
https://www.cnblogs.com/sinceret/p/10417941.html

Registering custom schema handlers.
https://www.magpcss.org/ceforum/viewtopic.php?f=6&t=18259

Chromium Embedded Framework (CEF) Automated Builds
https://cef-builds.spotifycdn.com/index.html

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值