add PI to xul content sink
authorZbigniew Braniecki <gandalf@mozilla.com>
Tue, 17 Aug 2010 11:24:16 +0200
changeset 48094 2c96975a8187bae711b2e94be69b9a7eae3959ab
parent 48093 0266e068129fa09abeba315ba21a84a65fc56fa8
child 48095 4c5916e5c647c4b56369fdd5ad0d9b2646ac988f
push id9
push userzbraniecki@mozilla.com
push dateWed, 18 Aug 2010 21:50:49 +0000
milestone2.0b3pre
add PI to xul content sink
content/xul/document/src/nsXULContentSink.cpp
content/xul/document/src/nsXULContentSink.h
--- a/content/xul/document/src/nsXULContentSink.cpp
+++ b/content/xul/document/src/nsXULContentSink.cpp
@@ -119,16 +119,52 @@ struct L20n_JS {
 };
 
 static JSClass global_class = {
     "global", JSCLASS_GLOBAL_FLAGS,
     JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,
     JS_EnumerateStub, JS_ResolveStub,   JS_ConvertStub,   nsnull
 };
 
+nsresult createContext(L20n_JS* l20n_js, nsIWeakReference* mDocument)
+{
+    printf("createContext launched!\n");
+    if (!l20n_js->cx) {
+        nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
+        nsIScriptGlobalObject *sgo = doc->GetScriptGlobalObject();
+        nsCOMPtr<nsIScriptContext> context = sgo->GetContext();
+        l20n_js->setContext((JSContext *)context->GetNativeContext());
+        JSAutoRequest request(l20n_js->cx);
+        l20n_js->global = JS_NewGlobalObject(l20n_js->cx, &global_class);
+        if (!JS_InitStandardClasses(l20n_js->cx, l20n_js->global))
+            return NS_ERROR_OUT_OF_MEMORY;
+
+    }
+    return NS_OK;
+}
+
+JSScript* appendJSCode(L20n_JS* l20n_js, nsCString data)
+{
+    JSAutoRequest request(l20n_js->cx);
+    JSScript *s = JS_CompileScript(l20n_js->cx,
+                                  l20n_js->global,
+                                  data.get(),
+                                  data.Length(),
+                                  "",
+                                  1);
+    if (s) {
+        //printf("script compiled\n");
+        jsval rval;
+        JS_ExecuteScript(l20n_js->cx, l20n_js->global, s, &rval);
+    } else {
+        //printf("error\n");
+    }
+    return s;
+}
+
 //----------------------------------------------------------------------
 
 XULContentSinkImpl::ContextStack::ContextStack()
     : mTop(nsnull), mDepth(0)
 {
 }
 
 XULContentSinkImpl::ContextStack::~ContextStack()
@@ -707,25 +743,25 @@ XULContentSinkImpl::HandleCharacterData(
 NS_IMETHODIMP 
 XULContentSinkImpl::HandleProcessingInstruction(const PRUnichar *aTarget, 
                                                 const PRUnichar *aData)
 {
     FlushText();
     nsresult rv;
     const nsDependentString target(aTarget);
     const nsDependentString data(aData);
-    printf("%s\n", NS_LossyConvertUTF16toASCII(aTarget).get());
-    printf("%s\n", NS_LossyConvertUTF16toASCII(aData).get());
+    //printf("%s\n", NS_LossyConvertUTF16toASCII(aTarget).get());
+    //printf("%s\n", NS_LossyConvertUTF16toASCII(aData).get());
     if (target.Equals(NS_LITERAL_STRING("localize"))) {
         nsCOMPtr<nsIURI> uri;
         rv = NS_NewURI(getter_AddRefs(uri), data);
         NS_ENSURE_SUCCESS(rv, rv);
         nsCAutoString u;
         uri->GetSpec(u);
-        printf("Spec string: %s\n", u.get());
+        //printf("Spec string: %s\n", u.get());
         nsIIOService* ioService = nsContentUtils::GetIOService();
         NS_ENSURE_SUCCESS(rv, rv);
         //NS_TIMELINE_MARK("woopop!");
         nsCOMPtr<nsIChannel> channel;
         rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel));
         NS_ENSURE_SUCCESS(rv, rv);
 
         nsIInputStream *inputStream;
@@ -739,65 +775,45 @@ XULContentSinkImpl::HandleProcessingInst
             if (NS_FAILED(rv)) {
                 break;
             }
             if (amt == 0)
                 break;
             temp.Append(buf, amt);
         }
 
-        printf("Read from file: %s\n", nsPromiseFlatCString(temp).get());
+        //printf("Read from file: %s\n", nsPromiseFlatCString(temp).get());
 
         if (l20n_js->cx) {
-            printf("the context already exists before we check for it!\n");
+            //printf("the context already exists before we check for it!\n");
         } else {
-            printf("the context does not exist yet\n");
+            //printf("the context does not exist yet\n");
         }
-        if (!l20n_js->cx) {
-            printf("getting context\n");
-            nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
-            nsIScriptGlobalObject *sgo = doc->GetScriptGlobalObject();
-            nsCOMPtr<nsIScriptContext> context = sgo->GetContext();
-            l20n_js->setContext((JSContext *)context->GetNativeContext());
-            printf("context aquired\n");
-
-            JSAutoRequest request(l20n_js->cx);
-            printf("New Global Object\n");
-            l20n_js->global = JS_NewGlobalObject(l20n_js->cx, &global_class);
-            
-            /* Populate the global object with the standard globals,
-            like Object and Array. */
-            if (!JS_InitStandardClasses(l20n_js->cx, l20n_js->global))
-                return NS_ERROR_OUT_OF_MEMORY;
-          }
+        createContext(l20n_js, mDocument);
+        JSScript *s = appendJSCode(l20n_js, temp);
 
