I would like to send a D-Bus signal from arbitrary locations. This CL
should be relatively self-explanatory:
https://chromium-review.googlesource.com/#/c/chromium/src/+/1062078
The D-Bus signal is then consumed by a "memory daemon" which collects
and correlates a number of memory manager stats and events. This is
functionally similar to a histogram sample generation, which can also
be inserted in arbitrary code and uses global objects. This
functionality relies on services/objects that aren't always present
(i.e. the dbus service at startup/shutdown) but in these cases it's
fine to do nothing.
I build a D-Bus service provider (which is the proper context for
sending D-Bus signals) and add it to the DBusServices object in the
ChromeBrowserMainPartsChromeos object, which is added to the
ChromeBrowserMainParts object, which is inserted in the
BrowserMainLoop object which I think is globally available as
g_current_browser_main_loop.
The problem is that there are four layers of private members to go
through---although some may have accessor functions.
There is one other place that sends D-Bus signals in the context of
the ComponentUpdater, but that one has a CrosComponentManager object
which helps expose this functionality.
In cros_component_installer.cc:
g_browser_process->platform_part()
->cros_component_manager()
->EmitInstalledSignal(GetName());
https://cs.chromium.org/chromium/src/chrome/browser/component_updater/cros_component_installer_chromeos.cc?q=EmitInstalledSignal&sq=package:chromium&dr=CSs&l=132
I could try to do something similar, but I am wondering if it would be
acceptable to use a static function instead, or a global pointer.
(The static function would also contain a static pointer, same idea.)
Thanks!
P.S. incidentally, I am aware that the D-Bus signal may need to be
posted in a different thread, but haven't looked into that part yet.