1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #if defined(__cplusplus)
6 #error "This file is written in C to make sure the C API works as intended."
11 #include "xwalk/extensions/public/XW_Extension.h"
12 #include "xwalk/extensions/public/XW_Extension_Runtime.h"
13 #include "xwalk/extensions/public/XW_Extension_SyncMessage.h"
15 static XW_Extension g_extension;
16 static const XW_CoreInterface* g_core;
17 static const XW_MessagingInterface* g_messaging;
18 static const XW_Internal_SyncMessagingInterface* g_sync_messaging;
19 static const XW_Internal_RuntimeInterface* g_runtime;
21 void instance_created(XW_Instance instance) {
22 printf("Instance %d created!\n", instance);
25 void instance_destroyed(XW_Instance instance) {
26 printf("Instance %d destroyed!\n", instance);
29 void handle_message(XW_Instance instance, const char* message) {
30 g_messaging->PostMessage(instance, message);
33 void handle_sync_message(XW_Instance instance, const char* message) {
34 static char out[8192];
35 g_runtime->GetRuntimeVariableString(g_extension, message, out, sizeof(out));
36 g_sync_messaging->SetSyncReply(instance, out);
39 void shutdown(XW_Extension extension) {
43 int32_t XW_Initialize(XW_Extension extension, XW_GetInterface get_interface) {
44 static const char* kAPI =
45 "exports.getVariable = function(name) {"
46 " return JSON.parse(extension.internal.sendSyncMessage(name));"
49 g_extension = extension;
50 g_core = get_interface(XW_CORE_INTERFACE);
51 g_core->SetExtensionName(extension, "runtime");
52 g_core->SetJavaScriptAPI(extension, kAPI);
53 g_core->RegisterInstanceCallbacks(
54 extension, instance_created, instance_destroyed);
55 g_core->RegisterShutdownCallback(extension, shutdown);
57 g_messaging = get_interface(XW_MESSAGING_INTERFACE);
58 g_messaging->Register(extension, handle_message);
60 g_sync_messaging = get_interface(XW_INTERNAL_SYNC_MESSAGING_INTERFACE);
61 g_sync_messaging->Register(extension, handle_sync_message);
63 g_runtime = get_interface(XW_INTERNAL_RUNTIME_INTERFACE);