Skip to content

Commit

Permalink
fix(extensions): devtools now open for background pages
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Feb 17, 2020
1 parent ed58168 commit 87fb6ba
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
55 changes: 50 additions & 5 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -114,6 +114,7 @@
#endif

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
#include "extensions/browser/view_type_utils.h"
#include "shell/browser/extensions/electron_extension_web_contents_observer.h"
#endif

Expand Down Expand Up @@ -374,21 +375,56 @@ base::string16 GetDefaultPrinterAsync() {

} // namespace

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)

WebContents::Type GetTypeFromViewType(extensions::ViewType view_type) {
switch (view_type) {
case extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE:
return WebContents::Type::BACKGROUND_PAGE;

case extensions::VIEW_TYPE_APP_WINDOW:
case extensions::VIEW_TYPE_COMPONENT:
case extensions::VIEW_TYPE_EXTENSION_DIALOG:
case extensions::VIEW_TYPE_EXTENSION_POPUP:
case extensions::VIEW_TYPE_BACKGROUND_CONTENTS:
case extensions::VIEW_TYPE_EXTENSION_GUEST:
case extensions::VIEW_TYPE_TAB_CONTENTS:
case extensions::VIEW_TYPE_INVALID:
return WebContents::Type::REMOTE;
}
}

#endif

WebContents::WebContents(v8::Isolate* isolate,
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
type_(Type::REMOTE),
weak_factory_(this) {
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent(),
false);
Init(isolate);
AttachAsUserData(web_contents);
InitZoomController(web_contents, gin::Dictionary::CreateEmpty(isolate));
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
// WebContents created by extension host will have valid ViewType set.
// Must reassign type prior to calling 'Init'.
extensions::ViewType view_type = extensions::GetViewType(web_contents);
if (view_type != extensions::VIEW_TYPE_INVALID) {
type_ = GetTypeFromViewType(view_type);

// Allow toggling DevTools for background pages
// TODO(samuelmaddock): this causes a crash on shutdown
Observe(web_contents);
InitWithWebContents(web_contents, GetBrowserContext(), IsGuest());
managed_web_contents()->GetView()->SetDelegate(this);
}

extensions::ElectronExtensionWebContentsObserver::CreateForWebContents(
web_contents);
script_executor_.reset(new extensions::ScriptExecutor(web_contents));
#endif

web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent(),
false);
Init(isolate);
AttachAsUserData(web_contents);
InitZoomController(web_contents, gin::Dictionary::CreateEmpty(isolate));
registry_.AddInterface(base::BindRepeating(&WebContents::BindElectronBrowser,
base::Unretained(this)));
bindings_.set_connection_error_handler(base::BindRepeating(
Expand Down Expand Up @@ -1259,6 +1295,7 @@ void WebContents::DevToolsFocused() {
void WebContents::DevToolsOpened() {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
DCHECK(managed_web_contents());
auto handle =
FromOrCreate(isolate(), managed_web_contents()->GetDevToolsWebContents());
devtools_web_contents_.Reset(isolate(), handle.ToV8());
Expand Down Expand Up @@ -1576,6 +1613,8 @@ void WebContents::OpenDevTools(gin_helper::Arguments* args) {
options.Get("activate", &activate);
}
}

DCHECK(managed_web_contents());
managed_web_contents()->SetDockState(state);
managed_web_contents()->ShowDevTools(activate);
}
Expand All @@ -1584,20 +1623,23 @@ void WebContents::CloseDevTools() {
if (type_ == Type::REMOTE)
return;

DCHECK(managed_web_contents());
managed_web_contents()->CloseDevTools();
}

bool WebContents::IsDevToolsOpened() {
if (type_ == Type::REMOTE)
return false;

DCHECK(managed_web_contents());
return managed_web_contents()->IsDevToolsViewShowing();
}

bool WebContents::IsDevToolsFocused() {
if (type_ == Type::REMOTE)
return false;

DCHECK(managed_web_contents());
return managed_web_contents()->GetView()->IsDevToolsViewFocused();
}

Expand All @@ -1606,6 +1648,7 @@ void WebContents::EnableDeviceEmulation(
if (type_ == Type::REMOTE)
return;

DCHECK(web_contents());
auto* frame_host = web_contents()->GetMainFrame();
if (frame_host) {
auto* widget_host =
Expand All @@ -1621,6 +1664,7 @@ void WebContents::DisableDeviceEmulation() {
if (type_ == Type::REMOTE)
return;

DCHECK(web_contents());
auto* frame_host = web_contents()->GetMainFrame();
if (frame_host) {
auto* widget_host =
Expand All @@ -1646,6 +1690,7 @@ void WebContents::InspectElement(int x, int y) {
if (!enable_devtools_)
return;

DCHECK(managed_web_contents());
if (!managed_web_contents()->GetDevToolsWebContents())
OpenDevTools(nullptr);
managed_web_contents()->InspectElement(x, y);
Expand Down
2 changes: 1 addition & 1 deletion shell/browser/api/electron_api_web_contents.h
Expand Up @@ -97,7 +97,7 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
public mojom::ElectronBrowser {
public:
enum class Type {
BACKGROUND_PAGE, // A DevTools extension background page.
BACKGROUND_PAGE, // An extension background page.
BROWSER_WINDOW, // Used by BrowserWindow.
BROWSER_VIEW, // Used by BrowserView.
REMOTE, // Thin wrap around an existing WebContents.
Expand Down

0 comments on commit 87fb6ba

Please sign in to comment.