1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/credential_provider/extension/service.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_executor.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/credential_provider/extension/os_service_manager.h"
#include "chrome/credential_provider/extension/task_manager.h"
#include "chrome/credential_provider/gaiacp/logging.h"
namespace credential_provider {
namespace extension {
// static
Service** Service::GetInstanceStorage() {
static Service* instance = new Service();
return &instance;
}
// static
Service* Service::Get() {
return *GetInstanceStorage();
}
DWORD Service::Run() {
return (this->*run_routine_)();
}
Service::Service()
: run_routine_(&Service::RunAsService),
service_status_(),
service_status_handle_(nullptr) {
service_status_.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
service_status_.dwCurrentState = SERVICE_STOPPED;
service_status_.dwControlsAccepted =
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN;
}
Service::~Service() = default;
DWORD Service::RunAsService() {
LOGFN(VERBOSE);
DWORD error_code =
extension::OSServiceManager::Get()->StartServiceCtrlDispatcher(
&Service::ServiceMain);
if (error_code != ERROR_SUCCESS) {
LOGFN(ERROR)
<< "OSServiceManager::StartServiceCtrlDispatcher failed with win32="
<< error_code;
}
return error_code;
}
void Service::StartMain() {
base::SingleThreadTaskExecutor main_task_executor;
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner =
main_task_executor.task_runner();
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
DWORD error_code = extension::OSServiceManager::Get()->RegisterCtrlHandler(
&Service::ServiceControlHandler, &service_status_handle_);
if (error_code != ERROR_SUCCESS) {
LOGFN(ERROR) << "OSServiceManager::RegisterCtrlHandler failed win32="
<< error_code;
return;
}
service_status_.dwCurrentState = SERVICE_RUNNING;
error_code = extension::OSServiceManager::Get()->SetServiceStatus(
service_status_handle_, service_status_);
if (error_code != ERROR_SUCCESS) {
LOGFN(ERROR) << "OSServiceManager::SetServiceStatus failed win32="
<< error_code;
return;
}
TaskManager::Get()->RunTasks(main_task_runner);
run_loop.Run();
service_status_.dwCurrentState = SERVICE_STOPPED;
service_status_.dwControlsAccepted = 0;
error_code = extension::OSServiceManager::Get()->SetServiceStatus(
service_status_handle_, service_status_);
if (error_code != ERROR_SUCCESS)
LOGFN(ERROR) << "OSServiceManager::SetServiceStatus failed win32="
<< error_code;
}
// static
VOID WINAPI Service::ServiceMain(DWORD argc /*unused*/,
WCHAR* argv[] /*unused*/) {
LOGFN(VERBOSE);
Service* self = Service::Get();
// Run the service.
self->StartMain();
}
// static
VOID WINAPI Service::ServiceControlHandler(DWORD control) {
LOGFN(VERBOSE);
Service* self = Service::Get();
switch (control) {
case SERVICE_CONTROL_PRESHUTDOWN:
case SERVICE_CONTROL_STOP:
self->service_status_.dwCurrentState = SERVICE_STOP_PENDING;
extension::OSServiceManager::Get()->SetServiceStatus(
self->service_status_handle_, self->service_status_);
std::move(self->quit_closure_).Run();
break;
default:
break;
}
}
} // namespace extension
} // namespace credential_provider
|