From c68ba5c0936ba1d1ab00e493c94b540c84255a02 Mon Sep 17 00:00:00 2001
From: Michal Klocek <michal.klocek@qt.io>
Date: Wed, 17 Apr 2019 12:24:42 +0200
Subject: [PATCH] Fix default profile for single-process mode

In single process mode there is only one profile,
and this profile is used for browser context.
Fix the case where there is no 'default' profile
created but only one user profile. In this case
make the user profile 'default' so it is deleted
in right moment in single process mode.

Change-Id: I8ba3c2da2d93d53fa569c7971410a4a74753377e
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
---
 src/core/web_engine_context.cpp | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/core/web_engine_context.cpp b/src/core/web_engine_context.cpp
index 7c6b6669d..33cc723f5 100644
--- a/src/core/web_engine_context.cpp
+++ b/src/core/web_engine_context.cpp
@@ -196,9 +196,9 @@ void WebEngineContext::destroyProfileAdapter()
 {
     if (content::RenderProcessHost::run_renderer_in_process()) {
         Q_ASSERT(m_profileAdapters.count() == 1);
-        // this might be default profile
-        m_defaultProfileAdapter.release();
-        delete m_profileAdapters.first();
+        // this is a default profile
+        m_defaultProfileAdapter.reset();
+        Q_ASSERT(m_profileAdapters.isEmpty());
     }
 }
 
@@ -216,9 +216,11 @@ void WebEngineContext::addProfileAdapter(ProfileAdapter *profileAdapter)
         }
     }
 
-    if (content::RenderProcessHost::run_renderer_in_process() &&
-            !m_profileAdapters.isEmpty()) {
-        qFatal("Single mode supports only single profile.");
+    if (content::RenderProcessHost::run_renderer_in_process()){
+        if (!m_profileAdapters.isEmpty())
+            qFatal("Single mode supports only single profile.");
+        // there is only one profle therefore make it 'default'
+        m_defaultProfileAdapter.reset(profileAdapter);
     }
     m_profileAdapters.append(profileAdapter);
 }
@@ -307,8 +309,13 @@ WebEngineContext *WebEngineContext::current()
 ProfileAdapter *WebEngineContext::createDefaultProfileAdapter()
 {
     Q_ASSERT(!m_destroyed);
-    if (!m_defaultProfileAdapter)
-        m_defaultProfileAdapter.reset(new ProfileAdapter(QStringLiteral("Default")));
+    if (!m_defaultProfileAdapter) {
+        ProfileAdapter *profile = new ProfileAdapter(QStringLiteral("Default"));
+        // profile when added to m_profileAdapters might be set default
+        // profile in case of single-process
+        if (!m_defaultProfileAdapter)
+            m_defaultProfileAdapter.reset(profile);
+    }
     return m_defaultProfileAdapter.get();
 }
 
-- 
GitLab