namespace Xilium.CefGlue
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Xilium.CefGlue.Interop;
///
/// Class used to represent a browser window. When used in the browser process
/// the methods of this class may be called on any thread unless otherwise
/// indicated in the comments. When used in the render process the methods of
/// this class may only be called on the main thread.
///
public sealed unsafe partial class CefBrowser
{
///
/// Returns the browser host object. This method can only be called in the
/// browser process.
///
public CefBrowserHost GetHost()
{
return CefBrowserHost.FromNative(
cef_browser_t.get_host(_self)
);
}
///
/// Returns true if the browser can navigate backwards.
///
public bool CanGoBack
{
get { return cef_browser_t.can_go_back(_self) != 0; }
}
///
/// Navigate backwards.
///
public void GoBack()
{
cef_browser_t.go_back(_self);
}
///
/// Returns true if the browser can navigate forwards.
///
public bool CanGoForward
{
get { return cef_browser_t.can_go_forward(_self) != 0; }
}
///
/// Navigate forwards.
///
public void GoForward()
{
cef_browser_t.go_forward(_self);
}
///
/// Returns true if the browser is currently loading.
///
public bool IsLoading
{
get { return cef_browser_t.is_loading(_self) != 0; }
}
///
/// Reload the current page.
///
public void Reload()
{
cef_browser_t.reload(_self);
}
///
/// Reload the current page ignoring any cached data.
///
public void ReloadIgnoreCache()
{
cef_browser_t.reload_ignore_cache(_self);
}
///
/// Stop loading the page.
///
public void StopLoad()
{
cef_browser_t.stop_load(_self);
}
///
/// Returns the globally unique identifier for this browser. This value is also
/// used as the tabId for extension APIs.
///
public int Identifier
{
get { return cef_browser_t.get_identifier(_self); }
}
///
/// Returns true if this object is pointing to the same handle as |that|
/// object.
///
public bool IsSame(CefBrowser that)
{
if (that == null) return false;
return cef_browser_t.is_same(_self, that.ToNative()) != 0;
}
///
/// Returns true if the window is a popup window.
///
public bool IsPopup
{
get { return cef_browser_t.is_popup(_self) != 0; }
}
///
/// Returns true if a document has been loaded in the browser.
///
public bool HasDocument
{
get { return cef_browser_t.has_document(_self) != 0; }
}
///
/// Returns the main (top-level) frame for the browser window.
///
public CefFrame GetMainFrame()
{
return CefFrame.FromNativeOrNull(
cef_browser_t.get_main_frame(_self)
);
}
///
/// Returns the focused frame for the browser window.
///
public CefFrame GetFocusedFrame()
{
return CefFrame.FromNativeOrNull(
cef_browser_t.get_focused_frame(_self)
);
}
///
/// Returns the frame with the specified identifier, or NULL if not found.
///
public CefFrame GetFrame(long identifier)
{
return CefFrame.FromNativeOrNull(
cef_browser_t.get_frame_byident(_self, identifier)
);
}
///
/// Returns the frame with the specified name, or NULL if not found.
///
public CefFrame GetFrame(string name)
{
fixed (char* name_str = name)
{
var n_name = new cef_string_t(name_str, name.Length);
return CefFrame.FromNativeOrNull(
cef_browser_t.get_frame(_self, &n_name)
);
}
}
///
/// Returns the number of frames that currently exist.
///
public int FrameCount
{
get { return (int)cef_browser_t.get_frame_count(_self); }
}
///
/// Returns the identifiers of all existing frames.
///
public long[] GetFrameIdentifiers()
{
var frameCount = FrameCount;
var identifiers = new long[frameCount * 2];
UIntPtr n_count = (UIntPtr)frameCount;
fixed (long* identifiers_ptr = identifiers)
{
cef_browser_t.get_frame_identifiers(_self, &n_count, identifiers_ptr);
}
if ((int)n_count < 0)
{
throw new InvalidOperationException("Invalid number of frames.");
}
if ((int)n_count > identifiers.Length)
{
throw new InvalidOperationException("Number of returned frames are too big.");
}
Array.Resize(ref identifiers, (int)n_count);
return identifiers;
}
///
/// Returns the names of all existing frames.
///
public string[] GetFrameNames()
{
var list = libcef.string_list_alloc();
cef_browser_t.get_frame_names(_self, list);
var result = cef_string_list.ToArray(list);
libcef.string_list_free(list);
return result;
}
}
}