Skip to content

Commit

Permalink
Merge pull request #25862 from brave/ios-search-engine-crash
Browse files Browse the repository at this point in the history
[iOS] Fix potential crash when launching the app from a shortcuts widget
  • Loading branch information
kylehickinson authored Oct 8, 2024
2 parents f04ca2a + 127ae18 commit 4323f0e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3414,7 +3414,14 @@ extension BrowserViewController {
}

executeAfterSetup {
NavigationPath.handle(nav: path, with: self)
Task { @MainActor in
if self.profile.searchEngines.orderedEngines.isEmpty {
// Wait until search engines are ready
await self.profile.searchEngines.waitForSearchEngines()
}

NavigationPath.handle(nav: path, with: self)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,33 @@ public class SearchEngines {
setInitialDefaultEngine(engine.legacyName ?? engine.rawValue)
}

private var loadingStream: AsyncStream<Void>?

public func loadSearchEngines() async {
let loading = AsyncStream<Void>.makeStream()
loadingStream = loading.stream

await loadCustomEngines()
orderedEngines = await getOrderedEngines()
loading.continuation.yield()

recordSearchEngineP3A()
if let defaultEngine = defaultEngine(forType: .standard) {
recordSearchEngineChangedP3A(from: defaultEngine)
}
}

/// Waits until a previous call to `loadSearchEngines` completes
public func waitForSearchEngines() async {
if !orderedEngines.isEmpty {
return
}
guard let loadingStream else { return }
for await _ in loadingStream {
return
}
}

/// If no engine type is specified this method returns search engine for regular browsing.
func defaultEngine(forType engineType: DefaultEngineType) -> OpenSearchEngine? {
if let name = engineType.option.value,
Expand All @@ -97,7 +114,7 @@ public class SearchEngines {
let defaultEngineName = initialSearchEngines.defaultSearchEngine.rawValue

let defaultEngine = orderedEngines.first(where: { $0.engineID == defaultEngineName })
return defaultEngine ?? orderedEngines[0]
return defaultEngine ?? orderedEngines.first
}

/// Initialize default engine and set order of remaining search engines.
Expand Down

0 comments on commit 4323f0e

Please sign in to comment.