WebKitFindController.cpp [plain text]
#include "config.h"
#include "WebKitFindController.h"
#include "WebKitEnumTypes.h"
#include "WebKitPrivate.h"
#include "WebKitWebView.h"
#include "WebKitWebViewBasePrivate.h"
#include <glib/gi18n-lib.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/text/CString.h>
using namespace WebKit;
using namespace WebCore;
enum {
FOUND_TEXT,
FAILED_TO_FIND_TEXT,
COUNTED_MATCHES,
LAST_SIGNAL
};
enum {
PROP_0,
PROP_TEXT,
PROP_OPTIONS,
PROP_MAX_MATCH_COUNT,
PROP_WEB_VIEW
};
typedef enum {
FindOperation,
FindNextPrevOperation,
CountOperation
} WebKitFindControllerOperation;
struct _WebKitFindControllerPrivate {
CString searchText;
uint32_t findOptions;
unsigned maxMatchCount;
WebKitWebView* webView;
};
static guint signals[LAST_SIGNAL] = { 0, };
WEBKIT_DEFINE_TYPE(WebKitFindController, webkit_find_controller, G_TYPE_OBJECT)
static inline WebKit::FindOptions toWebFindOptions(uint32_t findOptions)
{
return static_cast<WebKit::FindOptions>((findOptions & WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE ? FindOptionsCaseInsensitive : 0)
| (findOptions & WEBKIT_FIND_OPTIONS_AT_WORD_STARTS ? FindOptionsAtWordStarts : 0)
| (findOptions & WEBKIT_FIND_OPTIONS_TREAT_MEDIAL_CAPITAL_AS_WORD_START ? FindOptionsTreatMedialCapitalAsWordStart : 0)
| (findOptions & WEBKIT_FIND_OPTIONS_BACKWARDS ? FindOptionsBackwards : 0)
| (findOptions & WEBKIT_FIND_OPTIONS_WRAP_AROUND ? FindOptionsWrapAround : 0));
}
static inline WebKitFindOptions toWebKitFindOptions(uint32_t findOptions)
{
return static_cast<WebKitFindOptions>((findOptions & FindOptionsCaseInsensitive ? WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE : 0)
| (findOptions & FindOptionsAtWordStarts ? WEBKIT_FIND_OPTIONS_AT_WORD_STARTS : 0)
| (findOptions & FindOptionsTreatMedialCapitalAsWordStart ? WEBKIT_FIND_OPTIONS_TREAT_MEDIAL_CAPITAL_AS_WORD_START : 0)
| (findOptions & FindOptionsBackwards ? WEBKIT_FIND_OPTIONS_BACKWARDS : 0)
| (findOptions & FindOptionsWrapAround ? WEBKIT_FIND_OPTIONS_WRAP_AROUND : 0));
}
static void didFindString(WKPageRef, WKStringRef, unsigned matchCount, const void* clientInfo)
{
g_signal_emit(WEBKIT_FIND_CONTROLLER(clientInfo), signals[FOUND_TEXT], 0, matchCount);
}
static void didFailToFindString(WKPageRef, WKStringRef, const void* clientInfo)
{
g_signal_emit(WEBKIT_FIND_CONTROLLER(clientInfo), signals[FAILED_TO_FIND_TEXT], 0);
}
static void didCountStringMatches(WKPageRef, WKStringRef, unsigned matchCount, const void* clientInfo)
{
g_signal_emit(WEBKIT_FIND_CONTROLLER(clientInfo), signals[COUNTED_MATCHES], 0, matchCount);
}
static inline WebPageProxy* getPage(WebKitFindController* findController)
{
return webkitWebViewBaseGetPage(reinterpret_cast<WebKitWebViewBase*>(findController->priv->webView));
}
static void webkitFindControllerConstructed(GObject* object)
{
WebKitFindController* findController = WEBKIT_FIND_CONTROLLER(object);
WKPageFindClientV0 wkFindClient = {
{
0, findController, },
didFindString,
didFailToFindString,
didCountStringMatches
};
WKPageSetPageFindClient(toAPI(getPage(findController)), &wkFindClient.base);
}
static void webkitFindControllerGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
{
WebKitFindController* findController = WEBKIT_FIND_CONTROLLER(object);
switch (propId) {
case PROP_TEXT:
g_value_set_string(value, webkit_find_controller_get_search_text(findController));
break;
case PROP_OPTIONS:
g_value_set_uint(value, webkit_find_controller_get_options(findController));
break;
case PROP_MAX_MATCH_COUNT:
g_value_set_uint(value, webkit_find_controller_get_max_match_count(findController));
break;
case PROP_WEB_VIEW:
g_value_set_object(value, webkit_find_controller_get_web_view(findController));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
}
}
static void webkitFindControllerSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)
{
WebKitFindController* findController = WEBKIT_FIND_CONTROLLER(object);
switch (propId) {
case PROP_WEB_VIEW:
findController->priv->webView = WEBKIT_WEB_VIEW(g_value_get_object(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
}
}
static void webkit_find_controller_class_init(WebKitFindControllerClass* findClass)
{
GObjectClass* gObjectClass = G_OBJECT_CLASS(findClass);
gObjectClass->constructed = webkitFindControllerConstructed;
gObjectClass->get_property = webkitFindControllerGetProperty;
gObjectClass->set_property = webkitFindControllerSetProperty;
g_object_class_install_property(gObjectClass,
PROP_TEXT,
g_param_spec_string("text",
_("Search text"),
_("Text to search for in the view"),
0,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gObjectClass,
PROP_OPTIONS,
g_param_spec_flags("options",
_("Search Options"),
_("Search options to be used in the search operation"),
WEBKIT_TYPE_FIND_OPTIONS,
WEBKIT_FIND_OPTIONS_NONE,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gObjectClass,
PROP_MAX_MATCH_COUNT,
g_param_spec_uint("max-match-count",
_("Maximum matches count"),
_("The maximum number of matches in a given text to report"),
0, G_MAXUINT, 0,
WEBKIT_PARAM_READABLE));
g_object_class_install_property(gObjectClass,
PROP_WEB_VIEW,
g_param_spec_object("web-view",
_("WebView"),
_("The WebView associated with this find controller"),
WEBKIT_TYPE_WEB_VIEW,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
signals[FOUND_TEXT] =
g_signal_new("found-text",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
0, 0, 0,
g_cclosure_marshal_VOID__UINT,
G_TYPE_NONE, 1, G_TYPE_UINT);
signals[FAILED_TO_FIND_TEXT] =
g_signal_new("failed-to-find-text",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
0, 0, 0,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[COUNTED_MATCHES] =
g_signal_new("counted-matches",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
0, 0, 0,
g_cclosure_marshal_VOID__UINT,
G_TYPE_NONE, 1, G_TYPE_UINT);
}
const char* webkit_find_controller_get_search_text(WebKitFindController* findController)
{
g_return_val_if_fail(WEBKIT_IS_FIND_CONTROLLER(findController), 0);
return findController->priv->searchText.data();
}
guint32 webkit_find_controller_get_options(WebKitFindController* findController)
{
g_return_val_if_fail(WEBKIT_IS_FIND_CONTROLLER(findController), WEBKIT_FIND_OPTIONS_NONE);
return toWebKitFindOptions(findController->priv->findOptions);
}
guint webkit_find_controller_get_max_match_count(WebKitFindController* findController)
{
g_return_val_if_fail(WEBKIT_IS_FIND_CONTROLLER(findController), 0);
return findController->priv->maxMatchCount;
}
WebKitWebView* webkit_find_controller_get_web_view(WebKitFindController* findController)
{
g_return_val_if_fail(WEBKIT_IS_FIND_CONTROLLER(findController), 0);
return findController->priv->webView;
}
static void webKitFindControllerPerform(WebKitFindController* findController, WebKitFindControllerOperation operation)
{
WebKitFindControllerPrivate* priv = findController->priv;
if (operation == CountOperation) {
getPage(findController)->countStringMatches(String::fromUTF8(priv->searchText.data()),
static_cast<WebKit::FindOptions>(priv->findOptions), priv->maxMatchCount);
return;
}
uint32_t findOptions = priv->findOptions;
if (operation == FindOperation)
findOptions |= FindOptionsShowHighlight;
getPage(findController)->findString(String::fromUTF8(priv->searchText.data()), static_cast<WebKit::FindOptions>(findOptions),
priv->maxMatchCount);
}
static inline void webKitFindControllerSetSearchData(WebKitFindController* findController, const gchar* searchText, guint32 findOptions, guint maxMatchCount)
{
findController->priv->searchText = searchText;
findController->priv->findOptions = findOptions;
findController->priv->maxMatchCount = maxMatchCount;
}
void webkit_find_controller_search(WebKitFindController* findController, const gchar* searchText, guint findOptions, guint maxMatchCount)
{
g_return_if_fail(WEBKIT_IS_FIND_CONTROLLER(findController));
g_return_if_fail(searchText);
webKitFindControllerSetSearchData(findController, searchText, toWebFindOptions(findOptions), maxMatchCount);
webKitFindControllerPerform(findController, FindOperation);
}
void webkit_find_controller_search_next(WebKitFindController* findController)
{
g_return_if_fail(WEBKIT_IS_FIND_CONTROLLER(findController));
findController->priv->findOptions &= ~FindOptionsBackwards;
findController->priv->findOptions &= ~FindOptionsShowHighlight;
webKitFindControllerPerform(findController, FindNextPrevOperation);
}
void webkit_find_controller_search_previous(WebKitFindController* findController)
{
g_return_if_fail(WEBKIT_IS_FIND_CONTROLLER(findController));
findController->priv->findOptions |= FindOptionsBackwards;
findController->priv->findOptions &= ~FindOptionsShowHighlight;
webKitFindControllerPerform(findController, FindNextPrevOperation);
}
void webkit_find_controller_count_matches(WebKitFindController* findController, const gchar* searchText, guint32 findOptions, guint maxMatchCount)
{
g_return_if_fail(WEBKIT_IS_FIND_CONTROLLER(findController));
g_return_if_fail(searchText);
webKitFindControllerSetSearchData(findController, searchText, toWebFindOptions(findOptions), maxMatchCount);
webKitFindControllerPerform(findController, CountOperation);
}
void webkit_find_controller_search_finish(WebKitFindController* findController)
{
g_return_if_fail(WEBKIT_IS_FIND_CONTROLLER(findController));
getPage(findController)->hideFindUI();
}