chore: store LocalUtils on Connection (#963)

This commit is contained in:
Yury Semikhatsky 2022-06-24 16:25:58 -07:00 committed by GitHub
parent 2fdb89c94e
commit 3604aab710
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 11 deletions

View File

@ -352,7 +352,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
options = new RouteFromHAROptions();
}
UrlMatcher matcher = UrlMatcher.forOneOf(baseUrl, options.url);
HARRouter harRouter = new HARRouter(browser.localUtils, har, options.notFound);
HARRouter harRouter = new HARRouter(connection.localUtils, har, options.notFound);
onClose(context -> harRouter.dispose());
route(matcher, route -> harRouter.handle(route), null);
}

View File

@ -40,7 +40,6 @@ class BrowserImpl extends ChannelOwner implements Browser {
boolean isRemote;
boolean isConnectedOverWebSocket;
private boolean isConnected = true;
LocalUtils localUtils;
BrowserTypeImpl browserType;
enum EventType {
@ -212,7 +211,6 @@ class BrowserImpl extends ChannelOwner implements Browser {
context.setBaseUrl(options.baseURL);
}
context.recordHarPath = recordHarPath;
context.tracing().localUtils = localUtils;
contexts.add(context);
return context;
}

View File

@ -48,7 +48,6 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
JsonElement result = sendMessage("launch", params);
BrowserImpl browser = connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("browser").get("guid").getAsString());
browser.localUtils = localUtils;
browser.browserType = this;
return browser;
}
@ -97,7 +96,6 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
BrowserImpl browser = connection.getExistingObject(playwright.initializer.getAsJsonObject("preLaunchedBrowser").get("guid").getAsString());
browser.isRemote = true;
browser.isConnectedOverWebSocket = true;
browser.localUtils = localUtils;
browser.browserType = this;
Consumer<JsonPipe> connectionCloseListener = t -> browser.notifyRemoteClosed();
pipe.onClose(connectionCloseListener);
@ -132,7 +130,6 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
BrowserImpl browser = connection.getExistingObject(json.getAsJsonObject("browser").get("guid").getAsString());
browser.isRemote = true;
browser.localUtils = localUtils;
browser.browserType = this;
if (json.has("defaultContext")) {
String contextId = json.getAsJsonObject("defaultContext").get("guid").getAsString();
@ -199,7 +196,6 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
context.setBaseUrl(options.baseURL);
}
context.recordHarPath = options.recordHarPath;
context.tracing().localUtils = localUtils;
return context;
}

View File

@ -64,6 +64,7 @@ public class Connection {
String debug = System.getenv("DEBUG");
isLogging = (debug != null) && debug.contains("pw:channel");
}
LocalUtils localUtils;
class Root extends ChannelOwner {
Root(Connection connection) {
@ -138,6 +139,10 @@ public class Connection {
return (PlaywrightImpl) this.root.initialize();
}
LocalUtils localUtils() {
return localUtils;
}
public <T> T getExistingObject(String guid) {
@SuppressWarnings("unchecked") T result = (T) objects.get(guid);
if (result == null)
@ -270,7 +275,8 @@ public class Connection {
result = new JsonPipe(parent, type, guid, initializer);
break;
case "LocalUtils":
result = new LocalUtils(parent, type, guid, initializer);
localUtils = new LocalUtils(parent, type, guid, initializer);
result = localUtils;
break;
case "Page":
result = new PageImpl(parent, type, guid, initializer);

View File

@ -975,7 +975,7 @@ public class PageImpl extends ChannelOwner implements Page {
options = new RouteFromHAROptions();
}
UrlMatcher matcher = UrlMatcher.forOneOf(browserContext.baseUrl, options.url);
HARRouter harRouter = new HARRouter(browserContext.browser().localUtils, har, options.notFound);
HARRouter harRouter = new HARRouter(connection.localUtils, har, options.notFound);
onClose(context -> harRouter.dispose());
route(matcher, route -> harRouter.handle(route), null);
}

View File

@ -26,7 +26,6 @@ import java.nio.file.Path;
import static com.microsoft.playwright.impl.Serialization.gson;
class TracingImpl extends ChannelOwner implements Tracing {
LocalUtils localUtils;
boolean isRemote;
TracingImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
@ -60,7 +59,7 @@ class TracingImpl extends ChannelOwner implements Tracing {
// Add local sources to the remote trace if necessary.
if (isRemote && json.has("sourceEntries")) {
JsonArray entries = json.getAsJsonArray("sourceEntries");
localUtils.zip(path, entries);
connection.localUtils.zip(path, entries);
}
}