SpeculativeTransaction.cpp (3672B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 // HttpLog.h should generally be included first 7 #include "HttpLog.h" 8 9 #include "SpeculativeTransaction.h" 10 #include "HTTPSRecordResolver.h" 11 #include "nsICachingChannel.h" 12 #include "nsHttpHandler.h" 13 14 namespace mozilla { 15 namespace net { 16 17 SpeculativeTransaction::SpeculativeTransaction( 18 nsHttpConnectionInfo* aConnInfo, nsIInterfaceRequestor* aCallbacks, 19 uint32_t aCaps, std::function<void(bool)>&& aCallback) 20 : NullHttpTransaction(aConnInfo, aCallbacks, aCaps), 21 mCloseCallback(std::move(aCallback)) {} 22 23 SpeculativeTransaction::~SpeculativeTransaction() = default; 24 25 already_AddRefed<SpeculativeTransaction> 26 SpeculativeTransaction::CreateWithNewConnInfo(nsHttpConnectionInfo* aConnInfo) { 27 RefPtr<SpeculativeTransaction> trans = 28 new SpeculativeTransaction(aConnInfo, mCallbacks, mCaps); 29 trans->mParallelSpeculativeConnectLimit = mParallelSpeculativeConnectLimit; 30 trans->mIgnoreIdle = mIgnoreIdle; 31 trans->mAllow1918 = mAllow1918; 32 return trans.forget(); 33 } 34 35 nsresult SpeculativeTransaction::FetchHTTPSRR() { 36 LOG(("SpeculativeTransaction::FetchHTTPSRR [this=%p]", this)); 37 MOZ_ASSERT(OnSocketThread(), "not on socket thread"); 38 39 mResolver = new HTTPSRecordResolver(this); 40 nsCOMPtr<nsICancelable> dnsRequest; 41 nsresult rv = mResolver->FetchHTTPSRRInternal(GetCurrentSerialEventTarget(), 42 getter_AddRefs(dnsRequest)); 43 if (NS_FAILED(rv)) { 44 mResolver->Close(); 45 mResolver = nullptr; 46 } 47 48 return rv; 49 } 50 51 nsresult SpeculativeTransaction::OnHTTPSRRAvailable( 52 nsIDNSHTTPSSVCRecord* aHTTPSSVCRecord, 53 nsISVCBRecord* aHighestPriorityRecord, const nsACString& aCname) { 54 MOZ_ASSERT(OnSocketThread(), "not on socket thread"); 55 LOG(("SpeculativeTransaction::OnHTTPSRRAvailable [this=%p]", this)); 56 57 RefPtr<HTTPSRecordResolver> resolver = std::move(mResolver); 58 59 if (!aHTTPSSVCRecord || !aHighestPriorityRecord) { 60 gHttpHandler->ConnMgr()->DoSpeculativeConnection(this, false); 61 return NS_OK; 62 } 63 64 RefPtr<nsHttpConnectionInfo> connInfo = ConnectionInfo(); 65 RefPtr<nsHttpConnectionInfo> newInfo = 66 connInfo->CloneAndAdoptHTTPSSVCRecord(aHighestPriorityRecord); 67 RefPtr<SpeculativeTransaction> newTrans = CreateWithNewConnInfo(newInfo); 68 gHttpHandler->ConnMgr()->DoSpeculativeConnection(newTrans, false); 69 return NS_OK; 70 } 71 72 nsresult SpeculativeTransaction::ReadSegments(nsAHttpSegmentReader* aReader, 73 uint32_t aCount, 74 uint32_t* aCountRead) { 75 MOZ_ASSERT(OnSocketThread(), "not on socket thread"); 76 mTriedToWrite = true; 77 return NullHttpTransaction::ReadSegments(aReader, aCount, aCountRead); 78 } 79 80 void SpeculativeTransaction::Close(nsresult aReason) { 81 MOZ_ASSERT(OnSocketThread(), "not on socket thread"); 82 LOG(("SpeculativeTransaction::Close %p aReason=%" PRIx32, this, 83 static_cast<uint32_t>(aReason))); 84 NullHttpTransaction::Close(aReason); 85 if (mResolver) { 86 mResolver->Close(); 87 mResolver = nullptr; 88 } 89 90 if (aReason == NS_BASE_STREAM_CLOSED) { 91 aReason = NS_OK; 92 } 93 if (mCloseCallback) { 94 mCloseCallback(mTriedToWrite && NS_SUCCEEDED(aReason)); 95 mCloseCallback = nullptr; 96 } 97 } 98 99 void SpeculativeTransaction::InvokeCallback() { 100 if (mCloseCallback) { 101 mCloseCallback(true); 102 mCloseCallback = nullptr; 103 } 104 } 105 106 } // namespace net 107 } // namespace mozilla