ProtocolHandlerInfo.cpp (2924B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "ProtocolHandlerInfo.h" 8 #include "StaticComponents.h" 9 #include "nsIProtocolHandler.h" 10 11 namespace mozilla::net { 12 13 uint32_t ProtocolHandlerInfo::StaticProtocolFlags() const { 14 uint32_t flags = mInner.match( 15 [&](const xpcom::StaticProtocolHandler* handler) { 16 return handler->mProtocolFlags; 17 }, 18 [&](const RuntimeProtocolHandler& handler) { 19 return handler.mProtocolFlags; 20 }); 21 #if !IS_ORIGIN_IS_FULL_SPEC_DEFINED 22 MOZ_RELEASE_ASSERT(!(flags & nsIProtocolHandler::ORIGIN_IS_FULL_SPEC), 23 "ORIGIN_IS_FULL_SPEC is unsupported but used"); 24 #endif 25 return flags; 26 } 27 28 int32_t ProtocolHandlerInfo::DefaultPort() const { 29 return mInner.match( 30 [&](const xpcom::StaticProtocolHandler* handler) { 31 return handler->mDefaultPort; 32 }, 33 [&](const RuntimeProtocolHandler& handler) { 34 return handler.mDefaultPort; 35 }); 36 } 37 38 nsresult ProtocolHandlerInfo::DynamicProtocolFlags(nsIURI* aURI, 39 uint32_t* aFlags) const { 40 MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread()); 41 42 // If we're querying dynamic flags, we'll need to fetch the actual xpcom 43 // component in order to check them. 44 if (HasDynamicFlags()) { 45 nsCOMPtr<nsIProtocolHandler> handler = Handler(); 46 if (nsCOMPtr<nsIProtocolHandlerWithDynamicFlags> dynamic = 47 do_QueryInterface(handler)) { 48 nsresult rv = dynamic->GetFlagsForURI(aURI, aFlags); 49 NS_ENSURE_SUCCESS(rv, rv); 50 MOZ_DIAGNOSTIC_ASSERT( 51 (StaticProtocolFlags() & ~nsIProtocolHandler::DYNAMIC_URI_FLAGS) == 52 (*aFlags & ~nsIProtocolHandler::DYNAMIC_URI_FLAGS), 53 "only DYNAMIC_URI_FLAGS may be changed by a " 54 "nsIProtocolHandlerWithDynamicFlags implementation"); 55 return NS_OK; 56 } 57 } 58 59 // Otherwise, just check against static flags. 60 *aFlags = StaticProtocolFlags(); 61 return NS_OK; 62 } 63 64 bool ProtocolHandlerInfo::HasDynamicFlags() const { 65 return mInner.match( 66 [&](const xpcom::StaticProtocolHandler* handler) { 67 return handler->mHasDynamicFlags; 68 }, 69 [&](const RuntimeProtocolHandler&) { return false; }); 70 } 71 72 already_AddRefed<nsIProtocolHandler> ProtocolHandlerInfo::Handler() const { 73 MOZ_ASSERT(NS_IsMainThread()); 74 75 nsCOMPtr<nsIProtocolHandler> retval; 76 mInner.match( 77 [&](const xpcom::StaticProtocolHandler* handler) { 78 retval = handler->Module().GetService(); 79 }, 80 [&](const RuntimeProtocolHandler& handler) { 81 retval = handler.mHandler.get(); 82 }); 83 return retval.forget(); 84 } 85 86 } // namespace mozilla::net