修改虚拟方法参数值而不扩展类C ++

时间:2016-07-08 15:35:53

标签: c++ chromium-embedded

我认为问题是我如何设置referrer方法。我认为这个函数被正确调用了。

SimpleHandler类:

对我来说最重要的是以下两项功能:

 // 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

1 个答案:

答案 0 :(得分:0)

应该有实现CefClient接口的类。该类应该重写方法

///
// Return the handler for browser request events.
///
/*--cef()--*/
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() {
  return NULL;
}

GetRequestHandler应该使用您已编写的覆盖OnBeforeResourceLoad方法返回实际的CefRequestHandler。

它看起来像这样:

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。你可以在那里添加代码。