1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.components
7 import androidx.annotation.VisibleForTesting
8 import mozilla.components.concept.engine.EngineSession
9 import mozilla.components.concept.engine.EngineSession.LoadUrlFlags
10 import mozilla.components.concept.engine.EngineSession.LoadUrlFlags.Companion.ALLOW_ADDITIONAL_HEADERS
11 import mozilla.components.concept.engine.EngineSession.LoadUrlFlags.Companion.LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE
12 import mozilla.components.concept.engine.request.RequestInterceptor
15 * [RequestInterceptor] implementation for intercepting URL load requests to allow custom
18 * @param isDeviceRamAboveThreshold Whether or not the device ram is above a threshold.
20 class UrlRequestInterceptor(private val isDeviceRamAboveThreshold: Boolean) : RequestInterceptor {
22 private val isGoogleRequest by lazy {
23 Regex("^https://www\\.google\\..+")
27 internal fun getAdditionalHeaders(isDeviceRamAboveThreshold: Boolean): Map<String, String> {
28 val value = if (isDeviceRamAboveThreshold) {
35 "X-Search-Subdivision" to value,
40 internal fun shouldInterceptRequest(
42 isSubframeRequest: Boolean,
44 return !isSubframeRequest && isGoogleRequest.containsMatchIn(uri)
47 override fun onLoadRequest(
48 engineSession: EngineSession,
51 hasUserGesture: Boolean,
52 isSameDomain: Boolean,
54 isDirectNavigation: Boolean,
55 isSubframeRequest: Boolean,
56 ): RequestInterceptor.InterceptionResponse? {
57 if (!shouldInterceptRequest(uri = uri, isSubframeRequest = isSubframeRequest)) {
61 return RequestInterceptor.InterceptionResponse.Url(
63 flags = LoadUrlFlags.select(
64 LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE,
65 ALLOW_ADDITIONAL_HEADERS,
67 additionalHeaders = getAdditionalHeaders(isDeviceRamAboveThreshold),