Browser

Native webview using system browser.

Header#include "nativeui/browser.h"
Namespacenamespace nu
Typeclass (RefCounted)
InheritsView

Using Browser requires relatively new operating systems, for macOS the minimum version required is 10.10, for Linux it is required to install the webkit2gtk library with at least version 2.8.

On Linux due to poor support of hardware acceleration, the browser may fail to show anything, when that happens you can set WEBKIT_DISABLE_COMPOSITING_MODE environment variable to disable hardware acceleration.

Constructors

Browser(const Browser::Options& options)

Create a new browser view.

Parameters

Class methods

bool RegisterProtocol(const std::string& scheme, const std::function<ProtocolJob*(const std::string&)>& handler)

Register a custom protocol with scheme and handler.

When the browser sends a request with scheme, the handler will be called with handler(url), and the handler must return an instance of class that inherits from ProtocolJob.

The handler is guaranteed to be called in the main thread.

Parameters

Return

bool

void UnregisterProtocol(const std::string& scheme);

Unregister the custom protocol with scheme.

Parameters

Class properties

const char* kClassName

The class name of this view.

Methods

void LoadURL(const std::string& url)

Load the URL.

Parameters

void LoadHTML(const std::string& html, const std::string& baseurl)

Set the webpage contents and base URL.

Parameters

std::string GetURL()

Return current URL.

Return

std::string

std::string GetTitle()

Return the title of document.

Return

std::string

void SetUserAgent(const std::string& user_agent)

Change browser's user agent.

On Windows, due to Internet Explorer's limitations, calling SetUserAgent would change all web pages' user agents in current process.

Parameters

void ExecuteJavaScript(const std::string& code, const std::function<void(bool, base::Value)>& callback);

Evaluate code in browser and get the evaluated result.

The callback will be called with callback(success, result), the result argument is a generic value that created from the result of code.

Note that due to limitations of system toolkits, the execution may fail if the result of code can not be fully converted to JSON.

Parameters

void GoBack()

Navigate to the back item in the back-forward list.

bool CanGoBack() const

Return whether there is a back item in the back-forward list that can be navigated to.

Return

bool

void GoForward()

Navigate to the forward item in the back-forward list.

bool CanGoForward() const

Return whether there is a forward item in the back-forward list that can be navigated to.

Return

bool

void Reload()

Reload current page.

void Stop()

Stop loading all resources on the current page.

bool IsLoading() const

Return whether current page is loading content.

Return

bool

void SetBindingName(const std::string& name)

Set the name of object which would have the native bindings.

By default native bindings are added to the window object, by calling this API, native bindings will be added to the window[name] object.

Parameters

void AddBinding(const std::string& name, const std::function<void(...)>& func)

Add a native binding to web page with name.

You can pass an arbitrary function as long as the parameters can be converted from base::Value, othewise compilation error would happen.

For example, following native binding:

browser->AddBinding("AddRecord", [](const std::string&key, int value) {
});

accepts following calls:

window.addRecord('Books', 4);
window.addRecord('Videos', 8);

You can also use base::Value directly to accept arbitrary argument:

browser->AddBinding("AddRecord2", [](const std::string&key, base::Value value) {
})
window.addRecord2('PI', 3.14);
window.addRecord2('The Best Animal', 'Panda');

Note that only functors, function pointers, std::function and captureless labmda functions are accepted in AddBinding. Labmda functions with captures can not have their types deduced automatically, so you have to convert them to std::function first .

Parameters

void AddRawBinding(const std::string& name, const std::function<void(Browser*, base::Value)>& func)

Add a raw handler to web page with name.

The func will be called with a list of arguments passed from JavaScript.

Parameters

void RemoveBinding(const std::string& name)

Remove the native binding with name.

Parameters

Events

void on_close(Browser* self)

Emitted when the web page requests to close.

Parameters

Preventable

No.

void on_update_command(Browser* self)

Emitted when the back-forward list has changed.

Parameters

Preventable

No.

void on_change_loading(Browser* self)

Emitted when the browser starts or stops loading content.

Parameters

Preventable

No.

void on_update_title(Browser* self, const std::string& title)

Emitted when document's title is changed.

Parameters

Preventable

No.

void on_start_navigation(Browser* self, const std::string& url)

Emitted when the browser begins provisional navigation.

Parameters

Preventable

No.

void on_commit_navigation(Browser* self, const std::string& url)

Emitted when the browser begins to receive web content.

Parameters

Preventable

No.

void on_finish_navigation(Browser* self, const std::string& url)

Emitted when the navigation is complete.

Parameters

Preventable

No.

void on_fail_navigation(Browser* self, const std::string& url, int code)

Emitted when the navigation fails.

Parameters

Preventable

No.