Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick PR #2594: DIAL Service restarting #2604

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cobalt/browser/browser_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,11 @@ void BrowserModule::UnfreezeInternal(SbTimeMonotonic timestamp) {

FOR_EACH_OBSERVER(LifecycleObserver, lifecycle_observers_,
Unfreeze(GetResourceProvider(), timestamp));
#if defined(DIAL_SERVER)
if (network_module_) {
network_module_->RestartDialService();
}
#endif
}

void BrowserModule::OnMaybeFreeze() {
Expand Down
19 changes: 19 additions & 0 deletions cobalt/network/dial/dial_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,36 @@ void DialServiceProxy::Deregister(
FROM_HERE, base::Bind(&DialServiceProxy::OnDeregister, this, handler));
}

void DialServiceProxy::ReplaceDialService(
const base::WeakPtr<DialService>& service) {
task_runner_->PostTask(
FROM_HERE,
base::Bind(&DialServiceProxy::OnReplaceDialService, this, service));
}

void DialServiceProxy::OnRegister(
const scoped_refptr<DialServiceHandler>& handler) {
if (dial_service_) {
dial_service_->Register(handler);
handlers_.push_back(handler);
}
}

void DialServiceProxy::OnDeregister(
const scoped_refptr<DialServiceHandler>& handler) {
if (dial_service_) {
dial_service_->Deregister(handler);
handlers_.remove(handler);
}
}

void DialServiceProxy::OnReplaceDialService(
const base::WeakPtr<DialService>& service) {
dial_service_ = service;
if (dial_service_) {
for (auto handler : handlers_) {
dial_service_->Register(handler);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion cobalt/network/dial/dial_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef COBALT_NETWORK_DIAL_DIAL_SERVICE_H_
#define COBALT_NETWORK_DIAL_DIAL_SERVICE_H_

#include <list>
#include <map>
#include <memory>
#include <string>
Expand Down Expand Up @@ -63,7 +64,7 @@ class NET_EXPORT DialService : public base::SupportsWeakPtr<DialService> {

scoped_refptr<network::DialHttpServer> http_server_;
std::unique_ptr<network::DialUdpServer> udp_server_;
typedef std::map<std::string, scoped_refptr<DialServiceHandler> >
typedef std::map<std::string, scoped_refptr<DialServiceHandler>>
ServiceHandlerMap;
ServiceHandlerMap handlers_;
std::string http_host_address_;
Expand All @@ -82,17 +83,21 @@ class NET_EXPORT DialServiceProxy
explicit DialServiceProxy(const base::WeakPtr<DialService>& dial_service);
void Register(const scoped_refptr<DialServiceHandler>& handler);
void Deregister(const scoped_refptr<DialServiceHandler>& handler);
void ReplaceDialService(const base::WeakPtr<DialService>& service);
std::string host_address() const { return host_address_; }

private:
friend class base::RefCountedThreadSafe<DialServiceProxy>;
virtual ~DialServiceProxy();
void OnRegister(const scoped_refptr<DialServiceHandler>& handler);
void OnDeregister(const scoped_refptr<DialServiceHandler>& handler);
void OnReplaceDialService(const base::WeakPtr<DialService>& service);

base::WeakPtr<DialService> dial_service_;
std::string host_address_;

std::list<scoped_refptr<DialServiceHandler>> handlers_;

// Message loop to call DialService methods on.
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

Expand Down
23 changes: 23 additions & 0 deletions cobalt/network/network_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,29 @@ void NetworkModule::OnCreate(base::WaitableEvent* creation_event) {
creation_event->Signal();
}

#if defined(DIAL_SERVER)
void NetworkModule::RestartDialService() {
base::WaitableEvent creation_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
// Run Network module startup on IO thread,
// so the network delegate and URL request context are
// constructed on that thread.
task_runner()->PostTask(FROM_HERE,
base::Bind(&NetworkModule::OnRestartDialService,
base::Unretained(this), &creation_event));
// Wait for OnCreate() to run, so we can be sure our members
// have been constructed.
creation_event.Wait();
}

void NetworkModule::OnRestartDialService(base::WaitableEvent* creation_event) {
dial_service_.reset(new DialService());
dial_service_proxy_->ReplaceDialService(dial_service_->AsWeakPtr());
creation_event->Signal();
}
#endif

void NetworkModule::AddClientHintHeaders(
net::URLFetcher& url_fetcher, ClientHintHeadersCallType call_type) const {
if (kEnabledClientHintHeaders & call_type) {
Expand Down
4 changes: 4 additions & 0 deletions cobalt/network/network_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ class NetworkModule : public base::MessageLoop::DestructionObserver {
void StartNetLog();
base::FilePath StopNetLog();

#if defined(DIAL_SERVER)
void RestartDialService();
#endif

private:
void Initialize(const std::string& user_agent_string,
Expand All @@ -159,6 +162,7 @@ class NetworkModule : public base::MessageLoop::DestructionObserver {
std::unique_ptr<net::HttpUserAgentSettings> http_user_agent_settings_;
std::unique_ptr<network_bridge::CookieJar> cookie_jar_;
#if defined(DIAL_SERVER)
void OnRestartDialService(base::WaitableEvent* creation_event);
std::unique_ptr<network::DialService> dial_service_;
scoped_refptr<network::DialServiceProxy> dial_service_proxy_;
#endif
Expand Down
Loading