Last active
October 24, 2022 02:01
-
-
Save TotallyNotChase/8c9e79ed967ae911869aec82545b21ae to your computer and use it in GitHub Desktop.
Native Messaging - C
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
{ | |
"name": "pingpong", | |
"description": "Native messaging host example", | |
"path": "path/to/release_executable", | |
"type": "stdio", | |
"allowed_origins": [ | |
"chrome-extension://extension_id/" | |
] | |
} | |
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
var start; | |
/* | |
On a click on the browser action, send the app a message. | |
*/ | |
chrome.browserAction.onClicked.addListener(() => { | |
console.log('Sending: ping') | |
start = performance.now(); | |
chrome.runtime.sendNativeMessage("pingpong", {text: "ping"}, onResponse); | |
}); | |
function onResponse(res) { | |
let end = performance.now(); | |
console.log(`Received: ${res.msg}, Took: ${end - start} ms`); | |
} |
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
{ | |
"name": "Native Messaging Example", | |
"version": "1.0", | |
"manifest_version": 2, | |
"description": "Send a message to a native application.", | |
"background": { | |
"scripts": ["main.js"] | |
}, | |
"browser_action": { | |
"default_icon": "message.svg" | |
}, | |
"permissions": [ | |
"nativeMessaging" | |
] | |
} |
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
#include<stdio.h> | |
#include<stdlib.h> | |
#include<stdint.h> | |
uint8_t* read_input() | |
{ | |
uint32_t length; | |
size_t count; | |
int err; | |
count = fread(&length, sizeof(uint32_t), 1, stdin); | |
if (count != 1) | |
{ | |
if (feof(stdin)) | |
{ | |
fprintf(stderr, "Unexpectedly encountered EOF while reading file\n"); | |
} | |
else if ((err = ferror(stdin))) | |
{ | |
fprintf(stderr, "An error occured while reading file, err code: %d\n", err); | |
clearerr(stdin); | |
} | |
return NULL; | |
} | |
uint8_t* value = malloc((length + 1) * sizeof(*value)); | |
if (value == NULL) | |
{ | |
fprintf(stderr, "An error occured while allocating memory for value"); | |
return NULL; | |
} | |
count = fread(value, sizeof(*value), length, stdin); | |
if (count != length) | |
{ | |
if (feof(stdin)) | |
{ | |
fprintf(stderr, "Unexpectedly encountered EOF while reading file\n"); | |
} | |
else if ((err = ferror(stdin))) | |
{ | |
fprintf(stderr, "An error occured while reading file, err code: %d\n", err); | |
clearerr(stdin); | |
} | |
free(value); | |
return NULL; | |
} | |
return value; | |
} | |
size_t write_output(const uint8_t* const value, uint32_t length) | |
{ | |
size_t count; | |
int err; | |
if (length > (1024 * 1024)) { | |
fprintf(stderr, "Message too large"); | |
return 0; | |
} | |
count = fwrite(&length, sizeof(uint32_t), 1, stdout); | |
if (count != 1) | |
{ | |
if (feof(stdout)) | |
{ | |
fprintf(stderr, "Unexpectedly encountered EOF while reading file\n"); | |
} | |
else if ((err = ferror(stdout))) | |
{ | |
fprintf(stderr, "An error occured while reading file, err code: %d\n", err); | |
clearerr(stdout); | |
} | |
return 0; | |
} | |
count = fwrite(value, sizeof(char), length, stdout); | |
if (count != length) | |
{ | |
if (feof(stdout)) | |
{ | |
fprintf(stderr, "Unexpectedly encountered EOF while writing file\n"); | |
} | |
else if ((err = ferror(stdout))) | |
{ | |
fprintf(stderr, "An error occured while writing file, err code: %d\n", err); | |
clearerr(stdout); | |
} | |
return 0; | |
} | |
fflush(stdout); | |
return length + 4; | |
} | |
int main() | |
{ | |
size_t count; | |
uint8_t* val = read_input(); | |
if (val == NULL) | |
{ | |
exit(EXIT_FAILURE); | |
} | |
count = write_output((uint8_t*)"{\"msg\":\"pong\"}", 14); | |
if (count != 18) | |
{ | |
free(val); | |
exit(EXIT_FAILURE); | |
} | |
free(val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment