Skip to main content

atmos/os_lib/site_compat/
pure.rs

1//! サイト別の対応のうち、カーネルにも JS にも頼らない純粋な部分(2026-11-19、`site_impl.rs` から分けた)。
2//!
3//! アドレス欄の近道・既定のブックマーク・検索の候補と、地図の埋め込みの URL の読み方。
4//! ホスト試験(`arm/v1/tests`)はこのファイルだけを取り込んで、試験用の `SiteHooks` から呼ぶ。
5
6extern crate alloc;
7use super::{duckduckgo, google};
8use alloc::string::String;
9use alloc::vec::Vec;
10
11// [S4-AUDIT: 要継続] 根拠: 既定ブックマーク。UI 設定。
12pub fn default_bookmarks() -> Vec<(&'static str, &'static str)> {
13    alloc::vec![
14        ("Google", "https://www.google.com/"),
15        ("DuckDuckGo", "https://duckduckgo.com/"),
16        ("Sugi Lab (Nagoya Univ)", "https://www.sugi-lab.net/"),
17        ("YouTube", "https://www.youtube.com/"),
18        ("AtmOS GitHub", "https://github.com/sugi-lab/AtmOS"),
19    ]
20}
21
22// [S4-AUDIT: 要継続] 根拠: アドレス欄の yt 候補。UI 機能。
23pub fn omnibox_prefix_suggestion(q: &str) -> Option<(String, String)> {
24    // YouTube 特化プレフィックス(yt <query> / youtube <query>)の直接サジェスト
25    if let Some(yt_q) = q.strip_prefix("yt ").or_else(|| q.strip_prefix("youtube ")) {
26        let clean_q = yt_q.trim();
27        if !clean_q.is_empty() {
28            return Some((
29                alloc::format!("YouTube 検索: {}", clean_q),
30                alloc::format!("https://www.youtube.com/results?search_query={}", duckduckgo::url_encode(clean_q)),
31            ));
32        }
33    }
34    None
35}
36
37// [S4-AUDIT: 要継続] 根拠: 検索候補 URL。UI 機能。
38pub fn search_suggestion_url(s: &str) -> String {
39    alloc::format!("https://duckduckgo.com/?q={}", duckduckgo::url_encode(s))
40}
41
42// [S4-AUDIT: 要継続] 根拠: アドレス欄の近道。UI 機能。
43pub fn navigate_shortcut(input: &str) -> Option<(String, String)> {
44    if let Some(q) = input.strip_prefix("g ").or_else(|| input.strip_prefix("google ")) {
45        let encoded = google::url_encode(q.trim());
46        let path = alloc::format!("/search?q={}", encoded);
47        Some((String::from("www.google.com"), path))
48    } else if let Some(q) = input.strip_prefix("yt ").or_else(|| input.strip_prefix("youtube ")) {
49        let query = q.trim();
50        if query.is_empty() {
51            Some((String::from("www.youtube.com"), String::from("/")))
52        } else {
53            let encoded = duckduckgo::url_encode(query);
54            let path = alloc::format!("/results?search_query={}", encoded);
55            Some((String::from("www.youtube.com"), path))
56        }
57    } else if input.eq_ignore_ascii_case("yt") || input.eq_ignore_ascii_case("youtube") {
58        Some((String::from("www.youtube.com"), String::from("/")))
59    } else {
60        None
61    }
62}
63
64// [S4-AUDIT: 要継続] 根拠: 既定検索。UI 機能。
65pub fn default_search(input: &str) -> Option<(String, String)> {
66    let query = duckduckgo::url_encode(input);
67    let path = alloc::format!("/?q={}", query);
68    Some((String::from("duckduckgo.com"), path))
69}
70
71// [S4-AUDIT: 要継続] 根拠: 模擬の検索候補。UI 機能。
72pub fn search_suggestions(text: &str) -> Vec<String> {
73    let mut cands = duckduckgo::get_mock_suggestions(text);
74    let gcands = google::get_mock_suggestions(text);
75    for g in gcands {
76        if !cands.contains(&g) {
77            cands.push(g);
78        }
79    }
80    cands
81}
82
83/// 動画のストリーミングの取得(YouTube の `googlevideo.com` の `/videoplayback`。SABR を含む)か。
84/// これは XHR のワーカー(背景の取得)から出ても上限を掛けない(`kernel::sync_fetch::begin_exempt` で外す)。
85/// (元は `kernel/sync_fetch_limit.rs` にあった。2026-11-19 に移した)
86// [S4-AUDIT: 要継続] 根拠: googlevideo /videoplayback の取得上限除外。OS の取得設計に紐づく。
87pub fn is_streaming_url(host: &str, path: &str) -> bool {
88    let h = host.split(':').next().unwrap_or(host);
89    (h == "googlevideo.com" || h.ends_with(".googlevideo.com")) && path.starts_with("/videoplayback")
90}
91
92/// このホストへの既定の User-Agent(`None` なら既定の `AtmOS/1.0`)。
93/// (元は `kernel/tls.rs` にあった。2026-11-19 に移した。値・条件は変えていない)
94// [S4-AUDIT: 要継続] 根拠: YouTube 系への UA 固定。AGENTS.md §7 により変更しない。
95pub fn user_agent(host: &str) -> Option<&'static str> {
96    if host.contains("youtube.com") || host.contains("googlevideo.com") || host.contains("googleapis.com") {
97        Some("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
98    } else {
99        None
100    }
101}