blob: 8891b415fd1bb9bd649118e58b3a978767da03e9 [file] [log] [blame]
// Copyright 2023 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef COBALT_NETWORK_DIAL_DIAL_SERVICE_H_
#define COBALT_NETWORK_DIAL_DIAL_SERVICE_H_
#include <list>
#include <map>
#include <memory>
#include <string>
#include "base/gtest_prod_util.h"
#include "base/lazy_instance.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"
#include "cobalt/network/dial/dial_http_server.h"
#include "cobalt/network/dial/dial_service_handler.h"
#include "cobalt/network/dial/dial_udp_server.h"
namespace cobalt {
namespace network {
// DialService is part of an implementation of in-app DIAL.
// It starts up a UDP server to be used to respond to SSDP discovery requests,
// and an HTTP server to implement the DIAL commands.
// Clients register their DialServiceHandlers in order to get called back
// by the server when requests come in.
// Each registered DialServiceHandler should have a unique service path.
class NET_EXPORT DialService : public base::SupportsWeakPtr<DialService> {
public:
DialService();
~DialService();
void Register(const scoped_refptr<DialServiceHandler>& handler);
void Deregister(const scoped_refptr<DialServiceHandler>& handler);
scoped_refptr<DialServiceHandler> GetHandler(const std::string& service_name,
std::string* remaining_handler);
const std::string& http_host_address() const;
// Expose the DialHttpServer for unit tests.
network::DialHttpServer* http_server() const { return http_server_.get(); }
private:
FRIEND_TEST_ALL_PREFIXES(DialServiceTest, GetHandler);
FRIEND_TEST_ALL_PREFIXES(DialServiceTest, ReleasedHandler);
// Called in DialService destructor.
void Terminate();
scoped_refptr<network::DialHttpServer> http_server_;
std::unique_ptr<network::DialUdpServer> udp_server_;
typedef std::map<std::string, scoped_refptr<DialServiceHandler>>
ServiceHandlerMap;
ServiceHandlerMap handlers_;
std::string http_host_address_;
THREAD_CHECKER(thread_checker_);
DISALLOW_COPY_AND_ASSIGN(DialService);
};
// Thread-safe, refcounted interface to DialService.
// Each function call will dispatch to the DialService's thread.
// Keeps a WeakPtr to the DialService so if it has been destroyed nothing
// happens.
class NET_EXPORT DialServiceProxy
: public base::RefCountedThreadSafe<DialServiceProxy> {
public:
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_;
DISALLOW_COPY_AND_ASSIGN(DialServiceProxy);
};
} // namespace network
} // namespace cobalt
#endif // COBALT_NETWORK_DIAL_DIAL_SERVICE_H_