| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "PreconnectTask.h" |
| 28 | |
| 29 | #if ENABLE(SERVER_PRECONNECT) |
| 30 | |
| 31 | #include "Logging.h" |
| 32 | #include "NetworkLoad.h" |
| 33 | #include "NetworkLoadParameters.h" |
| 34 | #include "NetworkProcess.h" |
| 35 | #include "WebErrors.h" |
| 36 | #include <WebCore/ResourceError.h> |
| 37 | |
| 38 | namespace WebKit { |
| 39 | |
| 40 | using namespace WebCore; |
| 41 | |
| 42 | PreconnectTask::PreconnectTask(NetworkProcess& networkProcess, NetworkLoadParameters&& parameters, CompletionHandler<void(const ResourceError&)>&& completionHandler) |
| 43 | : m_completionHandler(WTFMove(completionHandler)) |
| 44 | , m_timeoutTimer([this] { didFinish(ResourceError { String(), 0, m_networkLoad->parameters().request.url(), "Preconnection timed out"_s , ResourceError::Type::Timeout }); }) |
| 45 | { |
| 46 | RELEASE_LOG(Network, "%p - PreconnectTask::PreconnectTask()" , this); |
| 47 | |
| 48 | auto* networkSession = networkProcess.networkSession(parameters.sessionID); |
| 49 | if (!networkSession) { |
| 50 | ASSERT_NOT_REACHED(); |
| 51 | m_completionHandler(internalError(parameters.request.url())); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | ASSERT(parameters.shouldPreconnectOnly == PreconnectOnly::Yes); |
| 56 | m_networkLoad = std::make_unique<NetworkLoad>(*this, nullptr, WTFMove(parameters), *networkSession); |
| 57 | |
| 58 | m_timeoutTimer.startOneShot(60000_s); |
| 59 | } |
| 60 | |
| 61 | PreconnectTask::~PreconnectTask() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void PreconnectTask::willSendRedirectedRequest(ResourceRequest&&, ResourceRequest&& redirectRequest, ResourceResponse&& redirectResponse) |
| 66 | { |
| 67 | ASSERT_NOT_REACHED(); |
| 68 | } |
| 69 | |
| 70 | void PreconnectTask::didReceiveResponse(ResourceResponse&& response, ResponseCompletionHandler&& completionHandler) |
| 71 | { |
| 72 | ASSERT_NOT_REACHED(); |
| 73 | completionHandler(PolicyAction::Ignore); |
| 74 | } |
| 75 | |
| 76 | void PreconnectTask::didReceiveBuffer(Ref<SharedBuffer>&&, int reportedEncodedDataLength) |
| 77 | { |
| 78 | ASSERT_NOT_REACHED(); |
| 79 | } |
| 80 | |
| 81 | void PreconnectTask::didFinishLoading(const NetworkLoadMetrics&) |
| 82 | { |
| 83 | RELEASE_LOG(Network, "%p - PreconnectTask::didFinishLoading" , this); |
| 84 | didFinish({ }); |
| 85 | } |
| 86 | |
| 87 | void PreconnectTask::didFailLoading(const ResourceError& error) |
| 88 | { |
| 89 | RELEASE_LOG(Network, "%p - PreconnectTask::didFailLoading, error_code: %d" , this, error.errorCode()); |
| 90 | didFinish(error); |
| 91 | } |
| 92 | |
| 93 | void PreconnectTask::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) |
| 94 | { |
| 95 | ASSERT_NOT_REACHED(); |
| 96 | } |
| 97 | |
| 98 | void PreconnectTask::didFinish(const ResourceError& error) |
| 99 | { |
| 100 | if (m_completionHandler) |
| 101 | m_completionHandler(error); |
| 102 | delete this; |
| 103 | } |
| 104 | |
| 105 | } // namespace WebKit |
| 106 | |
| 107 | #endif // ENABLE(SERVER_PRECONNECT) |
| 108 | |