2020-08-20 00:27:27 +02:00
|
|
|
// Copyright (c) 2020 The Chromium Embedded Framework 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 "tests/ceftests/test_server.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
|
|
|
#include "include/wrapper/cef_helpers.h"
|
|
|
|
|
|
|
|
namespace test_server {
|
|
|
|
|
|
|
|
// Must use a different port than server_unittest.cc.
|
|
|
|
const char kServerAddress[] = "127.0.0.1";
|
|
|
|
const uint16 kServerPort = 8098;
|
|
|
|
const char kServerScheme[] = "http";
|
|
|
|
const char kServerOrigin[] = "http://127.0.0.1:8098";
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ServerManager;
|
|
|
|
ServerManager* g_manager = nullptr;
|
|
|
|
|
|
|
|
// True if Stop() has been called.
|
|
|
|
bool g_stopping = false;
|
|
|
|
|
|
|
|
// Created on the UI thread and called on the dedicated server thread.
|
|
|
|
class ServerHandler : public CefServerHandler {
|
|
|
|
public:
|
|
|
|
ServerHandler() {
|
|
|
|
CefServer::CreateServer(kServerAddress, kServerPort, 10, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ServerHandler() override {
|
|
|
|
DCHECK(!server_);
|
|
|
|
NotifyServerHandlerDeleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() { server_->Shutdown(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// CefServerHandler methods:
|
|
|
|
void OnServerCreated(CefRefPtr<CefServer> server) override {
|
|
|
|
server_ = server;
|
|
|
|
NotifyServerCreated(kServerOrigin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnServerDestroyed(CefRefPtr<CefServer> server) override {
|
|
|
|
server_ = nullptr;
|
|
|
|
NotifyServerDestroyed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnClientConnected(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id) override {
|
|
|
|
DCHECK(server->HasConnection());
|
|
|
|
DCHECK(server->IsValidConnection(connection_id));
|
|
|
|
NotifyClientConnected(server, connection_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnClientDisconnected(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id) override {
|
|
|
|
DCHECK(!server->IsValidConnection(connection_id));
|
|
|
|
NotifyClientDisconnected(server, connection_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnHttpRequest(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id,
|
|
|
|
const CefString& client_address,
|
|
|
|
CefRefPtr<CefRequest> request) override {
|
|
|
|
NotifyHttpRequest(server, connection_id, client_address, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnWebSocketRequest(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id,
|
|
|
|
const CefString& client_address,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {}
|
|
|
|
void OnWebSocketConnected(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id) override {}
|
|
|
|
void OnWebSocketMessage(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id,
|
|
|
|
const void* data,
|
|
|
|
size_t data_size) override {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void NotifyServerCreated(const std::string& server_origin);
|
|
|
|
static void NotifyServerDestroyed();
|
|
|
|
static void NotifyServerHandlerDeleted();
|
|
|
|
static void NotifyClientConnected(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id);
|
|
|
|
static void NotifyClientDisconnected(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id);
|
|
|
|
static void NotifyHttpRequest(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id,
|
|
|
|
const CefString& client_address,
|
|
|
|
CefRefPtr<CefRequest> request);
|
|
|
|
|
|
|
|
CefRefPtr<CefServer> server_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(ServerHandler);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ServerHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only accessed on the UI thread. Deletes itself after the server is stopped.
|
|
|
|
class ServerManager {
|
|
|
|
public:
|
|
|
|
ServerManager() {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
DCHECK(!g_manager);
|
|
|
|
g_manager = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ServerManager() {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
DCHECK(observer_list_.empty());
|
|
|
|
DCHECK(start_callback_list_.empty());
|
|
|
|
DCHECK(stop_callback_.is_null());
|
|
|
|
|
|
|
|
g_manager = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void Start(StartDoneCallback callback) {
|
2020-08-20 00:27:27 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
if (!origin_.empty()) {
|
|
|
|
// The server is already running.
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(callback).Run(origin_);
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If tests run in parallel, and the server is starting, then there may be
|
|
|
|
// multiple pending callbacks.
|
2021-06-19 21:54:45 +02:00
|
|
|
start_callback_list_.push_back(std::move(callback));
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
// Only create the handler a single time.
|
|
|
|
if (!handler_) {
|
|
|
|
handler_ = new ServerHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void Stop(DoneCallback callback) {
|
2020-08-20 00:27:27 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
if (!handler_) {
|
|
|
|
// The server is not currently running.
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(callback).Run();
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only 1 stop callback supported.
|
|
|
|
DCHECK(stop_callback_.is_null());
|
2021-06-19 21:54:45 +02:00
|
|
|
stop_callback_ = std::move(callback);
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
handler_->Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddObserver(Observer* observer) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
observer_list_.push_back(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveObserver(Observer* observer) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
bool found = false;
|
|
|
|
ObserverList::iterator it = observer_list_.begin();
|
|
|
|
for (; it != observer_list_.end(); ++it) {
|
|
|
|
if (*it == observer) {
|
|
|
|
observer_list_.erase(it);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DCHECK(found);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyServerCreated(const std::string& server_origin) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
DCHECK(origin_.empty());
|
|
|
|
origin_ = server_origin;
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
for (auto& callback : start_callback_list_) {
|
|
|
|
std::move(callback).Run(origin_);
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
start_callback_list_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyServerDestroyed() {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
origin_.clear();
|
|
|
|
handler_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All server-related objects have been torn down.
|
|
|
|
void NotifyServerHandlerDeleted() {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
DCHECK(!stop_callback_.is_null());
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(stop_callback_).Run();
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyClientConnected(CefRefPtr<CefServer> server, int connection_id) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
if (observer_list_.empty())
|
|
|
|
return;
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
// Use a copy in case |observer_list_| is modified during iteration.
|
|
|
|
ObserverList list = observer_list_;
|
|
|
|
|
|
|
|
ObserverList::const_iterator it = list.begin();
|
|
|
|
for (; it != list.end(); ++it) {
|
|
|
|
if ((*it)->OnClientConnected(server, connection_id)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyClientDisconnected(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
if (observer_list_.empty())
|
|
|
|
return;
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
// Use a copy in case |observer_list_| is modified during iteration.
|
|
|
|
ObserverList list = observer_list_;
|
|
|
|
|
|
|
|
ObserverList::const_iterator it = list.begin();
|
|
|
|
for (; it != list.end(); ++it) {
|
|
|
|
if ((*it)->OnClientDisconnected(server, connection_id)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyHttpRequest(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id,
|
|
|
|
const CefString& client_address,
|
|
|
|
CefRefPtr<CefRequest> request) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// TODO(chrome-runtime): Debug why favicon requests don't always have the
|
|
|
|
// correct resource type.
|
|
|
|
const std::string& url = request->GetURL();
|
|
|
|
if (request->GetResourceType() == RT_FAVICON ||
|
|
|
|
url.find("/favicon.ico") != std::string::npos) {
|
|
|
|
// We don't currently handle favicon requests.
|
|
|
|
server->SendHttp404Response(connection_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(!observer_list_.empty()) << url;
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
// Use a copy in case |observer_list_| is modified during iteration.
|
|
|
|
ObserverList list = observer_list_;
|
|
|
|
|
2020-09-11 18:52:35 +02:00
|
|
|
bool handled = false;
|
|
|
|
|
2020-08-20 00:27:27 +02:00
|
|
|
ObserverList::const_iterator it = list.begin();
|
|
|
|
for (; it != list.end(); ++it) {
|
|
|
|
if ((*it)->OnHttpRequest(server, connection_id, client_address,
|
|
|
|
request)) {
|
2020-09-11 18:52:35 +02:00
|
|
|
handled = true;
|
2020-08-20 00:27:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-11 18:52:35 +02:00
|
|
|
|
|
|
|
if (!handled) {
|
|
|
|
server->SendHttp500Response(connection_id, "Unhandled request.");
|
|
|
|
}
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefRefPtr<ServerHandler> handler_;
|
|
|
|
std::string origin_;
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
using StartDoneCallbackList = std::vector<StartDoneCallback>;
|
2020-08-20 00:27:27 +02:00
|
|
|
StartDoneCallbackList start_callback_list_;
|
|
|
|
|
|
|
|
DoneCallback stop_callback_;
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
using ObserverList = std::vector<Observer*>;
|
2020-08-20 00:27:27 +02:00
|
|
|
ObserverList observer_list_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ServerManager);
|
|
|
|
};
|
|
|
|
|
|
|
|
ServerManager* GetServerManager() {
|
|
|
|
return g_manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerManager* GetOrCreateServerManager() {
|
|
|
|
if (!g_manager) {
|
|
|
|
new ServerManager();
|
|
|
|
DCHECK(g_manager);
|
|
|
|
}
|
|
|
|
return g_manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ServerHandler::NotifyServerCreated(const std::string& server_origin) {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(ServerHandler::NotifyServerCreated,
|
|
|
|
server_origin));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetServerManager()->NotifyServerCreated(server_origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ServerHandler::NotifyServerDestroyed() {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(ServerHandler::NotifyServerDestroyed));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetServerManager()->NotifyServerDestroyed();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ServerHandler::NotifyServerHandlerDeleted() {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::BindOnce(ServerHandler::NotifyServerHandlerDeleted));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetServerManager()->NotifyServerHandlerDeleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ServerHandler::NotifyClientConnected(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id) {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(ServerHandler::NotifyClientConnected,
|
|
|
|
server, connection_id));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetServerManager()->NotifyClientConnected(server, connection_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ServerHandler::NotifyClientDisconnected(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id) {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(ServerHandler::NotifyClientDisconnected,
|
|
|
|
server, connection_id));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetServerManager()->NotifyClientDisconnected(server, connection_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ServerHandler::NotifyHttpRequest(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id,
|
|
|
|
const CefString& client_address,
|
|
|
|
CefRefPtr<CefRequest> request) {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(ServerHandler::NotifyHttpRequest, server,
|
|
|
|
connection_id, client_address, request));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetServerManager()->NotifyHttpRequest(server, connection_id, client_address,
|
|
|
|
request);
|
|
|
|
}
|
|
|
|
|
|
|
|
// May be created on any thread but will be destroyed on the UI thread.
|
|
|
|
class ObserverRegistration : public CefRegistration {
|
|
|
|
public:
|
|
|
|
explicit ObserverRegistration(Observer* const observer)
|
|
|
|
: observer_(observer) {
|
|
|
|
DCHECK(observer_);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ObserverRegistration() override {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
ServerManager* manager = GetServerManager();
|
|
|
|
if (manager) {
|
|
|
|
manager->RemoveObserver(observer_);
|
|
|
|
observer_->OnUnregistered();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initialize() {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
GetOrCreateServerManager()->AddObserver(observer_);
|
|
|
|
observer_->OnRegistered();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Observer* const observer_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING_DELETE_ON_UIT(ObserverRegistration);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ObserverRegistration);
|
|
|
|
};
|
|
|
|
|
|
|
|
void InitializeRegistration(CefRefPtr<ObserverRegistration> registration,
|
2021-06-19 21:54:45 +02:00
|
|
|
DoneCallback callback) {
|
2020-08-20 00:27:27 +02:00
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(InitializeRegistration, registration,
|
|
|
|
std::move(callback)));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(!g_stopping);
|
|
|
|
|
|
|
|
registration->Initialize();
|
|
|
|
if (!callback.is_null())
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(callback).Run();
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void Start(StartDoneCallback callback) {
|
2020-08-20 00:27:27 +02:00
|
|
|
DCHECK(!callback.is_null());
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(Start, std::move(callback)));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(!g_stopping);
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
GetOrCreateServerManager()->Start(std::move(callback));
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void Stop(DoneCallback callback) {
|
2020-08-20 00:27:27 +02:00
|
|
|
DCHECK(!callback.is_null());
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(Stop, std::move(callback)));
|
2020-08-20 00:27:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop will be called one time on test framework shutdown.
|
|
|
|
DCHECK(!g_stopping);
|
|
|
|
g_stopping = true;
|
|
|
|
|
|
|
|
ServerManager* manager = GetServerManager();
|
|
|
|
if (manager) {
|
2021-06-19 21:54:45 +02:00
|
|
|
manager->Stop(std::move(callback));
|
2020-08-20 00:27:27 +02:00
|
|
|
} else {
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(callback).Run();
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefRegistration> AddObserver(Observer* observer,
|
2021-06-19 21:54:45 +02:00
|
|
|
DoneCallback callback) {
|
2020-08-20 00:27:27 +02:00
|
|
|
DCHECK(observer);
|
|
|
|
CefRefPtr<ObserverRegistration> registration =
|
|
|
|
new ObserverRegistration(observer);
|
2021-06-19 21:54:45 +02:00
|
|
|
InitializeRegistration(registration, std::move(callback));
|
2020-08-20 00:27:27 +02:00
|
|
|
return registration.get();
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
CefRefPtr<CefRegistration> AddObserverAndStart(Observer* observer,
|
|
|
|
StartDoneCallback callback) {
|
|
|
|
return AddObserver(observer, base::BindOnce(Start, std::move(callback)));
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 21:28:23 +02:00
|
|
|
void SendResponse(CefRefPtr<CefServer> server,
|
|
|
|
int connection_id,
|
|
|
|
CefRefPtr<CefResponse> response,
|
|
|
|
const std::string& response_data) {
|
|
|
|
const int response_code = response->GetStatus();
|
|
|
|
const CefString& content_type = response->GetMimeType();
|
|
|
|
int64 content_length = static_cast<int64>(response_data.size());
|
|
|
|
|
|
|
|
CefResponse::HeaderMap extra_headers;
|
|
|
|
response->GetHeaderMap(extra_headers);
|
|
|
|
|
|
|
|
server->SendHttpResponse(connection_id, response_code, content_type,
|
|
|
|
content_length, extra_headers);
|
|
|
|
|
|
|
|
if (content_length != 0) {
|
|
|
|
server->SendRawData(connection_id, response_data.data(),
|
|
|
|
response_data.size());
|
|
|
|
server->CloseConnection(connection_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 00:27:27 +02:00
|
|
|
// ObserverHelper
|
|
|
|
|
|
|
|
ObserverHelper::ObserverHelper() : weak_ptr_factory_(this) {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
}
|
|
|
|
|
|
|
|
ObserverHelper::~ObserverHelper() {
|
|
|
|
DCHECK(state_ == State::NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObserverHelper::Initialize() {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
DCHECK(state_ == State::NONE);
|
|
|
|
state_ = State::INITIALIZING;
|
2021-06-19 21:54:45 +02:00
|
|
|
registration_ =
|
|
|
|
AddObserverAndStart(this, base::BindOnce(&ObserverHelper::OnStartDone,
|
|
|
|
weak_ptr_factory_.GetWeakPtr()));
|
2020-08-20 00:27:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObserverHelper::Shutdown() {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
DCHECK(state_ == State::INITIALIZED);
|
|
|
|
state_ = State::SHUTTINGDOWN;
|
|
|
|
registration_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObserverHelper::OnStartDone(const std::string& server_origin) {
|
|
|
|
DCHECK(state_ == State::INITIALIZING);
|
|
|
|
state_ = State::INITIALIZED;
|
|
|
|
OnInitialized(server_origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObserverHelper::OnRegistered() {
|
|
|
|
DCHECK(state_ == State::INITIALIZING);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObserverHelper::OnUnregistered() {
|
|
|
|
DCHECK(state_ == State::SHUTTINGDOWN);
|
|
|
|
state_ = State::NONE;
|
|
|
|
OnShutdown();
|
|
|
|
}
|
|
|
|
|
2020-08-29 00:39:23 +02:00
|
|
|
} // namespace test_server
|