[totem] Add support for queueing Javascript commands



commit 66cbcd4cf6bf55bc35ef777d225e8fdd3a8d2529
Author: Bastien Nocera <hadess hadess net>
Date:   Fri Nov 20 16:17:01 2009 +0000

    Add support for queueing Javascript commands
    
    This should allow us to implement a more robust Javascript
    handling. Not all the commands are queued right now.

 browser-plugin/totemPlugin.cpp |  114 +++++++++++++++++++++++++++++++++------
 browser-plugin/totemPlugin.h   |   27 +++++++++
 2 files changed, 123 insertions(+), 18 deletions(-)
---
diff --git a/browser-plugin/totemPlugin.cpp b/browser-plugin/totemPlugin.cpp
index d40e14e..ca99ea9 100644
--- a/browser-plugin/totemPlugin.cpp
+++ b/browser-plugin/totemPlugin.cpp
@@ -243,16 +243,30 @@ totemPlugin::~totemPlugin ()
         TOTEM_LOG_DTOR ();
 }
 
+/* static */ void
+totemPlugin::QueueCommand (TotemQueueCommand *cmd)
+{
+	assert (mQueue);
+
+	g_queue_push_tail (mQueue, cmd);
+}
+
 /* public functions */
 
 void
 totemPlugin::Command (const char *aCommand)
 {
-	D ("Command '%s'", aCommand);
-
-	/* FIXME: queue the action instead */
-	if (!mViewerReady)
+	if (!mViewerReady) {
+		D("Queuing command '%s'", aCommand);
+		TotemQueueCommand *cmd;
+		cmd = g_new0 (TotemQueueCommand, 1);
+		cmd->type = TOTEM_QUEUE_TYPE_SET_STRING;
+		cmd->string = g_strdup (aCommand);
+		QueueCommand (cmd);
 		return;
+	}
+
+	D ("Command '%s'", aCommand);
 
 	assert (mViewerProxy);
 	dbus_g_proxy_call_no_reply (mViewerProxy,
@@ -303,11 +317,16 @@ totemPlugin::SetFullscreen (bool enabled)
 void
 totemPlugin::ClearPlaylist ()
 {
-	Dm ("ClearPlaylist");
-
-	/* FIXME: queue the action instead */
-	if (!mViewerReady)
+	if (!mViewerReady) {
+		Dm ("Queuing ClearPlaylist");
+		TotemQueueCommand *cmd;
+		cmd = g_new0 (TotemQueueCommand, 1);
+		cmd->type = TOTEM_QUEUE_TYPE_CLEAR_PLAYLIST;
+		QueueCommand (cmd);
 		return;
+	}
+
+	Dm ("ClearPlaylist");
 
 	assert (mViewerProxy);
 	dbus_g_proxy_call_no_reply (mViewerProxy,
@@ -328,25 +347,34 @@ totemPlugin::AddItem (const NPString& aURI,
 
         /* FIXMEchpe: resolve against mBaseURI or mSrcURI ?? */
 
-	/* FIXME: queue the action instead */
-	if (!mViewerReady) {
-		Dm("Viewer not ready in AddItem");
-		return false;
-	}
-
-	assert (mViewerProxy);
-
         char *uri = g_strndup (aURI.UTF8Characters, aURI.UTF8Length);
 
 	char *title;
-	if (aTitle.UTF8Characters)
+	if (aTitle.UTF8Characters && aURI.UTF8Length)
 		title = g_strndup (aTitle.UTF8Characters, aTitle.UTF8Length);
 	else
 		title = NULL;
 
+	/* FIXME: resolve the subtitle URI against the URI itself */
+
+	if (!mViewerReady) {
+		D ("Queuing AddItem '%s' (title: '%s' sub: '%s')",
+		   uri, title ? title : "", aSubtitle ? aSubtitle : "");
+		TotemQueueCommand *cmd;
+		cmd = g_new0 (TotemQueueCommand, 1);
+		cmd->type = TOTEM_QUEUE_TYPE_ADD_ITEM;
+		cmd->add_item.uri = uri;
+		cmd->add_item.title = title;
+		cmd->add_item.subtitle = g_strdup (aSubtitle);
+		QueueCommand (cmd);
+		return 0;
+	}
+
 	D ("AddItem '%s' (title: '%s' sub: '%s')",
 	   uri, title ? title : "", aSubtitle ? aSubtitle : "");
 
+	assert (mViewerProxy);
+
         dbus_g_proxy_call_no_reply (mViewerProxy,
 				    "AddItem",
 				    G_TYPE_STRING, uri,
@@ -589,6 +617,8 @@ totemPlugin::ViewerFork ()
 		return NPERR_GENERIC_ERROR;
 	}
 
+	mQueue = g_queue_new ();
+
 	/* Set mViewerFD nonblocking */
 	fcntl (mViewerFD, F_SETFL, O_NONBLOCK);
 
@@ -675,7 +705,6 @@ totemPlugin::ViewerSetup ()
 	}
 }
 
-
 void
 totemPlugin::ViewerCleanup ()
 {
@@ -770,6 +799,55 @@ totemPlugin::ViewerReady ()
 
 	mViewerReady = true;
 
+	/* Unqueue any queued commands, before any
+	 * new ones come in */
+	TotemQueueCommand *cmd;
+
+	while ((cmd = (TotemQueueCommand *) g_queue_pop_head (mQueue)) != NULL) {
+		D("Popping command %d", cmd->type);
+		switch (cmd->type) {
+		case TOTEM_QUEUE_TYPE_CLEAR_PLAYLIST:
+			ClearPlaylist();
+			break;
+		case TOTEM_QUEUE_TYPE_ADD_ITEM:
+			assert (mViewerProxy);
+
+			D ("AddItem '%s' (title: '%s' sub: '%s')",
+			   cmd->add_item.uri,
+			   cmd->add_item.title ? cmd->add_item.title : "",
+			   cmd->add_item.subtitle ? cmd->add_item.subtitle : "");
+			dbus_g_proxy_call_no_reply (mViewerProxy,
+						    "AddItem",
+						    G_TYPE_STRING, cmd->add_item.uri,
+						    G_TYPE_STRING, cmd->add_item.title,
+						    G_TYPE_STRING, cmd->add_item.subtitle,
+						    G_TYPE_INVALID,
+						    G_TYPE_INVALID);
+			g_free (cmd->add_item.uri);
+			g_free (cmd->add_item.title);
+			g_free (cmd->add_item.subtitle);
+			break;
+		case TOTEM_QUEUE_TYPE_SET_STRING:
+			assert (cmd->string);
+
+			if (g_str_equal (cmd->string, TOTEM_COMMAND_PLAY) ||
+			    g_str_equal (cmd->string, TOTEM_COMMAND_PAUSE) ||
+			    g_str_equal (cmd->string, TOTEM_COMMAND_STOP)) {
+				Command(cmd->string);
+			} else {
+				D("Unhandled queued string '%s'", cmd->string);
+			}
+			g_free (cmd->string);
+			break;
+		default:
+			D("Unhandled queued command type %d", cmd->type);
+		}
+
+		g_free (cmd);
+	}
+	g_queue_free (mQueue);
+	mQueue = NULL;
+
 	if (mAutoPlay) {
 		RequestStream (false);
 	} else {
diff --git a/browser-plugin/totemPlugin.h b/browser-plugin/totemPlugin.h
index e70ee11..c119c6d 100644
--- a/browser-plugin/totemPlugin.h
+++ b/browser-plugin/totemPlugin.h
@@ -46,6 +46,28 @@ typedef struct {
   const char *mime_alias;
 } totemPluginMimeEntry;
 
+typedef enum {
+	TOTEM_QUEUE_TYPE_SET_VOLUME,
+	TOTEM_QUEUE_TYPE_CLEAR_PLAYLIST,
+	TOTEM_QUEUE_TYPE_ADD_ITEM,
+	TOTEM_QUEUE_TYPE_SET_BOOLEAN,
+	TOTEM_QUEUE_TYPE_SET_STRING
+} TotemQueueCommandType;
+
+typedef struct {
+	TotemQueueCommandType type;
+	union {
+		float volume;
+		struct {
+			char *uri;
+			char *title;
+			char *subtitle;
+		} add_item;
+		gboolean boolean;
+		char *string;
+	};
+} TotemQueueCommand;
+
 class totemBasicPlayer;
 class totemConePlayer;
 class totemGMPControls;
@@ -139,6 +161,7 @@ class totemPlugin {
 						   DBusGProxyCall *aCall,
 						   void *aData);
 
+
     NPError ViewerFork ();
     void ViewerSetup ();
     void ViewerSetWindow ();
@@ -253,6 +276,8 @@ class totemPlugin {
     uint32_t mDuration;
     uint32_t mTime;
 
+    GQueue *mQueue;
+
 #ifdef TOTEM_GMP_PLUGIN
   public:
     void SetURL (const char* aURL);
@@ -371,6 +396,8 @@ class totemPlugin {
     void SetRate (double rate);
     double Rate () const;
 
+    void QueueCommand (TotemQueueCommand *cmd);
+
     double Duration () const { return double (mDuration); }
 
     int32_t BytesStreamed () const { return mBytesStreamed; }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]