Skip to content

Commit

Permalink
Add messaging support for DuckPlayer + Catalina (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane authored Oct 13, 2022
1 parent 105b869 commit d9ae757
Show file tree
Hide file tree
Showing 16 changed files with 872 additions and 205 deletions.
8 changes: 6 additions & 2 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@
9833913327AAAEEE00DAF119 /* EmbeddedTrackerDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9833913227AAAEEE00DAF119 /* EmbeddedTrackerDataTests.swift */; };
983DFB2528B67036006B7E34 /* UserContentUpdating.swift in Sources */ = {isa = PBXBuildFile; fileRef = 983DFB2428B67036006B7E34 /* UserContentUpdating.swift */; };
98EB5D1027516A4800681FE6 /* AppPrivacyConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98EB5D0F27516A4800681FE6 /* AppPrivacyConfigurationTests.swift */; };
A1DA5CF92367799FF3637FC9 /* YoutubeOverlayUserScript+Encryption.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1DA5A20BFD17B83D72DCB7C /* YoutubeOverlayUserScript+Encryption.swift */; };
AA06B6B72672AF8100F541C5 /* Sparkle in Frameworks */ = {isa = PBXBuildFile; productRef = AA06B6B62672AF8100F541C5 /* Sparkle */; };
AA0877B826D5160D00B05660 /* SafariVersionReaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0877B726D5160D00B05660 /* SafariVersionReaderTests.swift */; };
AA0877BA26D5161D00B05660 /* WebKitVersionProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0877B926D5161D00B05660 /* WebKitVersionProviderTests.swift */; };
Expand Down Expand Up @@ -1241,6 +1242,7 @@
9833913227AAAEEE00DAF119 /* EmbeddedTrackerDataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmbeddedTrackerDataTests.swift; sourceTree = "<group>"; };
983DFB2428B67036006B7E34 /* UserContentUpdating.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserContentUpdating.swift; sourceTree = "<group>"; };
98EB5D0F27516A4800681FE6 /* AppPrivacyConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppPrivacyConfigurationTests.swift; sourceTree = "<group>"; };
A1DA5A20BFD17B83D72DCB7C /* YoutubeOverlayUserScript+Encryption.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "YoutubeOverlayUserScript+Encryption.swift"; sourceTree = "<group>"; };
AA0877B726D5160D00B05660 /* SafariVersionReaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafariVersionReaderTests.swift; sourceTree = "<group>"; };
AA0877B926D5161D00B05660 /* WebKitVersionProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebKitVersionProviderTests.swift; sourceTree = "<group>"; };
AA0F3DB6261A566C0077F2D9 /* SuggestionLoadingMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuggestionLoadingMock.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1753,8 +1755,9 @@
3767190128E724B2003A2A15 /* PrivatePlayerURLExtension.swift */,
31F28C5228C8EECA00119F70 /* PrivatePlayerSchemeHandler.swift */,
315AA06F28CA5CC800200030 /* YoutubePlayerNavigationHandler.swift */,
31F28C4E28C8EEC500119F70 /* YoutubeOverlayUserScript.swift */,
31F28C4C28C8EEC500119F70 /* YoutubePlayerUserScript.swift */,
31F28C4E28C8EEC500119F70 /* YoutubeOverlayUserScript.swift */,
A1DA5A20BFD17B83D72DCB7C /* YoutubeOverlayUserScript+Encryption.swift */,
);
path = "Youtube Player";
sourceTree = "<group>";
Expand Down Expand Up @@ -5023,6 +5026,7 @@
4BE53374286E39F10019DBFD /* ChromiumKeychainPrompt.swift in Sources */,
B6553692268440D700085A79 /* WKProcessPool+GeolocationProvider.swift in Sources */,
AA5C1DD1285A154E0089850C /* RecentlyClosedMenu.swift in Sources */,
A1DA5CF92367799FF3637FC9 /* YoutubeOverlayUserScript+Encryption.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -6278,7 +6282,7 @@
repositoryURL = "https://github.com/duckduckgo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 32.3.0;
version = 32.4.0;
};
};
AA06B6B52672AF8100F541C5 /* XCRemoteSwiftPackageReference "Sparkle" */ = {
Expand Down
34 changes: 23 additions & 11 deletions DuckDuckGo/Browser Tab/Model/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -811,16 +811,25 @@ final class Tab: NSObject, Identifiable, ObservableObject {

youtubePlayerCancellables.removeAll()

if webView.url?.host?.droppingWwwPrefix() == "youtube.com" {
// only send push updates on macOS 11+ where it's safe to call window.* messages in the browser
let canPushMessagesToJS: Bool = {
if #available(macOS 11, *) {
return true
} else {
return false
}
}()

if webView.url?.host?.droppingWwwPrefix() == "youtube.com" && canPushMessagesToJS {
privatePlayer.$mode
.dropFirst()
.sink { [weak self] playerMode in
guard let self = self else {
return
}
let userValues = YoutubeOverlayUserScript.UserValues(
privatePlayerMode: playerMode,
overlayInteracted: self.privatePlayer.overlayInteracted
privatePlayerMode: playerMode,
overlayInteracted: self.privatePlayer.overlayInteracted
)
self.youtubeOverlayScript?.userValuesUpdated(userValues: userValues, inWebView: self.webView)
}
Expand All @@ -829,15 +838,18 @@ final class Tab: NSObject, Identifiable, ObservableObject {

if url?.isPrivatePlayerScheme == true {
youtubePlayerScript?.isEnabled = true
privatePlayer.$mode
.map { $0 == .enabled }
.sink { [weak self] shouldAlwaysOpenPrivatePlayer in
guard let self = self else {
return

if canPushMessagesToJS {
privatePlayer.$mode
.map { $0 == .enabled }
.sink { [weak self] shouldAlwaysOpenPrivatePlayer in
guard let self = self else {
return
}
self.youtubePlayerScript?.setAlwaysOpenInPrivatePlayer(shouldAlwaysOpenPrivatePlayer, inWebView: self.webView)
}
self.youtubePlayerScript?.setAlwaysOpenInPrivatePlayer(shouldAlwaysOpenPrivatePlayer, inWebView: self.webView)
}
.store(in: &youtubePlayerCancellables)
.store(in: &youtubePlayerCancellables)
}
} else {
youtubePlayerScript?.isEnabled = false
}
Expand Down
6 changes: 1 addition & 5 deletions DuckDuckGo/Youtube Player/PrivatePlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ final class PrivatePlayer {
static let shared = PrivatePlayer()

var isAvailable: Bool {
if #available(macOS 11.0, *) {
return isFeatureEnabled
} else {
return false
}
isFeatureEnabled
}

@Published var mode: PrivatePlayerMode
Expand Down
Loading

0 comments on commit d9ae757

Please sign in to comment.