1 // Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_SERVER_IMPL_H_ 6 #define CEF_LIBCEF_BROWSER_SERVER_IMPL_H_ 7 #pragma once 8 9 #include <map> 10 #include <memory> 11 12 #include "include/cef_server.h" 13 14 #include "base/task/single_thread_task_runner.h" 15 #include "net/server/http_server.h" 16 17 namespace base { 18 class Thread; 19 } 20 21 class CefServerImpl : public CefServer, net::HttpServer::Delegate { 22 public: 23 explicit CefServerImpl(CefRefPtr<CefServerHandler> handler); 24 25 CefServerImpl(const CefServerImpl&) = delete; 26 CefServerImpl& operator=(const CefServerImpl&) = delete; 27 28 void Start(const std::string& address, uint16 port, int backlog); 29 30 // CefServer methods: 31 CefRefPtr<CefTaskRunner> GetTaskRunner() override; 32 void Shutdown() override; 33 bool IsRunning() override; 34 CefString GetAddress() override; 35 bool HasConnection() override; 36 bool IsValidConnection(int connection_id) override; 37 void SendHttp200Response(int connection_id, 38 const CefString& content_type, 39 const void* data, 40 size_t data_size) override; 41 void SendHttp404Response(int connection_id) override; 42 void SendHttp500Response(int connection_id, 43 const CefString& error_message) override; 44 void SendHttpResponse(int connection_id, 45 int response_code, 46 const CefString& content_type, 47 int64 content_length, 48 const HeaderMap& extra_headers) override; 49 void SendRawData(int connection_id, 50 const void* data, 51 size_t data_size) override; 52 void CloseConnection(int connection_id) override; 53 void SendWebSocketMessage(int connection_id, 54 const void* data, 55 size_t data_size) override; 56 57 void ContinueWebSocketRequest(int connection_id, 58 const net::HttpServerRequestInfo& request_info, 59 bool allow); 60 61 private: 62 void SendHttp200ResponseInternal(int connection_id, 63 const CefString& content_type, 64 std::unique_ptr<std::string> data); 65 void SendRawDataInternal(int connection_id, 66 std::unique_ptr<std::string> data); 67 void SendWebSocketMessageInternal(int connection_id, 68 std::unique_ptr<std::string> data); 69 70 // HttpServer::Delegate methods: 71 void OnConnect(int connection_id) override; 72 void OnHttpRequest(int connection_id, 73 const net::HttpServerRequestInfo& request_info) override; 74 void OnWebSocketRequest( 75 int connection_id, 76 const net::HttpServerRequestInfo& request_info) override; 77 void OnWebSocketMessage(int connection_id, std::string data) override; 78 void OnClose(int connection_id) override; 79 80 void StartOnUIThread(const std::string& address, uint16 port, int backlog); 81 void StartOnHandlerThread(const std::string& address, 82 uint16 port, 83 int backlog); 84 85 void ShutdownOnHandlerThread(); 86 void ShutdownOnUIThread(); 87 88 bool ValidateServer() const; 89 90 struct ConnectionInfo; 91 ConnectionInfo* CreateConnectionInfo(int connection_id); 92 ConnectionInfo* GetConnectionInfo(int connection_id) const; 93 void RemoveConnectionInfo(int connection_id); 94 95 bool CurrentlyOnHandlerThread() const; 96 97 // Safe to access from any thread. 98 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 99 std::string address_; 100 101 // Only accessed on the UI thread. 102 std::unique_ptr<base::Thread> thread_; 103 104 // Only accessed on the server thread. 105 CefRefPtr<CefServerHandler> handler_; 106 std::unique_ptr<net::HttpServer> server_; 107 108 // Map of connection_id to ConnectionInfo. 109 using ConnectionInfoMap = std::map<int, std::unique_ptr<ConnectionInfo>>; 110 ConnectionInfoMap connection_info_map_; 111 112 IMPLEMENT_REFCOUNTING(CefServerImpl); 113 }; 114 115 #endif // CEF_LIBCEF_BROWSER_SERVER_IMPL_H_ 116