|
Lines 20-28
a/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.cpp_sec1
|
| 20 |
#include "config.h" |
20 |
#include "config.h" |
| 21 |
#include "WebKitUserContentManager.h" |
21 |
#include "WebKitUserContentManager.h" |
| 22 |
|
22 |
|
|
|
23 |
#include "WebKitJavascriptResult.h" |
| 24 |
#include "WebKitJavascriptResultPrivate.h" |
| 23 |
#include "WebKitPrivate.h" |
25 |
#include "WebKitPrivate.h" |
| 24 |
#include "WebKitUserContentManagerPrivate.h" |
26 |
#include "WebKitUserContentManagerPrivate.h" |
| 25 |
#include "WebKitUserContentPrivate.h" |
27 |
#include "WebKitUserContentPrivate.h" |
|
|
28 |
#include "WebKitWebContextPrivate.h" |
| 29 |
#include "WebScriptMessageHandler.h" |
| 30 |
#include "WebSerializedScriptValue.h" |
| 26 |
#include <wtf/gobject/GRefPtr.h> |
31 |
#include <wtf/gobject/GRefPtr.h> |
| 27 |
|
32 |
|
| 28 |
using namespace WebCore; |
33 |
using namespace WebCore; |
|
Lines 59-66
struct _WebKitUserContentManagerPrivate {
a/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.cpp_sec2
|
| 59 |
|
64 |
|
| 60 |
WEBKIT_DEFINE_TYPE(WebKitUserContentManager, webkit_user_content_manager, G_TYPE_OBJECT) |
65 |
WEBKIT_DEFINE_TYPE(WebKitUserContentManager, webkit_user_content_manager, G_TYPE_OBJECT) |
| 61 |
|
66 |
|
| 62 |
static void webkit_user_content_manager_class_init(WebKitUserContentManagerClass*) |
67 |
enum { |
|
|
68 |
SCRIPT_MESSAGE_RECEIVED, |
| 69 |
|
| 70 |
LAST_SIGNAL |
| 71 |
}; |
| 72 |
|
| 73 |
static guint signals[LAST_SIGNAL] = { 0, }; |
| 74 |
|
| 75 |
static void webkit_user_content_manager_class_init(WebKitUserContentManagerClass* klass) |
| 63 |
{ |
76 |
{ |
|
|
77 |
GObjectClass* gObjectClass = G_OBJECT_CLASS(klass); |
| 78 |
|
| 79 |
/** |
| 80 |
* WebKitUserContentManager::script-message-received: |
| 81 |
* @manager: the #WebKitUserContentManager |
| 82 |
* @js_result: the #WebKitJavascriptResult holding the value received |
| 83 |
* from the JavaScript world. |
| 84 |
* |
| 85 |
* This signal is emitted when JavaScript in a web view calls |
| 86 |
* <code>window.webkit.<name>.postMessage()</code>, after registering |
| 87 |
* <code><name></code> using |
| 88 |
* webkit_user_content_manager_register_script_message_handler() |
| 89 |
* |
| 90 |
* Since: 2.6 |
| 91 |
*/ |
| 92 |
signals[SCRIPT_MESSAGE_RECEIVED] = |
| 93 |
g_signal_new( |
| 94 |
"script-message-received", |
| 95 |
G_TYPE_FROM_CLASS(gObjectClass), |
| 96 |
static_cast<GSignalFlags>(G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED), |
| 97 |
0, nullptr, nullptr, |
| 98 |
g_cclosure_marshal_VOID__BOXED, |
| 99 |
G_TYPE_NONE, 1, |
| 100 |
WEBKIT_TYPE_JAVASCRIPT_RESULT); |
| 64 |
} |
101 |
} |
| 65 |
|
102 |
|
| 66 |
/** |
103 |
/** |
|
Lines 109-114
void webkit_user_content_manager_remove_all_style_sheets(WebKitUserContentManage
a/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.cpp_sec3
|
| 109 |
manager->priv->userContentController->removeAllUserStyleSheets(); |
146 |
manager->priv->userContentController->removeAllUserStyleSheets(); |
| 110 |
} |
147 |
} |
| 111 |
|
148 |
|
|
|
149 |
class ScriptMessageClientGtk final : public WebScriptMessageHandler::Client { |
| 150 |
public: |
| 151 |
ScriptMessageClientGtk(WebKitUserContentManager* manager, const gchar* handlerName) |
| 152 |
: m_handlerName(g_quark_from_string(handlerName)) |
| 153 |
, m_manager(manager) |
| 154 |
{ |
| 155 |
} |
| 156 |
|
| 157 |
virtual void didPostMessage(WebPageProxy& page, WebFrameProxy&, WebCore::SerializedScriptValue& serializedScriptValue) |
| 158 |
{ |
| 159 |
auto jsResult = adoptGRef(webkitJavascriptResultCreate(WEBKIT_WEB_VIEW(page.viewWidget()), serializedScriptValue)); |
| 160 |
g_signal_emit(m_manager.get(), signals[SCRIPT_MESSAGE_RECEIVED], m_handlerName, jsResult.get()); |
| 161 |
} |
| 162 |
|
| 163 |
virtual ~ScriptMessageClientGtk() { } |
| 164 |
|
| 165 |
private: |
| 166 |
GQuark m_handlerName; |
| 167 |
GRefPtr<WebKitUserContentManager> m_manager; |
| 168 |
}; |
| 169 |
|
| 170 |
/** |
| 171 |
* webkit_user_content_manager_register_script_message_handler: |
| 172 |
* @manager: A #WebKitUserContentManager |
| 173 |
* @name: Name of the script message channel |
| 174 |
* |
| 175 |
* Registers a new user script message handler name. After it is |
| 176 |
* registered, scripts can use `window.webkit.<name>.postMessage(value)` |
| 177 |
* to send messages. Those messages are received by connecting handlers |
| 178 |
* to the WebKitUserContentManager::script-message-received signal. The |
| 179 |
* handler name is used as the detail of the signal. To avoid race |
| 180 |
* conditions between registering the handler name, and starting to |
| 181 |
* receive the signals, it is recommended to connect to the signal |
| 182 |
* *before* registering the handler name: |
| 183 |
* |
| 184 |
* <informalexample><programlisting> |
| 185 |
* WebKitWebView *view = webkit_web_view_new (); |
| 186 |
* WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager (); |
| 187 |
* g_signal_connect (manager, "script-message-received::foobar", |
| 188 |
* G_CALLBACK (handle_script_message), NULL); |
| 189 |
* webkit_user_content_manager_register_script_message_handler (manager, "foobar"); |
| 190 |
* </programlisting></informalexample> |
| 191 |
* |
| 192 |
* Registering a script message handler will fail if the requested |
| 193 |
* name has been already registered before. |
| 194 |
* |
| 195 |
* Returns: Whether the message channel was registered successfully. |
| 196 |
* |
| 197 |
* Since: 2.6 |
| 198 |
*/ |
| 199 |
gboolean webkit_user_content_manager_register_script_message_handler(WebKitUserContentManager* manager, const gchar* name) |
| 200 |
{ |
| 201 |
g_return_val_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(manager), FALSE); |
| 202 |
g_return_val_if_fail(name, FALSE); |
| 203 |
|
| 204 |
const auto handler_name = String::fromUTF8(name); |
| 205 |
auto handler = WebScriptMessageHandler::create(std::make_unique<ScriptMessageClientGtk>(manager, name), handler_name); |
| 206 |
return manager->priv->userContentController->addUserScriptMessageHandler(handler.get()); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* webkit_user_content_manager_unregister_script_message_handler: |
| 211 |
* @manager: A #WebKitUserContentManager |
| 212 |
* @name: Name of the script message channel |
| 213 |
* |
| 214 |
* Unregisters a previously registered message handler name. |
| 215 |
* |
| 216 |
* Note that this does *not* disconnect handlers for the |
| 217 |
* WebKitUserContentManager::script-message-received signal: |
| 218 |
* the signal handlers will be kept connected, but the signal |
| 219 |
* will not be emitted unless the handler name is registered |
| 220 |
* again. |
| 221 |
* |
| 222 |
* See: webkit_user_content_manager_register_script_message_handler() |
| 223 |
* |
| 224 |
* Since: 2.6 |
| 225 |
*/ |
| 226 |
void webkit_user_content_manager_unregister_script_message_handler(WebKitUserContentManager* manager, const gchar* name) |
| 227 |
{ |
| 228 |
g_return_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(manager)); |
| 229 |
manager->priv->userContentController->removeUserMessageHandlerForName(String::fromUTF8(name)); |
| 230 |
} |
| 231 |
|
| 112 |
WebUserContentControllerProxy* webkitUserContentManagerGetUserContentControllerProxy(WebKitUserContentManager* manager) |
232 |
WebUserContentControllerProxy* webkitUserContentManagerGetUserContentControllerProxy(WebKitUserContentManager* manager) |
| 113 |
{ |
233 |
{ |
| 114 |
return manager->priv->userContentController.get(); |
234 |
return manager->priv->userContentController.get(); |