|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This code is licensed under the MIT License. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files(the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions : |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +package com.microsoft.identity.common.internal.providers.oauth2 |
| 24 | + |
| 25 | +import android.content.Intent |
| 26 | +import androidx.core.net.toUri |
| 27 | +import androidx.fragment.app.FragmentActivity |
| 28 | +import com.microsoft.identity.common.internal.ui.browser.CustomTabsManager |
| 29 | +import com.microsoft.identity.common.java.opentelemetry.AttributeName |
| 30 | +import com.microsoft.identity.common.java.opentelemetry.SpanExtension |
| 31 | +import com.microsoft.identity.common.logging.Logger |
| 32 | + |
| 33 | +/** |
| 34 | + * Browser launch strategy that uses Custom Tabs when supported. |
| 35 | + */ |
| 36 | +class CustomTabsLaunchStrategy( |
| 37 | + private val activity: FragmentActivity |
| 38 | +) : BrowserLaunchStrategy { |
| 39 | + |
| 40 | + private val customTabsManager = CustomTabsManager(activity) |
| 41 | + |
| 42 | + companion object { |
| 43 | + private val TAG: String = CustomTabsLaunchStrategy::class.java.simpleName |
| 44 | + } |
| 45 | + |
| 46 | + override fun launch() { |
| 47 | + val methodTag = "$TAG:launch" |
| 48 | + val extras = activity.intent.extras |
| 49 | + if (extras == null) { |
| 50 | + finishWithError( |
| 51 | + methodTag, |
| 52 | + "Intent extras are missing - Cannot proceed with browser switch" |
| 53 | + ) |
| 54 | + return |
| 55 | + } |
| 56 | + val browserPackageName = extras.getString(SwitchBrowserActivity.BROWSER_PACKAGE_NAME) |
| 57 | + val browserSupportsCustomTabs = extras.getBoolean(SwitchBrowserActivity.BROWSER_SUPPORTS_CUSTOM_TABS, false) |
| 58 | + val processUri = extras.getString(SwitchBrowserActivity.PROCESS_URI) |
| 59 | + |
| 60 | + if (browserPackageName.isNullOrBlank()) { |
| 61 | + finishWithError(methodTag, "No browser package name found in extras - Cannot proceed with browser switch") |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + if (processUri.isNullOrBlank()) { |
| 66 | + finishWithError(methodTag, "No process URI found in extras - Cannot proceed with browser switch") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + Logger.info( |
| 71 | + methodTag, |
| 72 | + "Launching switch browser request on browser: $browserPackageName, Custom Tabs supported: $browserSupportsCustomTabs" |
| 73 | + ) |
| 74 | + |
| 75 | + val browserIntent: Intent |
| 76 | + var isUsingCustomTabs = false |
| 77 | + if (browserSupportsCustomTabs) { |
| 78 | + Logger.info(methodTag, "CustomTabsService is supported.") |
| 79 | + if (!customTabsManager.bind(activity, browserPackageName)) { |
| 80 | + Logger.warn(methodTag, "Failed to bind CustomTabsService.") |
| 81 | + browserIntent = Intent(Intent.ACTION_VIEW) |
| 82 | + } else { |
| 83 | + isUsingCustomTabs = true |
| 84 | + browserIntent = customTabsManager.customTabsIntent.intent |
| 85 | + } |
| 86 | + } else { |
| 87 | + Logger.warn(methodTag, "CustomTabsService is NOT supported") |
| 88 | + browserIntent = Intent(Intent.ACTION_VIEW) |
| 89 | + browserIntent.setPackage(browserPackageName) |
| 90 | + } |
| 91 | + SpanExtension.current().setAttribute( |
| 92 | + AttributeName.auth_tab_fallback_to_custom_tabs.name, |
| 93 | + isUsingCustomTabs |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | + browserIntent.setData(processUri.toUri()) |
| 98 | + activity.startActivity(browserIntent) |
| 99 | + } |
| 100 | + |
| 101 | + override fun handlesCancellationOnResume(): Boolean = true |
| 102 | + |
| 103 | + override fun cleanup() { |
| 104 | + customTabsManager.unbind() |
| 105 | + } |
| 106 | + |
| 107 | + private fun finishWithError(methodTag: String, message: String) { |
| 108 | + Logger.error(methodTag, message, null) |
| 109 | + activity.finish() |
| 110 | + } |
| 111 | +} |
0 commit comments