PUDPSocketParent* NeckoParent::AllocPUDPSocketParent(const nsCString& aHost, const uint16_t& aPort, const nsCString& aFilter) { UDPSocketParent* p = nullptr; // Only allow socket if it specifies a valid packet filter. nsAutoCString contractId(NS_NETWORK_UDP_SOCKET_FILTER_HANDLER_PREFIX); contractId.Append(aFilter); if (!aFilter.IsEmpty()) { nsCOMPtr<nsIUDPSocketFilterHandler> filterHandler = do_GetService(contractId.get()); if (filterHandler) { nsCOMPtr<nsIUDPSocketFilter> filter; nsresult rv = filterHandler->NewFilter(getter_AddRefs(filter)); if (NS_SUCCEEDED(rv)) { p = new UDPSocketParent(filter); } else { printf_stderr("Cannot create filter that content specified. " "filter name: %s, error code: %d.", aFilter.get(), rv); } } else { printf_stderr("Content doesn't have a valid filter. " "filter name: %s.", aFilter.get()); } } NS_IF_ADDREF(p); return p; }
already_AddRefed<nsIUnicodeEncoder> EncodingUtils::EncoderForEncoding(const nsACString& aEncoding) { nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE); contractId.Append(aEncoding); nsCOMPtr<nsIUnicodeEncoder> encoder = do_CreateInstance(contractId.get()); MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding."); return encoder.forget(); }
bool TCPSocketParent::RecvOpenBind(const nsCString& aRemoteHost, const uint16_t& aRemotePort, const nsCString& aLocalAddr, const uint16_t& aLocalPort, const bool& aUseSSL, const bool& aUseArrayBuffers, const nsCString& aFilter) { if (net::UsingNeckoIPCSecurity() && !AssertAppProcessPermission(Manager()->Manager(), "tcp-socket")) { FireInteralError(this, __LINE__); return true; } nsresult rv; nsCOMPtr<nsISocketTransportService> sts = do_GetService("@mozilla.org/network/socket-transport-service;1", &rv); if (NS_FAILED(rv)) { FireInteralError(this, __LINE__); return true; } nsCOMPtr<nsISocketTransport> socketTransport; rv = sts->CreateTransport(nullptr, 0, aRemoteHost, aRemotePort, nullptr, getter_AddRefs(socketTransport)); if (NS_FAILED(rv)) { FireInteralError(this, __LINE__); return true; } PRNetAddr prAddr; if (PR_SUCCESS != PR_InitializeNetAddr(PR_IpAddrAny, aLocalPort, &prAddr)) { FireInteralError(this, __LINE__); return true; } if (PR_SUCCESS != PR_StringToNetAddr(aLocalAddr.BeginReading(), &prAddr)) { FireInteralError(this, __LINE__); return true; } mozilla::net::NetAddr addr; PRNetAddrToNetAddr(&prAddr, &addr); rv = socketTransport->Bind(&addr); if (NS_FAILED(rv)) { FireInteralError(this, __LINE__); return true; } if (!aFilter.IsEmpty()) { nsAutoCString contractId(NS_NETWORK_TCP_SOCKET_FILTER_HANDLER_PREFIX); contractId.Append(aFilter); nsCOMPtr<nsISocketFilterHandler> filterHandler = do_GetService(contractId.get()); if (!filterHandler) { NS_ERROR("Content doesn't have a valid filter"); FireInteralError(this, __LINE__); return true; } rv = filterHandler->NewFilter(getter_AddRefs(mFilter)); if (NS_FAILED(rv)) { NS_ERROR("Cannot create filter that content specified"); FireInteralError(this, __LINE__); return true; } } // Obtain App ID uint32_t appId = nsIScriptSecurityManager::NO_APP_ID; bool inIsolatedMozBrowser = false; const PContentParent *content = Manager()->Manager(); if (PBrowserParent* browser = SingleManagedOrNull(content->ManagedPBrowserParent())) { // appId's are for B2G only currently, where managees.Count() == 1 // This is not guaranteed currently in Desktop, so skip this there. TabParent *tab = TabParent::GetFrom(browser); appId = tab->OwnAppId(); inIsolatedMozBrowser = tab->IsIsolatedMozBrowserElement(); } mSocket = new TCPSocket(nullptr, NS_ConvertUTF8toUTF16(aRemoteHost), aRemotePort, aUseSSL, aUseArrayBuffers); mSocket->SetAppIdAndBrowser(appId, inIsolatedMozBrowser); mSocket->SetSocketBridgeParent(this); rv = mSocket->InitWithUnconnectedTransport(socketTransport); NS_ENSURE_SUCCESS(rv, true); return true; }