This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
… SE callbacks. (#3535)
- Loading branch information
Showing
13 changed files
with
246 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/23T6DVB4jXWiD76Gf1SIDerOOpzTy1n5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CBda4aY1gAmUHXqHEmr4jE2RqrUKm1js1iK3Bgz9bJgAMtov2Fe6olfHYsyxwIQTUxOE9e1r3tNIs0G/pJRu/t5iNX4ISbCJ5ysT5RydPnS86sfxrGCfNo1jGlgpAgKOTJysmVhH5pFO1XcIwMUH0tdV1B82Yfii5eNWDrDK9W0aj8Z5BXTlD7hTMky+gni/QFgFglDUJDcCVSq/Y6l3M6cS9jcDaiuLnPqvL+ayQal+ebWVmehQcjqEUI2WWcQdgJ5s0auk9CILawrYJQABT2iANfKN5pzMJOK+0hRAX+PPY7md217XCrX1c7b6JeC19R4ONPdKsYEavqvSsj4K00rC07ML2P0CwrDzXZtEmQM5HhbTBOOXEx3QqjfOAZ6z0PWMl2ZaX6oguCOPSZbSWUiCyRUrQ5KxvJZZlu3ieDZznFvTfHMmkVQGpiCYxUrtkrW/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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.