Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #3641, fix #3745 add Brave Search engine, add BraveSearch default…
Browse files Browse the repository at this point in the history
… SE callbacks. (#3535)
  • Loading branch information
iccub authored Jun 15, 2021
1 parent e1e1de3 commit 5df4eb4
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 11 deletions.
8 changes: 8 additions & 0 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
0ADCD45B231973650078CC67 /* UserAgentBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ADCD45A231973650078CC67 /* UserAgentBuilderTests.swift */; };
0ADCD45F2319799F0078CC67 /* RollingFileLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E61453BD1B750A1700C3F9D7 /* RollingFileLoggerTests.swift */; };
0AE16F8D251BEF8C00A688ED /* InitialSearchEnginesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE16F8C251BEF8C00A688ED /* InitialSearchEnginesTests.swift */; };
0AE50854261C63D70099C6A3 /* BraveSearchHelper.js in Resources */ = {isa = PBXBuildFile; fileRef = 0AE50853261C63D70099C6A3 /* BraveSearchHelper.js */; };
0AE5086A261C6F2E0099C6A3 /* BraveSearchHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE50869261C6F2E0099C6A3 /* BraveSearchHelper.swift */; };
0AE5C09922CAA01E00DFF3EE /* RewardsButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE5C09822CAA01E00DFF3EE /* RewardsButton.swift */; };
0AE5C69124F0059D004CBC9B /* OnboardingPrivacyConsentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE5C69024F0059D004CBC9B /* OnboardingPrivacyConsentViewController.swift */; };
0AE5C69424F005F9004CBC9B /* OnboardingPrivacyConsentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE5C69324F005F9004CBC9B /* OnboardingPrivacyConsentView.swift */; };
Expand Down Expand Up @@ -1532,6 +1534,8 @@
0ADCD4512319640F0078CC67 /* UserAgentBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserAgentBuilder.swift; sourceTree = "<group>"; };
0ADCD45A231973650078CC67 /* UserAgentBuilderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserAgentBuilderTests.swift; sourceTree = "<group>"; };
0AE16F8C251BEF8C00A688ED /* InitialSearchEnginesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InitialSearchEnginesTests.swift; sourceTree = "<group>"; };
0AE50853261C63D70099C6A3 /* BraveSearchHelper.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = BraveSearchHelper.js; sourceTree = "<group>"; };
0AE50869261C6F2E0099C6A3 /* BraveSearchHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BraveSearchHelper.swift; sourceTree = "<group>"; };
0AE5C09822CAA01E00DFF3EE /* RewardsButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RewardsButton.swift; sourceTree = "<group>"; };
0AE5C69024F0059D004CBC9B /* OnboardingPrivacyConsentViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingPrivacyConsentViewController.swift; sourceTree = "<group>"; };
0AE5C69324F005F9004CBC9B /* OnboardingPrivacyConsentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingPrivacyConsentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4769,6 +4773,7 @@
D0FCF7E81FE44D8F004A7995 /* AllFrames */,
D0FCF7E91FE44DA2004A7995 /* MainFrame */,
595E0EDA21CAD97C00813D49 /* CookieControl.js */,
0AE50853261C63D70099C6A3 /* BraveSearchHelper.js */,
F930CDAB227000F200A23FE1 /* U2F.js */,
F99505FE22937E3900CC6543 /* U2F-low-level.js */,
5E3477E822D7771700B0D5F8 /* ResourceDownloader.js */,
Expand Down Expand Up @@ -4962,6 +4967,7 @@
0A8A6224257905E300B035F4 /* UniversalLinkManager.swift */,
4452CAEF255412800053EFE6 /* DefaultBrowserIntroCalloutViewController.swift */,
0A0A5ED025B1F080007B3E74 /* DefaultBrowserIntroManager.swift */,
0AE50869261C6F2E0099C6A3 /* BraveSearchHelper.swift */,
);
indentWidth = 4;
path = Browser;
Expand Down Expand Up @@ -6306,6 +6312,7 @@
0A5E04F923FEADA800E5A3E9 /* LaunchScreen.storyboard in Resources */,
0AB22A8A257EADA900126ADC /* corwin-prescott_olympic.jpg in Resources */,
0AB22A8F257EADA900126ADC /* su-san-lee.jpg in Resources */,
0AE50854261C63D70099C6A3 /* BraveSearchHelper.js in Resources */,
5DB474F1237F4CC9007B7652 /* ntp-data.json in Resources */,
595E0EDB21CAD97C00813D49 /* CookieControl.js in Resources */,
0A0D3D3C21A4BE6400BEE65B /* adblock-list.txt in Resources */,
Expand Down Expand Up @@ -7197,6 +7204,7 @@
0A4BEF5D221AF1910005551A /* AdblockResourceDownloader.swift in Sources */,
0BA1E02E1B046F1E007675AF /* ErrorPageHelper.swift in Sources */,
5EC2C07325BF988E005EA984 /* PlaylistCell.swift in Sources */,
0AE5086A261C6F2E0099C6A3 /* BraveSearchHelper.swift in Sources */,
D3A9949C1A3686BD008AD1AC /* BrowserViewController.swift in Sources */,
59A681BDFC95A19F05E07223 /* SearchViewController.swift in Sources */,
0A8C6993225BC7B100988715 /* ToolbarUrlActionsProtocol.swift in Sources */,
Expand Down
3 changes: 3 additions & 0 deletions Client/Application/ClientPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ extension Preferences {
static let shouldShowRecentSearches = Option<Bool>(key: "search.should-show-recent-searches", default: false)
/// Whether or not to show recent searches opt-in
static let shouldShowRecentSearchesOptIn = Option<Bool>(key: "search.should-show-recent-searches.opt-in", default: true)
/// How many times Brave Search websites has asked the user to check whether Brave Search can be set as a default
static let braveSearchDefaultBrowserPromptCount =
Option<Int>(key: "search.brave-search-default-website-prompt", default: 0)
}
final class Privacy {
/// Forces all private tabs
Expand Down
19 changes: 19 additions & 0 deletions Client/Assets/SearchPlugins/bravesearch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- Copyright 2021 The Brave Authors. All rights reserved.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
<ShortName>Brave Search beta</ShortName>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16">data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAABmSSURBVHgB7V1vbB3VlT9nZmwCpOBKpFqotDhfGkoq1SmBb7txWqkfyrYkOAESO8TZbbf8kUqyEtDVFvLcP7ttqYRTCVghbe38ccwmTu3sbtlVpZKX/VjoYiRIQXyIs6vSVUGqSwkE+829PefOH8/Mm/fmzsx9z3Z4P+nlxe/duTNv7rnnz++ceweggw466KCDDjrooIOPHBAuc4yNTfemfDy/b9/2eehgdQsADW5Pd/diL/23T6DVB4jXWiD76Gf1SIDerOOpzTy1n5OA81LKVyzLPi8Wa6/Uas7sR0VAVpUA8IBfcYXb7wL0WwhbaOD6oEUg4ZizAGalxJlFxLP7dm2fg8sQK14A1Cy/EvZKKbbRn/2wTGCBoLcq1MThPXt2VuEywYoUAF+1b5OWvReWcdAbQQmDlKdraI+uds2wogRADfwaeEiA2E8X1gOrAHSdM6ImDq1WrbAiBGBscrrXlm7FQtwLqxSsFVBiZWj3nYdhFWFZBYBnvH2FO7qaBz6J1SYIyyIAq1HV5wUJwngNrJGV7iO0XQCOHj3ZD441hhpxelF48T1wbD9Pv3Ce43z6mF89CLLHb9QjEXtbLYAIVmVw1/YRW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K00rC07ML2P0CwrDzXZtEmQM5HhbTBOOXEx3QqjfOAZ6z0PWMl2ZaX6oguCOPSZbSWUiCyRUrQ5KxvJZZlu3ieDZznFvTfHMmkVQGpiCYxUrtkrW/F4Ots5aeDoD6j7SYgDcwgct0ftkwNtx4e7SoO1S45o2pdZQuh80TxDISEUNtNQBq4wITexhWVLCmRgwUIjmVCfOAHWjrwIYQ4ROatsB+FDoaaaUVogCR4naGUOEyU8h2mPHHzkFVEe2bhAyKqWjzjk1ARyxrxckGmNeQAGCtSAAIoImmN4HCMBWEbLDtklXcUqIEzvtzsZJE6jLQU/IoWgCh8VrGfLphfW9phJvx0atVCq7og4fRKo6TzcSvp4eiqEYAkPDVIxA0q4oaEQqoQsKjJoOPmBC9Hk/KChWKWvO3qShvwRmBuhQK6vemCIKvkL4wP3Z2eXFu1AtAIKi/fDb3CqilBIKKnN60dsQHEyTvzCxbMr5aBzgKbBRfdXhu9ZwytJiHuoIMOOuiggw466KCDDjrooIMOOuiggw5aiT8BXooK2TBBlQkAAAAASUVORK5CYII=</Image>
<Url type="application/x-suggestions+json" method="GET" template="https://search.brave.com/api/suggest">
<Param name="q" value="{searchTerms}"/>
<Param name="rich" value="false"/>
</Url>
<Url type="text/html" method="GET" template="https://search.brave.com/search">
<Param name="q" value="{searchTerms}"/>
<Param name="source" value="ios"/>
</Url>
<SearchForm>https://search.brave.com</SearchForm>
</SearchPlugin>
122 changes: 122 additions & 0 deletions Client/Frontend/Browser/BraveSearchHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2021 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import Foundation
import WebKit
import Shared
import BraveShared

private let log = Logger.browserLogger

class BraveSearchHelper: TabContentScript {
private weak var tab: Tab?
private let profile: Profile

/// Tracks how many in current browsing session the user has been prompted to set Brave Search as a default
/// while on one of Brave Search websites.
private static var canSetAsDefaultCounter = 0
/// How many times user should be shown the default browser prompt on Brave Search websites.
private let maxCountOfDefaultBrowserPromptsPerSession = 3
/// How many times user is shown the default browser prompt in total, this does not reset between app launches.
private let maxCountOfDefaultBrowserPromptsTotal = 10

required init(tab: Tab, profile: Profile) {
self.tab = tab
self.profile = profile
}

static func name() -> String { "BraveSearchHelper" }

func scriptMessageHandlerName() -> String? { BraveSearchHelper.name() }

private enum Method: Int {
case canSetBraveSearchAsDefault = 1
case setBraveSearchDefault = 2
}

private struct MethodModel: Codable {
enum CodingKeys: String, CodingKey {
case methodId = "method_id"
}

let methodId: Int
}

func userContentController(_ userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage) {
let allowedHosts = ["search.brave.com",
"search-dev.brave.com",
"search-dev-local.brave.com",
"search.brave.software",
"search.bravesoftware.com"]

guard let requestHost = message.frameInfo.request.url?.host,
allowedHosts.contains(requestHost),
message.frameInfo.isMainFrame else {
log.error("Backup search request called from disallowed host")
return
}

guard let data = try? JSONSerialization.data(withJSONObject: message.body, options: []),
let method = try? JSONDecoder().decode(MethodModel.self, from: data).methodId else {
log.error("Failed to retrieve method id")
return
}

switch method {
case Method.canSetBraveSearchAsDefault.rawValue:
handleCanSetBraveSearchAsDefault(methodId: method)
case Method.setBraveSearchDefault.rawValue:
handleSetBraveSearchDefault(methodId: method)
default:
break
}
}

private func handleCanSetBraveSearchAsDefault(methodId: Int) {

if PrivateBrowsingManager.shared.isPrivateBrowsing {
log.debug("Private mode detected, skipping setting Brave Search as a default")
callback(methodId: methodId, result: false)
return
}

let maximumPromptCount = Preferences.Search.braveSearchDefaultBrowserPromptCount
if Self.canSetAsDefaultCounter >= maxCountOfDefaultBrowserPromptsPerSession ||
maximumPromptCount.value >= maxCountOfDefaultBrowserPromptsTotal {
log.debug("Maximum number of tries of Brave Search website prompts reached")
callback(methodId: methodId, result: false)
return
}

Self.canSetAsDefaultCounter += 1
maximumPromptCount.value += 1

let defaultEngine = profile.searchEngines.defaultEngine(forType: .standard).shortName
let canSetAsDefault = defaultEngine != OpenSearchEngine.EngineNames.brave

callback(methodId: methodId, result: canSetAsDefault)
}

private func handleSetBraveSearchDefault(methodId: Int) {
profile.searchEngines.updateDefaultEngine(OpenSearchEngine.EngineNames.brave, forType: .standard)
callback(methodId: methodId, result: nil)
}

private func callback(methodId: Int, result: Bool?) {
let functionName =
"window.__firefox__.BSH\(UserScriptManager.messageHandlerTokenString).resolve"

var args: [Any] = [methodId]
if let result = result {
args.append(result)
}

self.tab?.webView?.evaluateSafeJavaScript(
functionName: functionName,
args: args,
sandboxed: false)
}
}
Loading

0 comments on commit 5df4eb4

Please sign in to comment.