Skip to content

Commit c1829bc

Browse files
feat: update browser to handle logout and scan state.
1 parent 7359fda commit c1829bc

6 files changed

Lines changed: 121 additions & 19 deletions

File tree

plugin/src/main/java/io/snyk/eclipse/plugin/html/BaseHtmlProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ public String replaceCssVariables(String html) {
107107
// Replace CSS variables with actual color values
108108
html = html.replace("var(--text-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR", "#000000"));
109109
html = html.replace("var(--background-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_BG_END", "#FFFFFF"));
110-
html = html.replace("var(--code-background-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
111-
html = html.replace("var(--button-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
112-
html = html.replace("var(--circle-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
110+
html = html.replace("var(--code-background-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
111+
html = html.replace("var(--button-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
112+
html = html.replace("var(--circle-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
113113

114114
html = html.replace("var(--border-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));
115115
html = html.replace("var(--link-color)", getColorAsHex("ACTIVE_HYPERLINK_COLOR", "#0000FF"));

plugin/src/main/java/io/snyk/eclipse/plugin/html/StaticPageHtmlProvider.java

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,31 @@ public static StaticPageHtmlProvider getInstance() {
1919
return instance;
2020
}
2121

22-
public String getInitHtml() {
23-
String snykWarningText = Platform.getResourceString(Platform.getBundle("io.snyk.eclipse.plugin"),
24-
"%snyk.panel.auth.trust.warning.text");
25-
26-
Bundle bundle = Platform.getBundle("io.snyk.eclipse.plugin");
27-
String base64Image = ResourceUtils.getBase64Image(bundle, "logo_snyk.png");
28-
29-
var html = """
30-
<!DOCTYPE html>
31-
<html lang="en">
22+
private String head = """
3223
<head>
3324
<meta charset="UTF-8">
3425
<meta name="viewport" content="width=device-width, initial-scale=1.0">
3526
<title>Snyk for Eclipse</title>
3627
<style>
28+
html {
29+
height:100%;
30+
}
3731
body {
3832
font-family: var(--default-font);
3933
background-color: var(--background-color);
4034
color: var(--text-color);
35+
display: flex;
36+
align-items: center;
37+
justify-content: center;
38+
height: 100%;
4139
}
4240
.container {
4341
display: flex;
4442
align-items: center;
43+
justify-content: center;
44+
}
45+
.status {
46+
text-align: center;
4547
}
4648
.welcome-text {
4749
width: 520px;
@@ -74,11 +76,59 @@ public String getInitHtml() {
7476
}
7577
</style>
7678
</head>
79+
""";
80+
81+
public String getScanningHtml() {
82+
var html = """
83+
<!DOCTYPE html>
84+
<html lang="en">
85+
%s
86+
<body>
87+
<div class="container">
88+
<p>
89+
Scanning project for vulnerabilities...<br/>
90+
<a class="stop_scan" onclick="window.stopScan();">Stop Scanning</a>
91+
</p>
92+
</div>
93+
</body>
94+
</html>
95+
""".formatted(head);
96+
return replaceCssVariables(html);
97+
}
98+
99+
public String getDefaultHtml() {
100+
var html = """
101+
<!DOCTYPE html>
102+
<html lang="en">
103+
%s
104+
<body>
105+
<div class="container">
106+
<p>
107+
Select an issue and start improving your project.
108+
</p>
109+
</div>
110+
</body>
111+
</html>
112+
""".formatted(head);
113+
return replaceCssVariables(html);
114+
}
115+
116+
public String getInitHtml() {
117+
String snykWarningText = Platform.getResourceString(Platform.getBundle("io.snyk.eclipse.plugin"),
118+
"%snyk.panel.auth.trust.warning.text");
119+
120+
Bundle bundle = Platform.getBundle("io.snyk.eclipse.plugin");
121+
String base64Image = ResourceUtils.getBase64Image(bundle, "logo_snyk.png");
122+
123+
var html = """
124+
<!DOCTYPE html>
125+
<html lang="en">
126+
%s
77127
<body>
78128
<div class="container">
79129
<img src='data:image/png;base64,%s' alt='Snyk Logo'>
80130
<div class="welcome-text">
81-
<p><strong>Welcome to Snyk for Eclipse</strong></p>
131+
<p><strong>Welcome to Snyk for Eclipse!</strong></p>
82132
<ol>
83133
<li align="left">Authenticate to Snyk.io</li>
84134
<li align="left">Analyze code for issues and vulnerabilities</li>
@@ -95,7 +145,7 @@ public String getInitHtml() {
95145
</div>
96146
</body>
97147
</html>
98-
""".formatted(base64Image, snykWarningText);
148+
""".formatted(head, base64Image, snykWarningText);
99149
return replaceCssVariables(html);
100150
}
101151

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/BrowserHandler.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.snyk.eclipse.plugin.html.BaseHtmlProvider;
2525
import io.snyk.eclipse.plugin.html.HtmlProviderFactory;
2626
import io.snyk.eclipse.plugin.html.StaticPageHtmlProvider;
27+
import io.snyk.eclipse.plugin.preferences.Preferences;
2728
import io.snyk.eclipse.plugin.utils.SnykLogger;
2829
import io.snyk.eclipse.plugin.wizards.SnykWizard;
2930

@@ -79,6 +80,14 @@ public Object function(Object[] arguments) {
7980
}
8081
};
8182

83+
new BrowserFunction(browser, "stopScan") {
84+
@Override
85+
public Object function(Object[] arguments) {
86+
//TODO Implement this!
87+
return null;
88+
}
89+
};
90+
8291
browser.addLocationListener(new LocationListener() {
8392
@Override
8493
public void changing(LocationEvent event) {
@@ -103,7 +112,7 @@ public void completed(ProgressEvent event) {
103112
}
104113
});
105114

106-
initBrowserText();
115+
setDefaultBrowserText();
107116
}
108117

109118
private record ErrorMessage(String error, String path) {
@@ -113,10 +122,12 @@ public CompletableFuture<Void> updateBrowserContent(TreeNode node) {
113122
// Generate HTML content based on the selected node
114123
var htmlProvider = getHtmlProvider(node);
115124
initScript = htmlProvider.getInitScript();
125+
boolean shouldShowDefaultMessage = true;
116126
if (node instanceof ProductTreeNode) {
117127
var ptn = (ProductTreeNode) node;
118128
String errorJson = ptn.getErrorMessage();
119129
if (errorJson != null && !errorJson.isBlank()) {
130+
shouldShowDefaultMessage = false;
120131
var error = new Gson().fromJson(errorJson, ErrorMessage.class);
121132
String errorHtml = htmlProvider.getErrorHtml(error.error, error.path);
122133
Display.getDefault().syncExec(() -> {
@@ -125,8 +136,12 @@ public CompletableFuture<Void> updateBrowserContent(TreeNode node) {
125136
}
126137
}
127138

128-
if (!(node instanceof IssueTreeNode))
139+
if (!(node instanceof IssueTreeNode)) {
140+
if (shouldShowDefaultMessage) {
141+
setDefaultBrowserText();
142+
}
129143
return CompletableFuture.completedFuture(null);
144+
}
130145

131146
return CompletableFuture.supplyAsync(() -> {
132147
return generateHtmlContent(node);
@@ -168,7 +183,16 @@ public String generateHtmlContent(String text) {
168183
return "<html><body<p>" + text + "</p></body></html>";
169184
}
170185

171-
public void initBrowserText() {
172-
browser.setText(StaticPageHtmlProvider.getInstance().getInitHtml());
186+
public void setDefaultBrowserText() {
187+
// If we are not authenticated, show the welcome page, else show the issue placeholder.
188+
if (Preferences.getInstance().getAuthToken().isBlank()) {
189+
browser.setText(StaticPageHtmlProvider.getInstance().getInitHtml());
190+
} else {
191+
browser.setText(StaticPageHtmlProvider.getInstance().getDefaultHtml());
192+
}
193+
}
194+
195+
public void setScanningBrowserText() {
196+
browser.setText(StaticPageHtmlProvider.getInstance().getScanningHtml());
173197
}
174198
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/ISnykToolView.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public interface ISnykToolView {
7575
*/
7676
abstract void refreshTree();
7777

78+
/**
79+
* Refreshes the browser
80+
*/
81+
abstract void refreshBrowser(String status);
82+
7883
/**
7984
* Returns the tree root
8085
*

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/SnykToolView.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.snyk.eclipse.plugin.views.snyktoolview;
22

3+
import static io.snyk.eclipse.plugin.domain.ProductConstants.SCAN_STATE_IN_PROGRESS;
4+
35
import java.nio.file.Path;
46
import java.nio.file.Paths;
57
import java.util.Map;
@@ -305,6 +307,17 @@ public void refreshTree() {
305307
});
306308
}
307309

310+
@Override
311+
public void refreshBrowser(String status) {
312+
Display.getDefault().asyncExec(() -> {
313+
if (status != null && status.equals(SCAN_STATE_IN_PROGRESS)) {
314+
this.browserHandler.setScanningBrowserText();
315+
} else {
316+
this.browserHandler.setDefaultBrowserText();
317+
}
318+
});
319+
};
320+
308321
@Override
309322
public void resetNode(BaseTreeNode node) {
310323
if (node != null)

plugin/src/main/java/io/snyk/languageserver/protocolextension/SnykExtendedLanguageClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public LanguageServer getConnectedLanguageServer() {
135135

136136
public void updateConfiguration() {
137137
this.configurationUpdater.configurationChanged();
138+
if (this.toolView != null) {
139+
this.toolView.refreshBrowser(null);
140+
}
138141
}
139142

140143
@Override
@@ -205,6 +208,12 @@ public void triggerScan(Path projectPath) {
205208
});
206209
}
207210

211+
public void stopScan() {
212+
CompletableFuture.runAsync(() -> {
213+
executeCommand("STOP_SCAN", new ArrayList<>());
214+
});
215+
}
216+
208217
private CompletableFuture<Object> executeCommand(String cmd, List<Object> args) {
209218
synchronized (chSyncObject) {
210219
if (commandHandler == null) {
@@ -375,6 +384,7 @@ public void snykScan(SnykScanParam param) {
375384
break;
376385
}
377386
setNodeState(param.getStatus(), affectedProductTreeNodes, issueCache);
387+
this.toolView.refreshBrowser(param.getStatus());
378388
}
379389

380390
@JsonNotification(value = LsConstants.SNYK_FOLDER_CONFIG)

0 commit comments

Comments
 (0)