1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/*
* Copyright (C) 2013 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebKitScriptWorld.h"
#include "WebKitScriptWorldPrivate.h"
#include <wtf/HashMap.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/glib/WTFGType.h>
using namespace WebKit;
using namespace WebCore;
enum {
WINDOW_OBJECT_CLEARED,
LAST_SIGNAL
};
typedef HashMap<InjectedBundleScriptWorld*, WebKitScriptWorld*> ScriptWorldMap;
static ScriptWorldMap& scriptWorlds()
{
static NeverDestroyed<ScriptWorldMap> map;
return map;
}
struct _WebKitScriptWorldPrivate {
~_WebKitScriptWorldPrivate()
{
ASSERT(scriptWorlds().contains(scriptWorld.get()));
scriptWorlds().remove(scriptWorld.get());
}
RefPtr<InjectedBundleScriptWorld> scriptWorld;
CString name;
};
static std::array<unsigned, LAST_SIGNAL> signals;
WEBKIT_DEFINE_FINAL_TYPE(WebKitScriptWorld, webkit_script_world, G_TYPE_OBJECT, GObject)
static void webkit_script_world_class_init(WebKitScriptWorldClass* klass)
{
/**
* WebKitScriptWorld::window-object-cleared:
* @world: the #WebKitScriptWorld on which the signal is emitted
* @page: a #WebKitWebPage
* @frame: the #WebKitFrame to which @world belongs
*
* Emitted when the JavaScript window object in a #WebKitScriptWorld has been
* cleared. This is the preferred place to set custom properties on the window
* object using the JavaScriptCore API. You can get the window object of @frame
* from the JavaScript execution context of @world that is returned by
* webkit_frame_get_js_context_for_script_world().
*
* Since: 2.2
*/
signals[WINDOW_OBJECT_CLEARED] = g_signal_new(
"window-object-cleared",
G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_LAST,
0, nullptr, nullptr,
g_cclosure_marshal_generic,
G_TYPE_NONE, 2,
WEBKIT_TYPE_WEB_PAGE,
WEBKIT_TYPE_FRAME);
}
WebKitScriptWorld* webkitScriptWorldGet(InjectedBundleScriptWorld* scriptWorld)
{
return scriptWorlds().get(scriptWorld);
}
InjectedBundleScriptWorld* webkitScriptWorldGetInjectedBundleScriptWorld(WebKitScriptWorld* world)
{
return world->priv->scriptWorld.get();
}
void webkitScriptWorldWindowObjectCleared(WebKitScriptWorld* world, WebKitWebPage* page, WebKitFrame* frame)
{
g_signal_emit(world, signals[WINDOW_OBJECT_CLEARED], 0, page, frame);
}
static WebKitScriptWorld* webkitScriptWorldCreate(Ref<InjectedBundleScriptWorld>&& scriptWorld)
{
WebKitScriptWorld* world = WEBKIT_SCRIPT_WORLD(g_object_new(WEBKIT_TYPE_SCRIPT_WORLD, nullptr));
world->priv->scriptWorld = WTFMove(scriptWorld);
world->priv->name = world->priv->scriptWorld->name().utf8();
ASSERT(!scriptWorlds().contains(world->priv->scriptWorld.get()));
scriptWorlds().add(world->priv->scriptWorld.get(), world);
return world;
}
static gpointer createDefaultScriptWorld(gpointer)
{
return webkitScriptWorldCreate(InjectedBundleScriptWorld::normalWorldSingleton());
}
/**
* webkit_script_world_get_default:
*
* Get the default #WebKitScriptWorld. This is the normal script world
* where all scripts are executed by default.
* You can get the JavaScript execution context of a #WebKitScriptWorld
* for a given #WebKitFrame with webkit_frame_get_javascript_context_for_script_world().
*
* Returns: (transfer none): the default #WebKitScriptWorld
*
* Since: 2.2
*/
WebKitScriptWorld* webkit_script_world_get_default(void)
{
static GOnce onceInit = G_ONCE_INIT;
return WEBKIT_SCRIPT_WORLD(g_once(&onceInit, createDefaultScriptWorld, 0));
}
/**
* webkit_script_world_new:
*
* Creates a new isolated #WebKitScriptWorld. Scripts executed in
* isolated worlds have access to the DOM but not to other variable
* or functions created by the page.
* The #WebKitScriptWorld is created with a generated unique name. Use
* webkit_script_world_new_with_name() if you want to create it with a
* custom name.
* You can get the JavaScript execution context of a #WebKitScriptWorld
* for a given #WebKitFrame with webkit_frame_get_javascript_context_for_script_world().
*
* Returns: (transfer full): a new isolated #WebKitScriptWorld
*
* Since: 2.2
*/
WebKitScriptWorld* webkit_script_world_new(void)
{
return webkitScriptWorldCreate(InjectedBundleScriptWorld::create(InjectedBundleScriptWorld::Type::User));
}
/**
* webkit_script_world_new_with_name:
* @name: a name for the script world
*
* Creates a new isolated #WebKitScriptWorld with a name. Scripts executed in
* isolated worlds have access to the DOM but not to other variable
* or functions created by the page.
* You can get the JavaScript execution context of a #WebKitScriptWorld
* for a given #WebKitFrame with webkit_frame_get_javascript_context_for_script_world().
*
* Returns: (transfer full): a new isolated #WebKitScriptWorld
*
* Since: 2.22
*/
WebKitScriptWorld* webkit_script_world_new_with_name(const char* name)
{
g_return_val_if_fail(name, nullptr);
return webkitScriptWorldCreate(InjectedBundleScriptWorld::create(String::fromUTF8(name), InjectedBundleScriptWorld::Type::User));
}
/**
* webkit_script_world_get_name:
* @world: a #WebKitScriptWorld
*
* Get the name of a #WebKitScriptWorld.
*
* Returns: the name of @world
*
* Since: 2.22
*/
const char* webkit_script_world_get_name(WebKitScriptWorld* world)
{
g_return_val_if_fail(WEBKIT_IS_SCRIPT_WORLD(world), nullptr);
return world->priv->name.data();
}
|