Skip to content

Commit

Permalink
Started working with the Google API.
Browse files Browse the repository at this point in the history
  • Loading branch information
98ahni committed May 24, 2024
1 parent 36f46c4 commit 724d079
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Private API keys file
*APIKeys.cpp

# Prerequisites
*.d

Expand Down
2 changes: 2 additions & 0 deletions .vscode/imgui_shell.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
})();
</script>
<script type="text/javascript" src="plugins/timestretch.js"></script>
<script async defer src="https://apis.google.com/js/api.js" onload="gapi_loaded()"></script>
<script async defer src="https://accounts.google.com/gsi/client" onload="gis_loaded()"></script>
{{{ SCRIPT }}}
</body>
</html>
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"__threading_support": "cpp",
"__tree": "cpp",
"__verbose_abort": "cpp",
"regex": "cpp"
"regex": "cpp",
"__memory": "cpp"
}
}
9 changes: 9 additions & 0 deletions Source/Extensions/APIKeys.cpp.TEMPLATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <emscripten.h>
#include <emscripten/val.h>
#include <Defines.h>

// This is the function getting called by the Google API once it's loaded.
extern"C" EMSCRIPTEN_KEEPALIVE void GAPI_Init_Client()
{
EM_ASM({init_gapi_with_key(Emval.toValue($0));}, VAR_TO_JS("API KEY GOES HERE"));
}
9 changes: 9 additions & 0 deletions Source/Extensions/APIKeys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <string>

// !! It's important to NOT save or return keys to javascript and to NEVER use this with function pointers for security reasons!
namespace APIKeys
{
// The Google API key.
// !! It's important to NOT save or return keys to javascript and to NEVER use this with function pointers for security reasons!
std::string Google();
}
95 changes: 95 additions & 0 deletions Source/Extensions/GoogleDrive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "GoogleDrive.h"
#include <emscripten.h>
#include <emscripten/val.h>
#include <Defines.h>
#include "APIKeys.h"

EM_JS(void, gapi_loaded, (), {
gapi.load('client', _GAPI_Init_Client);
gapi.load('client:picker', initializePicker);
});

EM_JS(bool, gapi_ready, (), {
return global_gapi_inited && global_gis_inited;
}
var global_gapi_inited = false;
var global_gis_inited = false;
);

EM_JS(void, gis_loaded, (), {
global_client_token = google.accounts.oauth2.initTokenClient({
client_id: CLIENT_ID,
scope: SCOPES,
callback: '', // defined later
});
global_gis_inited = true;
}
var global_client_token;
);

extern"C" EMSCRIPTEN_KEEPALIVE void GAPI_Init_Client()
{
EM_ASM({init_gapi_with_key(Emval.toValue($0));}, VAR_TO_JS(APIKeys::Google()));
}
EM_JS(void, init_gapi_with_key, (emscripten::EM_VAL APIKey), {
gapi.client.init({
apiKey: Emval.toValue(APIKey),
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest']
}).then(()=>{global_gapi_inited = true;});
});

EM_JS(bool, has_gapi_token, (), {
return gapi.client.getToken() !== null;
});

EM_JS(void, request_client_token, (), {
if (gapi.client.getToken() === null) {
// Prompt the user to select a Google Account and ask for consent to share their data
// when establishing a new session.
global_client_token.requestAccessToken({prompt: 'consent'});
} else {
// Skip display of account chooser and consent dialog for an existing session.
global_client_token.requestAccessToken({prompt: ''});
}
});

EM_JS(void, revoke_client_token, (), {
const token = gapi.client.getToken();
if (token !== null) {
google.accounts.oauth2.revoke(token.access_token);
gapi.client.setToken('');
}
});

EM_JS(void, create_picker, (emscripten::EM_VAL APIKey),
{

})

bool GoogleDrive::Ready()
{
return gapi_ready();
}

bool GoogleDrive::HasToken()
{
return has_gapi_token();
}

void GoogleDrive::RequestToken()
{
}

void GoogleDrive::LogOut()
{
}

std::string GoogleDrive::LoadProject()
{
return std::string();
}

std::string GoogleDrive::SaveProject()
{
return std::string();
}
12 changes: 12 additions & 0 deletions Source/Extensions/GoogleDrive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <string>

namespace GoogleDrive
{
bool Ready();
bool HasToken();
void RequestToken();
void LogOut();

std::string LoadProject();
std::string SaveProject();
}
18 changes: 18 additions & 0 deletions Source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extern "C" EMSCRIPTEN_KEEPALIVE void ShowInputDebugger() { g_showInputDebugger =
EM_JS(void, show_input_debugger, (), {_ShowInputDebugger(); });

bool g_closeFileTab = false;
bool g_hasGoogleAcc = false;

extern "C" EMSCRIPTEN_KEEPALIVE void LoadProject()
{
//AudioPlayback::PrepPlayback();
Expand Down Expand Up @@ -66,6 +68,22 @@ void loop(void* window){
{
}
ImGui::Ext::CreateHTMLButton("SaveProject", "click", "_SaveProject");
ImGui::Separator();
if(ImGui::BeginMenu("Google Drive"))
{
if(ImGui::IsItemActivated())
{
// Check if logged in.
}
if(!g_hasGoogleAcc)
{
if(ImGui::MenuItem("Log In With Google"))
{
// Log in
}
}
ImGui::EndMenu();
}
ImGui::EndMenu();
}
else
Expand Down

0 comments on commit 724d079

Please sign in to comment.