Search code examples
c++chromium-embedded

Modifying Virtual Method parameter value without extending the class C++


I think the issue is how I am setting the referrer method. The function is being called properly I think.

SimpleHandler class:

What matters for my concerns are the below 2 functions:

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

If there are issue with either of the above in the below class please let me know.

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_

How I call the OnBeforeResourceLoad function (from a simple_handler.cc file):

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;

}

The setpreferrer method I am using to modify behaviour:

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

Class containing the SetReferrer method.

  [1]: http://magpcss.org/ceforum/apidocs/projects/%28default%29/cef_request_handler.h.html

Solution

  • There should be class implementing CefClient interface. That class should override method

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

    GetRequestHandler should return actual CefRequestHandler with overriden OnBeforeResourceLoad method which you already wrote.

    It can look like this:

    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;
      }  
    };
    

    There is a class SimpleHandler in the CefSimple example. You can add code above there.