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

initial FB Click to Load (WIP) #329

Merged
merged 51 commits into from
Jan 25, 2022
Merged

initial FB Click to Load (WIP) #329

merged 51 commits into from
Jan 25, 2022

Conversation

ladamski
Copy link
Collaborator

@ladamski ladamski commented Nov 16, 2021

There are some temporary hacks in TrackerRadarManager and ContentBlockerRulesManager to handle issues at startup, these will have to be resolved before merging

There is some initial scaffolding to support loading of remote configuration but not yet enabled due to difference in config files for extension vs MacOS

Task/Issue URL: https://app.asana.com/0/72649045549333/1201196366848275/f
Tech Design URL: https://app.asana.com/0/72649045549333/1201230845721463
CC:

Description:

Steps to test this PR:

  1. https://privacy-test-pages.glitch.me/privacy-protections/click-to-load/

Testing checklist:

  • Test with Release configuration

Internal references:

Software Engineering Expectations
Technical Design Template
When ready for review, remember to post the PR in MM

@ladamski
Copy link
Collaborator Author

@tomasstrba here is the WIP, as basis for our discussion

@tomasstrba tomasstrba self-requested a review November 16, 2021 18:15
@tomasstrba tomasstrba self-assigned this Nov 16, 2021
Copy link
Contributor

@tomasstrba tomasstrba left a comment

Choose a reason for hiding this comment

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

Lucas, nice!!! 👏 👏 👏 The biggest issues are in comments below. Architecture looks pretty good. There are also details like debug prints, etc but I expect you have them just for debugging.

replyHandler(nil, "Image not found")
return
}
let base64String = try? Data(contentsOf: imgURL!).base64EncodedString()
Copy link
Contributor

Choose a reason for hiding this comment

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

My advice is to use Assets.xcassets and static variables since there are ~6 images. The advantage is lazy loading and having images in memory instead of reading them from disk every time.

Please see PrivacyIconViewModel.swift for an example

Copy link
Collaborator Author

@ladamski ladamski Nov 19, 2021

Choose a reason for hiding this comment

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

BTW do you know of a way to convert an SVG represented as NSImage (or cgImage for that matter) to a Data or String representation? For PNG I can use NSImage.tiffRepresentation, but not for SVG.

Does it really make sense to use Assets.xcassets since the app itself never uses these images but just passes b64 encoded representations to the user script?

Or if there's a way load the raw files from Assets? I've tried NSDataAsset(name: "blocked_facebook_logo") but it just returns nil...?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry Lucas, I didn't know it is tricky to get raw data if NSImage is SVG. Feel free to use your original approach, but please use lazy loading of images with some sort of cache. It means, images shouldn't be loaded immediately after start if they aren't used and also shouldn't be loaded from disk every time they are used.

static func loadEmbeddedAsData() -> Data {
let json = try? Data(contentsOf: embeddedUrl)
return json!
}

