dbus_server.cc revision 3320726e9ccf678a9accdd6994e28f0cb8454eab
1// Copyright 2015 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#include <libwebserv/dbus_server.h> 16 17#include <tuple> 18#include <vector> 19 20#include <libwebserv/dbus_protocol_handler.h> 21#include <libwebserv/request_impl.h> 22 23#include "dbus_bindings/org.chromium.WebServer.RequestHandler.h" 24#include "webservd/dbus-proxies.h" 25 26namespace libwebserv { 27 28class DBusServer::RequestHandler final 29 : public org::chromium::WebServer::RequestHandlerInterface { 30 public: 31 explicit RequestHandler(DBusServer* server) : server_{server} {} 32 bool ProcessRequest( 33 brillo::ErrorPtr* error, 34 const std::tuple<std::string, std::string, std::string, std::string, 35 std::string>& in_request_info, 36 const std::vector<std::tuple<std::string, std::string>>& in_headers, 37 const std::vector<std::tuple<bool, std::string, std::string>>& in_params, 38 const std::vector<std::tuple<int32_t, std::string, std::string, 39 std::string, std::string>>& in_files, 40 const dbus::FileDescriptor& in_body) override; 41 42 private: 43 DBusServer* server_{nullptr}; 44 DISALLOW_COPY_AND_ASSIGN(RequestHandler); 45}; 46 47bool DBusServer::RequestHandler::ProcessRequest( 48 brillo::ErrorPtr* error, 49 const std::tuple<std::string, std::string, std::string, std::string, 50 std::string>& in_request_info, 51 const std::vector<std::tuple<std::string, std::string>>& in_headers, 52 const std::vector<std::tuple<bool, std::string, std::string>>& in_params, 53 const std::vector<std::tuple<int32_t, std::string, std::string, std::string, 54 std::string>>& in_files, 55 const dbus::FileDescriptor& in_body) { 56 std::string protocol_handler_id = std::get<0>(in_request_info); 57 std::string request_handler_id = std::get<1>(in_request_info); 58 std::string request_id = std::get<2>(in_request_info); 59 std::string url = std::get<3>(in_request_info); 60 std::string method = std::get<4>(in_request_info); 61 DBusProtocolHandler* protocol_handler = 62 server_->GetProtocolHandlerByID(protocol_handler_id); 63 if (!protocol_handler) { 64 brillo::Error::AddToPrintf(error, FROM_HERE, 65 brillo::errors::dbus::kDomain, 66 DBUS_ERROR_FAILED, 67 "Unknown protocol handler '%s'", 68 protocol_handler_id.c_str()); 69 return false; 70 } 71 std::unique_ptr<RequestImpl> request{ 72 new RequestImpl{protocol_handler, url, method}}; 73 // Convert request data into format required by the Request object. 74 for (const auto& tuple : in_params) { 75 if (std::get<0>(tuple)) 76 request->post_data_.emplace(std::get<1>(tuple), std::get<2>(tuple)); 77 else 78 request->get_data_.emplace(std::get<1>(tuple), std::get<2>(tuple)); 79 } 80 81 for (const auto& tuple : in_headers) 82 request->headers_.emplace(std::get<0>(tuple), std::get<1>(tuple)); 83 84 for (const auto& tuple : in_files) { 85 request->file_info_.emplace( 86 std::get<1>(tuple), // field_name 87 std::unique_ptr<FileInfo>{new FileInfo{ 88 protocol_handler, 89 std::get<0>(tuple), // file_id 90 request_id, 91 std::get<2>(tuple), // file_name 92 std::get<3>(tuple), // content_type 93 std::get<4>(tuple)}}); // transfer_encoding 94 } 95 96 request->raw_data_fd_ = base::File(dup(in_body.value())); 97 CHECK(request->raw_data_fd_.IsValid()); 98 99 return protocol_handler->ProcessRequest(protocol_handler_id, 100 request_handler_id, 101 request_id, 102 std::move(request), 103 error); 104} 105 106DBusServer::DBusServer() 107 : request_handler_{new RequestHandler{this}}, 108 dbus_adaptor_{new org::chromium::WebServer::RequestHandlerAdaptor{ 109 request_handler_.get()}} {} 110 111void DBusServer::Connect( 112 const scoped_refptr<dbus::Bus>& bus, 113 const std::string& service_name, 114 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb, 115 const base::Closure& on_server_online, 116 const base::Closure& on_server_offline) { 117 service_name_ = service_name; 118 dbus_object_.reset(new brillo::dbus_utils::DBusObject{ 119 nullptr, bus, dbus_adaptor_->GetObjectPath()}); 120 dbus_adaptor_->RegisterWithDBusObject(dbus_object_.get()); 121 dbus_object_->RegisterAsync(cb); 122 on_server_online_ = on_server_online; 123 on_server_offline_ = on_server_offline; 124 object_manager_.reset(new org::chromium::WebServer::ObjectManagerProxy{bus}); 125 object_manager_->SetServerAddedCallback( 126 base::Bind(&DBusServer::Online, base::Unretained(this))); 127 object_manager_->SetServerRemovedCallback( 128 base::Bind(&DBusServer::Offline, base::Unretained(this))); 129 object_manager_->SetProtocolHandlerAddedCallback( 130 base::Bind(&DBusServer::ProtocolHandlerAdded, base::Unretained(this))); 131 object_manager_->SetProtocolHandlerRemovedCallback( 132 base::Bind(&DBusServer::ProtocolHandlerRemoved, base::Unretained(this))); 133} 134 135void DBusServer::Disconnect() { 136 on_server_offline_.Reset(); 137 on_server_online_.Reset(); 138 protocol_handlers_ids_.clear(); 139 protocol_handlers_names_.clear(); 140 // Release D-Bus object manager proxy after all the dependent maps are freed 141 // (e.g. |protocol_handlers_names_| contains pointers to ProtocolHandlerProxy, 142 // instances of which are owned by the D-Bus object manager). 143 object_manager_.reset(); 144 dbus_object_.reset(); 145} 146 147void DBusServer::Online( 148 org::chromium::WebServer::ServerProxyInterface* server) { 149 VLOG(1) << "Web server is on-line."; 150 proxy_ = server; 151 if (!on_server_online_.is_null()) 152 on_server_online_.Run(); 153} 154 155bool DBusServer::IsConnected() const { 156 return proxy_ != nullptr; 157} 158 159void DBusServer::Offline(const dbus::ObjectPath& object_path) { 160 if (!on_server_offline_.is_null()) 161 on_server_offline_.Run(); 162 proxy_ = nullptr; 163 VLOG(1) << "Web server is off-line."; 164} 165 166void DBusServer::ProtocolHandlerAdded( 167 org::chromium::WebServer::ProtocolHandlerProxyInterface* handler) { 168 VLOG(1) << "Server-side protocol handler with ID '" << handler->id() 169 << "' is on-line (" << handler->name() << ")"; 170 171 protocol_handler_id_map_.emplace(handler->GetObjectPath(), handler->id()); 172 DBusProtocolHandler* registered_handler = 173 GetProtocolHandlerImpl(handler->name()); 174 if (registered_handler) { 175 protocol_handlers_ids_.emplace(handler->id(), registered_handler); 176 registered_handler->Connect(handler); 177 if (!on_protocol_handler_connected_.is_null()) 178 on_protocol_handler_connected_.Run(registered_handler); 179 } 180} 181 182void DBusServer::ProtocolHandlerRemoved(const dbus::ObjectPath& object_path) { 183 auto p = protocol_handler_id_map_.find(object_path); 184 if (p == protocol_handler_id_map_.end()) 185 return; 186 187 VLOG(1) << "Server-side protocol handler with ID '" << p->second 188 << "' is off-line."; 189 190 DBusProtocolHandler* registered_handler = GetProtocolHandlerByID(p->second); 191 if (registered_handler) { 192 if (!on_protocol_handler_disconnected_.is_null()) 193 on_protocol_handler_disconnected_.Run(registered_handler); 194 registered_handler->Disconnect(object_path); 195 protocol_handlers_ids_.erase(p->second); 196 } 197 198 protocol_handler_id_map_.erase(p); 199} 200 201ProtocolHandler* DBusServer::GetProtocolHandler(const std::string& name) { 202 return GetProtocolHandlerImpl(name); 203} 204 205DBusProtocolHandler* DBusServer::GetProtocolHandlerImpl( 206 const std::string& name) { 207 auto p = protocol_handlers_names_.find(name); 208 if (p == protocol_handlers_names_.end()) { 209 VLOG(1) << "Creating a client-side instance of web server's protocol " 210 << "handler with name '" << name << "'"; 211 p = protocol_handlers_names_.emplace( 212 name, 213 std::unique_ptr<DBusProtocolHandler>{ 214 new DBusProtocolHandler{name, this}}).first; 215 } 216 return p->second.get(); 217} 218 219ProtocolHandler* DBusServer::GetDefaultHttpHandler() { 220 return GetProtocolHandler(ProtocolHandler::kHttp); 221} 222 223ProtocolHandler* DBusServer::GetDefaultHttpsHandler() { 224 return GetProtocolHandler(ProtocolHandler::kHttps); 225} 226 227DBusProtocolHandler* DBusServer::GetProtocolHandlerByID( 228 const std::string& id) const { 229 auto p = protocol_handlers_ids_.find(id); 230 if (p == protocol_handlers_ids_.end()) { 231 LOG(ERROR) << "Unable to locate protocol handler with ID '" << id << "'"; 232 return nullptr; 233 } 234 return p->second; 235} 236 237void DBusServer::OnProtocolHandlerConnected( 238 const base::Callback<void(ProtocolHandler*)>& callback) { 239 on_protocol_handler_connected_ = callback; 240} 241 242void DBusServer::OnProtocolHandlerDisconnected( 243 const base::Callback<void(ProtocolHandler*)>& callback) { 244 on_protocol_handler_disconnected_ = callback; 245} 246 247base::TimeDelta DBusServer::GetDefaultRequestTimeout() const { 248 int timeout_seconds = proxy_ ? proxy_->default_request_timeout() : -1; 249 if (timeout_seconds <= 0) 250 return base::TimeDelta::Max(); 251 return base::TimeDelta::FromSeconds(timeout_seconds); 252} 253 254} // namespace libwebserv 255