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/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 void Start(const std::string& address, uint16 port, int backlog); 26 27 // CefServer methods: 28 CefRefPtr<CefTaskRunner> GetTaskRunner() override; 29 void Shutdown() override; 30 bool IsRunning() override; 31 CefString GetAddress() override; 32 bool HasConnection() override; 33 bool IsValidConnection(int connection_id) override; 34 void SendHttp200Response(int connection_id, 35 const CefString& content_type, 36 const void* data, 37 size_t data_size) override; 38 void SendHttp404Response(int connection_id) override; 39 void SendHttp500Response(int connection_id, 40 const CefString& error_message) override; 41 void SendHttpResponse(int connection_id, 42 int response_code, 43 const CefString& content_type, 44 int64 content_length, 45 const HeaderMap& extra_headers) override; 46 void SendRawData(int connection_id, 47 const void* data, 48 size_t data_size) override; 49 void CloseConnection(int connection_id) override; 50 void SendWebSocketMessage(int connection_id, 51 const void* data, 52 size_t data_size) override; 53 54 void ContinueWebSocketRequest(int connection_id, 55 const net::HttpServerRequestInfo& request_info, 56 bool allow); 57 58 private: 59 void SendHttp200ResponseInternal(int connection_id, 60 const CefString& content_type, 61 std::unique_ptr<std::string> data); 62 void SendRawDataInternal(int connection_id, 63 std::unique_ptr<std::string> data); 64 void SendWebSocketMessageInternal(int connection_id, 65 std::unique_ptr<std::string> data); 66 67 // HttpServer::Delegate methods: 68 void OnConnect(int connection_id) override; 69 void OnHttpRequest(int connection_id, 70 const net::HttpServerRequestInfo& request_info) override; 71 void OnWebSocketRequest( 72 int connection_id, 73 const net::HttpServerRequestInfo& request_info) override; 74 void OnWebSocketMessage(int connection_id, std::string data) override; 75 void OnClose(int connection_id) override; 76 77 void StartOnUIThread(const std::string& address, uint16 port, int backlog); 78 void StartOnHandlerThread(const std::string& address, 79 uint16 port, 80 int backlog); 81 82 void ShutdownOnHandlerThread(); 83 void ShutdownOnUIThread(); 84 85 bool ValidateServer() const; 86 87 struct ConnectionInfo; 88 ConnectionInfo* CreateConnectionInfo(int connection_id); 89 ConnectionInfo* GetConnectionInfo(int connection_id) const; 90 void RemoveConnectionInfo(int connection_id); 91 92 bool CurrentlyOnHandlerThread() const; 93 94 // Safe to access from any thread. 95 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 96 std::string address_; 97 98 // Only accessed on the UI thread. 99 std::unique_ptr<base::Thread> thread_; 100 101 // Only accessed on the server thread. 102 CefRefPtr<CefServerHandler> handler_; 103 std::unique_ptr<net::HttpServer> server_; 104 105 // Map of connection_id to ConnectionInfo. 106 using ConnectionInfoMap = std::map<int, std::unique_ptr<ConnectionInfo>>; 107 ConnectionInfoMap connection_info_map_; 108 109 IMPLEMENT_REFCOUNTING(CefServerImpl); 110 DISALLOW_COPY_AND_ASSIGN(CefServerImpl); 111 }; 112 113 #endif // CEF_LIBCEF_BROWSER_SERVER_IMPL_H_ 114