aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt
blob: bf53c7eb343976feb4a1b263860d4fdfca0fe666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.pitchedapps.frost.web

import com.pitchedapps.frost.activities.MainActivity
import com.pitchedapps.frost.activities.WebOverlayActivity
import com.pitchedapps.frost.activities.WebOverlayActivityBase
import com.pitchedapps.frost.activities.WebOverlayBasicActivity
import com.pitchedapps.frost.facebook.FB_URL_BASE
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.facebook.USER_AGENT_BASIC
import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.isFacebookUrl
import com.pitchedapps.frost.utils.launchWebOverlay

/**
 * Created by Allan Wang on 2017-08-15.
 *
 * Due to the nature of facebook href's, many links
 * cannot be resolved on a new window and must instead
 * by loaded in the current page
 * This helper method will collect all known cases and launch the overlay accordingly
 * Returns {@code true} (default) if action is consumed, {@code false} otherwise
 *
 * If the request already comes from an instance of [WebOverlayActivity], we will then judge
 * whether the user agent string should be changed. All propagated results will return false,
 * as we have no need of sending a new intent to the same activity
 */
fun FrostWebViewCore.requestWebOverlay(url: String): Boolean {
    if (url == "#") return false
    if (context is WebOverlayActivityBase) {
        L.v("Check web request from overlay", url)
        //already overlay; manage user agent
        if (userAgentString != USER_AGENT_BASIC && url.formattedFbUrl.shouldUseBasicAgent) {
            L.i("Switch to basic agent overlay")
            context.launchWebOverlay(url, WebOverlayBasicActivity::class.java)
            return true
        }
        if (context is WebOverlayBasicActivity && !url.formattedFbUrl.shouldUseBasicAgent) {
            L.i("Switch from basic agent")
            context.launchWebOverlay(url)
            return true
        }
        L.i("return false switch")
        return false
    }
    /*
     * Non facebook urls can be loaded
     */
    if (!url.formattedFbUrl.isFacebookUrl) {
        context.launchWebOverlay(url)
        L.d("Request web overlay is not a facebook url", url)
        return true
    }
    /*
     * Check blacklist
     */
    if (overlayBlacklist.any { url.contains(it) }) return false
    /*
     * Facebook messages have the following cases for the tid query
     * mid* or id* for newer threads, which can be launched in new windows
     * or a hash for old threads, which must be loaded on old threads
     */
    if (url.contains("/messages/read/?tid=")) {
        if (!url.contains("?tid=id") && !url.contains("?tid=mid")) return false
    }
    L.v("Request web overlay passed", url)
    context.launchWebOverlay(url)
    return true
}

/**
 * If the url contains any one of the whitelist segments, switch to the chat overlay
 */
val messageWhitelist = setOf(FbItem.MESSAGES, FbItem.CHAT, FbItem.FEED_MOST_RECENT, FbItem.FEED_TOP_STORIES).map { it.url }.toSet()

val String.shouldUseBasicAgent
    get() = (messageWhitelist.any { contains(it) }) || this == FB_URL_BASE

/**
 * The following components should never be launched in a new overlay
 */
val overlayBlacklist = setOf("messages/?pageNum", "photoset_token", "sharer.php")