-        JSAutoRequest request(l20n_js->cx);
-        JSScript *s = JS_CompileScript(l20n_js->cx,
-                                      l20n_js->global,
-                                      temp.BeginReading(),
-                                      temp.Length(),
-                                      NS_LossyConvertUTF16toASCII(aData).get(),
-                                      1);
         if (s) {
-            printf("script compiled\n");
-            jsval rval;
-            JS_ExecuteScript(l20n_js->cx, l20n_js->global, s, &rval);
-            printf("script executed\n");
-            js::AutoIdArray prop_array(l20n_js->cx,
-                                       JS_Enumerate(l20n_js->cx,
-                                                    l20n_js->global));
-            printf("prop array length: %lu\n", prop_array.length());
-            for (size_t i = 0; i < prop_array.length(); ++i) {
-                    jsval id;
-                    JS_IdToValue(l20n_js->cx, prop_array[i], &id);
-                    printf("%s\n", JS_GetStringBytes(JSVAL_TO_STRING(id)));
-                }
+            //printf("script executed\n");
+            //js::AutoIdArray prop_array(l20n_js->cx,
+            //                          JS_Enumerate(l20n_js->cx,
+            //                                      l20n_js->global));
+            //printf("prop array length: %lu\n", prop_array.length());
+            //for (size_t i = 0; i < prop_array.length(); ++i) {
+            //        jsval id;
+            //        JS_IdToValue(l20n_js->cx, prop_array[i], &id);
+                    //printf("%s\n", JS_GetStringBytes(JSVAL_TO_STRING(id)));
+            //}
         } else {
-            printf("error\n");
+            //printf("error\n");
         }
+    } else if (target.Equals(NS_LITERAL_STRING("l10n-data"))) {
+        printf("l10n-data: %s\n", NS_LossyConvertUTF16toASCII(aData).get());
+        createContext(l20n_js, mDocument);
+        nsCString temp2 = NS_ConvertUTF16toUTF8(aData);
+        JSScript *s = appendJSCode(l20n_js, temp2);
     }
     // Note: the created nsXULPrototypePI has mRefCnt == 1
     nsRefPtr<nsXULPrototypePI> pi = new nsXULPrototypePI();
     if (!pi)
         return NS_ERROR_OUT_OF_MEMORY;
 
     pi->mTarget = target;
     pi->mData = data;
@@ -1297,23 +1313,23 @@ XULContentSinkImpl::AddAttributes(const 
   for (i = 0; i < aAttrLen; ++i) {
       nsString str;
       rv = NormalizeAttributeString(aAttributes[i * 2],
                                     aElement->mAttributes[i].mName);
       NS_ENSURE_SUCCESS(rv, rv);
       attrs[i].mName.GetQualifiedName(str);
       if (str.Equals(NS_LITERAL_STRING("l10n_id"))) {
           if (!l20n_js->cx) {
-            printf("found l10n id but I have not context. ignoring\n");
+            //printf("found l10n id but I have not context. ignoring\n");
             continue;
           }
         jsval rval;
         JSBool res;
-        printf("detected l10n id\n");
-        printf("%s\n", NS_LossyConvertUTF16toASCII(aAttributes[i*2 + 1]).get());
+        //printf("detected l10n id\n");
+        //printf("%s\n", NS_LossyConvertUTF16toASCII(aAttributes[i*2 + 1]).get());
         JSAutoRequest request(l20n_js->cx);
         res = JS_GetUCProperty(l20n_js->cx,
               l20n_js->global,
               aAttributes[i*2 + 1],
               nsDependentString(aAttributes[i*2 + 1]).Length(),
               &rval);
         if (res == JS_TRUE) {
             JSObject *obj;
@@ -1346,18 +1362,18 @@ XULContentSinkImpl::AddAttributes(const 
                                                  extAttrs[aAttrLen + j].mName);
                     NS_ENSURE_SUCCESS(rv, rv);
                     JS_GetPropertyById(l20n_js->cx, obj, idval, &rval);
                     nsAutoString val(NS_ConvertUTF8toUTF16(JS_GetStringBytes(JSVAL_TO_STRING(rval))));
                     rv = aElement->SetAttrAt(aAttrLen + j, val, mDocumentURL);
                     NS_ENSURE_SUCCESS(rv, rv);
                 }
             }
-        } else
-            printf("not found\n");
+        } else {}
+            //printf("not found\n");
       }
       rv = aElement->SetAttrAt(i, nsDependentString(aAttributes[i * 2 + 1]),
                                mDocumentURL);
       NS_ENSURE_SUCCESS(rv, rv);
 
 #ifdef PR_LOGGING
       if (PR_LOG_TEST(gLog, PR_LOG_DEBUG)) {
           nsAutoString extraWhiteSpace;
--- a/content/xul/document/src/nsXULContentSink.h
+++ b/content/xul/document/src/nsXULContentSink.h
@@ -53,16 +53,20 @@
 class nsIDocument;
 class nsIScriptSecurityManager;
 class nsAttrName;
 class nsXULPrototypeDocument;
 class nsXULPrototypeElement;
 class nsXULPrototypeNode;
 struct L20n_JS;
 
+nsresult createContext(L20n_JS* l20n_js, nsIDocument* mDocument);
+
+JSScript* appendJSCode(L20n_JS* l20n_js, nsCString data);
+
 class XULContentSinkImpl : public nsIXMLContentSink,
                            public nsIExpatSink
 {
 public:
     XULContentSinkImpl();
     virtual ~XULContentSinkImpl();
 
     // nsISupports