Revision: 4411
Author:   metaweta
Date:     Wed Mar 23 23:06:23 2011
Log: Amend caja.js API to support the four methods of providing cajoled code.
http://codereview.appspot.com/4211045

Changes caja.js to support
- telling the cajoling service to fetch and cajole content at a url
- fetching cached, already cajoled code at a url
- posting uncajoled content to the cajoling service
- when the cajoler is part of the web server, running cajoled code
  provided as a string in the page.

[email protected]

http://code.google.com/p/google-caja/source/detail?r=4411

Modified:
 /trunk/src/com/google/caja/demos/photon/container/main.js
 /trunk/src/com/google/caja/es53.js
 /trunk/src/com/google/caja/plugin/caja.js
 /trunk/tests/com/google/caja/plugin/default-test-driver.js
 /trunk/tests/com/google/caja/plugin/es53-test-taming-inout.js
 /trunk/tests/com/google/caja/plugin/es53-test-taming-tamed.js
 /trunk/tests/com/google/caja/plugin/es53-test-taming-untamed.js

=======================================
--- /trunk/src/com/google/caja/demos/photon/container/main.js Mon Mar 21 15:40:29 2011 +++ /trunk/src/com/google/caja/demos/photon/container/main.js Wed Mar 23 23:06:23 2011
@@ -80,11 +80,12 @@
     frameGroup.makeES5Frame(feralContainerDiv,
                             defaultURIPolicy,
                             function (frame) {
-      frame.run(moduleUrl, extraOuters, function (result) {
-        if (resultCallback) {
-          resultCallback(result);
-        }
-      });
+      frame.url(moduleUrl)
+           .run(extraOuters, function (result) {
+              if (resultCallback) {
+                resultCallback(result);
+              }
+            });
     });
   };

=======================================
--- /trunk/src/com/google/caja/es53.js  Wed Mar 23 15:56:33 2011
+++ /trunk/src/com/google/caja/es53.js  Wed Mar 23 23:06:23 2011
@@ -149,7 +149,7 @@
* {@code g.f___(dis, [as])} is the tamed version of {@code g},
    *                                      though it uses {@code apply}'s
    *                                      interface.
-   * {@code g.i___(as)}                   = g.f___(___.USELESS, [as])
+   * {@code g.i___(as)}                   = g.f___(USELESS, [as])
    * {@code g.new___(as)}                 is the tamed version of {@code g}
* used for constructing an object of
    *                                      class {@code g}.
@@ -3151,13 +3151,13 @@
   var poisonFuncArgs = makePoisonPill("A function's .arguments");

   /**
-   * Function calls g(args) get translated to g.f___(___.USELESS, args)
+   * Function calls g(args) get translated to g.f___(USELESS, args)
* Tamed functions and cajoled functions install an overriding fastpath f___
    * to apply, the original Function.prototype.apply.
    */
   Function.prototype.f___ = callFault;
   Function.prototype.i___ = function(var_args) {
-      return this.f___(___.USELESS, slice.call(arguments, 0));
+      return this.f___(USELESS, slice.call(arguments, 0));
     };
   Function.prototype.new___ = callFault;
   Function.prototype.DefineOwnProperty___('arguments', {
@@ -4060,7 +4060,7 @@
       }
       for (; i < len; i++) {
         if (i in this) {
-          rv = fun.f___(___.USELESS, [rv, this[i], i, this]);
+          rv = fun.f___(USELESS, [rv, this[i], i, this]);
         }
       }
       return rv;
@@ -4094,7 +4094,7 @@
       }
       for (; i >= 0; i--) {
         if (i in this) {
-          rv = fun.f___(___.USELESS, [rv, this[i], i, this]);
+          rv = fun.f___(USELESS, [rv, this[i], i, this]);
         }
       }
       return rv;
=======================================
--- /trunk/src/com/google/caja/plugin/caja.js   Mon Mar 21 15:40:29 2011
+++ /trunk/src/com/google/caja/plugin/caja.js   Wed Mar 23 23:06:23 2011
@@ -232,16 +232,16 @@
* @param div a <DIV> in the parent document within which the guest HTML's * virtual document will be confined. This parameter may be undefined,
        *     in which case a secure DOM document will not be constructed.
