| 1 | /* |
| 2 | * Copyright (C) 2006-2018 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 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
| 14 | * its contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "config.h" |
| 30 | #include "NavigationAction.h" |
| 31 | |
| 32 | #include "Document.h" |
| 33 | #include "Frame.h" |
| 34 | #include "FrameLoader.h" |
| 35 | #include "FrameLoaderClient.h" |
| 36 | #include "HistoryItem.h" |
| 37 | #include "MouseEvent.h" |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | NavigationAction::Requester::Requester(const Document& document) |
| 42 | : m_url { URL { document.url() } } |
| 43 | , m_origin { makeRefPtr(document.securityOrigin()) } |
| 44 | , m_pageIDAndFrameIDPair { document.frame() ? std::make_pair(document.frame()->loader().client().pageID().valueOr(0), document.frame()->loader().client().frameID().valueOr(0)) : std::make_pair<uint64_t, uint64_t>(0, 0) } |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | NavigationAction::UIEventWithKeyStateData::UIEventWithKeyStateData(const UIEventWithKeyState& uiEvent) |
| 49 | : isTrusted { uiEvent.isTrusted() } |
| 50 | , shiftKey { uiEvent.shiftKey() } |
| 51 | , ctrlKey { uiEvent.ctrlKey() } |
| 52 | , altKey { uiEvent.altKey() } |
| 53 | , metaKey { uiEvent.metaKey() } |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | NavigationAction::MouseEventData::MouseEventData(const MouseEvent& mouseEvent) |
| 58 | : UIEventWithKeyStateData { mouseEvent } |
| 59 | , absoluteLocation { mouseEvent.absoluteLocation() } |
| 60 | , locationInRootViewCoordinates { mouseEvent.locationInRootViewCoordinates() } |
| 61 | , button { mouseEvent.button() } |
| 62 | , syntheticClickType { mouseEvent.syntheticClickType() } |
| 63 | , buttonDown { mouseEvent.buttonDown() } |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | NavigationAction::NavigationAction() = default; |
| 68 | NavigationAction::~NavigationAction() = default; |
| 69 | |
| 70 | NavigationAction::NavigationAction(const NavigationAction&) = default; |
| 71 | NavigationAction::NavigationAction(NavigationAction&&) = default; |
| 72 | |
| 73 | NavigationAction& NavigationAction::operator=(const NavigationAction&) = default; |
| 74 | NavigationAction& NavigationAction::operator=(NavigationAction&&) = default; |
| 75 | |
| 76 | static bool shouldTreatAsSameOriginNavigation(const Document& document, const URL& url) |
| 77 | { |
| 78 | return url.protocolIsAbout() || url.protocolIsData() || (url.protocolIsBlob() && document.securityOrigin().canRequest(url)); |
| 79 | } |
| 80 | |
| 81 | static Optional<NavigationAction::UIEventWithKeyStateData> keyStateDataForFirstEventWithKeyState(Event* event) |
| 82 | { |
| 83 | if (UIEventWithKeyState* uiEvent = findEventWithKeyState(event)) |
| 84 | return NavigationAction::UIEventWithKeyStateData { *uiEvent }; |
| 85 | return WTF::nullopt; |
| 86 | } |
| 87 | |
| 88 | static Optional<NavigationAction::MouseEventData> mouseEventDataForFirstMouseEvent(Event* event) |
| 89 | { |
| 90 | for (Event* e = event; e; e = e->underlyingEvent()) { |
| 91 | if (e->isMouseEvent()) |
| 92 | return NavigationAction::MouseEventData { static_cast<const MouseEvent&>(*e) }; |
| 93 | } |
| 94 | return WTF::nullopt; |
| 95 | } |
| 96 | |
| 97 | NavigationAction::NavigationAction(Document& requester, const ResourceRequest& resourceRequest, InitiatedByMainFrame initiatedByMainFrame, NavigationType type, ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy, Event* event, const AtomicString& downloadAttribute) |
| 98 | : m_requester { requester } |
| 99 | , m_resourceRequest { resourceRequest } |
| 100 | , m_type { type } |
| 101 | , m_shouldOpenExternalURLsPolicy { shouldOpenExternalURLsPolicy } |
| 102 | , m_initiatedByMainFrame { initiatedByMainFrame } |
| 103 | , m_keyStateEventData { keyStateDataForFirstEventWithKeyState(event) } |
| 104 | , m_mouseEventData { mouseEventDataForFirstMouseEvent(event) } |
| 105 | , m_downloadAttribute { downloadAttribute } |
| 106 | , m_treatAsSameOriginNavigation { shouldTreatAsSameOriginNavigation(requester, resourceRequest.url()) } |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | static NavigationType navigationType(FrameLoadType frameLoadType, bool isFormSubmission, bool haveEvent) |
| 111 | { |
| 112 | if (isFormSubmission) |
| 113 | return NavigationType::FormSubmitted; |
| 114 | if (haveEvent) |
| 115 | return NavigationType::LinkClicked; |
| 116 | if (isReload(frameLoadType)) |
| 117 | return NavigationType::Reload; |
| 118 | if (isBackForwardLoadType(frameLoadType)) |
| 119 | return NavigationType::BackForward; |
| 120 | return NavigationType::Other; |
| 121 | } |
| 122 | |
| 123 | NavigationAction::NavigationAction(Document& requester, const ResourceRequest& resourceRequest, InitiatedByMainFrame initiatedByMainFrame, FrameLoadType frameLoadType, bool isFormSubmission, Event* event, ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy, const AtomicString& downloadAttribute) |
| 124 | : m_requester { requester } |
| 125 | , m_resourceRequest { resourceRequest } |
| 126 | , m_type { navigationType(frameLoadType, isFormSubmission, !!event) } |
| 127 | , m_shouldOpenExternalURLsPolicy { shouldOpenExternalURLsPolicy } |
| 128 | , m_initiatedByMainFrame { initiatedByMainFrame } |
| 129 | , m_keyStateEventData { keyStateDataForFirstEventWithKeyState(event) } |
| 130 | , m_mouseEventData { mouseEventDataForFirstMouseEvent(event) } |
| 131 | , m_downloadAttribute { downloadAttribute } |
| 132 | , m_treatAsSameOriginNavigation { shouldTreatAsSameOriginNavigation(requester, resourceRequest.url()) } |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | NavigationAction NavigationAction::copyWithShouldOpenExternalURLsPolicy(ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy) const |
| 137 | { |
| 138 | NavigationAction result(*this); |
| 139 | result.m_shouldOpenExternalURLsPolicy = shouldOpenExternalURLsPolicy; |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | void NavigationAction::setTargetBackForwardItem(HistoryItem& item) |
| 144 | { |
| 145 | m_targetBackForwardItemIdentifier = item.identifier(); |
| 146 | } |
| 147 | |
| 148 | void NavigationAction::setSourceBackForwardItem(HistoryItem* item) |
| 149 | { |
| 150 | m_sourceBackForwardItemIdentifier = item ? makeOptional(item->identifier()) : WTF::nullopt; |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |