service_main.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CHROME_ELEVATION_SERVICE_SERVICE_MAIN_H_
  5. #define CHROME_ELEVATION_SERVICE_SERVICE_MAIN_H_
  6. #include <windows.h>
  7. #include <wrl/implements.h>
  8. #include "base/no_destructor.h"
  9. #include "base/synchronization/waitable_event.h"
  10. namespace base {
  11. class CommandLine;
  12. } // namespace base
  13. namespace elevation_service {
  14. class ServiceMain {
  15. public:
  16. static ServiceMain* GetInstance();
  17. ServiceMain(const ServiceMain&) = delete;
  18. ServiceMain& operator=(const ServiceMain&) = delete;
  19. // This function parses the command line and selects the action routine.
  20. bool InitWithCommandLine(const base::CommandLine* command_line);
  21. // Start() is the entry point called by WinMain.
  22. int Start();
  23. // The following methods are public for the sake of testing.
  24. // Creates an out-of-proc WRL Module.
  25. void CreateWRLModule();
  26. // Registers the Service COM class factory object so other applications can
  27. // connect to it. Returns the registration status.
  28. HRESULT RegisterClassObject();
  29. // Unregisters the Service COM class factory object.
  30. void UnregisterClassObject();
  31. // Returns true when the last COM object is released, or if the service is
  32. // asked to exit.
  33. bool IsExitSignaled();
  34. // Resets the state of the |exit_signal_| event.
  35. void ResetExitSignaled();
  36. private:
  37. ServiceMain();
  38. ~ServiceMain();
  39. // This function handshakes with the service control manager and starts
  40. // the service.
  41. int RunAsService();
  42. // Runs the service on the service thread.
  43. void ServiceMainImpl();
  44. // Runs as a local server for testing purposes. RunInteractive returns an
  45. // HRESULT, not a Win32 error code.
  46. int RunInteractive();
  47. // The control handler of the service.
  48. static void WINAPI ServiceControlHandler(DWORD control);
  49. // The main service entry point.
  50. static void WINAPI ServiceMainEntry(DWORD argc, wchar_t* argv[]);
  51. // Calls ::SetServiceStatus().
  52. void SetServiceStatus(DWORD state);
  53. // Handles object registration, message loop, and unregistration. Returns
  54. // when all registered objects are released.
  55. HRESULT Run();
  56. // Calls ::CoInitializeSecurity to allow all users to create COM objects
  57. // within the server.
  58. static HRESULT InitializeComSecurity();
  59. // Waits until the last object is released or until the service is asked to
  60. // exit.
  61. void WaitForExitSignal();
  62. // Called when the last object is released or if the service is asked to exit.
  63. void SignalExit();
  64. // Registers |factory| as the factory for the elevator identified by |id|.
  65. void RegisterElevatorFactory(const std::u16string& id,
  66. IClassFactory* factory);
  67. // The action routine to be executed.
  68. int (ServiceMain::*run_routine_)();
  69. SERVICE_STATUS_HANDLE service_status_handle_;
  70. SERVICE_STATUS service_status_;
  71. // Identifier of registered class objects used for unregistration.
  72. DWORD cookies_[1];
  73. // This event is signaled when the last COM instance is released, or if the
  74. // service control manager asks the service to exit.
  75. base::WaitableEvent exit_signal_;
  76. friend class base::NoDestructor<ServiceMain>;
  77. };
  78. } // namespace elevation_service
  79. #endif // CHROME_ELEVATION_SERVICE_SERVICE_MAIN_H_