static func loadFBConfig() -> Data {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, is this used somewhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not yet, as mentioned in the intro I started doing the plumbing for loading the remote config, but shelved it for now since our config diverged from the extension version. We'll be revising these configs shortly at which point we'll use this.

)
var data = ""
do {
data = try String(contentsOf: sdkUrl!)
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of forced unwrapping that causes crash if sdkUrl is nil, we usually unwrap using guard let .... It is more stable and you can react to nil in more appropriate way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TBH we probably should crash if that file is missing? I don't see any way to recover & continue.

Copy link
Contributor

Choose a reason for hiding this comment

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

If the file is part of the bundle, then yeah crash makes sense. A slightly better practice is fatalError and appropriate error message.

@tomasstrba tomasstrba assigned ladamski and unassigned tomasstrba Nov 16, 2021
There are some temporary hacks in TrackerRadarManager and ContentBlockerRulesManager to handle issues at startup, these will have to be resolved before merging

There is some initial scaffolding to support loading of remote configuration but not yet enabled due to difference in config files for extension vs MacOS
@ladamski ladamski force-pushed the la/fb-click-to-load branch from c8f8c85 to 8272d5e Compare December 6, 2021 06:26
@ladamski ladamski requested a review from tomasstrba January 3, 2022 06:53
@tomasstrba tomasstrba assigned tomasstrba and unassigned ladamski Jan 3, 2022
@bstandaert-ddg
Copy link

@ladamski Do you have any suggestions for how to test this?

@ladamski
Copy link
Collaborator Author

ladamski commented Jan 3, 2022

@ladamski Do you have any suggestions for how to test this?

@bstandaert-ddg: Sure you can start with https://privacy-test-pages.glitch.me/privacy-protections/click-to-load/, comparing the behavior of this branch vs the current release build (from develop branch). You should see what is blocked, and be able to click through to unblock.

Copy link
Contributor

@tomasstrba tomasstrba left a comment

Choose a reason for hiding this comment

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

@ladamski, I compiled and tried this and it is such a cool feature! Can't wait to have this in the released app and tell all my friends :)

I only found one issue - "Custom Facebook Login" button opens two dialogs. I needed to click "Go back" twice to get rid of it.

Other than that it is perfect and I only have some comments related to code below, which are not important and not blockers.

DuckDuckGo/BrowserTab/Model/Tab.swift Outdated Show resolved Hide resolved
DuckDuckGo/BrowserTab/Model/Tab.swift Outdated Show resolved Hide resolved
DuckDuckGo/ContentBlocker/ClickToLoadModel.swift Outdated Show resolved Hide resolved

static var fbTrackerDataSet: TrackerRadarKit.TrackerData = {
let trackerData = (try? JSONDecoder().decode(TrackerData.self, from: fbTrackerDataFile)) ?? nil
return trackerData!
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be sure. Are we using just a local json file to decode tracker data?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, right now there is no corresponding remote file. All of the CTL configs + TDS need to be refactored, which will come after this lands (these all diverged between extension / Youtube / Mac and need to be integrated again).

@tomasstrba tomasstrba assigned ladamski and unassigned tomasstrba Jan 19, 2022
@ladamski
Copy link
Collaborator Author

I addressed the issue with the duplicate window opening

Copy link
Contributor

@tomasstrba tomasstrba left a comment

Choose a reason for hiding this comment

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

LGTM! 👍 🥇

Tested again, works like a charm

@tomasstrba
Copy link
Contributor

@ladamski, I can't merge because BSK points to feature/bartek/multiple-blocking-rule-lists. We can merge right after BSK changes land


static var fbTrackerDataFile: Data = {
let url = Bundle.main.url(forResource: "fb-tds", withExtension: "json")!
return (try? Data(contentsOf: url)) ?? Data()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Important question: do we have test to ensure that this file compiles? For every embedded rule list we should have tests that check if everything is ok before the release - both parsing to TrackerData format and compilation itself.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've noticed that embedded rules compilation has not been done on BSK either, so I've added that on the click to load BSK branch - you can use that as a reference.

Copy link
Collaborator Author

@ladamski ladamski Jan 25, 2022

Choose a reason for hiding this comment

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

@bwaresiak not an explicit loading test but from my testing it does fail here if I specify an invalid FB TDS. It would blow up on a nil value at #37, but I just made it a bit more explicit so it will do so with:

Test Case '-[Unit_Tests.AppStateChangePublisherTests testWhenAllTabsExceptOneClosedThenStateChangePublished]' started.
DuckDuckGo_Privacy_Browser/ContentBlockerRulesLists.swift:43: Fatal error: Failed to JSON decode FB-TDS
2022-01-24 20:55:46.322148-0800 DuckDuckGo[78928:4548503] DuckDuckGo_Privacy_Browser/ContentBlockerRulesLists.swift:43: Fatal error: Failed to JSON decode FB-TDS

And for compilation errors, I just added a test per your example, thank you!

@ladamski ladamski merged commit 1675e32 into develop Jan 25, 2022
@ladamski ladamski deleted the la/fb-click-to-load branch January 25, 2022 23:40
samsymons added a commit that referenced this pull request Feb 1, 2022
* develop:
  Version 0.18.5
  support privacy config for clickToLoad (#407)
  Automatically select available login (#405)
  initial FB Click to Load (WIP) (#329)
  onboarding updates (#398)
  Version 0.18.4
  Configuration of Sparkle - Setting SUAllowsAutomaticUpdates to NO (#404)
  Hide downloads button if the popover is opened/closed manually (#397)
samsymons added a commit that referenced this pull request Feb 18, 2022
# By Alexey Martemyanov (20) and others
# Via Tomas Strba (2) and others
* develop: (63 commits)
  Tweaks of suggestions and autocomplete (#403)
  Bump privacy dashboard to latest version (#409)
  Point to the latest BrowserServicesKit branch. (#414)
  Move embedded TDS from BSK to platform repo (#412)
  Image of shield with dot replaced (#410)
  Expand Fireproofing to include Local Storage and IndexedDB (#408)
  Version 0.18.5
  support privacy config for clickToLoad (#407)
  Automatically select available login (#405)
  initial FB Click to Load (WIP) (#329)
  onboarding updates (#398)
  Version 0.18.4
  Configuration of Sparkle - Setting SUAllowsAutomaticUpdates to NO (#404)
  Hide downloads button if the popover is opened/closed manually (#397)
  Textfield of the homepage is empty and unfocused right after switching to the homepage (#400)
  Remove navigatorCredentials (#392)
  Remove GPC header if it exists when not needed (#366)
  Version 0.18.3
  Fireproofing encrypted storage (#332)
  Fix Lock Screen UI issues (#399)
  ...

# Conflicts:
#	DuckDuckGo.xcodeproj/project.pbxproj
#	DuckDuckGo/Crash Reports/Model/CrashReportSender.swift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants