chromium/chrome/test/data/extensions/platform_apps/web_view/filesystem/main/guest_main.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 requests 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;
      window.requestFileSystem = window.requestFileSystem ||
                                 window.webkitRequestFileSystem;
      window.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL ||
                                         window.webkitResolveLocalFileSystemURL;

      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;
        window.console.log('set totalCallbackCount to 0');
        totalCallbackCount = 0;
        successCallbackCount = 0;
        window.console.log('Call requestFileSystemAccess');
        requestFileSystemAccess();
      };

      var requestFileSystemAccess = function() {
        navigator.webkitPersistentStorage.requestQuota(1024 * 1024,
          function(grantedBytes) {
            window.console.log('request Quota granted.');
            window.requestFileSystem(window.PERSISTENT, 1024*1024,
                                     onFileSystemSuccess,
                                     onFileSystemFailure);
           }, function(e) { window.console.log('Error' + e); });
      };

      var onFileSystemSuccess = function(filesystem) {
        ++totalCallbackCount;
        ++successCallbackCount;

        window.console.log('onFileSystemSuccess, successCallbackCount: ' +
                           successCallbackCount + ', totalCallbackCount: ' +
                           totalCallbackCount);

        maybeNotifyEmbedder();
      };

      var onFileSystemFailure = function(err) {
        ++totalCallbackCount;
        window.console.log('onFileSystemFailure, totalCallbackCount: ' +
                           totalCallbackCount);
        maybeNotifyEmbedder();
      };

      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 requests filesystem.</div>
    <script>
      window.console.log('Guest loaded');
    </script>
  </body>
</html>