1f206f8eddb54c196ca20fe762c202b8c2579f33
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / test / get_runtime_variable.c
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.
4
5 #if defined(__cplusplus)
6 #error "This file is written in C to make sure the C API works as intended."
7 #endif
8
9 #include <stdio.h>
10 #include <stdlib.h>
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"
14
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;
20
21 void instance_created(XW_Instance instance) {
22   printf("Instance %d created!\n", instance);
23 }
24
25 void instance_destroyed(XW_Instance instance) {
26   printf("Instance %d destroyed!\n", instance);
27 }
28
29 void handle_message(XW_Instance instance, const char* message) {
30   g_messaging->PostMessage(instance, message);
31 }
32
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);
37 }
38
39 void shutdown(XW_Extension extension) {
40   printf("Shutdown\n");
41 }
42
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));"
47       "};";
48
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);
56
57   g_messaging = get_interface(XW_MESSAGING_INTERFACE);
58   g_messaging->Register(extension, handle_message);
59
60   g_sync_messaging = get_interface(XW_INTERNAL_SYNC_MESSAGING_INTERFACE);
61   g_sync_messaging->Register(extension, handle_sync_message);
62
63   g_runtime = get_interface(XW_INTERNAL_RUNTIME_INTERFACE);
64   if (!g_runtime)
65     return XW_ERROR;
66
67   return XW_OK;
68 }