Page 1 of 1

CrossWalk API?

Posted: Sat Feb 11, 2017 12:27 pm
by Peter
Hello,

i'm not really familiar with CrossWalk, but isn't there an API for accessing the hardware of the device (for example the filesystem)?

For Cordova there are special plugins (see e.g.: https://cordova.apache.org/docs/en/late ... index.html)

So as long as there is no native Solution to store data on an Android-Device, perhaps we can code our own wrapper.

Thanks in advance & Greetings ... Peter

Re: CrossWalk API?

Posted: Sun Feb 12, 2017 11:05 am
by tj1010
You can reverse engineer a binary or you can trial and error with inline JS. No docs on this or hints. I don't have time to dig in to a APK looking for interfaces but want to do stuff with Android keystore and the sandbox file system and probably audio too. Definitly GPS and probably sleep policy too.

Re: CrossWalk API?

Posted: Sun Feb 12, 2017 1:33 pm
by Fred
You can take a look here: https://crosswalk-project.org/documenta ... _apis.html

All supported API should be available from JS without issue

Re: CrossWalk API?

Posted: Mon Feb 13, 2017 7:08 am
by tj1010
They use wrappers for W3 stuff so things like domain policies and implementation absence are still in effect.

Re: CrossWalk API?

Posted: Mon Feb 13, 2017 4:36 pm
by Peter
tj1010 wrote:They use wrappers for W3 stuff [...]
... and that's my problem. After deploying the project to a mobile device, the so called persistent files are gone.

Here is a little snippet to demonstrate that effect. Create an Android-App and deploy it to a device. Click on 'Create' and several times on 'Append'. After a click on 'Read' several lines are printed out in the debug-window. After deloying the code again, the 'log.txt' is gone.

Code: Select all

Enumeration
  #frmMain
EndEnumeration

Enumeration
  #frmMain_cmdCreate
  #frmMain_cmdAppend
  #frmMain_cmdRead
EndEnumeration

Procedure cmdCreateEvent()
  
  ! function create_errorHandler(e) { spider.debug.Print( 'create file error: ', e.name, ', ', e.message ); }
  
  ! function create_onInitFs(fs) {
  !   fs.root.getFile('log.txt', {create: true}, function(fileEntry) {
  !     fileEntry.createWriter(function(fileWriter) {
  !       fileWriter.onwriteend = function(e) { spider.debug.Print('Create completed.'); };
  !       fileWriter.onerror    = function(e) { spider.debug.Print('Create failed: ' + e.toString()); };
  !       var blob = new Blob(['filecreated\n'], {type: 'text/plain'});
  !       fileWriter.write(blob);
  !     }, create_errorHandler);
  !   }, create_errorHandler);
  ! }
  
  ! var requestedBytes = 1024*1024*280; 
  ! 
  ! navigator.webkitPersistentStorage.requestQuota (
  !   requestedBytes, function(grantedBytes) {  
  !     window.webkitRequestFileSystem(PERSISTENT, grantedBytes, create_onInitFs, create_errorHandler); 
  !   },
  !   function(e) { spider.debug.Print('Error', e); }
  ! );
  
EndProcedure

Procedure cmdAppendEvent()
  
  ! function append_errorHandler(e) { spider.debug.Print('write file error: ', e.name, ', ', e.message); }
  
  ! function append_onInitFs(fs) {
  !   fs.root.getFile('log.txt', {create: false}, function(fileEntry) {
  !     fileEntry.createWriter(function(fileWriter) {
  !       fileWriter.onwriteend = function(e) { spider.debug.Print('Append completed.'); };
  !       fileWriter.onerror    = function(e) { spider.debug.Print('Append failed: ' + e.toString()); };
  !       fileWriter.seek(fileWriter.length); // Start write position at EOF.
  !       var blob = new Blob(['Lorem Ipsum\n'], {type: 'text/plain'});
  !       fileWriter.write(blob);
  !     }, append_errorHandler);
  !   }, append_errorHandler);
  ! }
  
  ! var requestedBytes = 1024*1024*280; 
  ! 
  ! navigator.webkitPersistentStorage.requestQuota (
  !   requestedBytes, function(grantedBytes) {  
  !     window.webkitRequestFileSystem(PERSISTENT, grantedBytes, append_onInitFs, append_errorHandler); 
  !   },
  !   function(e) { spider.debug.Print('Error', e); }
  ! );
  
EndProcedure

Procedure cmdReadEvent()
  
  ! function read_errorHandler(e) { spider.debug.Print('write file error: ', e.name, ', ', e.message); }
  
  ! function read_onInitFs(fs) {
  !   fs.root.getFile('log.txt', {}, function(fileEntry) {
  !     fileEntry.file(function(file) {
  !        var reader = new FileReader();
  !        reader.onloadend = function(e) { spider.debug.Print(this.result); };
  !        reader.readAsText(file);
  !     }, read_errorHandler);
  !   }, read_errorHandler);
  ! }
  
  ! var requestedBytes = 1024*1024*280; 
  ! 
  ! navigator.webkitPersistentStorage.requestQuota (
  !   requestedBytes, function(grantedBytes) {  
  !     window.webkitRequestFileSystem(PERSISTENT, grantedBytes, read_onInitFs, read_errorHandler); 
  !   },
  !   function(e) { spider.debug.Print('Error', e); }
  ! );
  
EndProcedure

OpenWindow(#frmMain, 0, 0, 0, 0, "Test", #PB_Window_Background)

ButtonGadget(#frmMain_cmdCreate, 10,  10, 300, 50, "Create file")
ButtonGadget(#frmMain_cmdAppend, 10,  70, 300, 50, "Append text")
ButtonGadget(#frmMain_cmdRead,   10, 130, 300, 50, "Read file")

BindGadgetEvent(#frmMain_cmdCreate, @cmdCreateEvent())
BindGadgetEvent(#frmMain_cmdAppend, @cmdAppendEvent())
BindGadgetEvent(#frmMain_cmdRead,   @cmdReadEvent())
Greetings ... Peter (now coding a cordova-wrapper)

Re: CrossWalk API?

Posted: Mon Feb 13, 2017 4:44 pm
by Fred
before coding it, did you take a look to this plugin ? https://github.com/apache/cordova-plugin-file

Re: CrossWalk API?

Posted: Mon Feb 13, 2017 5:02 pm
by Peter
Fred wrote:before coding it, did you take a look to this plugin ? https://github.com/apache/cordova-plugin-file
yes, i already use this plugin for cordova. Or can it be used also for crosswalk?


sneak preview:

Code: Select all

DeclareModule Cordova
  
  Enumeration
    #applicationDirectory
    #applicationStorageDirectory
    #cacheDirectory
    #dataDirectory
    #documentsDirectory
    #externalApplicationStorageDirectory
    #externalCacheDirectory
    #externalDataDirectory
    #externalRootDirectory
    #sharedDirectory
    #syncedDataDirectory
    #tempDirectory
  EndEnumeration
  
  Declare Init(OnDeviceReadyCallback)
  
  Declare.s GetSpecialDirectory(SpecialDirectoryType)
  
  Declare   DeleteFile(Directory.s, Filename.s, Callback = 0)
  
  Declare.s FileToString(Directory.s, Filename.s, Callback = 0)
  
  Declare   StringToFile(Directory.s, Filename.s, FileContent.s, Callback = 0)
  
  Declare   FileExists(Directory.s, Filename.s, Callback = 0)
  
EndDeclareModule

Module Cordova
  
  Procedure   Init(OnDeviceReadyCallback)
    
    ! $('<script type="text/javascript"></script>').attr('src', 'cordova.js').appendTo('head');
    
    ! document.addEventListener("deviceready", v_ondevicereadycallback, false);
    
  EndProcedure
  
  Procedure.s GetSpecialDirectory(SpecialDirectoryType)
    
    ! if (!cordova.file) {
    Debug "Missing plugin?"
    ProcedureReturn ""
    ! }
    
    Select SpecialDirectoryType
      Case #applicationDirectory
        ! return cordova.file.applicationDirectory
      Case #applicationStorageDirectory
        ! return cordova.file.applicationStorageDirectory
      Case #cacheDirectory
        ! return cordova.file.cacheDirectory
      Case #dataDirectory
        ! return cordova.file.dataDirectory
      Case #documentsDirectory
        ! return cordova.file.documentsDirectory
      Case #externalApplicationStorageDirectory
        ! return cordova.file.externalApplicationStorageDirectory
      Case #externalCacheDirectory
        ! return cordova.file.externalCacheDirectory
      Case #externalDataDirectory
        ! return cordova.file.externalDataDirectory
      Case #externalRootDirectory
        ! return cordova.file.externalRootDirectory
      Case #sharedDirectory
        ! return cordova.file.sharedDirectory
      Case #syncedDataDirectory
        ! return cordova.file.syncedDataDirectory
      Case #tempDirectory
        ! return cordova.file.tempDirectory
      Default
        ProcedureReturn ""
    EndSelect
    
  EndProcedure
  
  Procedure   DeleteFile(Directory.s, Filename.s, Callback = 0)
    
    ! var DeleteFile_onSuccess = function(fileEntry) {
    !   // console.log("The file has been removed succesfully");
    !   if (typeof v_callback == 'function') v_callback(true);
    ! };    
    
    ! var DeleteFile_onError = function(e) {
    !   // console.log("Error removing file: " + e.code);
    !   if (typeof v_callback == 'function') v_callback(false);
    ! };    
    
    ! window.resolveLocalFileSystemURL(v_directory + v_filename,
    !   function(fileEntry) {
    !     fileEntry.remove(DeleteFile_onSuccess, DeleteFile_onError);
    !   },
    !   function(e) {
    !     if (typeof v_callback == 'function') v_callback(false);
    !   }
    ! );
    
  EndProcedure
    
  Procedure.s FileToString(Directory.s, Filename.s, Callback = 0)
    
    ! var FileToString_onSuccess = function(fileEntry) {
    !   fileEntry.file(function(file) {
    !     var reader = new FileReader();
    !     reader.onloadend = function(e) {
    !       if (typeof v_callback == 'function') v_callback(true, this.result);
    !     }
    !     reader.readAsText(file);
    !   });
    ! };
    
    ! var FileToString_onError = function(e) {
    !   // console.log("FileToString: error");
    !   // console.log(e);
    !   if (typeof v_callback == 'function') v_callback(false, "");
    ! };
    
    ! window.resolveLocalFileSystemURL(v_directory + v_filename, FileToString_onSuccess, FileToString_onError);
    
  EndProcedure
  
  Procedure   StringToFile(Directory.s, Filename.s, FileContent.s, Callback = 0)
    
    ! var StringToFile_getFile_onSuccess = function(file) {
    !   file.createWriter(function(fileWriter) {
    !     var blob = new Blob([v_filecontent], {type:'text/plain'});
    !     fileWriter.write(blob);
    !     if (typeof v_callback == 'function') v_callback(true);
    !   });
    ! };
    
    ! var StringToFile_getFile_onError = function(e) {
    !   // console.log("StringToFile (getFile): error");
    !   // console.log(e);
    !   if (typeof v_callback == 'function') v_callback(false);
    ! };
    
    ! var StringToFile_onSuccess = function(dir) {
    !   dir.getFile( v_filename, {create:true}, StringToFile_getFile_onSuccess, StringToFile_getFile_onError);
    ! };
    
    ! var StringToFile_onError = function(e) {
    !   // console.log("StringToFile: error");
    !   // console.log(e);
    !   if (typeof v_callback == 'function') v_callback(false);
    ! };
    
    ! window.resolveLocalFileSystemURL(v_directory, StringToFile_onSuccess, StringToFile_onError);
    
  EndProcedure
  
  Procedure FileExists(Directory.s, Filename.s, Callback = 0)
    
    ! var FileExists_onSuccess = function(fileEntry) {
    !   if (typeof v_callback == 'function') v_callback(fileEntry.isFile);
    ! };
    
    ! var FileExists_onError = function(e) {
    !   if (typeof v_callback == 'function') v_callback(false);
    ! };
    
    ! window.resolveLocalFileSystemURL(v_directory + v_filename, FileExists_onSuccess, FileExists_onError);
    
  EndProcedure
  
EndModule

; Example

EnableExplicit

Global SpecialDirectory.s

Procedure DeleteFileCallback(Success)
  
  Debug "DeleteFileCallback()"
  Debug "Success: " + Str(Success)
  Debug "---"
  
EndProcedure

Procedure FileToStringCallback(Success, Result.s)
  
  Debug "FileToStringCallback()"
  Debug "Success: " + Str(Success)
  Debug "Result: " + Result
  Debug "---"
  
  Debug "now i try to delete..."
  Cordova::DeleteFile(SpecialDirectory, "file.txt", @DeleteFileCallback())
  
EndProcedure

Procedure FileExistsCallback(FileExists)
  
  Debug "FileExistsCallback()"
  Debug "FileExists: " + Str(FileExists)
  
  If FileExists
    
    Debug "now i try to read the file..."
    Cordova::FileToString(SpecialDirectory, "file.txt", @FileToStringCallback())
    
  EndIf
  
EndProcedure

Procedure StringToFileCallback(Success)
  
  Debug "StringToFileCallback()"
  Debug "Success: " + Str(Success)
  Debug "---"
  
  Debug "now i check if file exists..."  
  Cordova::FileExists(SpecialDirectory, "11file.txt", @FileExistsCallback())
    
EndProcedure

Procedure Main()
  
  SpecialDirectory = Cordova::GetSpecialDirectory(Cordova::#applicationStorageDirectory)
  
  If SpecialDirectory = ""
    Debug "!SpecialDirectory()"
  Else
    
    Debug "trying to create the file..."
    Cordova::StringToFile(SpecialDirectory, "file.txt", "this is an important filecontent-stuff", @StringToFileCallback())
    
  EndIf
  
  
EndProcedure

Cordova::Init(@Main())
Greetings ... Peter

Re: CrossWalk API?

Posted: Mon Feb 13, 2017 10:30 pm
by tj1010
All SB networking functions are build on top of these so none of the networking stuff in SB works with IOS or Android. Otherwise you could use "cloud" storage..