Skip to content

Implement firefox container tab support #263

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Finicky/Finicky/AppDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public struct BrowserOpts: CustomStringConvertible {
public var bundleId: String?
public var appPath: String?
public var profile: String?
public var container: String?
public var args: [String]

public var description: String {
Expand All @@ -39,6 +40,7 @@ public struct BrowserOpts: CustomStringConvertible {
appType: AppDescriptorType,
openInBackground: Bool?,
profile: String?,
container: String?,
args: [String]
) throws {
self.name = name
Expand All @@ -54,6 +56,7 @@ public struct BrowserOpts: CustomStringConvertible {
}

self.profile = profile
self.container = container
self.args = args

if appType == AppDescriptorType.bundleId {
Expand Down
37 changes: 34 additions & 3 deletions Finicky/Finicky/Browsers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public func getActiveApp(browsers: [BrowserOpts]) -> BrowserOpts? {

public func openUrlWithBrowser(_ url: URL, browserOpts: BrowserOpts) {
print("Opening \(browserOpts) at: " + url.absoluteString)
let command = getBrowserCommand(browserOpts, url: url)
let browserUrl = getBrowserUrl(browserOpts, incomingUrl: url)
let command = getBrowserCommand(browserOpts, url: browserUrl)
shell(command)
}

Expand All @@ -52,7 +53,37 @@ enum Browser: String {
case Wavebox = "com.bookry.wavebox"
}

public func getBrowserCommand(_ browserOpts: BrowserOpts, url: URL) -> [String] {
public func getBrowserUrl(_ browserOpts: BrowserOpts, incomingUrl: URL) -> String {
var url = incomingUrl
if let container = browserOpts.container, let bundleId: String = browserOpts.bundleId {
if let browserUrl: URL = getContainerUrl(bundleId: bundleId, container: container, url: url) {
url = browserUrl
}
}
return url.absoluteString
}

private func getContainerUrl(bundleId: String, container: String, url: URL) -> URL? {
var containerUrl: URL? {
switch bundleId.lowercased() {
case
Browser.Firefox.rawValue,
Browser.FirefoxDeveloperEdition.rawValue:
var urlComponents = URLComponents()
urlComponents.scheme = "ext+container"
urlComponents.queryItems = [
URLQueryItem(name: "name", value: container),
URLQueryItem(name: "url", value: url.absoluteString)
]
return urlComponents.url

default: return nil
}
}
return containerUrl
}

public func getBrowserCommand(_ browserOpts: BrowserOpts, url: String) -> [String] {
var command = ["open"]
var commandArgs: [String] = []
var appendUrl = true
Expand Down Expand Up @@ -88,7 +119,7 @@ public func getBrowserCommand(_ browserOpts: BrowserOpts, url: URL) -> [String]
}

if appendUrl {
command.append(url.absoluteString)
command.append(url)
}

return command
Expand Down
2 changes: 2 additions & 0 deletions Finicky/Finicky/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ open class FinickyConfig {
let openInBackground: Bool? = dict["openInBackground"] as? Bool
let browserName = dict["name"] as! String
let browserProfile: String? = dict["profile"] as? String
let browserContainer: String? = dict["container"] as? String
let args: [String] = dict["args"] as? [String] ?? []

if browserName == "" {
Expand All @@ -326,6 +327,7 @@ open class FinickyConfig {
appType: appType!,
openInBackground: openInBackground,
profile: browserProfile,
container: browserContainer,
args: args
)
return browser
Expand Down
46 changes: 46 additions & 0 deletions Finicky/FinickyTests/FinickyUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ struct Fixture<T: Decodable & Equatable> {
}
}

func createBrowserOpts(name: String = "",
appType: String = "appName",
openInBackground: Bool = false,
profile: String? = nil,
container: String? = nil,
args: [String] = []) -> BrowserOpts? {
if let appType = AppDescriptorType(rawValue: appType){
do {
let browserOpts = try BrowserOpts(
name: name,
appType: appType,
openInBackground: openInBackground,
profile: profile,
container: container,
args: args
)
return browserOpts
}
catch _ {
return nil
}
}
return nil
}

class FinickyUnitTests: XCTestCase {
func testRewrite() {
XCTAssertEqual(try compareVersions("1.2.3", "0.9"), ComparisonResult.orderedDescending)
Expand Down Expand Up @@ -52,6 +77,27 @@ class FinickyUnitTests: XCTestCase {

center.removeDeliveredNotification(notification)
}

func test_getContainerUrl() throws {
if let browserOpts = createBrowserOpts(name: "Firefox"), let incomingUrl = URL(string: "https://www.example.com"){
let url = getBrowserUrl(browserOpts, incomingUrl: incomingUrl)
XCTAssertEqual(url, "https://www.example.com")
} else {
XCTFail()
}
if let browserOpts = createBrowserOpts(name: "Firefox", container: "TestContainer"), let incomingUrl = URL(string: "https://www.example.com"){
let url = getBrowserUrl(browserOpts, incomingUrl: incomingUrl)
XCTAssertEqual(url, "ext+container:?name=TestContainer&url=https://www.example.com")
}
if let browserOpts = createBrowserOpts(name: "Firefox", container: "TestContainer"), let incomingUrl = URL(string: "https://www.example.com?test=123&test1=1234"){
let url = getBrowserUrl(browserOpts, incomingUrl: incomingUrl)
XCTAssertEqual(url, "ext+container:?name=TestContainer&url=https://www.example.com?test%3D123%26test1%3D1234")
}
if let browserOpts = createBrowserOpts(name: "Safari", container: "TestContainer"), let incomingUrl = URL(string: "https://www.example.com"){
let url = getBrowserUrl(browserOpts, incomingUrl: incomingUrl)
XCTAssertEqual(url, "https://www.example.com")
}
}

func test_makeVersionParts_error() {
var capturedError: VersionParseError?
Expand Down
2 changes: 2 additions & 0 deletions config-api/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const browserSchema = validate.oneOf([
appType: validate.oneOf(["appName", "appPath", "bundleId"]),
openInBackground: validate.boolean,
profile: validate.string,
container: validate.string,
args: validate.arrayOf(validate.string),
}),
validate.function("options"),
Expand Down Expand Up @@ -100,5 +101,6 @@ export const appDescriptorSchema = {
]).isRequired,
openInBackground: validate.boolean,
profile: validate.string,
container: validate.string,
args: validate.arrayOf(validate.string),
};
1 change: 1 addition & 0 deletions config-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface BrowserObject {
appType?: AppType;
openInBackground?: boolean;
profile?: string;
container?: string;
args?: string[];
}

Expand Down