-       * @param uriCallback a policy callback that is called to allow or
+       * @param uriPolicy a policy callback that is called to allow or
* disallow access each time guest code attempts to fetch from a URI. - * This is of the form <code>uriCallback(uri, mimeType)</code>, where + * This is of the form <code>uriPolicy(uri, mimeType)</code>, where * <code>uri</code> is a string URI, and <code>mimeType</code> is a * string MIME type based on the context in which the URI is being
        *     requested.
        * @param callback a function that is called back when the newly
        *     constructed ES5 frame has been created.
        */
-      function makeES5Frame(div, uriCallback, callback) {
+      function makeES5Frame(div, uriPolicy, callback) {
         if (div && (document !== div.ownerDocument)) {
           throw '<div> provided for ES5 frame must be in main document';
         }
@@ -261,7 +261,13 @@
             var innerContainer = div.ownerDocument.createElement('div');

             outerContainer.setAttribute('class', 'caja_outerContainer___');
-            innerContainer.setAttribute('class', 'caja_innerContainer___');
+            innerContainer.setAttribute('class', 'caja_innerContainer___');
+
+ // Copy over any existing children (like static HTML produced by
+            // the cajoler) into the inner container.
+            while (div.childNodes[0]) {
+              innerContainer.appendChild(div.childNodes[0]);
+            }

             div.appendChild(outerContainer);
             outerContainer.appendChild(innerContainer);
@@ -271,43 +277,117 @@
             // ability of guest code to modify the shared primordials.
             tamingWindow.attachDocumentStub(
                 '-CajaGadget-' + guestDocumentIdIndex++ + '___',
-                uriCallback,
+                uriPolicy,
                 imports,
                 innerContainer);
             imports.htmlEmitter___ =
new tamingWindow.HtmlEmitter(innerContainer, imports.document);
+            guestWindow.plugin_dispatchEvent___ =
+                tamingWindow.plugin_dispatchEvent___;
+            guestWindow.plugin_dispatchToHandler___ =
+                tamingWindow.plugin_dispatchToHandler___;
           }

-          /**
-           * Run some guest code in this ES5 frame.
-           *
-           * @param url the URL of a cajoleable "guest" HTML file to load.
- * @param extraImports a map of extra imports to be provided as global
-           *     variables to the guest HTML.
- * @param callback a function that is called providing the completion
-           *     value of the guest code.
-           */
-          function run(url, extraImports, callback) {
-            if (!extraImports.hasOwnProperty('onerror')) {
-              extraImports.onerror = tame(markFunction(
-                  function (message, source, lineNum) {
-                    console.log('Uncaught script error: ' + message
- + ' in source: "' + source + '" at line: ' + lineNum);
-                  }));
-            }
-            copyToImports(imports, extraImports);
-            guestWindow.Q.when(loader.async(url), function (moduleFunc) {
-              callback(moduleFunc(imports));
-            });
+          function runMaker(func) {
+            return {
+                /**
+                 * Run some guest code in this ES5 frame.
+                 *
+ * @param extraImports a map of extra imports to be provided
+                 *     as global variables to the guest HTML.
+                 * @param callback a function that is called providing the
+                 *     completion value of the guest code.
+                 */
+                run: function(extraImports, opt_callback) {
+                    if (!extraImports.hasOwnProperty('onerror')) {
+                      extraImports.onerror = tame(markFunction(
+                          function (message, source, lineNum) {
+ console.log('Uncaught script error: ' + message +
+                                ' in source: "' + source +
+                                '" at line: ' + lineNum);
+                          }));
+                    }
+                    copyToImports(imports, extraImports);
+                    func(imports, opt_callback);
+                  }
+              };
+          }
+
+          function cajoledRunner(baseUrl, cajoledJs, opt_staticHtml) {
+            if (!div && opt_staticHtml) {
+              throw new Error('Must have supplied a div in order to ' +
+                'set staticHtml.');
+            }
+            // TODO: How to tell the module to use baseUrl?
+            // Or does that have to happen at cajoling time?
+            return function(imports, opt_callback) {
+                if (opt_staticHtml) {
+                  innerContainer.innerHTML = opt_staticHtml;
+                }
+                var preparedModule = guestWindow.prepareModuleFromText___(
+                   cajoledJs);
+                var result = preparedModule(imports);
+                // If a callback is provided, we call it
+                // with the completion value.
+                if (opt_callback) {
+                  opt_callback(result);
+                }
+              };
           }

-          // An ES5 frame
+          function contentCajoled(baseUrl, cajoledJs, opt_staticHtml) {
+ return runMaker(cajoledRunner(baseUrl, cajoledJs, opt_staticHtml));
+          }
+
+          function urlCajoled(url) {
+            return runMaker(function (imports, opt_callback) {
+                // XHR get the cajoled content.
+                // cajoledRunner(
+                //     url,
+                //     content.js,
+                //     content.staticHtml)(
+                //     imports,
+                //     opt_callback);
+                throw new Error("Not yet implemented.");
+              });
+          }
+
+          function content(baseUrl, html) {
+            return runMaker(function (imports, opt_callback) {
+                // XHR post the html through the cajoler to get
+                //   cajoled content.
+                // cajoledRunner(
+                //     baseUrl,
+                //     content.js,
+                //     content.staticHtml)(
+                //     imports,
+                //     opt_callback);
+                throw new Error('Not yet implemented.');
+              });
+          }
+
+          function url(theUrl) {
+            return runMaker(function (imports, opt_callback) {
+                guestWindow.Q.when(
+                    loader.async(theUrl),
+                    function (moduleFunc) {
+                      var result = moduleFunc(imports);
+                      if (opt_callback) {
+                        opt_callback(result);
+                      }
+                    });
+              });
+          }
+
           callback({
-            run: run,
-            iframe: guestFrame,
-            imports: imports,
-            loader: loader
-          });
+              url: url,
+              urlCajoled: urlCajoled,
+              content: content,
+              contentCajoled: contentCajoled,
+              iframe: guestFrame,
+              imports: imports,
+              loader: loader
+            });
         });
       }

