-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JavaBridge Implementation of H5vccPlatformService (#4525)
1. Fixed the race condition of injecting the java bridge object and loading the url from shell. 2. The pattern to polyfill the embeded javascript in the APK binary before Kabuki loads won't work, see context on b/379731250, we will move the javascript file chrobalt_preload.js and its sub modules on to Kabuki codebase. I will create a backlog bug for it when this code is submitted, and work with Kabuki eng to integrate the code on Kabuki. 3. A lot of H5vccPlatformService implementations are on google3. We had ClientLogInfo in the Cobalt repo, it did not cover all use cases H5vccPlatformService provides. I expand it to cover all use cases. b/379731250 b/379184982 b/378571210 --------- Co-authored-by: Colin Liang <[email protected]>
- Loading branch information
1 parent
0116de3
commit 399c7ca
Showing
17 changed files
with
338 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
cobalt/android/apk/app/src/app/assets/html_media_element_extension.js
This file was deleted.
Oops, something went wrong.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
cobalt/android/apk/app/src/kabuki_polyfill/chrobalt_preload.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { initializeH5vccPlatformService } from './h5vcc_platform_service.js'; | ||
import { initializeHTMLMediaElement } from './html_media_element_extension.js'; | ||
|
||
export function chrobaltPreload() { | ||
initializeH5vccPlatformService(); | ||
initializeHTMLMediaElement(); | ||
} |
74 changes: 74 additions & 0 deletions
74
cobalt/android/apk/app/src/kabuki_polyfill/h5vcc_platform_service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
function arrayBufferToBase64(buffer) { | ||
const bytes = new Uint8Array(buffer); | ||
let binary = ''; | ||
for (let i = 0; i < bytes.byteLength; i++) { | ||
binary += String.fromCharCode(bytes[i]); | ||
} | ||
return window.btoa(binary); // Encode binary string to Base64 | ||
} | ||
|
||
function base64ToArrayBuffer(base64) { | ||
const binaryString = window.atob(base64); // Decode Base64 string to binary string | ||
const bytes = new Uint8Array(binaryString.length); | ||
for (let i = 0; i < binaryString.length; i++) { | ||
bytes[i] = binaryString.charCodeAt(i); | ||
} | ||
return bytes.buffer; | ||
} | ||
|
||
class PlatformServiceClient { | ||
constructor(name) { | ||
this.name = name; | ||
} | ||
|
||
send(data) { | ||
// convert the ArrayBuffer to base64 string because java bridge only takes primitive types as input. | ||
const convertToB64 = arrayBufferToBase64(data); | ||
const responseData = Android_H5vccPlatformService.platformServiceSend(this.name, convertToB64); | ||
if (responseData === "") { | ||
return null; | ||
} | ||
|
||
// response_data has the synchronize response data converted to base64 string. | ||
// convert it to ArrayBuffer, and return the ArrayBuffer to client. | ||
return base64ToArrayBuffer(responseData); | ||
} | ||
|
||
close() { | ||
Android_H5vccPlatformService.closePlatformService(this.name); | ||
} | ||
} | ||
|
||
export function initializeH5vccPlatformService() { | ||
if (typeof Android_H5vccPlatformService === 'undefined') { | ||
return; | ||
} | ||
|
||
// On Chrobalt, register window.H5vccPlatformService | ||
window.H5vccPlatformService = { | ||
// Holds the callback functions for the platform services when open() is called. | ||
callbacks: { | ||
}, | ||
callbackFromAndroid: (serviceID, dataFromJava) => { | ||
const arrayBuffer = base64ToArrayBuffer(dataFromJava); | ||
window.H5vccPlatformService.callbacks[serviceID].callback(serviceID, arrayBuffer); | ||
}, | ||
has: (name) => { | ||
return Android_H5vccPlatformService.hasPlatformService(name); | ||
}, | ||
open: function(name, callback) { | ||
if (typeof callback !== 'function') { | ||
throw new Error("window.H5vccPlatformService.open(), missing or invalid callback function."); | ||
} | ||
|
||
const serviceID = Object.keys(this.callbacks).length + 1; | ||
// Store the callback with the service ID, name, and callback | ||
window.H5vccPlatformService.callbacks[serviceID] = { | ||
name: name, | ||
callback: callback | ||
}; | ||
Android_H5vccPlatformService.openPlatformService(serviceID, name); | ||
return new PlatformServiceClient(name); | ||
}, | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
cobalt/android/apk/app/src/kabuki_polyfill/html_media_element_extension.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @license | ||
* Copyright The Cobalt Authors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export function initializeHTMLMediaElement() { | ||
if (typeof HTMLMediaElementExtension !== 'undefined') { | ||
HTMLMediaElement.prototype.canPlayType = HTMLMediaElementExtension.canPlayType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.