Use new Crosswalk Extension C API
authorCaio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Wed, 4 Sep 2013 22:10:29 +0000 (19:10 -0300)
committerCaio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Wed, 11 Sep 2013 14:54:09 +0000 (11:54 -0300)
This commit changed ExtensionAdapater to use new Extension API without
changing any of the existing Context classes. That said,
ExtensionAdapter could be rewrote now with less templates and in a more
straightforward way.

16 files changed:
bluetooth/bluetooth_context.cc
common/XW_Extension.h [new file with mode: 0644]
common/XW_Extension_SyncMessage.h [new file with mode: 0644]
common/common.gypi
common/extension_adapter.cc [new file with mode: 0644]
common/extension_adapter.h
download/download_context.cc
network_bearer_selection/network_bearer_selection_context_desktop.cc
network_bearer_selection/network_bearer_selection_context_mobile.cc
notification/notification_context.cc
power/power_context.cc
system_info/system_info_context.cc
system_setting/system_setting_context.cc
tizen/tizen_context.cc
tools/check-coding-style
xwalk_extension_public.h [deleted file]

index eb3a15e..f012089 100644 (file)
@@ -9,7 +9,7 @@
 #include <bluetooth.h>
 #endif
 
-CXWalkExtension* xwalk_extension_init(int32_t api_version) {
+int32_t XW_Initialize(XW_Extension extension, XW_GetInterface get_interface) {
 #if defined(TIZEN_MOBILE)
   int init = bt_initialize();
   if (init != BT_ERROR_NONE)
@@ -28,7 +28,8 @@ CXWalkExtension* xwalk_extension_init(int32_t api_version) {
   }
 #endif
 
-  return ExtensionAdapter<BluetoothContext>::Initialize();
+  return ExtensionAdapter<BluetoothContext>::Initialize(extension,
+                                                        get_interface);
 }
 
 BluetoothContext::BluetoothContext(ContextAPI* api)
diff --git a/common/XW_Extension.h b/common/XW_Extension.h
new file mode 100644 (file)
index 0000000..174915a
--- /dev/null
@@ -0,0 +1,185 @@
+// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_H_
+#define XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_H_
+
+// Crosswalk Extensions are modules of code loaded by Crosswalk runtime that
+// allow extending its capabilities. The extension is expected to define a
+// XW_Initialize() function as declared below, get the interfaces it need to
+// use and register to whatever callbacks it needs, then return XW_OK.
+//
+// The Extension is represented by the type XW_Extension. Each extension
+// loaded may be used multiple times for different pages, so to each execution
+// there will be an associated XW_Instance. A reasonable analogy is that the
+// XW_Extension represent a "class", and have concrete instances running.
+//
+// An interface is a struct with a set of functions, provided by Crosswalk,
+// that allow the extension code to interact with the web content. Certain
+// functions in an interface are used to register callbacks, so that Crosswalk
+// can call the extension at specific situations.
+//
+// Crosswalk won't call an extension's XW_Initialize() multiple times in the
+// same process.
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if __GNUC__ >= 4
+#define XW_EXPORT __attribute__ ((visibility("default")))
+#elif defined(_MSC_VER)
+#define XW_EXPORT __declspec(dllexport)
+#endif
+
+#include <stdint.h>
+
+
+// XW_Extension is used to identify your extension when calling functions from
+// the API. You should always use the XW_Extension received at XW_Initialize().
+//
+// XW_Instance is used to identify different web contents using your
+// extension. Each time a new web content is created you can be notified
+// registering the XW_CreatedInstanceCallback, that receives the new
+// XW_Instance. When interacting with an Instance (for example to post a
+// message), you should pass the corresponding XW_Instance.
+//
+// In both types the zero value is never used by Crosswalk, so can be used to
+// initialize variables.
+typedef int32_t XW_Extension;
+typedef int32_t XW_Instance;
+
+enum {
+  XW_OK = 0,
+  XW_ERROR = -1
+};
+
+// Returns a struct containing functions to be used by the extension. Those
+// structs can be stored statically and used until the extension is unloaded.
+// Extensions should use definitions like XW_CORE_INTERFACE, instead of using
+// the versioned definition or the literal string. Returns NULL if the
+// interface is not supported.
+typedef const void* (*XW_GetInterface)(const char* interface_name);
+
+
+typedef int32_t (*XW_Initialize_Func)(XW_Extension extension,
+                                      XW_GetInterface get_interface);
+
+// XW_Initialize is called after the extension code is loaded. The 'extension'
+// value should be used in further calls that expect XW_Extension argument.
+//
+// The 'get_interface' function should be used to get access to functions that
+// interact with the web content. It is only valid during the execution of the
+// XW_Initialize() function.
+//
+// This function should return XW_OK when the extension was succesfully
+// loaded, otherwise XW_ERROR.
+XW_EXPORT int32_t XW_Initialize(XW_Extension extension,
+                                XW_GetInterface get_interface);
+
+
+//
+// XW_CORE_INTERFACE: Basic functionality for Crosswalk Extensions. All
+// extensions should use this interface to set at least their name.
+//
+
+#define XW_CORE_INTERFACE_1 "XW_CoreInterface_1"
+#define XW_CORE_INTERFACE XW_CORE_INTERFACE_1
+
+typedef void (*XW_CreatedInstanceCallback)(XW_Instance instance);
+typedef void (*XW_DestroyedInstanceCallback)(XW_Instance instance);
+typedef void (*XW_ShutdownCallback)(XW_Extension extension);
+
+struct XW_CoreInterface_1 {
+  // Set the name of the extension. It is used as the namespace for the
+  // JavaScript code exposed by the extension. So extension named
+  // 'my_extension', will expose its JavaScript functionality inside
+  // the 'my_extension' namespace.
+  //
+  // This function should be called only during XW_Initialize().
+  void (*SetExtensionName)(XW_Extension extension, const char* name);
+
+  // Set the JavaScript code loaded in the web content when the extension is
+  // used. This can be used together with the messaging mechanism to implement
+  // a higher-level API that posts messages to extensions, see
+  // XW_MESSAGING_INTERFACE below.
+  //
+  // The code will be executed inside a JS function context with the following
+  // objects available:
+  //
+  // - exports: this object should be filled with properties and functions
+  //            that will be exposed in the namespace associated with this
+  //            extension.
+  //
+  // - extension.postMessage(): post a string message to the extension native
+  //                            code. See below for details.
+  // - extension.setMessageListener(): allow setting a callback that is called
+  //                                   when the native code sends a message
+  //                                   to JavaScript. Callback takes a string.
+  //
+  // This function should be called only during XW_Initialize().
+  void (*SetJavaScriptAPI)(XW_Extension extension, const char* api);
+
+  // Register callbacks that are called when an instance of this extension
+  // is created or destroyed. Everytime a new web content is loaded, it will
+  // get a new associated instance.
+  //
+  // This function should be called only during XW_Initialize().
+  void (*RegisterInstanceCallbacks)(XW_Extension extension,
+                                    XW_CreatedInstanceCallback created,
+                                    XW_DestroyedInstanceCallback destroyed);
+
+  // Register a callback to be executed when the extension will be unloaded.
+  //
+  // This function should be called only during XW_Initialize().
+  void (*RegisterShutdownCallback)(XW_Extension extension,
+                                   XW_ShutdownCallback shutdown_callback);
+
+  // These two functions are conveniences used to associated arbitrary data
+  // with a given XW_Instance. They can be used only with instances that were
+  // created but not yet completely destroyed. GetInstanceData() can be used
+  // during the destroyed instance callback. If not instance data was set,
+  // getting it returns NULL.
+  void (*SetInstanceData)(XW_Instance instance, void* data);
+  void* (*GetInstanceData)(XW_Instance instance);
+};
+
+typedef struct XW_CoreInterface_1 XW_CoreInterface;
+
+
+//
+// XW_MESSAGING_INTERFACE: Exchange asynchronous messages with JavaScript
+// code provided by extension.
+//
+
+#define XW_MESSAGING_INTERFACE_1 "XW_MessagingInterface_1"
+#define XW_MESSAGING_INTERFACE XW_MESSAGING_INTERFACE_1
+
+typedef void (*XW_HandleMessageCallback)(XW_Instance instance,
+                                         const char* message);
+
+struct XW_MessagingInterface_1 {
+  // Register a callback to be called when the JavaScript code associated
+  // with the extension posts a message. Note that the callback will be called
+  // with the XW_Instance that posted the message as well as the message
+  // contents.
+  void (*Register)(XW_Extension extension,
+                   XW_HandleMessageCallback handle_message);
+
+  // Post a message to the web content associated with the instance. To
+  // receive this message the extension's JavaScript code should set a
+  // listener using extension.setMessageListener() function.
+  //
+  // This function is thread-safe and can be called until the instance is
+  // destroyed.
+  void (*PostMessage)(XW_Instance instance, const char* message);
+};
+
+typedef struct XW_MessagingInterface_1 XW_MessagingInterface;
+
+#ifdef __cplusplus
+}  // extern "C"
+#endif
+
+#endif  // XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_H_
diff --git a/common/XW_Extension_SyncMessage.h b/common/XW_Extension_SyncMessage.h
new file mode 100644 (file)
index 0000000..4fd706a
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_SYNCMESSAGE_H_
+#define XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_SYNCMESSAGE_H_
+
+// NOTE: This file and interfaces marked as internal are not considered stable
+// and can be modified in incompatible ways between Crosswalk versions.
+
+#ifndef XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_H_
+#error "You should include XW_Extension.h before this file"
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//
+// XW_INTERNAL_SYNC_MESSAGING_INTERFACE: allow JavaScript code to send a
+// synchronous message to extension code and block until response is
+// available.
+//
+
+#define XW_INTERNAL_SYNC_MESSAGING_INTERFACE_1 \
+  "XW_InternalSyncMessagingInterface_1"
+#define XW_INTERNAL_SYNC_MESSAGING_INTERFACE \
+  XW_INTERNAL_SYNC_MESSAGING_INTERFACE_1
+
+typedef void (*XW_HandleSyncMessageCallback)(XW_Instance instance,
+                                             const char* message);
+
+struct XW_Internal_SyncMessagingInterface_1 {
+  void (*Register)(XW_Extension extension,
+                   XW_HandleSyncMessageCallback handle_sync_message);
+  void (*SetSyncReply)(XW_Instance instance, const char* reply);
+};
+
+typedef struct XW_Internal_SyncMessagingInterface_1
+    XW_Internal_SyncMessagingInterface;
+
+#ifdef __cplusplus
+}  // extern "C"
+#endif
+
+#endif  // XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_SYNCMESSAGE_H_
index c0db83e..6b25a79 100644 (file)
       '<(SHARED_INTERMEDIATE_DIR)',
     ],
     'sources': [
+      'extension_adapter.cc',
       'extension_adapter.h',
+      'XW_Extension.h',
+      'XW_Extension_SyncMessage.h',
       'picojson.h',
       'utils.h',
     ],
