Ë
    H~éjù  ã                  ó€   — d Z ddlmZ ddlZddlZddlZddlZddlmZ  ej                  d«      Z
dZd
d„Zdd„Zdd	„Zy)u}  Widevine CDM hint-file seeding for persistent contexts.

CloakBrowser's binary is built with Widevine support but ships no CDM (the CDM
is a proprietary Google binary we can't redistribute). Users sideload it by
copying a ``WidevineCdm/`` directory from a real Chrome install next to the
binary (see issue #96).

Chromium discovers a sideloaded CDM in two phases: an early-startup pass that
reads a "hint file" from the user-data-dir, and a later async component-updater
pass that writes that hint file. On a fresh profile the hint file doesn't exist
on the first launch, and Playwright passes ``--disable-component-update``, so the
updater never writes it â€” Widevine only works after a manual two-launch dance.

This module pre-seeds the hint file before launch so a sideloaded CDM works on
the very first launch. It never bundles, downloads, or copies the CDM itself â€”
it only writes the hint when a CDM the user provided is already present.

Linux only: Chromium's hint-file mechanism is Linux/ChromeOS-specific. On Windows
the CDM can't initialise (DRM host verification), and macOS uses a different CDM
layout, so seeding is a no-op there.
é    )ÚannotationsN)ÚPathÚcloakbrowserz%latest-component-updated-widevine-cdmc                 ó‚   — t         j                  j                  dd«      j                  «       j	                  «       } | dv S )zETrue if CLOAKBROWSER_WIDEVINE is set to a falsey value (kill switch).ÚCLOAKBROWSER_WIDEVINEÚ )Ú0ÚfalseÚoffÚno)ÚosÚenvironÚgetÚstripÚlower)Úvals    úL/home/boxd/.venv-cloak/lib/python3.12/site-packages/cloakbrowser/widevine.pyÚ_seeding_disabledr   %   s5   € ä
�*‰*�.‰.Ð0°"Ó
5×
;Ñ
;Ó
=×
CÑ
CÓ
E€CØÐ-Ð-Ð-ó    c                ó’  — t         j                  j                  d«      }|�A|j                  «       syt	        |«      }|dz  j                  «       r|j                  «       S dS ddlm} t	        t        j                  | «      «      j                  dz   |«       dz  fD ]'  }|dz  j                  «       sŒ|j                  «       c S  y)u¸  Locate a sideloaded Widevine CDM directory, or None if absent.

    Resolution order:
      1. If CLOAKBROWSER_WIDEVINE_CDM is set, it is used **exclusively** (overrides
         auto-detection). An invalid value (no ``manifest.json``) skips seeding.
      2. ``<dir of the chrome binary>/WidevineCdm`` â€” where a user naturally drops
         a manual sideload, per Chromium binary version.
      3. ``<cache dir>/WidevineCdm`` (``~/.cloakbrowser/WidevineCdm``) â€” the
         version-independent location the Docker auto-fetch and ``fetch-widevine.py``
         write to. This fallback lets one fetched CDM serve any binary (free or
         Pro, any version) with no env var â€” the CDM ``.so`` is arch-specific but
         not version-specific.

    A directory counts only if it contains ``manifest.json`` (so we don't seed a
    hint pointing at a bogus path). The returned path is absolute and
    symlink-resolved (``Path.resolve()``).
    ÚCLOAKBROWSER_WIDEVINE_CDMNzmanifest.jsoné   )Úget_cache_dirÚWidevineCdm)r   r   r   r   r   Úis_fileÚresolveÚconfigr   ÚfspathÚparent)Úbinary_pathÚcustomÚcdm_dirr   s       r   Úresolve_widevine_cdm_dirr#   +   s³   € ô$ �Z‰Z�^‰^Ð7Ó8€FØÐð �|‰|Œ~ØÜ�v“,ˆØ%,¨Ñ%>×$GÑ$GÔ$Iˆw�‰Ó ÐSÈtÐSå%äœŸ™ ;Ó/Ó0×7Ñ7¸-ÑGÙ!“O mÑ3ð5ò %ˆà�oÑ%×.Ñ.Õ0Ø—?‘?Ó$Ò$ð%ð r   c                ó,  — t        j                  «       dk7  ryt        «       rt        j	                  d«       y| sy	 t        |«      }|€Kt        j                  j                  d«      �t        j                  d«       yt        j	                  d«       yt        t        j                  | «      «      dz  }|j                  dd¬	«       |t        z  }t        j                  d
t!        |«      idd¬«      }	 |j#                  «       r|j%                  d¬«      |k(  ry|j)                  |d¬«       t        j+                  d|«       y# t&        $ r t        j                  d«       Y ŒJw xY w# t&        $ r }t        j                  d|«       Y d}~yd}~ww xY w)uo  Write the Widevine CDM hint file into a persistent profile before launch.

    ``binary_path`` is the resolved chrome executable; the CDM is looked for next
    to it. No-op on non-Linux platforms, when seeding is disabled via
    CLOAKBROWSER_WIDEVINE, or when no sideloaded CDM is present. Never raises â€”
    a failure here must not break the browser launch.
    ÚLinuxNz8Widevine hint seeding disabled via CLOAKBROWSER_WIDEVINEr   zYCLOAKBROWSER_WIDEVINE_CDM is set but has no manifest.json; skipping Widevine hint seedingz7No sideloaded Widevine CDM found; skipping hint seedingr   T)ÚparentsÚexist_okr   )ú,ú:F)Ú
separatorsÚensure_asciizutf-8)Úencodingz,Existing Widevine hint unreadable; rewritingzSeeded Widevine CDM hint -> %sz)Failed to seed Widevine CDM hint file: %s)ÚplatformÚsystemr   ÚloggerÚdebugr#   r   r   r   Úwarningr   r   ÚmkdirÚ_HINT_FILENAMEÚjsonÚdumpsÚstrr   Ú	read_textÚ	ExceptionÚ
write_textÚinfo)Úuser_data_dirr    r"   Úhint_dirÚ	hint_fileÚcontentÚes          r   Úseed_widevine_hintr@   P   si  € ô ‡�Ó˜GÒ#ØÜÔÜ�‰ÐOÔPØÙð 	ðGÜ*¨;Ó7ˆØˆ?Ü�z‰z�~‰~Ð9Ó:ÐFÜ—‘ð5ôð ô —‘ÐVÔWØäœŸ	™	 -Ó0Ó1°MÑAˆØ�‰˜t¨dˆÔ3Øœ~Ñ-ˆ	ô —*‘*˜f¤c¨'£lÐ3À
ÐY^Ô_ˆð	KØ× Ñ Ô" y×':Ñ':ÀGÐ':Ó'LÐPWÒ'WØð 	×Ñ˜W¨wÐÔ7Ü�‰Ð4°gÕ>øô	 ò 	KÜ�N‰NÐIÖJð	Kûô
 ò GÜ�‰ÐBÀA×FÑFûðGúsI   ½AE* Á?E* ÂA E* Ã6%E Ä)E* ÅE'Å$E* Å&E'Å'E* Å*	FÅ3FÆF)ÚreturnÚbool)r    ústr | os.PathLikerA   zPath | None)r;   rC   r    rC   rA   ÚNone)Ú__doc__Ú
__future__r   r4   Úloggingr   r-   Úpathlibr   Ú	getLoggerr/   r3   r   r#   r@   © r   r   ú<module>rK      sH   ðñõ, #ã Û Û 	Û Ý à	ˆ×	Ñ	˜>Ó	*€ð 9€ó.ó"ôJ0Gr   