electron/shell/renderer/extensions/electron_extensions_renderer_client.cc
Samuel Maddock 3250ef551c
feat: support registering MV3 extension service workers (#27562)
* feat: support registering MV3 extension service workers

* feat: load chrome extension APIs in worker context

* feat: add more ContentRendererClient service worker overrides

* fix: lint error

* refactor: emit object for 'registration-completed'

* docs: clarify when registration-completed emits
2021-02-11 10:58:03 -05:00

82 lines
2.6 KiB
C++

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "shell/renderer/extensions/electron_extensions_renderer_client.h"
#include <string>
#include "content/public/renderer/render_thread.h"
#include "extensions/common/constants.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/renderer/dispatcher.h"
#include "shell/common/world_ids.h"
#include "shell/renderer/extensions/electron_extensions_dispatcher_delegate.h"
namespace electron {
ElectronExtensionsRendererClient::ElectronExtensionsRendererClient()
: dispatcher_(std::make_unique<extensions::Dispatcher>(
std::make_unique<ElectronExtensionsDispatcherDelegate>())) {
dispatcher_->OnRenderThreadStarted(content::RenderThread::Get());
}
ElectronExtensionsRendererClient::~ElectronExtensionsRendererClient() {}
bool ElectronExtensionsRendererClient::IsIncognitoProcess() const {
// app_shell doesn't support off-the-record contexts.
return false;
}
int ElectronExtensionsRendererClient::GetLowestIsolatedWorldId() const {
return WorldIDs::ISOLATED_WORLD_ID_EXTENSIONS;
}
extensions::Dispatcher* ElectronExtensionsRendererClient::GetDispatcher() {
return dispatcher_.get();
}
bool ElectronExtensionsRendererClient::
ExtensionAPIEnabledForServiceWorkerScript(const GURL& scope,
const GURL& script_url) const {
if (!script_url.SchemeIs(extensions::kExtensionScheme))
return false;
const extensions::Extension* extension =
extensions::RendererExtensionRegistry::Get()->GetExtensionOrAppByURL(
script_url);
if (!extension ||
!extensions::BackgroundInfo::IsServiceWorkerBased(extension))
return false;
if (scope != extension->url())
return false;
const std::string& sw_script =
extensions::BackgroundInfo::GetBackgroundServiceWorkerScript(extension);
return extension->GetResourceURL(sw_script) == script_url;
}
bool ElectronExtensionsRendererClient::AllowPopup() {
// TODO(samuelmaddock):
return false;
}
void ElectronExtensionsRendererClient::RunScriptsAtDocumentStart(
content::RenderFrame* render_frame) {
dispatcher_->RunScriptsAtDocumentStart(render_frame);
}
void ElectronExtensionsRendererClient::RunScriptsAtDocumentEnd(
content::RenderFrame* render_frame) {
dispatcher_->RunScriptsAtDocumentEnd(render_frame);
}
void ElectronExtensionsRendererClient::RunScriptsAtDocumentIdle(
content::RenderFrame* render_frame) {
dispatcher_->RunScriptsAtDocumentIdle(render_frame);
}
} // namespace electron