chromium/chrome/test/data/extensions/platform_apps/web_view/filesystem/worker/guest_worker.html

<!--
Copyright 2014 The Chromium Authors
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<html>
  <head>
    <script type="text/javascript">
      // A guest that ceate a worker to request filesystem.
      // Notifies the embedder about the result of the request (success/fail)
      // via post message. Note that the embedder has to initiate a postMessage
      // first so that guest has a reference to the embedder's window.

      // The window reference of the embedder to send post message reply.
      var embedderWindowChannel = null;

      var expectedTotalCallbackCount;
      var totalCallbackCount;
      var successCallbackCount;
      var testName = 'uninitialized';

      var maybeNotifyEmbedder = function() {
        window.console.log('maybeNotifyEmbedder' +
                           ', expectedTotalCallbackCount: ' +
                           expectedTotalCallbackCount +
                           ', successCallbackCount: ' +
                           successCallbackCount +
                           ', totalCallbackCount: ' +
                           totalCallbackCount);
        if(expectedTotalCallbackCount == totalCallbackCount) {
          var status = (expectedTotalCallbackCount == successCallbackCount) ?
                  'access-granted' : 'access-denied';
          var responseArray = [testName, status];
          notifyEmbedder(responseArray);
        }
      };

      var notifyEmbedder = function(msg_array) {
        embedderWindowChannel.postMessage(JSON.stringify(msg_array), '*');
      };

      var startTest = function() {
        expectedTotalCallbackCount = 1;
        totalCallbackCount = 0;
        successCallbackCount = 0;

        window.console.log('Call initWorker');
        initWorker();
      };

      var initWorker = function() {
        window.worker = new Worker('worker.js');
        worker.addEventListener('message', function(e) {
          var data = e.data;
          switch (data.type) {
           case 'echo':
              window.console.log('echo: ' + data.msg);
              break;
           case 'error':
              window.console.log('error: ' + data.msg);
              break;
           case 'result':
              window.console.log('result: ' + data.msg);
              successCallbackCount += parseInt(data.msg);
              ++totalCallbackCount;
              maybeNotifyEmbedder();
              break;
           default:
              window.console.log('UNKNOWN MESSAGE FROM WORKER');
              break;
           }
        }, false);
        worker.postMessage({'type': 'requestFileSystem'});
      };

      var onPostMessageReceived = function(e) {
        window.console.log('guest.onPostMessageReceived');
        var data = JSON.parse(e.data);
        if (data[0] == 'check-filesystem-permission') {
          testName = data[1];
          embedderWindowChannel = e.source;
          // Start the test once we have |embedderWindowChannel|.
          startTest();
        }
      };
      addEventListener('message', onPostMessageReceived, false);
    </script>
  </head>
  <body>
    <div>This is a guest that create a worker to request filesystem.</div>
    <script>
      window.console.log('Guest loaded');
    </script>
  </body>
</html>