electron headless

electron加入headless包。打印到pdf:-print-to-pdf  打印到png图片: --screenshot

 chrome的启动main:srcchromeappchrome_main.cc

用electron执行headless:

e run  --headless --enable-logging --v=2 --disable-gpu --screenshot  http://192.168.50.206:8889

Running "/home/a/dev0/e9.2.1/src/out/ReleaseSym0/electron --headless --enable-logging --v=2 --disable-gpu --screenshot http://192.168.50.206:8889" [1028/172650.483932:INFO:cpu_info.cc(53)] Available number of cores: 4 [1028/172650.484061:VERBOSE1:zygote_main_linux.cc(217)] ZygoteMain: initializing 0 fork delegates [1028/172650.484400:INFO:cpu_info.cc(53)] Available number of cores: 4 [1028/172650.484465:VERBOSE1:zygote_main_linux.cc(217)] ZygoteMain: initializing 0 fork delegates [1028/172650.493514:VERBOSE1:webrtc_internals.cc(119)] Could not get the download directory. [1028/172650.494623:VERBOSE1:proxy_config_service_linux.cc(500)] All gsettings tests OK. Will get proxy config from gsettings. [1028/172650.494764:VERBOSE1:proxy_config_service_linux.cc(1261)] Obtained proxy settings from annotation hash code 11258689 [1028/172650.494873:VERBOSE1:proxy_config_service_linux.cc(500)] All gsettings tests OK. Will get proxy config from gsettings. [1028/172650.494919:VERBOSE1:proxy_config_service_linux.cc(1261)] Obtained proxy settings from annotation hash code 11258689 [1028/172650.504033:VERBOSE1:sandbox_linux.cc(69)] Activated seccomp-bpf sandbox for process type: renderer. [1028/172650.505596:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded [1028/172650.511468:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded [1028/172650.524408:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded [1028/172650.524916:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded [1028/172650.525173:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded [1028/172650.525963:VERBOSE1:sandbox_linux.cc(69)] Activated seccomp-bpf sandbox for process type: gpu-process. [1028/172650.526373:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded [1028/172650.528735:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded [1028/172650.531839:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded [1028/172650.535051:ERROR:paint_controller.cc(646)] PaintController::FinishCycle() completed [1028/172650.550076:VERBOSE1:configured_proxy_resolution_service.cc(873)] PAC support disabled because there is no system implementation [1028/172650.550312:VERBOSE1:configured_proxy_resolution_service.cc(873)] PAC support disabled because there is no system implementation [1028/172650.550923:VERBOSE1:network_delegate.cc(32)] NetworkDelegate::NotifyBeforeURLRequest: http://192.168.50.206:8889/ [1028/172650.575829:VERBOSE1:url_loader.cc(418)] To buffer: http://192.168.50.206:8889/ [1028/172650.580122:VERBOSE1:v8_context_snapshot.cc(153)] A context is created from snapshot for main world [1028/172650.587399:VERBOSE1:v8_context_snapshot.cc(153)] A context is created from snapshot for main world [1028/172650.595294:ERROR:paint_controller.cc(646)] PaintController::FinishCycle() completed [1028/172650.612295:ERROR:paint_controller.cc(646)] PaintController::FinishCycle() completed [1028/172650.676553:INFO:headless_shell.cc(620)] Written to file screenshot.png.

用chromium执行:

chromium -disable-gpu -headless --screenshot http://192.168.50.206:8889 --enable-logging --v=2

[1028/172313.589085:INFO:cpu_info.cc(53)] Available number of cores: 4 [1028/172313.589050:INFO:cpu_info.cc(53)] Available number of cores: 4 [1028/172313.589665:VERBOSE1:zygote_main_linux.cc(218)] ZygoteMain: initializing 0 fork delegates [1028/172313.589646:VERBOSE1:zygote_main_linux.cc(218)] ZygoteMain: initializing 0 fork delegates [1028/172313.904647:VERBOSE1:webrtc_internals.cc(118)] Could not get the download directory. [1028/172314.273904:VERBOSE1:proxy_config_service_linux.cc(500)] All gsettings tests OK. Will get proxy config from gsettings. [1028/172314.282438:VERBOSE1:proxy_config_service_linux.cc(1261)] Obtained proxy settings from annotation hash code 11258689 [1028/172314.313809:VERBOSE1:proxy_config_service_linux.cc(500)] All gsettings tests OK. Will get proxy config from gsettings. [1028/172314.313966:VERBOSE1:proxy_config_service_linux.cc(1261)] Obtained proxy settings from annotation hash code 11258689 [1028/172314.375146:VERBOSE1:bus.cc(884)] Method call: message_type: MESSAGE_METHOD_CALL interface: org.freedesktop.DBus member: GetNameOwner signature: s string "org.bluez" [1028/172314.376150:VERBOSE1:bus.cc(884)] Method call: message_type: MESSAGE_METHOD_CALL interface: org.freedesktop.DBus member: GetNameOwner signature: s string "org.bluez" [1028/172314.377416:VERBOSE1:bus.cc(884)] Method call: message_type: MESSAGE_METHOD_CALL interface: org.freedesktop.DBus member: GetNameOwner signature: s string "org.chromium.Bluetooth" [1028/172314.377974:VERBOSE1:bus.cc(884)] Method call: message_type: MESSAGE_METHOD_CALL interface: org.freedesktop.DBus member: GetNameOwner signature: s string "org.chromium.Bluetooth" [1028/172314.519935:VERBOSE1:configured_proxy_resolution_service.cc(845)] PAC support disabled because there is no system implementation [1028/172314.558362:VERBOSE1:configured_proxy_resolution_service.cc(845)] PAC support disabled because there is no system implementation [1028/172314.558720:VERBOSE1:sandbox_linux.cc(68)] Activated seccomp-bpf sandbox for process type: gpu-process. [1028/172314.572644:VERBOSE1:device_data_manager_x11.cc(216)] X Input extension not available [1028/172314.677476:VERBOSE1:bus.cc(681)] Filter function already exists: 1 with associated data: 0x55bc5cc268f0 [1028/172314.677539:VERBOSE1:bus.cc(884)] Method call: message_type: MESSAGE_METHOD_CALL interface: org.freedesktop.DBus member: GetNameOwner signature: s string "org.bluez" [1028/172314.690087:VERBOSE1:bus.cc(681)] Filter function already exists: 1 with associated data: 0x55bc5cc268f0 [1028/172314.690318:VERBOSE1:bus.cc(884)] Method call: message_type: MESSAGE_METHOD_CALL interface: org.freedesktop.DBus member: GetNameOwner signature: s string "org.bluez" [1028/172314.690740:VERBOSE1:bus.cc(681)] Filter function already exists: 1 with associated data: 0x55bc5cc88a60 [1028/172314.690763:VERBOSE1:bus.cc(884)] Method call: message_type: MESSAGE_METHOD_CALL interface: org.freedesktop.DBus member: GetNameOwner signature: s string "org.chromium.Bluetooth" [1028/172314.691081:VERBOSE1:bus.cc(681)] Filter function already exists: 1 with associated data: 0x55bc5cc88a60 [1028/172314.691118:VERBOSE1:bus.cc(884)] Method call: message_type: MESSAGE_METHOD_CALL interface: org.freedesktop.DBus member: GetNameOwner signature: s string "org.chromium.Bluetooth" [1028/172314.700108:VERBOSE1:proxy_config_service_linux.cc(1368)] Detected no-op change to proxy settings. Doing nothing. [1028/172314.700179:VERBOSE1:proxy_config_service_linux.cc(1368)] Detected no-op change to proxy settings. Doing nothing. [1028/172314.701004:VERBOSE1:network_delegate.cc(32)] NetworkDelegate::NotifyBeforeURLRequest: http://192.168.50.206:8889/ [1028/172314.740322:VERBOSE1:sandbox_linux.cc(68)] Activated seccomp-bpf sandbox for process type: renderer. [1028/172314.781201:VERBOSE1:url_loader.cc(402)] To buffer: http://192.168.50.206:8889/ [1028/172314.813797:VERBOSE2:thread_state.cc(429)] [state:0x5556c9cbf680] ScheduleGCIfNeeded [1028/172314.996829:VERBOSE2:thread_state.cc(429)] [state:0x5556c9cbf680] ScheduleGCIfNeeded [1028/172314.997088:VERBOSE2:thread_state.cc(429)] [state:0x5556c9cbf680] ScheduleGCIfNeeded [1028/172315.022090:VERBOSE2:thread_state.cc(429)] [state:0x5556c9cbf680] ScheduleGCIfNeeded [1028/172315.034964:VERBOSE2:thread_state.cc(429)] [state:0x5556c9cbf680] ScheduleGCIfNeeded [1028/172315.070170:VERBOSE2:thread_state.cc(429)] [state:0x5556c9cbf680] ScheduleGCIfNeeded [1028/172315.085356:VERBOSE2:thread_state.cc(429)] [state:0x5556c9cbf680] ScheduleGCIfNeeded [1028/172315.111739:VERBOSE2:thread_state.cc(429)] [state:0x5556c9cbf680] ScheduleGCIfNeeded [1028/172315.122704:ERROR:paint_controller.cc(634)] PaintController::FinishCycle() completed [1028/172315.156658:ERROR:paint_controller.cc(634)] PaintController::FinishCycle() completed [1028/172315.175984:ERROR:paint_controller.cc(634)] PaintController::FinishCycle() completed [1028/172315.236399:INFO:headless_shell.cc(616)] Written to file screenshot.png.

 electron不用 --headess启动时报错:

#0  0x000055840b03c9ad in ui::DeviceDataManagerX11::DeviceDataManagerX11()::$_0::operator()() const (this=<optimized out>)
    at ../../ui/events/devices/x11/device_data_manager_x11.cc:169
169      CHECK(gfx::GetXDisplay());
--Type <RET> for more, q to quit, c to continue without paging--
[Current thread is 1 (Thread 0x7f8fa78ba240 (LWP 1421))]
(gdb) bt
#0  0x000055840b03c9ad in ui::DeviceDataManagerX11::DeviceDataManagerX11()::$_0::operator()() const (this=<optimized out>)
    at ../../ui/events/devices/x11/device_data_manager_x11.cc:169
#1  0x000055840b03c9ad in ui::DeviceDataManagerX11::DeviceDataManagerX11()
    (this=0x21f40a5c7000)
    at ../../ui/events/devices/x11/device_data_manager_x11.cc:169
#2  0x000055840b03c851 in ui::DeviceDataManagerX11::CreateInstance() ()
    at ../../ui/events/devices/x11/device_data_manager_x11.cc:153
#3  0x000055840b041efb in ui::X11EventSource::X11EventSource(_XDisplay*) (this=
    0x21f40a35e8c0, display=display@entry=0x0)
    at ../../ui/events/platform/x11/x11_event_source.cc:123
#4  0x000055840b043abd in std::__1::make_unique<ui::X11EventSource, _XDisplay*>(_XDisplay*&&) (__args=<optimized out>)
    at ../../buildtools/third_party/libc++/trunk/include/memory:3043
#5  0x000055840b043abd in ui::PlatformEventSource::CreateDefault() ()
    at ../../ui/events/platform/x11/x11_event_source.cc:448
#6  0x000055840b445135 in aura::Env::Init() (this=<optimized out>)
    at ../../ui/aura/env.cc:226
#7  0x000055840b445135 in aura::Env::CreateInstance() ()
    at ../../ui/aura/env.cc:96
#8  0x0000558409a6f3a4 in content::BrowserMainLoop::InitializeToolkit()
    (this=0x21f40a352b40) at ../../content/browser/browser_main_loop.cc:1480
--Type <RET> for more, q to quit, c to continue without paging--c
inFunctionParams const&) (this=0x21f40a37f780, parameters=...) at ../../content/browser/browser_main_runner_impl.cc:113
#10 0x0000558409a6b61d in content::BrowserMain(content::MainFunctionParams const&) (parameters=...) at ../../content/browser/browser_main.cc:43
#11 0x000055840995d379 in content::RunBrowserProcessMain(content::MainFunctionParams const&, content::ContentMainDelegate*) (main_function_params=..., delegate=<optimized out>) at ../../content/app/content_main_runner_impl.cc:530
#12 0x000055840995d379 in content::ContentMainRunnerImpl::RunServiceManager(content::MainFunctionParams&, bool) (this=<optimized out>, this@entry=0x21f40a34ce40, main_params=..., start_service_manager_only=<optimized out>) at ../../content/app/content_main_runner_impl.cc:980
#13 0x000055840995cfaa in content::ContentMainRunnerImpl::Run(bool) (this=0x21f40a34ce40, start_service_manager_only=false) at ../../content/app/content_main_runner_impl.cc:879
#14 0x000055840b86a5fa in service_manager::Main(service_manager::MainParams const&) (params=...) at ../../services/service_manager/embedder/main.cc:454
#15 0x0000558408cd6101 in content::ContentMain(content::ContentMainParams const&) (params=...) at ../../content/app/content_main.cc:19
#16 0x00005584080ce9fd in main(int, char**) (argc=3, argv=0x7fff2ba1c368) at ../../electron/shell/app/electron_main.cc:231

可以看到从electron_main启动时,会从content调到aura,

aura::Env::Init()中,调用到了PlatformEventSource::CreateDefault()
 if (!ui::PlatformEventSource::GetInstance())
    event_source_ = ui::PlatformEventSource::CreateDefault();
这个ui::PlatformEventSource::CreateDefault()是个虚静态函数。具体互斥实现在两个地方(windows下)platform_event_source_stub.cc和linux x11下x11_event_source.cc。
从D:develectron9srcuieventsplatformBUILD.gn可以看到
if (use_x11) {
sources -= [ "platform_event_source_stub.cc" ] 排除掉了!!!
}
从 D:develectron9srcuieventsplatformx11x11_event_source.cc看到在x11下,引用的是
// static
#if !defined(USE_OZONE)
std::unique_ptr<PlatformEventSource> PlatformEventSource::CreateDefault() {
  return std::make_unique<X11EventSource>(gfx::GetXDisplay());
}
#endif

 调到依赖库:ui/events/platform/x11 ui/events/devices/x11

而在Headless中, ui::PlatformEventSource::CreateDefault()这句话不会走到,因为前面有个判断已经为true了。这个true是由于在
D:develectron9srcheadlesslibheadless_content_main_delegate.h中,类里面有句
#if !defined(CHROME_MULTIPLE_DLL_CHILD)
  HeadlessPlatformEventSource platform_event_source_;
#endif
platform_event_source_ 导致了实例在程序开始就做了初始化,调用了HeadlessPlatformEventSource::HeadlessPlatformEventSource()。


HeadlessBrowserImpl中, RunOnStartCallback中调用了 PlatformStart启动UI设置。
void HeadlessBrowserImpl::PlatformStart() {
  DCHECK(aura::Env::GetInstance());
  ui::DeviceDataManager::CreateInstance();
}
剩下再看window tree host如何移去了window窗口。 


electron windows版本中:

 从这里调用,new Env时会调用父类 new TargetEvent。

// static
std::unique_ptr<Env> Env::CreateInstance() {
  DCHECK(!g_primary_instance);
  // No make_unique as constructor is private.
  std::unique_ptr<Env> env(new Env());
  g_primary_instance = env.get();
  env->Init();
  return env;
}

调用到init到:

void Env::Init() {
#if defined(USE_OZONE)
  // The ozone platform can provide its own event source. So initialize the
  // platform before creating the default event source. If running inside mus
  // let the mus process initialize ozone instead.
  ui::OzonePlatform::InitParams params;
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  // TODO(kylechar): Pass in single process information to Env::CreateInstance()
  // instead of checking flags here.
  params.single_process = command_line->HasSwitch("single-process") ||
                          command_line->HasSwitch("in-process-gpu");
  params.using_mojo = features::IsOzoneDrmMojo();
  params.viz_display_compositor = features::IsVizDisplayCompositorEnabled();

  ui::OzonePlatform::InitializeForUI(params);
#endif
  if (!ui::PlatformEventSource::GetInstance())
    event_source_ = ui::PlatformEventSource::CreateDefault();
}

 ui::PlatformEventSource::CreateDefault();返回时null。

下面是从js调用来的:new browserWindow

D:develectron7srcelectronshellrowseratom_browser_main_parts.cc 里面是browser的一些启动需要执行函数:PreMainMessageLoopRun

D:develectron7srcelectronshellrowserapiatom_api_browser_window.cc   BrowserWindow::New->  new BrowserWindow(args->isolate(), args->GetThis(), options); 创建窗口

D:develectron7srcelectronshellrowserapiatom_api_top_level_window.cc  TopLevelWindow::TopLevelWindow ->window_.reset(NativeWindow::Create(

options, parent.IsEmpty() ? nullptr : parent->window_.get()));

        里面还初始化了contents:D:develectron7srcelectronshellrowserapiatom_api_web_contents.cc WebContents::Create(isolate, web_preferences);

D:develectron7srcelectronshellrowsernative_window_views.cc  NativeWindow::Create 调用windows本地窗口类

D:develectron7srcelectronshellrowser ative_window_views.cc NativeWindowViews::NativeWindowViews 通过ui widget的init又调用到

D:develectron7srcelectronshellrowseruiwinatom_desktop_native_widget_aura.cc 这里是对windows上用aura的electron实现!!!在哪里指定了这个实现????

 



原文地址:https://www.cnblogs.com/bigben0123/p/13892322.html