Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Unified Diff: remoting/host/win/host_service.cc

Issue 16143004: Use a weak pointer to post service control events and session change notifications. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CR feedback Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « remoting/host/win/host_service.h ('k') | remoting/remoting.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/host/win/host_service.cc
diff --git a/remoting/host/win/host_service.cc b/remoting/host/win/host_service.cc
index 42a8ed070b0fc6c430ac49c8565c2bd536bc4773..b3cf6b0321b35658c0774f412e24b9eb7debf875 100644
--- a/remoting/host/win/host_service.cc
+++ b/remoting/host/win/host_service.cc
@@ -25,7 +25,6 @@
#include "base/win/windows_version.h"
#include "remoting/base/auto_thread.h"
#include "remoting/base/scoped_sc_handle_win.h"
-#include "remoting/base/stoppable.h"
#include "remoting/host/branding.h"
#include "remoting/host/daemon_process.h"
#include "remoting/host/host_exit_codes.h"
@@ -203,7 +202,8 @@ void HostService::RemoveWtsTerminalObserver(WtsTerminalObserver* observer) {
HostService::HostService() :
run_routine_(&HostService::RunAsService),
service_status_handle_(0),
- stopped_event_(true, false) {
+ stopped_event_(true, false),
+ weak_factory_(this) {
}
HostService::~HostService() {
@@ -275,17 +275,10 @@ void HostService::CreateLauncher(
return;
}
- child_ = DaemonProcess::Create(
+ daemon_process_ = DaemonProcess::Create(
task_runner,
io_task_runner,
- base::Bind(&HostService::OnChildStopped,
- base::Unretained(this))).PassAs<Stoppable>();
-}
-
-void HostService::OnChildStopped() {
- DCHECK(main_task_runner_->BelongsToCurrentThread());
-
- child_.reset(NULL);
+ base::Bind(&HostService::StopDaemonProcess, weak_ptr_));
}
int HostService::RunAsService() {
@@ -312,6 +305,7 @@ void HostService::RunAsServiceImpl() {
base::MessageLoop message_loop(base::MessageLoop::TYPE_UI);
base::RunLoop run_loop;
main_task_runner_ = message_loop.message_loop_proxy();
+ weak_ptr_ = weak_factory_.GetWeakPtr();
// Register the service control handler.
service_status_handle_ = RegisterServiceCtrlHandlerExW(
@@ -351,6 +345,7 @@ void HostService::RunAsServiceImpl() {
// Run the service.
run_loop.Run();
+ weak_factory_.InvalidateWeakPtrs();
// Tell SCM that the service is stopped.
service_status.dwCurrentState = SERVICE_STOPPED;
@@ -366,6 +361,7 @@ int HostService::RunInConsole() {
base::MessageLoop message_loop(base::MessageLoop::TYPE_UI);
base::RunLoop run_loop;
main_task_runner_ = message_loop.message_loop_proxy();
+ weak_ptr_ = weak_factory_.GetWeakPtr();
int result = kInitializationFailed;
@@ -410,6 +406,8 @@ int HostService::RunInConsole() {
}
cleanup:
+ weak_factory_.InvalidateWeakPtrs();
+
// Unsubscribe from console events. Ignore the exit code. There is nothing
// we can do about it now and the program is about to exit anyway. Even if
// it crashes nothing is going to be broken because of it.
@@ -418,6 +416,12 @@ cleanup:
return result;
}
+void HostService::StopDaemonProcess() {
+ DCHECK(main_task_runner_->BelongsToCurrentThread());
+
+ daemon_process_.reset();
+}
+
bool HostService::HandleMessage(
HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, LRESULT* result) {
if (message == WM_WTSSESSION_CHANGE) {
@@ -438,9 +442,9 @@ BOOL WINAPI HostService::ConsoleControlHandler(DWORD event) {
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
- self->main_task_runner_->PostTask(FROM_HERE, base::Bind(
- &Stoppable::Stop, base::Unretained(self->child_.get())));
- self->stopped_event_.Wait();
+ self->main_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&HostService::StopDaemonProcess,
+ self->weak_ptr_));
return TRUE;
default:
@@ -460,14 +464,14 @@ DWORD WINAPI HostService::ServiceControlHandler(DWORD control,
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
- self->main_task_runner_->PostTask(FROM_HERE, base::Bind(
- &Stoppable::Stop, base::Unretained(self->child_.get())));
- self->stopped_event_.Wait();
+ self->main_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&HostService::StopDaemonProcess,
+ self->weak_ptr_));
return NO_ERROR;
case SERVICE_CONTROL_SESSIONCHANGE:
self->main_task_runner_->PostTask(FROM_HERE, base::Bind(
- &HostService::OnSessionChange, base::Unretained(self), event_type,
+ &HostService::OnSessionChange, self->weak_ptr_, event_type,
reinterpret_cast<WTSSESSION_NOTIFICATION*>(event_data)->dwSessionId));
return NO_ERROR;
« no previous file with comments | « remoting/host/win/host_service.h ('k') | remoting/remoting.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698