Remove unused cruft, change tracking JS object identity to use an expando
property on JS objects.  With this, the Chrome plugin is usable but the
access control still needs to be implemented before we can ship it.


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@6106 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/plugins/npapi/LocalObjectTable.cpp b/plugins/npapi/LocalObjectTable.cpp
index e0c381c..a450381 100644
--- a/plugins/npapi/LocalObjectTable.cpp
+++ b/plugins/npapi/LocalObjectTable.cpp
@@ -1,37 +1,8 @@
 #include "mozincludes.h"
 #include "LocalObjectTable.h"
 
-// Mirrors of NPObjectWrapper and NPObjectProxy from Chrome
-// TODO(jat): this is very fragile and may break if Chrome changes
-struct ChromeNPObjectProxy {
-  // IPC::Channel::Listener and IPC::Message::Sender are pure interfaces, so we don't need to
-  // account for any data fields from their inheritance
-  void* channel_; // scoped_refptr keeps only a single pointer
-  void* unknown; // looks like another pointer before route id
-  int route_id_;
-  intptr_t npobject_ptr_;
-};
-
-struct ChromeNPObjectWrapper {
-  NPObject object;
-  ChromeNPObjectProxy* proxy;
-};
-
-NPClass* LocalObjectTable::wrappedObjectClass = 0;
-
 LocalObjectTable::~LocalObjectTable() {
   if (!dontFree) {
     freeAll();
   }
 }
-
-void* LocalObjectTable::getIdentityFrom(NPObject* obj) {
-  void* id = obj;
-  if (obj->_class == wrappedObjectClass) {
-    ChromeNPObjectWrapper* wrapper = reinterpret_cast<ChromeNPObjectWrapper*>(obj);
-    ChromeNPObjectProxy* proxy = wrapper->proxy;
-    id = reinterpret_cast<void*>(proxy->npobject_ptr_);
-    Debug::log(Debug::Info) << "Mapped obj=" << (void*)obj << " to " << id << Debug::flush;
-  }
-  return id;
-}
diff --git a/plugins/npapi/LocalObjectTable.h b/plugins/npapi/LocalObjectTable.h
index 4943ff3..9ff809d 100644
--- a/plugins/npapi/LocalObjectTable.h
+++ b/plugins/npapi/LocalObjectTable.h
@@ -18,7 +18,6 @@
 
 #include <vector>
 #include <algorithm>
-#include <map>
 
 #include "Debug.h"
 
@@ -30,9 +29,7 @@
 
   int nextFree;
   std::vector<NPObject*> objects;
-  std::map<void*, int> objectIndex;
   bool dontFree;
-  static NPClass* wrappedObjectClass;
 
   bool isFree(int id) {
     // low bit is set for free pointers, object pointers can't be odd
@@ -45,15 +42,6 @@
     nextFree = id;
   }
 
-  int findIndex(void* ptr) {
-    std::map<void*, int>::iterator it = objectIndex.find(ptr);
-    if (it == objectIndex.end()) {
-      return -1;
-    }
-    return it->second;
-  }
-
-  static void* getIdentityFrom(NPObject* obj);
 public:
   LocalObjectTable() {
     nextFree = -1;
@@ -63,18 +51,11 @@
 
   virtual ~LocalObjectTable();
 
-  static void setWrappedObjectClass(NPClass* clazz) {
-    wrappedObjectClass = clazz;
-  }
-
+  /**
+   * Add a new object, which must not be in the table, and return a new id for it.
+   */
   int add(NPObject* obj) {
-    void* objId = getIdentityFrom(obj);
-    int id = findIndex(objId);
-    if (id >= 0) {
-      Debug::log(Debug::Spam) << "LocalObjectTable::add(obj=" << obj
-          << "): returning old id=" << id << Debug::flush;
-      return id;
-    }
+    int id;
     if (nextFree >= 0) {
       id = nextFree;
       nextFree = int(reinterpret_cast<long long>(objects[nextFree])) >> 1;
@@ -83,7 +64,6 @@
       id = static_cast<int>(objects.size());
       objects.push_back(obj);
     }
-    objectIndex[objId] = id;
     Debug::log(Debug::Spam) << "LocalObjectTable::add(obj=" << obj << "): id=" << id
         << Debug::flush;
     // keep track that we hold a reference in the table
@@ -91,11 +71,6 @@
     return id;
   }
 
-  int find(NPObject* obj) {
-    void* objId = getIdentityFrom(obj);
-    return findIndex(objId);
-  }
-
   void free(int id) {
     Debug::log(Debug::Spam) << "LocalObjectTable::free(id=" << id << ")" << Debug::flush;
     if (unsigned(id) >= objects.size()) {
diff --git a/plugins/npapi/NPVariantWrapper.h b/plugins/npapi/NPVariantWrapper.h
index 0a469c9..91bcaaf 100644
--- a/plugins/npapi/NPVariantWrapper.h
+++ b/plugins/npapi/NPVariantWrapper.h
@@ -490,6 +490,12 @@
     return *this;
   }
 
+  // Convenience method for C++ code
+  NPVariantWrapper& operator=(int intval) {
+    NPVariantProxy::assignFrom(variant, intval);
+    return *this;
+  }
+
   void release() {
     NPVariantProxy::release(variant);
   }
diff --git a/plugins/npapi/ScriptableInstance.cpp b/plugins/npapi/ScriptableInstance.cpp
index b3c5c18..05547d8 100644
--- a/plugins/npapi/ScriptableInstance.cpp
+++ b/plugins/npapi/ScriptableInstance.cpp
@@ -65,7 +65,7 @@
     toStringID(NPN_GetStringIdentifier("toString")),
     connectedID(NPN_GetStringIdentifier("connected")),
     statsID(NPN_GetStringIdentifier("stats")),
-    savedID(NPN_GetStringIdentifier("saved")),
+    gwtId(NPN_GetStringIdentifier("__gwt_ObjectId")),
     jsInvokeID(NPN_GetStringIdentifier("__gwt_jsInvoke")),
     jsResultID(NPN_GetStringIdentifier("__gwt_makeResult")),
     jsTearOffID(NPN_GetStringIdentifier("__gwt_makeTearOff")),
@@ -145,7 +145,7 @@
     return true;
   }
   // TODO: special-case toString tear-offs
-  return name == statsID || name == connectedID || name == savedID;
+  return name == statsID || name == connectedID;
 }
 
 bool ScriptableInstance::getProperty(NPIdentifier name, NPVariant* variant) {
@@ -159,13 +159,6 @@
   } else if (name == statsID) {
     NPVariantProxy::assignFrom(*variant, "<stats data>");
     retVal = true;
-  } else if (name == savedID) {
-    if (savedValueIdx >= 0) {
-      NPObject* npObj = localObjects.get(savedValueIdx);
-      OBJECT_TO_NPVARIANT(npObj, *variant);
-      NPN_RetainObject(npObj);
-    }
-    retVal = true;
   }
   if (retVal) {
     // TODO: testing
@@ -179,14 +172,6 @@
   Debug::log(Debug::Debugging) << "ScriptableInstance::setProperty(name="
       << NPN_UTF8FromIdentifier(name) << ", value=" << *variant << ")"
       << Debug::flush; 
-  if (NPN_IdentifierIsString(name)) {
-    if (name == savedID && NPVariantProxy::isObject(*variant)) {
-      Debug::log(Debug::Debugging) << " set saved value" << Debug::flush;
-      savedValueIdx = localObjects.add(NPVariantProxy::getAsObject(*variant));
-      return true;
-    }
-    return false;
-  }
   return false;
 }
 
@@ -259,7 +244,6 @@
   // replace our window object with that passed by the caller
   window = NPVariantProxy::getAsObject(args[0]);
   NPN_RetainObject(window);
-  LocalObjectTable::setWrappedObjectClass(window->_class);
   BOOLEAN_TO_NPVARIANT(true, *result);
   result->type = NPVariantType_Bool;
 }
@@ -345,6 +329,22 @@
   result->type = NPVariantType_Bool;
 }
 
+int ScriptableInstance::getLocalObjectRef(NPObject* obj) {
+  NPVariantWrapper wrappedRetVal(*this);
+  int id;
+  if (NPN_GetProperty(getNPP(), obj, gwtId, wrappedRetVal.addressForReturn())
+      && wrappedRetVal.isInt()) {
+    id = wrappedRetVal.getAsInt();
+  } else {
+    id = localObjects.add(obj);
+    wrappedRetVal = id;
+    if (!NPN_SetProperty(getNPP(), obj, gwtId, wrappedRetVal.address())) {
+      Debug::log(Debug::Error) << "Setting GWT id on object failed" << Debug::flush;
+    }
+  }
+  return id;
+}
+
 void ScriptableInstance::fatalError(HostChannel& channel, const std::string& message) {
   // TODO(jat): better error handling
   Debug::log(Debug::Error) << "Fatal error: " << message << Debug::flush;
diff --git a/plugins/npapi/ScriptableInstance.h b/plugins/npapi/ScriptableInstance.h
index 035f823..cdce6f3 100644
--- a/plugins/npapi/ScriptableInstance.h
+++ b/plugins/npapi/ScriptableInstance.h
@@ -63,7 +63,7 @@
   
   void dumpJSresult(const char* js);
   
-  int getLocalObjectRef(NPObject* obj) { return localObjects.add(obj); }
+  int getLocalObjectRef(NPObject* obj);
   NPObject* getLocalObject(int refid) { return localObjects.get(refid); }
   
   bool tryGetStringPrimitive(NPObject* obj, NPVariant& result);
@@ -92,7 +92,7 @@
   
   const NPIdentifier connectedID;
   const NPIdentifier statsID;
-  const NPIdentifier savedID;
+  const NPIdentifier gwtId;
 
   const NPIdentifier jsInvokeID;
   const NPIdentifier jsResultID;
diff --git a/plugins/npapi/main.cpp b/plugins/npapi/main.cpp
index 065c0c0..c846834 100644
--- a/plugins/npapi/main.cpp
+++ b/plugins/npapi/main.cpp
@@ -280,11 +280,11 @@
     Debug::log(Debug::Info) << "NP_GetValue(var=" << variable << ")" << Debug::flush;
     switch (variable) {
       case NPPVpluginNameString:
-        *static_cast<const char **>(value) = "GWT Hosted-mode Plugin";
+        *static_cast<const char **>(value) = "GWT Development-Mode Plugin";
         break;
       case NPPVpluginDescriptionString:
         *static_cast<const char **>(value) = "Plugin to enable debugging of Google Web Toolkit "
-            "applications in hosted mode.";
+            "applications in development mode.";
         break;
       default:
         Debug::log(Debug::Info) << "NPP_GetValue(var=" << variable
diff --git a/plugins/npapi/prebuilt/winnt_x86-msvc/npOOPHM.dll b/plugins/npapi/prebuilt/winnt_x86-msvc/npOOPHM.dll
index 8e91518..a675dd9 100644
--- a/plugins/npapi/prebuilt/winnt_x86-msvc/npOOPHM.dll
+++ b/plugins/npapi/prebuilt/winnt_x86-msvc/npOOPHM.dll
Binary files differ