diff --git a/common/extension_adapter.cc b/common/extension_adapter.cc
new file mode 100644 (file)
index 0000000..c1b501a
--- /dev/null
@@ -0,0 +1,76 @@
+// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "common/extension_adapter.h"
+
+#include <iostream>
+
+namespace {
+
+XW_Extension g_extension = 0;
+
+const XW_CoreInterface* g_core = NULL;
+const XW_MessagingInterface* g_messaging = NULL;
+const XW_Internal_SyncMessagingInterface* g_sync_messaging = NULL;
+
+}  // namespace
+
+namespace internal {
+
+int32_t InitializeExtension(XW_Extension extension,
+                            XW_GetInterface get_interface,
+                            const char* name,
+                            const char* api,
+                            XW_CreatedInstanceCallback created,
+                            XW_DestroyedInstanceCallback destroyed,
+                            XW_HandleMessageCallback handle_message,
+                            XW_HandleSyncMessageCallback handle_sync_message) {
+  if (g_extension != 0) {
+    std::cerr << "Can't initialize same extension multiple times!\n";
+    return XW_ERROR;
+  }
+
+  g_extension = extension;
+
+  g_core = reinterpret_cast<const XW_CoreInterface*>(
+      get_interface(XW_CORE_INTERFACE));
+  if (!g_core) {
+    std::cerr << "Can't initialize extension: error getting Core interface.\n";
+    return XW_ERROR;
+  }
+  g_core->SetExtensionName(extension, name);
+  g_core->SetJavaScriptAPI(extension, api);
+  g_core->RegisterInstanceCallbacks(extension, created, destroyed);
+
+  g_messaging = reinterpret_cast<const XW_MessagingInterface*>(
+      get_interface(XW_MESSAGING_INTERFACE));
+  if (!g_messaging) {
+    std::cerr <<
+        "Can't initialize extension: error getting Messaging interface.\n";
+    return XW_ERROR;
+  }
+  g_messaging->Register(extension, handle_message);
+
+  g_sync_messaging =
+      reinterpret_cast<const XW_Internal_SyncMessagingInterface*>(
+          get_interface(XW_INTERNAL_SYNC_MESSAGING_INTERFACE));
+  if (!g_sync_messaging) {
+    std::cerr <<
+        "Can't initialize extension: error getting Messaging interface.\n";
+    return XW_ERROR;
+  }
+  g_sync_messaging->Register(extension, handle_sync_message);
+
+  return XW_OK;
+}
+
+void PostMessage(XW_Instance instance, const char* message) {
+  g_messaging->PostMessage(instance, message);
+}
+
+void SetSyncReply(XW_Instance instance, const char* reply) {
+  g_sync_messaging->SetSyncReply(instance, reply);
+}
+
+}  // namespace internal
index a0c080e..76638ad 100644 (file)
 #define COMMON_EXTENSION_ADAPTER_H_
 
 #include <cstdlib>
