2019-07-12 20:38:15 +02:00
/ * 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/. */
2019-02-12 20:13:09 +01:00
package org.mozilla.fenix.ext
2019-11-23 08:15:51 +01:00
import android.content.Context
2019-11-15 23:25:50 +01:00
import android.net.Uri
import android.util.Patterns
import android.webkit.URLUtil
2019-06-10 16:12:11 +02:00
import androidx.core.net.toUri
2019-06-10 22:46:55 +02:00
import kotlinx.coroutines.runBlocking
#4596 migrate collections (#5911)
* For #4596: move code from CollectionCreationComponent to CollectionCreationStore
Other than adding comments, no changes were made. The code will be updated in a following commit. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreateStore to libstate
* For 4596: copied CollectionCreationUIView into CollectionCreationView
Otherwise, no code was changed. The next commit will update this code. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreationView to LibState
Note that the minimal changes possible to enable migration were made. Refactoring will happen in a later commit.
* For 4596: updated CollectionCreationTabListAdapter to work with the new View
* For 4596: updated SaveCollectionListAdapter to work with the new View
* For 4596: implemented CollectionCreationController
For now, it has an identical interface to the interactor. In a later commit several of its responsibilities will be moved around, some to the interactor and some to the reducer
* For 4596: copied over previous reducer code
No other changes were made. The code will be updated in the following commit. This is done to make changes more readable for the reviewer
* For 4596: update reducer code param names
Otherwise, no changes at this time
* For 4596: add arguments to CreateCollectionFragment in nav_graph
These will be used to replace the current CreateCollectionViewModel, which shares data between fragments in a way that doesn't fit within our architecture.
* For 4596: pass arguments to collection via transaction instead of VM
The VM will be removed in a later commit
* For 4596: update BrowserToolbarController to share state to collection via its Direction
* For 4596: removed CreateCollectionViewModel
* For 4596: test tab retrieval in CreateCollectionFragment
* For 4596: fix crashing CreateCollectionFragmentTest
* For 4596: removed classes create collection classes used by old architecture
* For 4596: collection interactor rename + kdoc
* For 4596: moved collection interactor interface
* For 4596: renamed CreateCollectionFragment
All related classes followed the pattern of CollectionCreationX
* For 4596: kdoc CollectionCreationController
There's no effective difference between these calls and their interactor equivalent, so I linked to them
* For 4596: fix bug that caused rename to not work
* For 4596: removed unused collection actions
These were unused before the LibState refactor
* For 4596: kdoc StepChanged
* For 4596: removed todos about moving logic to the reducer
saveTabsToCollection: this could be moved, but that would involve creating a new action. SaveCollectionStep should probably be refactored out, so adding this layer of indirection seemed counterproductive
handleBackPress: needs to be able to call dismiss(). The reducer doesn't (and shouldn't) be able to do that, so this needs to live here
stepBack: called by handleBackPress. See above
* For 4596: wrote tests for CollectionCreationController#stepback
* For 4596: fixed tests broken by changes to collections
* For 4596: small readability refactor for CollectionController#stepBack
No change to functionality (see tests)
* For 4596: broke apart CollectionView#update
There's probably a lot more that could be done here, but smaller changes were made to reduce scope
* For 4596: remove unnecessary todos
It looks like we don't follow the suggested pattern in this project
* For 4596: test CollectionCreationController#normalSessionSize
* For 4596: updated naming in CollectionCreationController per review
2019-10-23 02:33:54 +02:00
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
2019-11-23 08:15:51 +01:00
import mozilla.components.lib.publicsuffixlist.ext.urlToTrimmedHost
2019-09-13 23:43:54 +02:00
import mozilla.components.support.ktx.android.net.hostWithoutCommonPrefixes
2019-11-15 23:25:50 +01:00
import java.net.IDN
import java.util.Locale
const val FILE _PREFIX = " file:// "
const val MAX _VALID _PORT = 65 _535
2019-04-11 20:58:07 +02:00
2019-06-07 18:33:07 +02:00
/ * *
2019-11-15 23:25:50 +01:00
* Shortens URLs to be more user friendly .
*
* The algorithm used to generate these strings is a combination of FF desktop ' top sites ' ,
* feedback from the security team , and documentation regarding url elision . See
* StringTest . kt for details .
*
* This method is complex because URLs have a lot of edge cases . Be sure to thoroughly unit
* test any changes you make to it .
2019-06-07 18:33:07 +02:00
* /
2019-11-15 23:25:50 +01:00
@Suppress ( " UNUSED_PARAMETER " , " ReturnCount " , " ComplexCondition " )
// Unused Parameter: We may resume stripping eTLD, depending on conversations between security and UX
// Return count: This is a complex method, but it would not be more understandable if broken up
// ComplexCondition: Breaking out the complex condition would make this logic harder to follow
fun String . toShortUrl ( publicSuffixList : PublicSuffixList ) : String {
val inputString = this
val uri = inputString . toUri ( )
if (
inputString . isEmpty ( ) ||
! URLUtil . isValidUrl ( inputString ) ||
2019-12-09 19:46:24 +01:00
inputString . startsWith ( FILE _PREFIX ) ||
2019-11-15 23:25:50 +01:00
uri . port !in - 1. . MAX _VALID _PORT
) {
return inputString
}
if ( uri . host ?. isIpv4 ( ) == true ||
uri . isIpv6 ( ) ||
// If inputString is just a hostname and not a FQDN, use the entire hostname.
uri . host ?. contains ( " . " ) == false
) {
return uri . host ?: inputString
}
fun String . stripUserInfo ( ) : String {
val userInfo = this . toUri ( ) . encodedUserInfo
return if ( userInfo != null ) {
val infoIndex = this . indexOf ( userInfo )
this . removeRange ( infoIndex .. infoIndex + userInfo . length )
} else {
this
}
}
fun String . stripPrefixes ( ) : String = this . toUri ( ) . hostWithoutCommonPrefixes ?: this
fun String . toUnicode ( ) = IDN . toUnicode ( this )
return inputString
. stripUserInfo ( )
. toLowerCase ( Locale . getDefault ( ) )
. stripPrefixes ( )
. toUnicode ( )
}
// impl via FFTV https://searchfox.org/mozilla-mobile/source/firefox-echo-show/app/src/main/java/org/mozilla/focus/utils/FormattedDomain.java#129
fun String . isIpv4 ( ) : Boolean = Patterns . IP_ADDRESS . matcher ( this ) . matches ( )
// impl via FFiOS: https://github.com/mozilla-mobile/firefox-ios/blob/deb9736c905cdf06822ecc4a20152df7b342925d/Shared/Extensions/NSURLExtensions.swift#L292
// True IPv6 validation is difficult. This is slightly better than nothing
private fun Uri . isIpv6 ( ) : Boolean {
val host = this . host ?: return false
return host . isNotEmpty ( ) && host . contains ( " : " )
}
#4596 migrate collections (#5911)
* For #4596: move code from CollectionCreationComponent to CollectionCreationStore
Other than adding comments, no changes were made. The code will be updated in a following commit. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreateStore to libstate
* For 4596: copied CollectionCreationUIView into CollectionCreationView
Otherwise, no code was changed. The next commit will update this code. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreationView to LibState
Note that the minimal changes possible to enable migration were made. Refactoring will happen in a later commit.
* For 4596: updated CollectionCreationTabListAdapter to work with the new View
* For 4596: updated SaveCollectionListAdapter to work with the new View
* For 4596: implemented CollectionCreationController
For now, it has an identical interface to the interactor. In a later commit several of its responsibilities will be moved around, some to the interactor and some to the reducer
* For 4596: copied over previous reducer code
No other changes were made. The code will be updated in the following commit. This is done to make changes more readable for the reviewer
* For 4596: update reducer code param names
Otherwise, no changes at this time
* For 4596: add arguments to CreateCollectionFragment in nav_graph
These will be used to replace the current CreateCollectionViewModel, which shares data between fragments in a way that doesn't fit within our architecture.
* For 4596: pass arguments to collection via transaction instead of VM
The VM will be removed in a later commit
* For 4596: update BrowserToolbarController to share state to collection via its Direction
* For 4596: removed CreateCollectionViewModel
* For 4596: test tab retrieval in CreateCollectionFragment
* For 4596: fix crashing CreateCollectionFragmentTest
* For 4596: removed classes create collection classes used by old architecture
* For 4596: collection interactor rename + kdoc
* For 4596: moved collection interactor interface
* For 4596: renamed CreateCollectionFragment
All related classes followed the pattern of CollectionCreationX
* For 4596: kdoc CollectionCreationController
There's no effective difference between these calls and their interactor equivalent, so I linked to them
* For 4596: fix bug that caused rename to not work
* For 4596: removed unused collection actions
These were unused before the LibState refactor
* For 4596: kdoc StepChanged
* For 4596: removed todos about moving logic to the reducer
saveTabsToCollection: this could be moved, but that would involve creating a new action. SaveCollectionStep should probably be refactored out, so adding this layer of indirection seemed counterproductive
handleBackPress: needs to be able to call dismiss(). The reducer doesn't (and shouldn't) be able to do that, so this needs to live here
stepBack: called by handleBackPress. See above
* For 4596: wrote tests for CollectionCreationController#stepback
* For 4596: fixed tests broken by changes to collections
* For 4596: small readability refactor for CollectionController#stepBack
No change to functionality (see tests)
* For 4596: broke apart CollectionView#update
There's probably a lot more that could be done here, but smaller changes were made to reduce scope
* For 4596: remove unnecessary todos
It looks like we don't follow the suggested pattern in this project
* For 4596: test CollectionCreationController#normalSessionSize
* For 4596: updated naming in CollectionCreationController per review
2019-10-23 02:33:54 +02:00
/ * *
* Trim a host ' s prefix and suffix
* /
2019-11-23 08:15:51 +01:00
fun String . urlToTrimmedHost ( context : Context ) : String = runBlocking {
urlToTrimmedHost ( context . components . publicSuffixList ) . await ( )
2019-05-06 20:20:19 +02:00
}
2019-07-17 19:24:01 +02:00
/ * *
* Trims a URL string of its scheme and common prefixes .
*
* This is intended to act much like [ PublicSuffixList . getPublicSuffixPlusOne ( ) ] but unlike
* that method , leaves the path , anchor , etc intact .
*
* /
fun String . simplifiedUrl ( ) : String {
val afterScheme = this . substringAfter ( " :// " )
for ( prefix in listOf ( " www. " , " m. " , " mobile. " ) ) {
if ( afterScheme . startsWith ( prefix ) ) {
return afterScheme . substring ( prefix . length )
}
}
return afterScheme
}