[Notification] Use new C++ wrapper in common/
authorCaio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Fri, 20 Sep 2013 16:31:15 +0000 (13:31 -0300)
committerCaio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Mon, 23 Sep 2013 17:06:29 +0000 (14:06 -0300)
NotificationExtension now hosts the NotificationManager for mobile and
passes to the instances. For convenience, two different instance
interfaces were created, one for Desktop other for Mobile.

Behavior should not change after this patch.

notification/notification.gyp
notification/notification_context.cc [deleted file]
notification/notification_context.h [deleted file]
notification/notification_extension.cc [new file with mode: 0644]
notification/notification_extension.h [new file with mode: 0644]
notification/notification_instance_desktop.cc [moved from notification/notification_context_desktop.cc with 59% similarity]
notification/notification_instance_desktop.h [new file with mode: 0644]
notification/notification_instance_mobile.cc [moved from notification/notification_context_mobile.cc with 52% similarity]
notification/notification_instance_mobile.h [new file with mode: 0644]

index 10ad535..a6639c2 100644 (file)
       ],
       'sources': [
         'notification_api.js',
-        'notification_context.cc',
-        'notification_context.h',
-        'notification_context_desktop.cc',
-        'notification_context_mobile.cc',
+        'notification_extension.cc',
+        'notification_extension.h',
+        'notification_instance_desktop.cc',
+        'notification_instance_desktop.h',
+        'notification_instance_mobile.cc',
+        'notification_instance_mobile.h',
         'mobile/notification_manager.cc',
         'mobile/notification_manager.h',
+        '../common/extension.h',
+        '../common/extension.cc',
       ],
 
       'conditions': [
diff --git a/notification/notification_context.cc b/notification/notification_context.cc
deleted file mode 100644 (file)
index 734b5df..0000000
+++ /dev/null
@@ -1,38 +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.
-
-#include "notification/notification_context.h"
-#include "common/picojson.h"
-
-int32_t XW_Initialize(XW_Extension extension, XW_GetInterface get_interface) {
-  NotificationContext::PlatformInitialize();
-  return ExtensionAdapter<NotificationContext>::Initialize(extension,
-                                                           get_interface);
-}
-
-const char NotificationContext::name[] = "tizen.notification";
-
-// This will be generated from notification_api.js.
-extern const char kSource_notification_api[];
-
-const char* NotificationContext::GetJavaScript() {
-  return kSource_notification_api;
-}
-
-void NotificationContext::HandleMessage(const char* message) {
-  picojson::value v;
-
-  std::string err;
-  picojson::parse(v, message, message + strlen(message), &err);
-  if (!err.empty()) {
-    std::cout << "Ignoring message.\n";
-    return;
-  }
-
-  std::string cmd = v.get("cmd").to_str();
-  if (cmd == "NotificationPost")
-    HandlePost(v);
-  else if (cmd == "NotificationRemove")
-    HandleRemove(v);
-}
diff --git a/notification/notification_context.h b/notification/notification_context.h
deleted file mode 100644 (file)
index e47580b..0000000
+++ /dev/null
@@ -1,66 +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 NOTIFICATION_NOTIFICATION_CONTEXT_H_
-#define NOTIFICATION_NOTIFICATION_CONTEXT_H_
-
-#if defined(GENERIC_DESKTOP)
-#include <libnotify/notify.h>
-#endif
-
-#include <map>
-#include <string>
-#include "common/extension_adapter.h"
-
-#if defined(TIZEN_MOBILE)
-#include "notification/mobile/notification_manager.h"
-#endif
-
-namespace picojson {
-class value;
-}
-
-class NotificationContext
-#if defined(TIZEN_MOBILE)
-    : public NotificationClient
-#endif
-{
- public:
-  explicit NotificationContext(ContextAPI* api);
-  ~NotificationContext();
-
-  static void PlatformInitialize();
-
-  // ExtensionAdapter implementation.
-  static const char name[];
-  static const char* GetJavaScript();
-  void HandleMessage(const char* message);
-  void HandleSyncMessage(const char* message) {}
-
- private:
-  void HandlePost(const picojson::value& msg);
-  void HandleRemove(const picojson::value& msg);
-
-  ContextAPI* api_;
-
-#if defined(GENERIC_DESKTOP)
-  std::string IdFromNotification(NotifyNotification* notification);
-  static void OnNotificationClosedThunk(NotifyNotification* notification,
-                                        gpointer data);
-  void OnNotificationClosed(NotifyNotification* notification);
-
-  typedef std::map<std::string, NotifyNotification*> NotificationsMap;
-  NotificationsMap notifications_;
-#endif
-
-#if defined(TIZEN_MOBILE)
-  // NotificationClient implementation.
-  virtual void OnNotificationRemoved(const std::string& id);
-
-  typedef std::map<std::string, int> NotificationsMap;
-  NotificationsMap notifications_;
-#endif
-};
-
-#endif  // NOTIFICATION_NOTIFICATION_CONTEXT_H_
diff --git a/notification/notification_extension.cc b/notification/notification_extension.cc
new file mode 100644 (file)
index 0000000..a09234e
--- /dev/null
@@ -0,0 +1,38 @@
+// 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 "notification/notification_extension.h"
+
+#if defined(GENERIC_DESKTOP)
+#include <libnotify/notify.h>
+#include "notification/notification_instance_desktop.h"
+#elif defined(TIZEN_MOBILE)
+#include "notification/notification_instance_mobile.h"
+#endif
+
+common::Extension* CreateExtension() {
+  return new NotificationExtension;
+}
+
+// This will be generated from notification_api.js.
+extern const char kSource_notification_api[];
+
+NotificationExtension::NotificationExtension() {
+#if defined(GENERIC_DESKTOP)
+  notify_init("Crosswalk");
+#endif
+  SetExtensionName("tizen.notification");
+  SetJavaScriptAPI(kSource_notification_api);
+}
+
+NotificationExtension::~NotificationExtension() {}
+
+common::Instance* NotificationExtension::CreateInstance() {
+#if defined(GENERIC_DESKTOP)
+  return new NotificationInstanceDesktop;
+#elif defined(TIZEN_MOBILE)
+  return new NotificationInstanceMobile(&manager_);
+#endif
+  return NULL;
+}
diff --git a/notification/notification_extension.h b/notification/notification_extension.h
new file mode 100644 (file)
index 0000000..1d45df6
--- /dev/null
@@ -0,0 +1,28 @@
+// 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 NOTIFICATION_NOTIFICATION_EXTENSION_H_
+#define NOTIFICATION_NOTIFICATION_EXTENSION_H_
+
+#include "common/extension.h"
+
+#if defined(TIZEN_MOBILE)
+#include "notification/mobile/notification_manager.h"
+#endif
+
+class NotificationExtension : public common::Extension {
+ public:
+  NotificationExtension();
+  virtual ~NotificationExtension();
+
+ private:
+  // common::Extension implementation.
+  virtual common::Instance* CreateInstance();
+
+#if defined(TIZEN_MOBILE)
+  NotificationManager manager_;
+#endif
+};
+
+#endif  // NOTIFICATION_NOTIFICATION_EXTENSION_H_
similarity index 59%
rename from notification/notification_context_desktop.cc
rename to notification/notification_instance_desktop.cc
index 99b7c4b..93e555a 100644 (file)
@@ -2,25 +2,32 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "notification/notification_context.h"
+#include "notification/notification_instance_desktop.h"
+
 #include "common/picojson.h"
 
-// static
-void NotificationContext::PlatformInitialize() {
-  notify_init("Tizen-WRT");
-}
+NotificationInstanceDesktop::NotificationInstanceDesktop() {}
 
-NotificationContext::NotificationContext(ContextAPI* api)
-    : api_(api) {}
+NotificationInstanceDesktop::~NotificationInstanceDesktop() {}
 
-NotificationContext::~NotificationContext() {
-  NotificationsMap::iterator it;
-  for (it = notifications_.begin(); it != notifications_.end(); it++)
-    g_object_unref(it->second);
-  delete api_;
+void NotificationInstanceDesktop::HandleMessage(const char* message) {
+  picojson::value v;
+
+  std::string err;
+  picojson::parse(v, message, message + strlen(message), &err);
+  if (!err.empty()) {
+    std::cout << "Ignoring message.\n";
+    return;
+  }
+
+  std::string cmd = v.get("cmd").to_str();
+  if (cmd == "NotificationPost")
+    HandlePost(v);
+  else if (cmd == "NotificationRemove")
+    HandleRemove(v);
 }
 
-void NotificationContext::HandlePost(const picojson::value& msg) {
+void NotificationInstanceDesktop::HandlePost(const picojson::value& msg) {
   NotifyNotification* notification =
       notify_notification_new(msg.get("title").to_str().c_str(),
                               msg.get("content").to_str().c_str(),
@@ -34,7 +41,7 @@ void NotificationContext::HandlePost(const picojson::value& msg) {
   notifications_[id] = notification;
 }
 
-void NotificationContext::HandleRemove(const picojson::value& msg) {
+void NotificationInstanceDesktop::HandleRemove(const picojson::value& msg) {
   std::string id = msg.get("id").to_str();
   NotificationsMap::iterator it = notifications_.find(id);
   if (it == notifications_.end())
@@ -42,7 +49,7 @@ void NotificationContext::HandleRemove(const picojson::value& msg) {
   notify_notification_close(it->second, NULL);
 }
 
-std::string NotificationContext::IdFromNotification(
+std::string NotificationInstanceDesktop::IdFromNotification(
     NotifyNotification* notification) {
   NotificationsMap::iterator it;
   for (it = notifications_.begin(); it != notifications_.end(); ++it) {
@@ -52,13 +59,14 @@ std::string NotificationContext::IdFromNotification(
   return std::string();
 }
 
-void NotificationContext::OnNotificationClosedThunk(
+void NotificationInstanceDesktop::OnNotificationClosedThunk(
     NotifyNotification* notification, gpointer data) {
-  NotificationContext* handler = reinterpret_cast<NotificationContext*>(data);
+  NotificationInstanceDesktop* handler =
+      reinterpret_cast<NotificationInstanceDesktop*>(data);
   handler->OnNotificationClosed(notification);
 }
 
-void NotificationContext::OnNotificationClosed(
+void NotificationInstanceDesktop::OnNotificationClosed(
     NotifyNotification* notification) {
   const std::string id = IdFromNotification(notification);
   if (id.empty())
@@ -67,7 +75,7 @@ void NotificationContext::OnNotificationClosed(
   o["cmd"] = picojson::value("NotificationRemoved");
   o["id"] = picojson::value(id);
   picojson::value v(o);
-  api_->PostMessage(v.serialize().c_str());
+  PostMessage(v.serialize().c_str());
 
   g_object_unref(notification);
   notifications_.erase(id);
diff --git a/notification/notification_instance_desktop.h b/notification/notification_instance_desktop.h
new file mode 100644 (file)
index 0000000..988ac94
--- /dev/null
@@ -0,0 +1,41 @@
+// 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 NOTIFICATION_NOTIFICATION_INSTANCE_DESKTOP_H_
+#define NOTIFICATION_NOTIFICATION_INSTANCE_DESKTOP_H_
+
+#if defined(GENERIC_DESKTOP)
+#include <libnotify/notify.h>
+#endif
+
+#include <string>
+#include <map>
+#include "common/extension.h"
+
+namespace picojson {
+class value;
+}
+
+class NotificationInstanceDesktop : public common::Instance {
+ public:
+  NotificationInstanceDesktop();
+  virtual ~NotificationInstanceDesktop();
+
+ private:
+  // common::Instance implementation.
+  virtual void HandleMessage(const char* msg);
+
+  void HandlePost(const picojson::value& msg);
+  void HandleRemove(const picojson::value& msg);
+
+  std::string IdFromNotification(NotifyNotification* notification);
+  static void OnNotificationClosedThunk(NotifyNotification* notification,
+                                        gpointer data);
+  void OnNotificationClosed(NotifyNotification* notification);
+
+  typedef std::map<std::string, NotifyNotification*> NotificationsMap;
+  NotificationsMap notifications_;
+};
+
+#endif  // NOTIFICATION_NOTIFICATION_INSTANCE_DESKTOP_H_
similarity index 52%
rename from notification/notification_context_mobile.cc
rename to notification/notification_instance_mobile.cc
index 102d776..8c1f5be 100644 (file)
@@ -2,18 +2,12 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "notification/notification_context.h"
+#include "notification/notification_instance_mobile.h"
 
-#include <iostream>
 #include "common/picojson.h"
 
 namespace {
 
-// We have one NotificationManager per extension (so, per process). This is
-// like this because we can have only one callback function to track the
-// eventual removal of notifications.
-NotificationManager* g_notification_manager = NULL;
-
 void NotificationSetText(notification_h notification,
                          notification_text_type_e type,
                          const std::string& text) {
@@ -23,47 +17,59 @@ void NotificationSetText(notification_h notification,
 
 }  // namespace
 
-// static
-void NotificationContext::PlatformInitialize() {
-  // FIXME(cmarcelo): Handle shutdown.
-  g_notification_manager = new NotificationManager();
+NotificationInstanceMobile::NotificationInstanceMobile(
+    NotificationManager* manager)
+    : manager_(manager) {}
+
+NotificationInstanceMobile::~NotificationInstanceMobile() {
+  manager_->DetachClient(this);
 }
 
-NotificationContext::NotificationContext(ContextAPI* api)
-    : api_(api) {}
+void NotificationInstanceMobile::HandleMessage(const char* message) {
+  picojson::value v;
+
+  std::string err;
+  picojson::parse(v, message, message + strlen(message), &err);
+  if (!err.empty()) {
+    std::cout << "Ignoring message.\n";
+    return;
+  }
 
-NotificationContext::~NotificationContext() {
-  g_notification_manager->DetachClient(this);
-  delete api_;
+  std::string cmd = v.get("cmd").to_str();
+  if (cmd == "NotificationPost")
+    HandlePost(v);
+  else if (cmd == "NotificationRemove")
+    HandleRemove(v);
 }
 
-void NotificationContext::HandlePost(const picojson::value& msg) {
-  notification_h notification = g_notification_manager->CreateNotification();
+void NotificationInstanceMobile::HandlePost(const picojson::value& msg) {
+  notification_h notification = manager_->CreateNotification();
   NotificationSetText(notification, NOTIFICATION_TEXT_TYPE_TITLE,
                       msg.get("title").to_str());
   NotificationSetText(notification, NOTIFICATION_TEXT_TYPE_CONTENT,
                       msg.get("content").to_str());
 
   std::string id = msg.get("id").to_str();
-  if (!g_notification_manager->PostNotification(id, notification, this)) {
+  if (!manager_->PostNotification(id, notification, this)) {
     std::cerr << "tizen.notification error: "
               << " couldn't post notification with id '" << id << "'";
     return;
   }
 }
 
-void NotificationContext::HandleRemove(const picojson::value& msg) {
+void NotificationInstanceMobile::HandleRemove(const picojson::value& msg) {
   std::string id = msg.get("id").to_str();
-  if (!g_notification_manager->RemoveNotification(id)) {
+  if (!manager_->RemoveNotification(id)) {
     std::cerr << "tizen.notification error: "
               << "couldn't remove notification with id '" << id << "'\n";
   }
 }
 
-void NotificationContext::OnNotificationRemoved(const std::string& id) {
+void NotificationInstanceMobile::OnNotificationRemoved(const std::string& id) {
   picojson::value::object o;
   o["cmd"] = picojson::value("NotificationRemoved");
   o["id"] = picojson::value(id);
   picojson::value v(o);
-  api_->PostMessage(v.serialize().c_str());
+  PostMessage(v.serialize().c_str());
 }
+
diff --git a/notification/notification_instance_mobile.h b/notification/notification_instance_mobile.h
new file mode 100644 (file)
index 0000000..b1e5343
--- /dev/null
@@ -0,0 +1,37 @@
+// 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 NOTIFICATION_NOTIFICATION_INSTANCE_MOBILE_H_
+#define NOTIFICATION_NOTIFICATION_INSTANCE_MOBILE_H_
+
+#include <string>
+#include <map>
+#include "common/extension.h"
+#include "notification/mobile/notification_manager.h"
+
+namespace picojson {
+class value;
+}
+
+class NotificationInstanceMobile
+    : public common::Instance,
+      public NotificationClient {
+ public:
+  explicit NotificationInstanceMobile(NotificationManager* manager);
+  virtual ~NotificationInstanceMobile();
+
+ private:
+  // common::Instance implementation.
+  virtual void HandleMessage(const char* msg);
+
+  void HandlePost(const picojson::value& msg);
+  void HandleRemove(const picojson::value& msg);
+
+  // NotificationClient implementation.
+  virtual void OnNotificationRemoved(const std::string& id);
+
+  NotificationManager* manager_;
+};
+
+#endif  // NOTIFICATION_NOTIFICATION_INSTANCE_MOBILE_H_