-#include "xwalk_extension_public.h" // NOLINT
+#include <map>
+#include "common/XW_Extension.h"
+#include "common/XW_Extension_SyncMessage.h"
+
+namespace internal {
+
+int32_t InitializeExtension(XW_Extension extension,
+                         XW_GetInterface get_interface,
+                         const char* name,
+                         const char* api,
+                         XW_CreatedInstanceCallback created,
+                         XW_DestroyedInstanceCallback destroyed,
+                         XW_HandleMessageCallback handle_message,
+                         XW_HandleSyncMessageCallback handle_sync_message);
+
+void PostMessage(XW_Instance instance, const char* message);
+void SetSyncReply(XW_Instance instance, const char* reply);
+
+}  // namespace internal
 
 class ContextAPI {
  public:
-  explicit ContextAPI(CXWalkExtensionContext* context) : context_(context) {}
+  explicit ContextAPI(XW_Instance instance) : instance_(instance) {}
+
   void PostMessage(const char* message) {
-    xwalk_extension_context_post_message(context_, message);
+    internal::PostMessage(instance_, message);
   }
-
-  void SetSyncReply(const char* message) {
-    xwalk_extension_context_set_sync_reply(context_, message);
+  void SetSyncReply(const char* reply) {
+    internal::SetSyncReply(instance_, reply);
   }
 
  private:
-  CXWalkExtensionContext* context_;
+  XW_Instance instance_;
 };
 
 template <class T>
 class ExtensionAdapter {
  public:
-  static CXWalkExtension* Initialize();
+  static int32_t Initialize(XW_Extension extension,
+                            XW_GetInterface get_interface);
 
  private:
-  struct Context {
-    CXWalkExtensionContext context;
-    T* cpp_context;
-  };
-
-  static const char* GetJavaScript(CXWalkExtension* extension);
-  static void Shutdown(CXWalkExtension* extension);
-  static CXWalkExtensionContext* ContextCreate(CXWalkExtension* extension);
-  static void ContextHandleMessage(CXWalkExtensionContext* context,
-                                   const char* message);
-  static void ContextHandleSyncMessage(CXWalkExtensionContext* context,
-                                   const char* message);
-  static void ContextDestroy(CXWalkExtensionContext* context);
+  static void DidCreateInstance(XW_Instance instance);
+  static void DidDestroyInstance(XW_Instance instance);
+
+  static void HandleMessage(XW_Instance instance, const char* message);
+  static void HandleSyncMessage(XW_Instance instance, const char* message);
+
+  typedef std::map<XW_Instance, T*> InstanceMap;
+  static InstanceMap g_instances;
 };
 
 template <class T>
-CXWalkExtension* ExtensionAdapter<T>::Initialize() {
-  CXWalkExtension* xwalk_extension =
-      static_cast<CXWalkExtension*>(calloc(1, sizeof(CXWalkExtension)));
-  xwalk_extension->name = T::name;
-  xwalk_extension->api_version = 1;
-  xwalk_extension->get_javascript = GetJavaScript;
-  xwalk_extension->shutdown = Shutdown;
-  xwalk_extension->context_create = ContextCreate;
-  return xwalk_extension;
-}
+typename ExtensionAdapter<T>::InstanceMap ExtensionAdapter<T>::g_instances;
 
 template <class T>
-const char* ExtensionAdapter<T>::GetJavaScript(CXWalkExtension* extension) {
-  return T::GetJavaScript();
+int32_t ExtensionAdapter<T>::Initialize(XW_Extension extension,
+                                        XW_GetInterface get_interface) {
+  return internal::InitializeExtension(
+      extension, get_interface, T::name, T::GetJavaScript(),
+      DidCreateInstance, DidDestroyInstance, HandleMessage, HandleSyncMessage);
 }
 
 template <class T>
-void ExtensionAdapter<T>::Shutdown(CXWalkExtension* extension) {
-  free(extension);
+void ExtensionAdapter<T>::DidCreateInstance(XW_Instance instance) {
+  g_instances[instance] = new T(new ContextAPI(instance));
 }
 
 template <class T>
-CXWalkExtensionContext* ExtensionAdapter<T>::ContextCreate(
-    CXWalkExtension* extension) {
-  Context* adapter = static_cast<Context*>(calloc(1, sizeof(Context)));
-  if (!adapter)
-    return NULL;
-
-  adapter->context.destroy = ContextDestroy;
-  adapter->context.handle_message = ContextHandleMessage;
-  adapter->context.handle_sync_message = ContextHandleSyncMessage;
-  adapter->cpp_context = new T(new ContextAPI(&adapter->context));
-
-  return reinterpret_cast<CXWalkExtensionContext*>(adapter);
+void ExtensionAdapter<T>::DidDestroyInstance(XW_Instance instance) {
+  delete g_instances[instance];
+  g_instances.erase(instance);
 }
 
 template <class T>
-void ExtensionAdapter<T>::ContextHandleSyncMessage(
-    CXWalkExtensionContext* context, const char* message) {
-  Context* adapter = reinterpret_cast<Context*>(context);
-  adapter->cpp_context->HandleSyncMessage(message);
+void ExtensionAdapter<T>::HandleMessage(XW_Instance instance,
+                                        const char* message) {
+  g_instances[instance]->HandleMessage(message);
 }
 
-template <class T>
-void ExtensionAdapter<T>::ContextHandleMessage(
-    CXWalkExtensionContext* context, const char* message) {
-  Context* adapter = reinterpret_cast<Context*>(context);
-  adapter->cpp_context->HandleMessage(message);
-}
 
 template <class T>
-void ExtensionAdapter<T>::ContextDestroy(CXWalkExtensionContext* context) {
-  Context* adapter = reinterpret_cast<Context*>(context);
-  delete adapter->cpp_context;
-  free(adapter);
+void ExtensionAdapter<T>::HandleSyncMessage(XW_Instance instance,
+                                            const char* message) {
+  g_instances[instance]->HandleSyncMessage(message);
 }
 
-#define DEFINE_XWALK_EXTENSION(NAME)                            \
-  CXWalkExtension* xwalk_extension_init(int32_t api_version) {  \
-    return ExtensionAdapter<NAME>::Initialize();                \
+#define DEFINE_XWALK_EXTENSION(NAME)                                    \
+  int32_t XW_Initialize(XW_Extension extension,                         \
+                        XW_GetInterface get_interface) {                \
+    return ExtensionAdapter<NAME>::Initialize(extension, get_interface); \
   }
 
 #endif  // COMMON_EXTENSION_ADAPTER_H_
index a719483..75442c0 100644 (file)
@@ -7,6 +7,8 @@
 
 #include "common/picojson.h"
 
+DEFINE_XWALK_EXTENSION(DownloadContext);
+
 using download_utils::EnumToPChar;
 using download_utils::ToString;
 
@@ -30,10 +32,6 @@ using download_utils::ToString;
   } \
 } while (0)
 
-CXWalkExtension* xwalk_extension_init(int32_t api_version) {
-  return ExtensionAdapter<DownloadContext>::Initialize();
-}
-
 DownloadContext::DownloadContext(ContextAPI* api)
     : api_(api) {
 }
index 967da64..332478f 100644 (file)
@@ -8,9 +8,7 @@
 #include "common/extension_adapter.h"
 #include "common/picojson.h"
 
-CXWalkExtension* xwalk_extension_init(int32_t api_version) {
-  return ExtensionAdapter<NetworkBearerSelectionContextDesktop>::Initialize();
-}
+DEFINE_XWALK_EXTENSION(NetworkBearerSelectionContextDesktop);
 
 NetworkBearerSelectionContextDesktop::NetworkBearerSelectionContextDesktop(
     ContextAPI* api) : NetworkBearerSelectionContext(api) {
index f2c006a..f2c5011 100644 (file)
@@ -8,9 +8,7 @@
 #include "network_bearer_selection/network_bearer_selection_request.h"
 #include "network_bearer_selection/network_bearer_selection_connection_mobile.h"
 
-CXWalkExtension* xwalk_extension_init(int32_t api_version) {
-  return ExtensionAdapter<NetworkBearerSelectionContextMobile>::Initialize();
-}
+DEFINE_XWALK_EXTENSION(NetworkBearerSelectionContextMobile);
 
 NetworkBearerSelectionContextMobile::NetworkBearerSelectionContextMobile(
     ContextAPI* api) : NetworkBearerSelectionContext(api) {
index 0c05f23..734b5df 100644 (file)
@@ -5,9 +5,10 @@
 #include "notification/notification_context.h"
 #include "common/picojson.h"
 
-CXWalkExtension* xwalk_extension_init(int32_t api_version) {
+int32_t XW_Initialize(XW_Extension extension, XW_GetInterface get_interface) {
   NotificationContext::PlatformInitialize();
-  return ExtensionAdapter<NotificationContext>::Initialize();
+  return ExtensionAdapter<NotificationContext>::Initialize(extension,
+                                                           get_interface);
 }
 
 const char NotificationContext::name[] = "tizen.notification";
index 638c948..160f7a9 100644 (file)
@@ -5,9 +5,7 @@
 #include "power/power_context.h"
 #include "common/picojson.h"
 
-CXWalkExtension* xwalk_extension_init(int32_t api_version) {
-  return ExtensionAdapter<PowerContext>::Initialize();
-}
+DEFINE_XWALK_EXTENSION(PowerContext);
 
 PowerContext::PowerContext(ContextAPI* api)
   : api_(api) {
index 1612656..6d71cd7 100644 (file)
@@ -14,9 +14,7 @@
 #include "common/picojson.h"
 #include "system_info/system_info_utils.h"
 
-CXWalkExtension* xwalk_extension_init(int32_t api_version) {
-  return ExtensionAdapter<SystemInfoContext>::Initialize();
-}
+DEFINE_XWALK_EXTENSION(SystemInfoContext);
 
 SystemInfoContext::SystemInfoContext(ContextAPI* api)
     : api_(api),
index 4ee66ae..1ad7607 100644 (file)
@@ -7,9 +7,7 @@
 #include <string>
 #include "common/picojson.h"
 
-CXWalkExtension* xwalk_extension_init(int32_t api_version) {
-  return ExtensionAdapter<SystemSettingContext>::Initialize();
-}
+DEFINE_XWALK_EXTENSION(SystemSettingContext);
 
 SystemSettingContext::SystemSettingContext(ContextAPI* api)
     : api_(api) {}
index d5be9b5..8f10c35 100644 (file)
@@ -4,9 +4,7 @@
 
 #include "tizen/tizen_context.h"
 
-CXWalkExtension* xwalk_extension_init(int32_t api_version) {
-  return ExtensionAdapter<TizenContext>::Initialize();
-}
+DEFINE_XWALK_EXTENSION(TizenContext);
 
 TizenContext::TizenContext(ContextAPI* api) : api_(api) {}
 
index b793df8..bea8bc7 100755 (executable)
@@ -18,7 +18,8 @@ cpplint.py --filter="$FILTERS" $(find \
                                ! -path './out*' ! -path './.git*' \
                                ! -path './demos' ! -path './examples' \
                                ! -path './packaging' \
-                               ! -name 'xwalk_extension_public.h' \
+                               ! -name 'XW_Extension.h' \
+                               ! -name 'XW_Extension_SyncMessage.h' \
                                ! -name 'picojson.*' \
                                ! -path './bluetooth/*' \
                                ! -path './download/*' \
diff --git a/xwalk_extension_public.h b/xwalk_extension_public.h
deleted file mode 100644 (file)
index e9219bc..0000000
+++ /dev/null
@@ -1,142 +0,0 @@
-// Copyright (c) 2013 Intel Corporation. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef XWALK_EXTENSIONS_PUBLIC_XWALK_EXTENSION_PUBLIC_H_
-#define XWALK_EXTENSIONS_PUBLIC_XWALK_EXTENSION_PUBLIC_H_
-
-#ifndef INTERNAL_IMPLEMENTATION
-#include <assert.h>
-#endif  // INTERNAL_IMPLEMENTATION
-
-#include <stdint.h>
-
-typedef struct CXWalkExtension_           CXWalkExtension;
-typedef struct CXWalkExtensionContext_    CXWalkExtensionContext;
-typedef struct CXWalkExtensionContextAPI_ CXWalkExtensionContextAPI;
-
-typedef void (*ExtensionShutdownCallback)(CXWalkExtension* extension);
-typedef const char* (*ExtensionGetJavaScriptCallback)(
-      CXWalkExtension* extension);
-typedef CXWalkExtensionContext* (*ExtensionContextCreateCallback)(
-      CXWalkExtension* extension);
-
-typedef void (*ExtensionContextDestroyCallback)(
-      CXWalkExtensionContext* context);
-typedef void (*ExtensionContextHandleMessageCallback)(
-      CXWalkExtensionContext* context, const char* message);
-typedef void (*ExtensionContextHandleSyncMessageCallback)(
-      CXWalkExtensionContext* context, const char* message);
-
-typedef void (*ExtensionContextPostMessageCallback)(
-      CXWalkExtensionContext* context, const char* message);
-typedef void (*ExtensionContextSetSyncReplyCallback)(
-      CXWalkExtensionContext* context, const char* message);
-
-struct CXWalkExtension_ {
-  int32_t api_version;
-
-  // Version 1
-  const char* name;
-
-  ExtensionGetJavaScriptCallback get_javascript;
-  ExtensionShutdownCallback shutdown;
-  ExtensionContextCreateCallback context_create;
-};
-
-struct CXWalkExtensionContextAPI_ {
-  // Version 1
-  ExtensionContextPostMessageCallback post_message;
-  ExtensionContextSetSyncReplyCallback set_sync_reply;
-};
-
-struct CXWalkExtensionContext_ {
-  void* internal_data;
-  const CXWalkExtensionContextAPI* api;
-
-  // Version 1
-  ExtensionContextDestroyCallback destroy;
-  ExtensionContextHandleMessageCallback handle_message;
-  ExtensionContextHandleSyncMessageCallback handle_sync_message;
-};
-
-#ifndef INTERNAL_IMPLEMENTATION
-// This function should be implemented and exported in the shared
-// object. The ``api_version'' parameter will contain the maximum
-// version supported by Crosswalk.
-// On a successful invocation, this function should return a pointer
-// to a CXWalkExtension structure, with the fields:
-// - api_version, filled with the API version the extension implements.
-// - name, with the extension name (used by xwalk.postMessage() and
-//   friends).
-// - get_javascript, filled with a pointer to a function that returns
-//   the JavaScript shim to be available in all page contexts.
-// - shutdown, filled with a pointer to a function that is called
-//   whenever this extension is shut down (e.g. Crosswalk terminating). NULL
-//   is fine.
-// - context_create, filled with a pointer to a function that creates
-//   an extension context (see comment below).
-
-#if defined(_WIN32)
-#define PUBLIC_EXPORT __declspec(dllexport)
-#else
-#define PUBLIC_EXPORT __attribute__((visibility("default")))
-#endif
-
-#if defined(__cplusplus)
-#define EXTERN_C extern "C"
-#else
-#define EXTERN_C
-#endif
-
-EXTERN_C PUBLIC_EXPORT CXWalkExtension* xwalk_extension_init(
-      int32_t api_version);
-
-// A CXWalkExtension structure holds the global state for a extension.
-// Due to the multithreaded way Crosswalk is written, one should not
-// store mutable state there. That's the reason CXWalkExtensionContext
-// exists: so each page context has its own state and there's no need
-// to worry about race conditions while keeping state between contexts.
-//
-// The first two fields of a context (internal_data and api) should not
-// be tampered with. The following fields, though, should be filled:
-// - destroy, with a pointer to a function that will be called whenever
-//   a particular context is about to be destroyed.
-// - handle_message, with a pointer to a function that will be called
-//   whenever a message arrives from the JavaScript side.
-// - handle_sync_message, with a pointer to a function that will be
-//   called whenever a synchronous message arrives, the reply should
-//   be set before the function returns using
-//   xwalk_extension_context_set_sync_reply().
-//
-// To post a message to the JavaScript side, one can simply call
-// xwalk_extension_context_post_message(), defined below. Crosswalk will
-// handle all the multithreading details so it is safe to call this
-// whenever necessary.
-static void xwalk_extension_context_post_message(
-      CXWalkExtensionContext* context, const char* message) {
-  assert(context);
-  assert(context->api);
-  assert(context->api->post_message);
-  assert(message);
-
-  context->api->post_message(context, message);
-}
-
-// To be used in the function passed as handle_sync_message.
-static void xwalk_extension_context_set_sync_reply(
-    CXWalkExtensionContext* context, const char* reply) {
-  assert(context);
-  assert(context->api);
-  assert(context->api->set_sync_reply);
-  assert(reply);
-
-  context->api->set_sync_reply(context, reply);
-}
-
-#undef PUBLIC_EXPORT
-#undef EXTERN_C
-
-#endif  // INTERNAL_IMPLEMENTATION
-
-#endif  // XWALK_EXTENSIONS_PUBLIC_XWALK_EXTENSION_PUBLIC_H_