我认为问题是我如何设置引荐来源网址方法。我认为该函数被正确调用。
简单处理程序类:
对于我来说,重要的是以下 2 个功能:
// Return the handler for browser request events.
///
/*--cef()--*/
CefRefPtr<CefRequestHandler> GetRequestHandler() override {
return this;
}
virtual CefRequestHandler::ReturnValue OnBeforeResourceLoad(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
CefRefPtr<CefRequestCallback> callback) OVERRIDE;
如果以下课程中的上述任一内容有问题,请告诉我。
class SimpleHandler : public CefClient,
public CefDisplayHandler,
public CefLifeSpanHandler,
public CefLoadHandler,
public CefRequestHandler{
public:
explicit SimpleHandler(bool use_views);
~SimpleHandler();
// Provide access to the single global instance of this object.
static SimpleHandler* GetInstance();
// CefClient methods:
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE {
return this;
}
// CefDisplayHandler methods:
virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) OVERRIDE;
// CefLifeSpanHandler methods:
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
// CefLoadHandler methods:
virtual void OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl) OVERRIDE;
// Request that all existing browser windows close.
void CloseAllBrowsers(bool force_close);
bool IsClosing() const { return is_closing_; }
virtual CefRequestHandler::ReturnValue OnBeforeResourceLoad(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
CefRefPtr<CefRequestCallback> callback) OVERRIDE;
private:
// Platform-specific implementation.
void PlatformTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title);
// True if the application is using the Views framework.
const bool use_views_;
// List of existing browser windows. Only accessed on the CEF UI thread.
typedef std::list<CefRefPtr<CefBrowser> > BrowserList;
BrowserList browser_list_;
bool is_closing_;
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(SimpleHandler);
};
#endif // CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_
我如何调用OnBeforeResourceLoad函数(从simple_handler.cc文件):
CefRequestHandler::ReturnValue SimpleHandler::OnBeforeResourceLoad(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
CefRefPtr<CefRequestCallback> callback)
{
CefRequest::ReferrerPolicy origin = REFERRER_POLICY_ALWAYS;
request->SetReferrer("www.google.com",origin);
return RV_CONTINUE;
}
我用来修改行为的 setpreferrer 方法:
///
// Set the referrer URL and policy. If non-empty the referrer URL must be
// fully qualified with an HTTP or HTTPS scheme component. Any username,
// password or ref component will be removed.
///
/*--cef()--*/
virtual void SetReferrer(const CefString& referrer_url,
ReferrerPolicy policy) =0;
包含 SetReferrer 方法的类。
[1]: http://magpcss.org/ceforum/apidocs/projects/%28default%29/cef_request_handler.h.html
应该有实现 CefClient 接口的类。该类应重写方法
///
// Return the handler for browser request events.
///
/*--cef()--*/
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() {
return NULL;
}
GetRequestHandler 应该返回实际的 CefRequestHandler,其中包含您已经编写的覆盖的 OnBeforeResourceLoad 方法。
它可能看起来像这样:
class MyCefClient : public CefClient, public CefRequestHandler {
public:
CefRefPtr<CefRequestHandler> GetRequestHandler() override {
return this;
}
ReturnValue OnBeforeResourceLoad(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
CefRefPtr<CefRequestCallback> callback) override {
// write your code here
return RV_CONTINUE;
}
};
在 CefSimple 示例中有一个类 SimpleHandler。您可以在上面添加代码。