#!/usr/bin/env python3 """Capture before/after screenshots for CL 8626307 (crbug.com/40819284). Serves the cross-site repro from `cl-8626307-offscreencanvas` and opens it in a local Chrome build with a.test and b.test mapped to the local server, so the iframe really ends up in another renderer process. The CL has no runtime flag, so the caller builds the matching binary first: git checkout HEAD~1 -- && autoninja -C out/Default chrome python3 capture_screenshots.py before git checkout HEAD -- && autoninja -C out/Default chrome python3 capture_screenshots.py after """ import base64 import functools import http.server import json import os import shutil import subprocess import sys import tempfile import threading import time import urllib.request import websocket HERE = os.path.dirname(os.path.abspath(__file__)) REPRO = os.path.expanduser("~/lab/cl-8626307-offscreencanvas") CHROME = os.path.expanduser("~/chromium_2/src/out/Default/chrome") PORT = 8765 VIEWPORT_WIDTH = 520 VIEWPORT_HEIGHT = 340 def serve(): handler = functools.partial( http.server.SimpleHTTPRequestHandler, directory=REPRO ) server = http.server.ThreadingHTTPServer(("127.0.0.1", PORT), handler) threading.Thread(target=server.serve_forever, daemon=True).start() return server def wait_devtools(port): for _ in range(60): try: resp = urllib.request.urlopen("http://127.0.0.1:{}/json".format(port)) tabs = json.loads(resp.read()) pages = [t for t in tabs if t["type"] == "page"] if pages: return pages[0] except Exception: time.sleep(0.5) raise RuntimeError("DevTools did not come up on port %d" % port) def devtools(ws, counter, method, params=None): counter[0] += 1 msg = {"id": counter[0], "method": method} if params: msg["params"] = params ws.send(json.dumps(msg)) while True: resp = json.loads(ws.recv()) if resp.get("id") == counter[0]: return resp def capture(label, port=9485): profile = tempfile.mkdtemp(prefix="cl8626307-") proc = subprocess.Popen( [ CHROME, "--remote-debugging-port={}".format(port), "--remote-allow-origins=*", "--user-data-dir=" + profile, "--no-first-run", "--no-default-browser-check", "--host-resolver-rules=MAP * 127.0.0.1", "--window-size={},{}".format(VIEWPORT_WIDTH, VIEWPORT_HEIGHT), "http://a.test:{}/parent.html".format(PORT), ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) try: page = wait_devtools(port) ws = websocket.create_connection(page["webSocketDebuggerUrl"]) counter = [0] devtools( ws, counter, "Emulation.setDeviceMetricsOverride", { "width": VIEWPORT_WIDTH, "height": VIEWPORT_HEIGHT, "deviceScaleFactor": 1, "mobile": False, }, ) # Long enough for the ping/pong loop to settle either way. time.sleep(6) shot = devtools( ws, counter, "Page.captureScreenshot", {"format": "png"} ) out_path = os.path.join(HERE, "rendered", "repro-{}.png".format(label)) os.makedirs(os.path.dirname(out_path), exist_ok=True) with open(out_path, "wb") as f: f.write(base64.b64decode(shot["result"]["data"])) status = devtools( ws, counter, "Runtime.evaluate", {"expression": "document.getElementById('status').textContent"}, ) print(" {:<6} {} -> {}".format( label, status["result"]["result"]["value"], out_path)) ws.close() finally: proc.terminate() try: proc.wait(timeout=10) except subprocess.TimeoutExpired: proc.kill() shutil.rmtree(profile, ignore_errors=True) def main(): label = sys.argv[1] if len(sys.argv) > 1 else "after" server = serve() try: capture(label) finally: server.shutdown() print("Done.") if __name__ == "__main__": sys.exit(main())