=======================================
--- /trunk/tests/com/google/caja/plugin/default-test-driver.js Thu Nov 18 22:30:18 2010 +++ /trunk/tests/com/google/caja/plugin/default-test-driver.js Wed Mar 23 23:06:23 2011
@@ -23,13 +23,12 @@
         createDiv(),
         function(uri, mimeType) { return uri; },
         function(frame) {
-          frame.run(
-              testCase,
-              createExtraImportsForTesting(frameGroup, frame),
-              function(result) {
-                readyToTest();
-                jsunitRun();
-              });
+          frame.url(testCase)
+               .run(createExtraImportsForTesting(frameGroup, frame),
+                   function(result) {
+                     readyToTest();
+                     jsunitRun();
+                   });
         });
   });
 } else {
=======================================
--- /trunk/tests/com/google/caja/plugin/es53-test-taming-inout.js Mon Feb 21 22:02:20 2011 +++ /trunk/tests/com/google/caja/plugin/es53-test-taming-inout.js Wed Mar 23 23:06:23 2011
@@ -114,11 +114,11 @@
       extraImports.getTamedTestObject = getTamedTestObject;
       extraImports.evalInHost = evalInHost;

-      frame.run('es53-test-taming-inout-cajoled.html', extraImports,
-          function (_) {
-            readyToTest();
-            jsunitRun();
-          });
+      frame.url('es53-test-taming-inout-cajoled.html')
+           .run(extraImports, function (_) {
+               readyToTest();
+               jsunitRun();
+             });
     });
   });
 })();
=======================================
--- /trunk/tests/com/google/caja/plugin/es53-test-taming-tamed.js Mon Feb 21 22:02:20 2011 +++ /trunk/tests/com/google/caja/plugin/es53-test-taming-tamed.js Wed Mar 23 23:06:23 2011
@@ -312,11 +312,11 @@
       };
       extraImports.evalInHost.i___ = extraImports.evalInHost;

-      frame.run('es53-test-taming-tamed-cajoled.html', extraImports,
-          function (_) {
-            readyToTest();
-            jsunitRun();
-          });
+      frame.url('es53-test-taming-tamed-cajoled.html')
+           .run(extraImports, function (_) {
+               readyToTest();
+               jsunitRun();
+             });
     });
   });
 })();
=======================================
--- /trunk/tests/com/google/caja/plugin/es53-test-taming-untamed.js Mon Feb 21 22:02:20 2011 +++ /trunk/tests/com/google/caja/plugin/es53-test-taming-untamed.js Wed Mar 23 23:06:23 2011
@@ -59,11 +59,11 @@
             return eval(String(s));
           }));

-      frame.run('es53-test-taming-untamed-cajoled.html', extraImports,
-          function (_) {
-            readyToTest();
-           jsunitRun();
-         });
+      frame.url('es53-test-taming-untamed-cajoled.html')
+           .run(extraImports, function (_) {
+               readyToTest();
+               jsunitRun();
+             });
     });
   });
 })();

Reply via email to