Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webview upgrade and fixes #385

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
webviewVersion=0.2.1
webviewVersion=0.2.2
sandboxVersion=2022.3.2
webviewChecksum=5dc4bd0d2e2b5dd8eeadf713fc10ac2b1bac6acf11b34861b74d25c7e0800c51
webviewChecksum=a510a0961d55227ae1c737e0233d15b8d2990f48e23271c1f2adec1c7b7fb455
currentVersion=2.3.x-SNAPSHOT
4 changes: 2 additions & 2 deletions src/main/java/com/jfrog/ide/idea/inspections/JumpToCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ private void highlightCode(int startRow, int endRow, int startColumn, int endCol
if (editor == null) return;
Document document = getDocument(editor);
if (document == null) return;
int startOffset = getOffset(document, startRow - 1, startColumn - 1);
int endOffset = getOffset(document, endRow - 1, endColumn - 1);
int startOffset = getOffset(document, startRow, startColumn);
int endOffset = getOffset(document, endRow, endColumn);
highlightCode(editor, startOffset, endOffset);
scrollToHighlightedCode(editor, startOffset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ private void setLeftPanelContent(JComponent component) {
}

private JBCefBrowser initVulnerabilityInfoBrowser(@NotNull Project project) throws IOException, URISyntaxException {
webviewManager = new WebviewManager(project);
// When the webview is first opened, the issue/license message might be sent before the page is loaded, so we
// send the message again when the page is done loading.
webviewManager = new WebviewManager(project, () -> updateIssueOrLicenseInWebview(selectedIssue));
Disposer.register(this, webviewManager);
asafgabai marked this conversation as resolved.
Show resolved Hide resolved
return webviewManager.getBrowser();
}
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/com/jfrog/ide/idea/ui/webview/WebviewManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.jfrog.ide.idea.ui.webview;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.jcef.JBCefBrowser;
import com.jfrog.ide.idea.log.Logger;
import com.jfrog.ide.idea.ui.JfrogContextMenuHandler;
import com.jfrog.ide.idea.ui.webview.event.EventManager;
import com.jfrog.ide.idea.ui.webview.event.model.WebviewEvent;
import org.cef.CefApp;
Expand All @@ -19,22 +19,18 @@
public class WebviewManager implements Disposable {
private final JBCefBrowser jbCefBrowser;
public EventManager eventManager;
private boolean schemeHandlerRegistered = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private boolean schemeHandlerRegistered = false;
private boolean schemeHandlerRegistered;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's not a must, but it makes it more clear that it's on purpose, especially when looking for a property's usages.


public WebviewManager(@NotNull Project project) {
public WebviewManager(@NotNull Project project, Runnable onLoadEnd) {
jbCefBrowser = new JBCefBrowser();
// EventManager must be created before the webview is initialized
eventManager = new EventManager(jbCefBrowser, project);
Disposer.register(this, jbCefBrowser);
jbCefBrowser.createImmediately();
jbCefBrowser.setOpenLinksInExternalBrowser(true);
streamConsoleMessagesToLog();
handleLoadEvent(() -> eventManager.onWebviewLoadEnd());
DumbService.getInstance(project).smartInvokeLater(() -> {
// Register the scheme handler factory when in smart mode.
// Performing this action immediately after opening IntelliJ sometimes results in a crash, particularly in IntelliJ 2022.3.
CefApp.getInstance().registerSchemeHandlerFactory("http", "jfrog-idea-plugin", new WebviewSchemeHandlerFactory());
jbCefBrowser.loadURL("http://jfrog-idea-plugin/index.html");
});
handleLoadEvent(() -> eventManager.onWebviewLoadEnd(onLoadEnd));
jbCefBrowser.getJBCefClient().addContextMenuHandler(new JfrogContextMenuHandler(), jbCefBrowser.getCefBrowser());
}

public JBCefBrowser getBrowser() {
Expand Down Expand Up @@ -75,9 +71,20 @@ public boolean onConsoleMessage(CefBrowser browser, CefSettings.LogSeverity leve
}

public void sendMessage(WebviewEvent.Type type, Object data) {
loadPageIfNeeded();
eventManager.send(type, data);
}

private void loadPageIfNeeded() {
if (!schemeHandlerRegistered) {
// Register the scheme handler factory right before the webview is first opened.
// Performing this action earlier sometimes results in a crash or a fatal error, particularly in IntelliJ 2022.3.
CefApp.getInstance().registerSchemeHandlerFactory("http", "jfrog-idea-plugin", new WebviewSchemeHandlerFactory());
jbCefBrowser.loadURL("http://jfrog-idea-plugin/index.html");
schemeHandlerRegistered = true;
}
asafgabai marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void dispose() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ public EventManager(JBCefBrowser jbBrowser, @NotNull Project project) {
/**
* Invoked when the webview finishes loading.
* Creates the IDE send function body and sends it to the webview.
* Finally, it runs onLoadEvent, if provided.
*
* @param onLoadEnd A {@link Runnable} to run when the webview finishes loading.
*/
public void onWebviewLoadEnd() {
public void onWebviewLoadEnd(Runnable onLoadEnd) {
String ideSendFuncBody = this.receiver.createIdeSendFuncBody(ideSendFuncName);
this.sender.sendIdeSendFunc(ideSendFuncName, ideSendFuncBody);
if (onLoadEnd != null) {
onLoadEnd.run();
}
}

/**
Expand Down
Loading