forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop and unregister existing host when running start_host.
Change-Id: I6580d6544444c9c78deb280d2d7c35094c16a07a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2511856 Commit-Queue: Jamie Walch <jamiewalch@chromium.org> Reviewed-by: Joe Downing <joedow@google.com> Cr-Commit-Position: refs/heads/master@{#823299}
- Loading branch information
1 parent
feb9871
commit dc597a5
Showing
6 changed files
with
162 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "remoting/host/setup/host_stopper.h" | ||
#include "base/logging.h" | ||
#include "base/threading/platform_thread.h" | ||
#include "base/values.h" | ||
|
||
namespace remoting { | ||
|
||
HostStopper::HostStopper(std::unique_ptr<ServiceClient> service_client, | ||
scoped_refptr<DaemonController> daemon_controller) | ||
: service_client_(std::move(service_client)), | ||
daemon_controller_(daemon_controller) { | ||
weak_ptr_ = weak_ptr_factory_.GetWeakPtr(); | ||
} | ||
|
||
HostStopper::~HostStopper() = default; | ||
|
||
void HostStopper::StopLocalHost(std::string access_token, | ||
base::OnceClosure on_done) { | ||
DCHECK(!on_done_); | ||
access_token_ = access_token; | ||
on_done_ = std::move(on_done); | ||
daemon_controller_->GetConfig( | ||
base::BindOnce(&HostStopper::OnConfigLoaded, weak_ptr_)); | ||
} | ||
|
||
void HostStopper::OnConfigLoaded( | ||
std::unique_ptr<base::DictionaryValue> config) { | ||
const std::string* hostId = nullptr; | ||
if (!config || !(hostId = config->FindStringPath("host_id"))) { | ||
std::move(on_done_).Run(); | ||
return; | ||
} | ||
|
||
LOG(INFO) << "Stopping existing host: " << *hostId | ||
<< ". This may take a few seconds."; | ||
service_client_->UnregisterHost(*hostId, access_token_, this); | ||
} | ||
|
||
void HostStopper::StopHost() { | ||
daemon_controller_->Stop(base::BindOnce(&HostStopper::OnStopped, weak_ptr_)); | ||
} | ||
|
||
void HostStopper::OnStopped(DaemonController::AsyncResult) { | ||
bool stopped = false; | ||
for (auto i = 0; !stopped && i < 10; i++) { | ||
stopped = | ||
(daemon_controller_->GetState() == DaemonController::STATE_STOPPED); | ||
if (!stopped) | ||
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | ||
} | ||
if (!stopped) | ||
LOG(WARNING) << "Unable to stop existing host process. Setup will " | ||
<< "continue, but you may need to reboot to complete it."; | ||
std::move(on_done_).Run(); | ||
} | ||
|
||
void HostStopper::OnHostRegistered(const std::string& authorization_code) { | ||
NOTREACHED(); | ||
} | ||
|
||
void HostStopper::OnHostUnregistered() { | ||
StopHost(); | ||
} | ||
|
||
void HostStopper::OnOAuthError() { | ||
StopHost(); | ||
} | ||
|
||
void HostStopper::OnNetworkError(int response_code) { | ||
StopHost(); | ||
} | ||
|
||
} // namespace remoting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef REMOTING_HOST_SETUP_HOST_STOPPER_H_ | ||
#define REMOTING_HOST_SETUP_HOST_STOPPER_H_ | ||
|
||
#include "base/callback.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "remoting/host/setup/daemon_controller.h" | ||
#include "remoting/host/setup/service_client.h" | ||
|
||
namespace remoting { | ||
|
||
// A helper class that stops and unregisters a host. | ||
class HostStopper : public ServiceClient::Delegate { | ||
public: | ||
HostStopper(std::unique_ptr<ServiceClient> service_client, | ||
scoped_refptr<DaemonController> daemon_controller); | ||
HostStopper(const HostStopper&) = delete; | ||
HostStopper& operator=(const HostStopper&) = delete; | ||
~HostStopper() final; | ||
|
||
// Stops the host running on the local computer, if any, and unregisters it. | ||
void StopLocalHost(std::string access_token, base::OnceClosure on_done); | ||
|
||
private: | ||
void OnConfigLoaded(std::unique_ptr<base::DictionaryValue> config); | ||
void StopHost(); | ||
void OnStopped(DaemonController::AsyncResult); | ||
|
||
// remoting::ServiceClient::Delegate | ||
void OnHostRegistered(const std::string& authorization_code) override; | ||
void OnHostUnregistered() override; | ||
void OnOAuthError() override; | ||
void OnNetworkError(int response_code) override; | ||
|
||
std::unique_ptr<remoting::ServiceClient> service_client_; | ||
scoped_refptr<remoting::DaemonController> daemon_controller_; | ||
std::string access_token_; | ||
base::OnceClosure on_done_; | ||
|
||
base::WeakPtr<HostStopper> weak_ptr_; | ||
base::WeakPtrFactory<HostStopper> weak_ptr_factory_{this}; | ||
}; | ||
|
||
} // namespace remoting | ||
|
||
#endif // REMOTING_HOST_SETUP_HOST_STOPPER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters