Rollout r231818, as it introduced regression on tickets.com.
[WebKit-https.git] / Source / WebKit / UIProcess / WebURLSchemeTask.cpp
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 "WebURLSchemeTask.h"
28
29 #include "DataReference.h"
30 #include "WebPageMessages.h"
31 #include "WebPageProxy.h"
32 #include "WebURLSchemeHandler.h"
33
34 using namespace WebCore;
35
36 namespace WebKit {
37
38 Ref<WebURLSchemeTask> WebURLSchemeTask::create(WebURLSchemeHandler& handler, WebPageProxy& page, uint64_t resourceIdentifier, ResourceRequest&& request)
39 {
40     return adoptRef(*new WebURLSchemeTask(handler, page, resourceIdentifier, WTFMove(request)));
41 }
42
43 WebURLSchemeTask::WebURLSchemeTask(WebURLSchemeHandler& handler, WebPageProxy& page, uint64_t resourceIdentifier, ResourceRequest&& request)
44     : m_urlSchemeHandler(handler)
45     , m_page(&page)
46     , m_identifier(resourceIdentifier)
47     , m_pageIdentifier(page.pageID())
48     , m_request(WTFMove(request))
49 {
50 }
51
52 auto WebURLSchemeTask::didPerformRedirection(WebCore::ResourceResponse&& response, WebCore::ResourceRequest&& request) -> ExceptionType
53 {
54     if (m_stopped)
55         return ExceptionType::TaskAlreadyStopped;
56     
57     if (m_completed)
58         return ExceptionType::CompleteAlreadyCalled;
59     
60     if (m_dataSent)
61         return ExceptionType::DataAlreadySent;
62     
63     if (m_responseSent)
64         return ExceptionType::RedirectAfterResponse;
65     
66     m_request = request;
67     m_page->send(Messages::WebPage::URLSchemeTaskDidPerformRedirection(m_urlSchemeHandler->identifier(), m_identifier, response, request));
68
69     return ExceptionType::None;
70 }
71
72 auto WebURLSchemeTask::didReceiveResponse(const ResourceResponse& response) -> ExceptionType
73 {
74     if (m_stopped)
75         return ExceptionType::TaskAlreadyStopped;
76
77     if (m_completed)
78         return ExceptionType::CompleteAlreadyCalled;
79
80     if (m_dataSent)
81         return ExceptionType::DataAlreadySent;
82
83     m_responseSent = true;
84
85     response.includeCertificateInfo();
86     m_page->send(Messages::WebPage::URLSchemeTaskDidReceiveResponse(m_urlSchemeHandler->identifier(), m_identifier, response));
87     return ExceptionType::None;
88 }
89
90 auto WebURLSchemeTask::didReceiveData(Ref<SharedBuffer> buffer) -> ExceptionType
91 {
92     if (m_stopped)
93         return ExceptionType::TaskAlreadyStopped;
94
95     if (m_completed)
96         return ExceptionType::CompleteAlreadyCalled;
97
98     if (!m_responseSent)
99         return ExceptionType::NoResponseSent;
100
101     m_dataSent = true;
102     m_page->send(Messages::WebPage::URLSchemeTaskDidReceiveData(m_urlSchemeHandler->identifier(), m_identifier, IPC::SharedBufferDataReference(buffer.ptr())));
103     return ExceptionType::None;
104 }
105
106 auto WebURLSchemeTask::didComplete(const ResourceError& error) -> ExceptionType
107 {
108     if (m_stopped)
109         return ExceptionType::TaskAlreadyStopped;
110
111     if (m_completed)
112         return ExceptionType::CompleteAlreadyCalled;
113
114     if (!m_responseSent && error.isNull())
115         return ExceptionType::NoResponseSent;
116
117     m_completed = true;
118     m_page->send(Messages::WebPage::URLSchemeTaskDidComplete(m_urlSchemeHandler->identifier(), m_identifier, error));
119     m_urlSchemeHandler->taskCompleted(*this);
120
121     return ExceptionType::None;
122 }
123
124 void WebURLSchemeTask::pageDestroyed()
125 {
126     ASSERT(m_page);
127     m_page = nullptr;
128     m_stopped = true;
129 }
130
131 void WebURLSchemeTask::stop()
132 {
133     ASSERT(!m_stopped);
134     m_stopped = true;
135 }
136
137 } // namespace WebKit