Skip to content

Commit

Permalink
[Cloud]Close iOS modal when finished authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Dec 17, 2023
1 parent aa2b64c commit 14f0e53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
17 changes: 9 additions & 8 deletions src/cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ static void em_flush_fs()
}

#ifndef EMSCRIPTEN

size_t curl_write_data(void* buffer, size_t size, size_t nmemb, void* d)
{
std::vector<uint8_t>* data = (std::vector<uint8_t>*)d;
Expand Down Expand Up @@ -185,7 +184,6 @@ EM_JS(void, em_oath_sign_in, (void* drive, const char* client_id), {
if (win.closed) {
clearInterval(timer);
if (typeof authorization_code !== 'undefined') {
console.log('Got authorization code: ' + authorization_code);
var xhr = new XMLHttpRequest();
var method = 'POST';
var url_str = 'https://oauth2.googleapis.com/token' +
Expand All @@ -195,7 +193,6 @@ EM_JS(void, em_oath_sign_in, (void* drive, const char* client_id), {
'&grant_type=authorization_code' +
'&access_type=offline&' +
'&redirect_uri=' + redirect_uri;
console.log('Sending request to ' + url_str);
xhr.responseType = 'json';
xhr.open(method, url_str);
xhr.onload = function() {
Expand Down Expand Up @@ -225,8 +222,7 @@ EM_JS(void, em_oath_sign_in, (void* drive, const char* client_id), {

extern "C" void em_https_request_callback_wrapper(void* callback, void* data, int size)
{
std::vector<uint8_t> result;
result.insert(result.end(), (uint8_t*)data, (uint8_t*)data + size);
std::vector<uint8_t> result((uint8_t*)data, (uint8_t*)data + (size_t)size);
std::function<void(const std::vector<uint8_t>&)>* fcallback =
(std::function<void(const std::vector<uint8_t>&)>*)callback;
(*fcallback)(result);
Expand All @@ -244,8 +240,8 @@ extern "C" void em_oath_sign_in_callback(cloud_drive_t* drive, const char* refre

if (refresh_token && access_token)
{
drive->access_token = std::string(access_token);
drive->refresh_token = std::string(refresh_token);
drive->access_token = access_token;
drive->refresh_token = refresh_token;
drive->expire_timestamp = time(NULL) + 3600;
std::string refresh_path = drive->save_directory + "refresh_token.txt";
sb_save_file_data(refresh_path.c_str(), (uint8_t*)drive->refresh_token.c_str(),
Expand Down Expand Up @@ -736,7 +732,7 @@ void cloud_drive_authenticate(cloud_drive_t* drive)
pJavaVM->DetachCurrentThread();
}
#elif SE_PLATFORM_IOS
se_ios_open_url(request.c_str());
se_ios_open_modal(request.c_str());
#else
printf("Navigate to the following URL to authorize the application:\n%s\n", request.c_str());
#endif
Expand All @@ -759,6 +755,11 @@ void cloud_drive_authenticate(cloud_drive_t* drive)
{
// Exchange authorization code for refresh and access tokens
res.set_content("You may close this tab", "text/plain");

#ifdef SE_PLATFORM_IOS
se_ios_close_modal();
#endif

std::string auth_code = req.get_param_value("code");
std::string query =
"client_id=" GOOGLE_CLIENT_ID "&client_secret=" + std::string(gsecret()) +
Expand Down
3 changes: 2 additions & 1 deletion src/ios_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
void se_ios_open_file_picker( int num_extensions, const char ** extensions);
void se_ios_get_safe_ui_padding(float *top, float* bottom,float* left, float *right);
void se_ios_set_documents_working_directory();
void se_ios_open_url(const char * url);
void se_ios_open_modal(const char* url);
void se_ios_close_modal();
#endif
26 changes: 17 additions & 9 deletions src/ios_support.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,36 @@ void se_ios_get_safe_ui_padding(float *top, float* bottom,float* left, float *ri
if(right)*right = window.safeAreaInsets.right;
}
}
void se_ios_open_url(const char * url){
UIViewController* webViewController = nil;
void se_ios_open_modal(const char * url){
if (webViewController != nil) {
return;
}

NSDictionary *dictionary = @{@"UserAgent": @"SkyEmu Browser"};
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
[[NSUserDefaults standardUserDefaults] synchronize];

[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
UIViewController * view = (UIViewController*)sapp_ios_get_view_ctrl();
UIWebView *webView = [[UIWebView alloc] initWithFrame:view.view.frame];
UIViewController* view = (UIViewController*)sapp_ios_get_view_ctrl();
UIWebView* webView = [[UIWebView alloc] initWithFrame:view.view.frame];

// Create a UIViewController to present modally
UIViewController *webViewController = [[UIViewController alloc] init];
webViewController = [[UIViewController alloc] init];
[webViewController.view addSubview:webView];

// Load a URL
NSURL *nsurl = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
NSURLRequest *request = [NSURLRequest requestWithURL:nsurl];
NSURL* nsurl = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
NSURLRequest* request = [NSURLRequest requestWithURL:nsurl];
[webView loadRequest:request];

// Present the UIViewController modally
[view presentViewController:webViewController animated:YES completion:nil];
//Your code goes in here
NSLog(@"Main Thread Code");

}];
}
void se_ios_close_modal(){
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[webViewController dismissViewControllerAnimated:YES completion:nil];
webViewController = nil;
}];
}

0 comments on commit 14f0e53

Please sign in to comment.