From a897b9a836f6f71b6e93baa2ec22976e3d589efd Mon Sep 17 00:00:00 2001
From: yellows8 <yellows8@users.noreply.github.com>
Date: Mon, 4 Mar 2019 10:46:19 -0500
Subject: [PATCH] Moved web common AppletHolder into WebCommonConfig, _webShow
 now takes AppletHolder as a param. Added webConfigRequestExit. Renamed
 WebExitReason_Unknown2 to WebExitReason_Requested.

---
 nx/include/switch/applets/web.h |  9 ++++++++-
 nx/source/applets/web.c         | 18 +++++++++++-------
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/nx/include/switch/applets/web.h b/nx/include/switch/applets/web.h
index f8feacd7..b3dd0f8a 100644
--- a/nx/include/switch/applets/web.h
+++ b/nx/include/switch/applets/web.h
@@ -23,7 +23,7 @@ typedef enum {
 typedef enum {
     WebExitReason_ExitButton = 0x0,  ///< User pressed the X button to exit.
     WebExitReason_BackButton = 0x1,  ///< User pressed the B button to exit, on the initial page.
-    WebExitReason_Unknown2   = 0x2,
+    WebExitReason_Requested  = 0x2,  ///< The applet exited since \ref webConfigRequestExit was used.
     WebExitReason_LastUrl    = 0x3,  ///< The applet exited due to LastUrl handling, see \ref webReplyGetLastUrl.
 } WebExitReason;
 
@@ -79,6 +79,7 @@ typedef struct {
     WebCommonTLVStorage arg;  ///< TLV storage.
     AppletId appletid;        ///< AppletId
     u32 version;              ///< CommonArgs applet version.
+    AppletHolder holder;      ///< AppletHolder
 } WebCommonConfig;
 
 /// Common container struct for applets' reply data, from the output storage.
@@ -545,6 +546,12 @@ Result webConfigSetPageScrollIndicator(WebCommonConfig* config, bool flag);
  */
 Result webConfigShow(WebCommonConfig* config, WebCommonReply *out);
 
+/**
+ * @brief Request the applet to exit after \ref webConfigShow was used, while the applet is still running. This is for use from another thread.
+ * @param config WebCommonConfig object.
+ */
+Result webConfigRequestExit(WebCommonConfig* config);
+
 /**
  * @brief Gets the ExitReason from the specified reply.
  * @param reply WebCommonReply object.
diff --git a/nx/source/applets/web.c b/nx/source/applets/web.c
index ae97c264..47b0f104 100644
--- a/nx/source/applets/web.c
+++ b/nx/source/applets/web.c
@@ -44,15 +44,14 @@ static Result _webHandleExit(AppletHolder* holder, void* reply_buffer, size_t re
     return rc;
 }
 
-static Result _webShow(AppletId id, u32 version, void* arg, size_t arg_size, void* reply_buffer, size_t reply_size) {
+static Result _webShow(AppletHolder *holder, AppletId id, u32 version, void* arg, size_t arg_size, void* reply_buffer, size_t reply_size) {
     Result rc = 0;
-    AppletHolder holder;
 
-    rc = _webLaunch(&holder, id, version, arg, arg_size);
+    rc = _webLaunch(holder, id, version, arg, arg_size);
 
-    if (R_SUCCEEDED(rc)) rc = _webHandleExit(&holder, reply_buffer, reply_size);
+    if (R_SUCCEEDED(rc)) rc = _webHandleExit(holder, reply_buffer, reply_size);
 
-    appletHolderClose(&holder);
+    appletHolderClose(holder);
 
     return rc;
 }
@@ -70,7 +69,8 @@ void webWifiCreate(WebWifiConfig* config, const char* conntest_url, const char*
 }
 
 Result webWifiShow(WebWifiConfig* config, WebWifiReturnValue *out) {
-    return _webShow(AppletId_wifiWebAuth, 0, &config->arg, sizeof(config->arg), out, sizeof(*out));
+    AppletHolder holder;
+    return _webShow(&holder, AppletId_wifiWebAuth, 0, &config->arg, sizeof(config->arg), out, sizeof(*out));
 }
 
 static void _webArgInitialize(WebCommonConfig* config, AppletId appletid, WebShimKind shimKind) {
@@ -609,7 +609,11 @@ Result webConfigShow(WebCommonConfig* config, WebCommonReply *out) {
         size = sizeof(out->storage);
     }
 
-    return _webShow(config->appletid, config->version, &config->arg, sizeof(config->arg), reply, size);
+    return _webShow(&config->holder, config->appletid, config->version, &config->arg, sizeof(config->arg), reply, size);
+}
+
+Result webConfigRequestExit(WebCommonConfig* config) {
+    return appletHolderRequestExit(&config->holder);
 }
 
 // For strings only available via TLVs.