From 2f03d393c32cb26703b24b49675618ac6b466fca Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Wed, 2 Oct 2019 14:38:27 +0200 Subject: [PATCH] feat: allow inspection of specific shared workers (#20389) --- docs/api/structures/shared-worker-info.md | 4 ++ docs/api/web-contents.md | 10 ++++ filenames.auto.gni | 1 + shell/browser/api/atom_api_web_contents.cc | 54 +++++++++++++++++++ shell/browser/api/atom_api_web_contents.h | 3 ++ spec-main/api-web-contents-spec.ts | 36 +++++++++++++ .../api/shared-worker/shared-worker.html | 25 +++++++++ .../api/shared-worker/shared-worker1.js | 4 ++ .../api/shared-worker/shared-worker2.js | 4 ++ 9 files changed, 141 insertions(+) create mode 100644 docs/api/structures/shared-worker-info.md create mode 100644 spec/fixtures/api/shared-worker/shared-worker.html create mode 100644 spec/fixtures/api/shared-worker/shared-worker1.js create mode 100644 spec/fixtures/api/shared-worker/shared-worker2.js diff --git a/docs/api/structures/shared-worker-info.md b/docs/api/structures/shared-worker-info.md new file mode 100644 index 0000000000000..1e47c0c3ce348 --- /dev/null +++ b/docs/api/structures/shared-worker-info.md @@ -0,0 +1,4 @@ +# SharedWorkerInfo Object + +* `id` String - The unique id of the shared worker. +* `url` String - The url of the shared worker. diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 331166f1f98d7..670657b777757 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1460,6 +1460,16 @@ Starts inspecting element at position (`x`, `y`). Opens the developer tools for the shared worker context. +#### `contents.inspectSharedWorkerById(workerId)` + +* `workerId` String + +Inspects the shared worker based on its ID. + +#### `contents.getAllSharedWorkers()` + +Returns [`SharedWorkerInfo[]`](structures/shared-worker-info.md) - Information about all Shared Workers. + #### `contents.inspectServiceWorker()` Opens the developer tools for the service worker context. diff --git a/filenames.auto.gni b/filenames.auto.gni index e3750705d4ed8..0445c386d66bd 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -110,6 +110,7 @@ auto_filenames = { "docs/api/structures/remove-password.md", "docs/api/structures/scrubber-item.md", "docs/api/structures/segmented-control-segment.md", + "docs/api/structures/shared-worker-info.md", "docs/api/structures/shortcut-details.md", "docs/api/structures/size.md", "docs/api/structures/stream-protocol-response.md", diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index 4aef7865b9408..0ea92cced2062 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include "base/message_loop/message_loop_current.h" #include "base/no_destructor.h" @@ -298,6 +299,18 @@ struct Converter { } }; +template <> +struct Converter> { + static v8::Local ToV8( + v8::Isolate* isolate, + const scoped_refptr& val) { + mate::Dictionary dict(isolate, v8::Object::New(isolate)); + dict.Set("id", val->GetId()); + dict.Set("url", val->GetURL().spec()); + return dict.GetHandle(); + } +}; + } // namespace mate namespace electron { @@ -1569,6 +1582,44 @@ void WebContents::InspectElement(int x, int y) { managed_web_contents()->InspectElement(x, y); } +void WebContents::InspectSharedWorkerById(const std::string& workerId) { + if (type_ == Type::REMOTE) + return; + + if (!enable_devtools_) + return; + + for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) { + if (agent_host->GetType() == + content::DevToolsAgentHost::kTypeSharedWorker) { + if (agent_host->GetId() == workerId) { + OpenDevTools(nullptr); + managed_web_contents()->AttachTo(agent_host); + break; + } + } + } +} + +std::vector> +WebContents::GetAllSharedWorkers() { + std::vector> shared_workers; + + if (type_ == Type::REMOTE) + return shared_workers; + + if (!enable_devtools_) + return shared_workers; + + for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) { + if (agent_host->GetType() == + content::DevToolsAgentHost::kTypeSharedWorker) { + shared_workers.push_back(agent_host); + } + } + return shared_workers; +} + void WebContents::InspectSharedWorker() { if (type_ == Type::REMOTE) return; @@ -2493,6 +2544,9 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("getOwnerBrowserWindow", &WebContents::GetOwnerBrowserWindow) .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker) .SetMethod("inspectSharedWorker", &WebContents::InspectSharedWorker) + .SetMethod("inspectSharedWorkerById", + &WebContents::InspectSharedWorkerById) + .SetMethod("getAllSharedWorkers", &WebContents::GetAllSharedWorkers) #if BUILDFLAG(ENABLE_PRINTING) .SetMethod("_print", &WebContents::Print) .SetMethod("_getPrinters", &WebContents::GetPrinterList) diff --git a/shell/browser/api/atom_api_web_contents.h b/shell/browser/api/atom_api_web_contents.h index 4316384afb9f5..b45e9bddd16b9 100644 --- a/shell/browser/api/atom_api_web_contents.h +++ b/shell/browser/api/atom_api_web_contents.h @@ -13,6 +13,7 @@ #include "base/observer_list.h" #include "base/observer_list_types.h" #include "content/common/cursors/webcursor.h" +#include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/keyboard_event_processing_result.h" #include "content/public/browser/render_widget_host.h" #include "content/public/browser/web_contents.h" @@ -171,6 +172,8 @@ class WebContents : public mate::TrackableObject, void DisableDeviceEmulation(); void InspectElement(int x, int y); void InspectSharedWorker(); + void InspectSharedWorkerById(const std::string& workerId); + std::vector> GetAllSharedWorkers(); void InspectServiceWorker(); void SetIgnoreMenuShortcuts(bool ignore); void SetAudioMuted(bool muted); diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index 108305cfc1b35..691e65da8cb6b 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -1479,4 +1479,40 @@ describe('webContents module', () => { expect(val).to.equal('test value', 'value should eventually become the pasted value') }) }) + + describe('Shared Workers', () => { + afterEach(closeAllWindows) + + it('can get multiple shared workers', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }) + + const ready = emittedOnce(ipcMain, 'ready') + w.loadFile(path.join(fixturesPath, 'api', 'shared-worker', 'shared-worker.html')) + await ready + + const sharedWorkers = w.webContents.getAllSharedWorkers() + + expect(sharedWorkers).to.have.lengthOf(2) + expect(sharedWorkers[0].url).to.contain('shared-worker') + expect(sharedWorkers[1].url).to.contain('shared-worker') + }) + + it('can inspect a specific shared worker', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }) + + const ready = emittedOnce(ipcMain, 'ready') + w.loadFile(path.join(fixturesPath, 'api', 'shared-worker', 'shared-worker.html')) + await ready + + const sharedWorkers = w.webContents.getAllSharedWorkers() + + const devtoolsOpened = emittedOnce(w.webContents, 'devtools-opened') + w.webContents.inspectSharedWorkerById(sharedWorkers[0].id) + await devtoolsOpened + + const devtoolsClosed = emittedOnce(w.webContents, 'devtools-closed') + w.webContents.closeDevTools() + await devtoolsClosed + }) + }) }) diff --git a/spec/fixtures/api/shared-worker/shared-worker.html b/spec/fixtures/api/shared-worker/shared-worker.html new file mode 100644 index 0000000000000..3ce51d36f6398 --- /dev/null +++ b/spec/fixtures/api/shared-worker/shared-worker.html @@ -0,0 +1,25 @@ + + + + + diff --git a/spec/fixtures/api/shared-worker/shared-worker1.js b/spec/fixtures/api/shared-worker/shared-worker1.js new file mode 100644 index 0000000000000..468144a9d02e5 --- /dev/null +++ b/spec/fixtures/api/shared-worker/shared-worker1.js @@ -0,0 +1,4 @@ +self.onconnect = function (e) { + const port = e.ports[0] + port.postMessage('ready') +} diff --git a/spec/fixtures/api/shared-worker/shared-worker2.js b/spec/fixtures/api/shared-worker/shared-worker2.js new file mode 100644 index 0000000000000..468144a9d02e5 --- /dev/null +++ b/spec/fixtures/api/shared-worker/shared-worker2.js @@ -0,0 +1,4 @@ +self.onconnect = function (e) { + const port = e.ports[0] + port.postMessage('ready') +}