Chrome for AndroidRemote Inspector 原理

    xiaoxiao2025-04-10  15

    remote inspector架构

    remote inpsector 问题诊断

    判断是否开启了unix domain socket

    运行下列命令,如果查到abstract socket:@chrome_devtools_remote,则表明chrome for android 开启了unix domain socket。如果查到abstract socket@webview_devtools_remote_27594,则说明了AndroidWebView也开启了unix domain socket

    $ adb shell cat /proc/net/unix | grep --text _devtools_remote 00000000: 00000002 00000000 00010000 0001 01 3517288 @webview_devtools_remote_27594 00000000: 00000002 00000000 00010000 0001 01 3535173 @chrome_devtools_remote 00000000: 00000003 00000000 00000000 0001 03 3546131 @chrome_devtools_remote

    其中AndroidWebView的unix domain socket格式为:@webview_devtools_remote_,定义在文件aw_dev_tools_server.cc中

    const char kSocketNameFormat[] = "webview_devtools_remote_%d"; ... void AwDevToolsServer::Start() { if (devtools_http_handler_) return; scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( new UnixDomainServerSocketFactory( base::StringPrintf(kSocketNameFormat, getpid()))); devtools_http_handler_.reset(new DevToolsHttpHandler( std::move(factory), base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()), new AwDevToolsServerDelegate(), base::FilePath(), base::FilePath(), GetProduct(), GetUserAgent())); }

    判断是否开启了HTTP服务器

    如果运行下列命令,curl能够返回如下数据,说明已经在@chrome_devtools_remote上开启了HTTP服务器 其中webSocketDebuggerUrl关键字的value说明remote inpsector protocal所使用的websocket地址。

    $ adb forward tcp:4000 localabstract:chrome_devtools_remote $ curl localhost:4000/json [ { "description": "", "devtoolsFrontendUrl": "http://chrome-devtools-frontend.appspot.com/serve_rev/@164914/devtools.html?ws=localhost:4000/devtools/page/140", "faviconUrl": "https://m.baidu.com/favicon.ico", "id": "140", "title": "\u767E\u5EA6\u4E00\u4E0B", "type": "page", "url": "https://m.baidu.com/?from=844b&vit=fps", "webSocketDebuggerUrl": "ws://localhost:4000/devtools/page/140" } ]

    判断是否开启了websocket服务器

    可以通过ptyhon库websocket-client来测试.

    $ sudo pip install websocket-client python >>> import websocket >>> ws = websocket.create_connection("ws://localhost:4000/devtools/page/140") >>> ws.send("Hello,world") 17 >>> result = ws.recv() >>> print result {"error":{"code":-32700,"message":"Message must be in JSON format"},"id":null}

    如果能够收到返回值,则说明websocket服务正常。

    参考引用

    1,How remote debugging works in Chrome 2,Google Chrome for Android Remote Debugging - “localhost:9222” Not Available 3,websocket原理

    转载请注明原文地址: https://ju.6miu.com/read-1297920.html
    最新回复(0)