2 * Copyright (C) 2014 Ryuan Choi <ryuan.choi@gmail.com>. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "EWebKit_Extension.h"
27 #include <JavaScriptCore/JavaScript.h>
33 static JSValueRef helloCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
35 Ewk_Extension* extension = static_cast<Ewk_Extension*>(JSObjectGetPrivate(thisObject));
37 Eina_Value* value = eina_value_new(EINA_VALUE_TYPE_STRING);
38 eina_value_set(value, "Hello from extension");
39 ewk_extension_message_post(extension, "hello", value);
40 eina_value_free(value);
45 void loadFinished(Ewk_Page* page, void* data)
47 JSGlobalContextRef jsContext = ewk_page_js_global_context_get(page);
48 JSObjectRef windowObject = JSContextGetGlobalObject(jsContext);
50 static JSStaticFunction functions[] = {
51 { "hello", helloCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
54 static JSClassDefinition class_definition = {
55 0, kJSClassAttributeNone, "Test", 0, 0, functions,
56 0, 0/* */, 0, 0, 0, 0, 0, 0, 0, 0, 0
58 JSClassRef clasz = JSClassCreate(&class_definition);
59 JSObjectRef object = JSObjectMake(jsContext, clasz, data);
60 JSClassRelease(clasz);
62 JSObjectSetPrivate(object, data);
64 JSValueRef exception = 0;
65 JSStringRef property = JSStringCreateWithUTF8CString("test");
66 JSObjectSetProperty(jsContext, windowObject, property, object, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, &exception);
67 JSStringRelease(property);
70 static Ewk_Page_Client pageClient;
72 static void pageAdded(Ewk_Page* page, void* data)
74 pageClient.version = 1;
75 pageClient.data = data;
76 pageClient.load_finished = loadFinished;
78 ewk_page_client_register(page, &pageClient);
81 static void pageDelete(Ewk_Page* page, void*)
83 ewk_page_client_unregister(page, &pageClient);
86 static void messageReceived(const char* name, const Eina_Value* body, void* data)
88 Eina_Value* value = eina_value_new(EINA_VALUE_TYPE_STRING);
89 eina_value_set(value, "From extension");
90 ewk_extension_message_post(static_cast<Ewk_Extension*>(data), "pong", value);
91 eina_value_free(value);
94 void ewk_extension_init(Ewk_Extension* extension)
96 static EwkExtensionClient client;
98 client.data = (void *)extension;
99 client.page_add = pageAdded;
100 client.page_del = pageDelete;
101 client.message_received = messageReceived;
103 ewk_extension_client_add(extension, &client);