title: Cordova Phonegap inappbrowser losing File Api functionality
body: <p>I am developing an app using Web Audio Api. I have discovered that there is a memory leak in the way that Safari handles audio and doesn't garbage college the Audio Context correctly.  For this reason I wish to load a new page. Have that page create the Audio Context, complete the operation and then close the window, so that the memory is released.  </p>
  
  <p>I have done the following to achieve this. </p>
  
  <p><code>ref = window.open('record.html', '_self');</code>  This will open the record.html page in the Cordova WebView according to <a href="https://wiki.apache.org/cordova/InAppBrowser" rel="nofollow">https://wiki.apache.org/cordova/InAppBrowser</a></p>
  
  <blockquote>
    <p>1 window.open('local-url.html');// loads in the
    Cordova WebView<br>
    2 window.open('local-url.html', '_self');<br>
    // loads in the Cordova WebView</p>
  </blockquote>
  
  <p>The record.html page loads a javascript file, that runs the operations that I wish to run.  Here is the recordLoad.js file that makes some calls to native operations ( The native API is only available if loaded in the Cordova Webview and as you can see I need to access the file system, so this is the only way I can see to do it. </p>
  
  <pre><code>    window.onload = createAudioContext;
  ref = null;
  
  function createAudioContext(){
      console.log('createAudioContext');
      window.AudioContext = window.AudioContext || window.webkitAudioContext;
      navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
      window.URL = window.URL || window.webkitURL;
      audioContext = new AudioContext;
  
      getDirectory();
  }
  
  function getDirectory(){
      console.log('getDirectory');
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, getFileSystem, fail);
  
  }
  
  function getFileSystem(directory){
      console.log('getFileSystem');
      var audioPath = localStorage.getItem('audioPath');
      directory.root.getFile(audioPath, null, getVocalFile, fail);
  
  }
  
  function getVocalFile(fileEntry){
      console.log('getVocalFile');
      fileEntry.file(readVocalsToBuffer, fail);
  
  }
  
  function readVocalsToBuffer(file){
      console.log('readVocalsToBuffer');
      var reader = new FileReader();
      reader.onloadend = function(evt){
          var x = audioContext.decodeAudioData(evt.target._result, function(buffer){
              if(!buffer){
                  console.log('error decoding file to Audio Buffer');
                  return;
              }
              window.voiceBuffer = buffer;
              buffer = null;
              loadBuffers();
          });
  
      }
      reader.readAsArrayBuffer(file);
  }
  
  //web
  function loadBuffers(){
      console.log('loadBuffers');
      var srcSong = localStorage.getItem('srcSong');
      try{
          var bufferLoader = new BufferLoader(
              audioContext,
              [
                  "."+srcSong
              ],
              createOffLineContext
          );
          bufferLoader.load()
      }
      catch(e){
          console.log(e.message);
      }
  }
  
  
  //
  function createOffLineContext(bufferList){
      console.log('createOfflineContext');
      offline = new webkitOfflineAudioContext(2, window.voiceBuffer.length, 44100);
      var vocalSource = offline.createBufferSource();
      vocalSource.buffer = window.voiceBuffer;
      vocalSource.connect(offline.destination);
  
      var backing = offline.createBufferSource();
      backing.buffer = bufferList[0];
      backing.connect(offline.destination);
  
      vocalSource.start(0);
      backing.start(0);
  
      offline.oncomplete = function(ev){
          bufferList = null;
          console.log('audioContext');
          console.log(audioContext);
          audioContext = null;
          console.log(audioContext);
          vocalSource.stop(0);
          backing.stop(0);
          vocalSource.disconnect(0);
          backing.disconnect(0);
          vocalSource = null;
          backing = null;
          window.voiceBuffer = null;
          window.renderedFile = ev.renderedBuffer;
          var bufferR = ev.renderedBuffer.getChannelData(0);
          var bufferL = ev.renderedBuffer.getChannelData(1);
          var interleaved = interleave(bufferL, bufferR);
          var dataview = encodeWAV(interleaved);
          window.audioBlob = new Blob([dataview], {type: 'Wav'});
  
          saveFile();
      }
  
      offline.startRendering();
  
  }
  </code></pre>
  
  <p>// This file is very long, but once it is finished mixing the two audio buffers it writes a new file to the file system.  And when that operation is complete I use</p>
  
  <pre><code>function gotFileWriter(writer){
  console.log('gotFileWriter');
  writer.onwriteend = function(evt){
      console.log('onwriteEnd');
      console.log(window.audioBlob);
      delete window.audioBlob;
      console.log(window.audioBlob);
     // checkDirectory();
      var ref = window.open('index.html', '_self');
     // ref.addEventListener('exit', windowClose);
  
  }
  writer.write(audioBlob);
  </code></pre>
  
  <p>}</p>
  
  <p>I return back to the original index.html file. This solves the memory issues.  However, once I try to run the same operation a second time. ie load in the record.html file, and run the recordLoad.js file I receive an <strong>error ReferenceError: Can't find variable: LocalFileSystem</strong></p>
  
  <p>It would appear that in reload index.html some, but not all of the links to the Cordova API have been lost.  I can still for example use the Media Api but not the File Api.  I understand that this is a bit of a hacky way, (opening and closing windows) to solve the memory leak, but I cannot find any other way of doing it.  I really need some help with this. So any pointers very very welcome. </p>