Skip to content

Commit 9f3ccb4

Browse files
authored
browser(webkit): wait for all pages to close in deleteContext (#1197)
1 parent 4a9a155 commit 9f3ccb4

File tree

2 files changed

+86
-21
lines changed

2 files changed

+86
-21
lines changed

browser_patches/webkit/BUILD_NUMBER

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1164
1+
1165

browser_patches/webkit/patches/bootstrap.diff

+85-20
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,10 @@ index 1eb7abb2fa21d7a8ec0833160f53e5c523ec4317..7709bcc2ec69aab0589ca1b954db1fb2
383383
FrontendChannel::ConnectionType connectionType() const;
384384
diff --git a/Source/JavaScriptCore/inspector/protocol/Browser.json b/Source/JavaScriptCore/inspector/protocol/Browser.json
385385
new file mode 100644
386-
index 0000000000000000000000000000000000000000..0f9b1c8950b8f5631ddfd8180a851d1ecea4c475
386+
index 0000000000000000000000000000000000000000..d4f5cbc813facbd8036c0be46304e4a607cbcb79
387387
--- /dev/null
388388
+++ b/Source/JavaScriptCore/inspector/protocol/Browser.json
389-
@@ -0,0 +1,210 @@
389+
@@ -0,0 +1,211 @@
390390
+{
391391
+ "domain": "Browser",
392392
+ "availability": ["web"],
@@ -476,6 +476,7 @@ index 0000000000000000000000000000000000000000..0f9b1c8950b8f5631ddfd8180a851d1e
476476
+ },
477477
+ {
478478
+ "name": "deleteContext",
479+
+ "async": true,
479480
+ "description": "Deletes browser context previously created with createContect. The command will automatically close all pages that use the context.",
480481
+ "parameters": [
481482
+ { "name": "browserContextId", "$ref": "ContextID", "description": "Identifier of the context to delete." }
@@ -7694,10 +7695,10 @@ index 78caedf0c0ce83675569502d150fcc44e5f9868c..42070f8b1969caa0d00863279fcefe01
76947695
} // namespace WebKit
76957696
diff --git a/Source/WebKit/UIProcess/InspectorBrowserAgent.cpp b/Source/WebKit/UIProcess/InspectorBrowserAgent.cpp
76967697
new file mode 100644
7697-
index 0000000000000000000000000000000000000000..f119a3bcf90af6d3183a4079a4a3b06a6b75cabc
7698+
index 0000000000000000000000000000000000000000..28221673d566aff5e6e839d36ef9c83d244291ba
76987699
--- /dev/null
76997700
+++ b/Source/WebKit/UIProcess/InspectorBrowserAgent.cpp
7700-
@@ -0,0 +1,526 @@
7701+
@@ -0,0 +1,587 @@
77017702
+/*
77027703
+ * Copyright (C) 2019 Microsoft Corporation.
77037704
+ *
@@ -7799,6 +7800,55 @@ index 0000000000000000000000000000000000000000..f119a3bcf90af6d3183a4079a4a3b06a
77997800
+
78007801
+} // namespace
78017802
+
7803+
+Vector<WebPageProxy*> BrowserContext::pages() const {
7804+
+ Vector<WebPageProxy*> pages;
7805+
+ for (auto& process : processPool->processes()) {
7806+
+ for (auto* page : process->pages())
7807+
+ pages.append(page);
7808+
+ }
7809+
+ return pages;
7810+
+}
7811+
+
7812+
+class InspectorBrowserAgent::BrowserContextDeletion {
7813+
+ WTF_MAKE_NONCOPYABLE(BrowserContextDeletion);
7814+
+ WTF_MAKE_FAST_ALLOCATED;
7815+
+public:
7816+
+ BrowserContextDeletion(const BrowserContext& context, size_t numberOfPages, Ref<DeleteContextCallback>&& callback)
7817+
+ : m_browserContext(context)
7818+
+ , m_numberOfPages(numberOfPages)
7819+
+ , m_callback(WTFMove(callback)) { }
7820+
+
7821+
+ void willDestroyPage(const WebPageProxy& page)
7822+
+ {
7823+
+ ASSERT(m_browserContext.dataStore->sessionID() == page.sessionID());
7824+
+ // Check if new pages have been created during the context destruction and
7825+
+ // close all of them if necessary.
7826+
+ if (m_numberOfPages == 1) {
7827+
+ Vector<WebPageProxy*> pages = m_browserContext.pages();
7828+
+ size_t numberOfPages = pages.size();
7829+
+ if (numberOfPages > 1) {
7830+
+ m_numberOfPages = numberOfPages;
7831+
+ for (auto* existingPage : pages) {
7832+
+ if (existingPage != &page)
7833+
+ existingPage->closePage();
7834+
+ }
7835+
+ }
7836+
+ }
7837+
+ --m_numberOfPages;
7838+
+ if (m_numberOfPages)
7839+
+ return;
7840+
+ m_callback->sendSuccess();
7841+
+ }
7842+
+
7843+
+ bool isFinished() const { return !m_numberOfPages; }
7844+
+
7845+
+private:
7846+
+ BrowserContext m_browserContext;
7847+
+ size_t m_numberOfPages;
7848+
+ Ref<DeleteContextCallback> m_callback;
7849+
+};
7850+
+
7851+
+
78027852
+InspectorBrowserAgent::InspectorBrowserAgent(Inspector::FrontendRouter& frontendRouter, Inspector::BackendDispatcher& backendDispatcher, InspectorBrowserAgentClient* client, PageProxyIDMap& pageProxyIDMap)
78037853
+ : InspectorAgentBase("Browser"_s)
78047854
+ , m_frontendDispatcher(makeUnique<BrowserFrontendDispatcher>(frontendRouter))
@@ -7818,8 +7868,18 @@ index 0000000000000000000000000000000000000000..f119a3bcf90af6d3183a4079a4a3b06a
78187868
+
78197869
+void InspectorBrowserAgent::willDestroyWebPageProxy(const WebPageProxy& page)
78207870
+{
7821-
+ if (m_isConnected)
7822-
+ m_frontendDispatcher->pageProxyDestroyed(toPageProxyIDProtocolString(page));
7871+
+ if (!m_isConnected)
7872+
+ return;
7873+
+
7874+
+ m_frontendDispatcher->pageProxyDestroyed(toPageProxyIDProtocolString(page));
7875+
+
7876+
+ auto it = m_browserContextDeletions.find(page.sessionID());
7877+
+ if (it == m_browserContextDeletions.end())
7878+
+ return;
7879+
+
7880+
+ it->value->willDestroyPage(page);
7881+
+ if (it->value->isFinished())
7882+
+ m_browserContextDeletions.remove(it);
78237883
+}
78247884
+
78257885
+void InspectorBrowserAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
@@ -7838,6 +7898,7 @@ index 0000000000000000000000000000000000000000..f119a3bcf90af6d3183a4079a4a3b06a
78387898
+void InspectorBrowserAgent::willDestroyFrontendAndBackend(DisconnectReason)
78397899
+{
78407900
+ m_isConnected = false;
7901+
+ m_browserContextDeletions.clear();
78417902
+}
78427903
+
78437904
+void InspectorBrowserAgent::close(Ref<CloseCallback>&& callback)
@@ -7885,24 +7946,25 @@ index 0000000000000000000000000000000000000000..f119a3bcf90af6d3183a4079a4a3b06a
78857946
+ m_browserContexts.set(*browserContextID, browserContext);
78867947
+}
78877948
+
7888-
+void InspectorBrowserAgent::deleteContext(ErrorString& errorString, const String& browserContextID)
7949+
+void InspectorBrowserAgent::deleteContext(const String& browserContextID, Ref<DeleteContextCallback>&& callback)
78897950
+{
7951+
+ String errorString;
78907952
+ BrowserContext browserContext = lookupBrowserContext(errorString, &browserContextID);
7891-
+ if (!errorString.isEmpty())
7953+
+ if (!errorString.isEmpty()) {
7954+
+ callback->sendFailure(errorString);
78927955
+ return;
7893-
+
7894-
+ Vector<WebPageProxy*> pages;
7895-
+ for (auto& process : browserContext.processPool->processes()) {
7896-
+ for (auto* page : process->pages())
7897-
+ pages.append(page);
78987956
+ }
78997957
+
7958+
+ Vector<WebPageProxy*> pages = browserContext.pages();
7959+
+ PAL::SessionID sessionID = browserContext.dataStore->sessionID();
7960+
+
7961+
+ m_browserContexts.remove(browserContextID);
7962+
+ m_browserContextDeletions.set(sessionID, makeUnique<BrowserContextDeletion>(browserContext, pages.size(), WTFMove(callback)));
7963+
+
79007964
+ for (auto* page : pages)
79017965
+ page->closePage();
79027966
+
7903-
+ PAL::SessionID sessionID = browserContext.dataStore->sessionID();
79047967
+ m_client->deleteBrowserContext(errorString, sessionID);
7905-
+ m_browserContexts.remove(browserContextID);
79067968
+}
79077969
+
79087970
+void InspectorBrowserAgent::createPage(ErrorString& errorString, const String* browserContextID, String* pageProxyID)
@@ -8226,10 +8288,10 @@ index 0000000000000000000000000000000000000000..f119a3bcf90af6d3183a4079a4a3b06a
82268288
+#endif // ENABLE(REMOTE_INSPECTOR)
82278289
diff --git a/Source/WebKit/UIProcess/InspectorBrowserAgent.h b/Source/WebKit/UIProcess/InspectorBrowserAgent.h
82288290
new file mode 100644
8229-
index 0000000000000000000000000000000000000000..ad8f3d99f45fa6284738c62fe813330b2c208c92
8291+
index 0000000000000000000000000000000000000000..56ecacafe2d97971880f77913c40ec32393e1997
82308292
--- /dev/null
82318293
+++ b/Source/WebKit/UIProcess/InspectorBrowserAgent.h
8232-
@@ -0,0 +1,114 @@
8294+
@@ -0,0 +1,116 @@
82338295
+/*
82348296
+ * Copyright (C) 2019 Microsoft Corporation.
82358297
+ *
@@ -8310,7 +8372,7 @@ index 0000000000000000000000000000000000000000..ad8f3d99f45fa6284738c62fe813330b
83108372
+ // BrowserBackendDispatcherHandler
83118373
+ void close(Ref<CloseCallback>&&) override;
83128374
+ void createContext(Inspector::ErrorString&, String* browserContextID) override;
8313-
+ void deleteContext(Inspector::ErrorString&, const String& browserContextID) override;
8375+
+ void deleteContext(const String& browserContextID, Ref<DeleteContextCallback>&& callback) override;
83148376
+ void createPage(Inspector::ErrorString&, const String* browserContextID, String* pageProxyID) override;
83158377
+ void navigate(const String& url, const String& pageProxyID, const String* frameId, const String* referrer, Ref<NavigateCallback>&&) override;
83168378
+ void setIgnoreCertificateErrors(Inspector::ErrorString&, const String* browserContextID, bool ignore) override;
@@ -8328,6 +8390,7 @@ index 0000000000000000000000000000000000000000..ad8f3d99f45fa6284738c62fe813330b
83288390
+ static String toPageProxyIDProtocolString(const WebPageProxy&);
83298391
+
83308392
+private:
8393+
+ class BrowserContextDeletion;
83318394
+ BrowserContext lookupBrowserContext(Inspector::ErrorString&, const String* browserContextID);
83328395
+ WebFrameProxy* frameForID(const String& frameID, String& error);
83338396
+
@@ -8338,6 +8401,7 @@ index 0000000000000000000000000000000000000000..ad8f3d99f45fa6284738c62fe813330b
83388401
+ using Permissions = HashMap<String, HashSet<String>>;
83398402
+ HashMap<String, Permissions> m_permissions;
83408403
+ HashMap<String, BrowserContext> m_browserContexts;
8404+
+ HashMap<PAL::SessionID, std::unique_ptr<BrowserContextDeletion>> m_browserContextDeletions;
83418405
+ bool m_isConnected { false };
83428406
+};
83438407
+
@@ -8346,10 +8410,10 @@ index 0000000000000000000000000000000000000000..ad8f3d99f45fa6284738c62fe813330b
83468410
+#endif // ENABLE(REMOTE_INSPECTOR)
83478411
diff --git a/Source/WebKit/UIProcess/InspectorBrowserAgentClient.h b/Source/WebKit/UIProcess/InspectorBrowserAgentClient.h
83488412
new file mode 100644
8349-
index 0000000000000000000000000000000000000000..21f8cc9bffd8f2d4a88764a4eafa13f367aa1e7c
8413+
index 0000000000000000000000000000000000000000..199295c91249d0c400589249feaa2ed24ee35f19
83508414
--- /dev/null
83518415
+++ b/Source/WebKit/UIProcess/InspectorBrowserAgentClient.h
8352-
@@ -0,0 +1,55 @@
8416+
@@ -0,0 +1,56 @@
83538417
+/*
83548418
+ * Copyright (C) 2019 Microsoft Corporation.
83558419
+ *
@@ -8389,6 +8453,7 @@ index 0000000000000000000000000000000000000000..21f8cc9bffd8f2d4a88764a4eafa13f3
83898453
+class WebProcessPool;
83908454
+
83918455
+struct BrowserContext {
8456+
+ Vector<WebPageProxy*> pages() const;
83928457
+ RefPtr<WebsiteDataStore> dataStore;
83938458
+ RefPtr<WebProcessPool> processPool;
83948459
+};

0 commit comments

Comments
 (0)