1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #pragma once
  27 
  28 #include "Document.h"
  29 #include "DocumentLoader.h"
  30 #include "Frame.h"
  31 #include "FrameLoader.h"
  32 #include "FrameLoaderClient.h"
  33 #include "FrameView.h"
  34 #include "FormState.h"
  35 #include "HTMLFrameOwnerElement.h"
  36 
  37 #include "ResourceRequest.h"
  38 #include "ResourceResponse.h"
  39 #include "Widget.h"
  40 #include "PluginWidgetJava.h"
  41 #include "ProgressTrackerClient.h"
  42 
  43 #include <wtf/java/JavaEnv.h>
  44 
  45 namespace WebCore {
  46 
  47 class FrameLoaderClientJava final : public FrameLoaderClient {
  48 public:
  49     FrameLoaderClientJava(const JLObject &webPage);
  50     void frameLoaderDestroyed() override;
  51 
  52     bool hasWebView() const override;
  53 
  54     void makeRepresentation(DocumentLoader*) override;
  55     void forceLayoutForNonHTML() override;
  56 
  57     std::optional<uint64_t> pageID() const final;
  58     std::optional<uint64_t> frameID() const final;
  59     PAL::SessionID sessionID() const final;
  60 
  61 
  62     void setCopiesOnScroll() override;
  63 
  64     void detachedFromParent2() override;
  65     void detachedFromParent3() override;
  66 
  67     void assignIdentifierToInitialRequest(unsigned long identifier, DocumentLoader*, const ResourceRequest&) override;
  68 
  69     void dispatchWillSendRequest(DocumentLoader*, unsigned long  identifier, ResourceRequest&, const ResourceResponse& redirectResponse) override;
  70     void dispatchDidReceiveResponse(DocumentLoader*, unsigned long  identifier, const ResourceResponse&) override;
  71     void dispatchDidReceiveContentLength(DocumentLoader*, unsigned long identifier, int lengthReceived) override;
  72     void dispatchDidFinishLoading(DocumentLoader*, unsigned long  identifier) override;
  73     void dispatchDidFailLoading(DocumentLoader*, unsigned long  identifier, const ResourceError&) override;
  74     bool dispatchDidLoadResourceFromMemoryCache(DocumentLoader*, const ResourceRequest&, const ResourceResponse&, int length) override;
  75 
  76     void dispatchDidDispatchOnloadEvents() override;
  77     void dispatchDidPushStateWithinPage() override;
  78     void dispatchDidReplaceStateWithinPage() override;
  79     void dispatchDidPopStateWithinPage() override;
  80     void dispatchDidReceiveServerRedirectForProvisionalLoad() override;
  81     void dispatchDidCancelClientRedirect() override;
  82     void dispatchWillPerformClientRedirect(const URL&, double, WallTime) override;
  83     void dispatchDidNavigateWithinPage() override;
  84     void dispatchDidChangeLocationWithinPage() override;
  85     void dispatchWillClose() override;
  86     void dispatchDidReceiveIcon() override;
  87     void dispatchDidStartProvisionalLoad() override;
  88     void dispatchDidReceiveTitle(const StringWithDirection&) override;
  89     void dispatchDidCommitLoad(std::optional<HasInsecureContent>) override;
  90     void dispatchDidFailProvisionalLoad(const ResourceError&) override;
  91     void dispatchDidFailLoad(const ResourceError&) override;
  92     void dispatchDidFinishDocumentLoad() override;
  93     void dispatchDidFinishLoad() override;
  94     void dispatchDidClearWindowObjectInWorld(WebCore::DOMWrapperWorld&) override;
  95 
  96     Frame* dispatchCreatePage(const NavigationAction&) override;
  97     void dispatchShow() override;
  98 
  99     void dispatchDecidePolicyForResponse(const ResourceResponse&, const ResourceRequest&, FramePolicyFunction&&) override;
 100     void dispatchDecidePolicyForNewWindowAction(const NavigationAction&, const ResourceRequest&, FormState*, const String& frameName, FramePolicyFunction&&) override;
 101     void dispatchDecidePolicyForNavigationAction(const NavigationAction&, const ResourceRequest&, bool didReceiveRedirectResponse, FormState*, FramePolicyFunction&&) override;
 102     void cancelPolicyCheck() override;
 103 
 104     void dispatchUnableToImplementPolicy(const ResourceError&) override;
 105 
 106     void dispatchWillSendSubmitEvent(Ref<FormState>&&) override {}
 107     void dispatchWillSubmitForm(FormState&, WTF::Function<void(void)>&&) override;
 108 
 109     void dispatchDidLoadMainResource(DocumentLoader*);
 110 
 111     void revertToProvisionalState(DocumentLoader*) override;
 112     void setMainDocumentError(DocumentLoader*, const ResourceError&) override;
 113 
 114     RefPtr<Frame> createFrame(const URL& url, const String& name, HTMLFrameOwnerElement& ownerElement,
 115                                const String& referrer, bool allowsScrolling, int marginWidth, int marginHeight) override;
 116     ObjectContentType objectContentType(const URL& url, const String& mimeTypeIn) override;
 117     RefPtr<Widget> createPlugin(const IntSize&, HTMLPlugInElement&, const URL&, const Vector<String>&, const Vector<String>&, const String&, bool loadManually) override;
 118     void recreatePlugin(Widget*) override {}
 119     void redirectDataToPlugin(Widget&) override;
 120     RefPtr<Widget> createJavaAppletWidget(const IntSize&, HTMLAppletElement&, const URL& baseURL, const Vector<String>& paramNames, const Vector<String>& paramValues) override;
 121     String overrideMediaType() const override;
 122 
 123     void setMainFrameDocumentReady(bool) override;
 124 
 125     void startDownload(const ResourceRequest&, const String& suggestedName = String()) override;
 126 
 127     void willChangeTitle(DocumentLoader*) override;
 128     void didChangeTitle(DocumentLoader*) override;
 129 
 130     void committedLoad(DocumentLoader*, const char*, int) override;
 131     void finishedLoading(DocumentLoader*) override;
 132 
 133     void updateGlobalHistory() override;
 134     void updateGlobalHistoryRedirectLinks() override;
 135 
 136     bool shouldGoToHistoryItem(HistoryItem*) const override;
 137 
 138     // This frame has displayed inactive content (such as an image) from an
 139     // insecure source.  Inactive content cannot spread to other frames.
 140     void didDisplayInsecureContent() override;
 141 
 142     // The indicated security origin has run active content (such as a
 143     // script) from an insecure source.  Note that the insecure content can
 144     // spread to other frames in the same origin.
 145     void didRunInsecureContent(SecurityOrigin&, const URL&) override;
 146     void didDetectXSS(const URL&, bool) override;
 147 
 148     ResourceError cancelledError(const ResourceRequest&) override;
 149     ResourceError blockedByContentBlockerError(const ResourceRequest& request) override;
 150     ResourceError blockedError(const ResourceRequest&) override;
 151     ResourceError cannotShowURLError(const ResourceRequest&) override;
 152     ResourceError interruptedForPolicyChangeError(const ResourceRequest&) override;
 153 
 154     ResourceError cannotShowMIMETypeError(const ResourceResponse&) override;
 155     ResourceError fileDoesNotExistError(const ResourceResponse&) override;
 156     ResourceError pluginWillHandleLoadError(const ResourceResponse&) override;
 157 
 158     bool shouldFallBack(const ResourceError&) override;
 159 
 160     bool shouldUseCredentialStorage(DocumentLoader*, unsigned long identifier) override;
 161     void dispatchDidReceiveAuthenticationChallenge(DocumentLoader*, unsigned long identifier, const AuthenticationChallenge&) override;
 162 
 163     bool canHandleRequest(const ResourceRequest&) const override;
 164     bool canShowMIMEType(const String&) const override;
 165     bool canShowMIMETypeAsHTML(const String& MIMEType) const override;
 166     bool representationExistsForURLScheme(const String&) const override;
 167     String generatedMIMETypeForURLScheme(const String&) const override;
 168 
 169     void frameLoadCompleted() override;
 170     void saveViewStateToItem(HistoryItem&) override;
 171     void restoreViewState() override;
 172     void provisionalLoadStarted() override;
 173     void didFinishLoad() override;
 174     void prepareForDataSourceReplacement() override;
 175 
 176     Ref<DocumentLoader> createDocumentLoader(const ResourceRequest&, const SubstituteData&) override;
 177     void setTitle(const StringWithDirection& title, const URL&) override;
 178 
 179     void willReplaceMultipartContent() override;
 180     void didReplaceMultipartContent() override;
 181     void updateCachedDocumentLoader(DocumentLoader&) override;
 182 
 183     String userAgent(const URL&) override;
 184 
 185     void savePlatformDataToCachedFrame(CachedFrame*) override;
 186     void transitionToCommittedFromCachedFrame(CachedFrame*) override;
 187     void transitionToCommittedForNewPage() override;
 188 
 189     bool canCachePage() const override;
 190     void convertMainResourceLoadToDownload(DocumentLoader*, PAL::SessionID, const ResourceRequest&, const ResourceResponse&) override;
 191 
 192     void didSaveToPageCache() override;
 193     void didRestoreFromPageCache() override;
 194 
 195     void dispatchDidBecomeFrameset(bool) override; // Can change due to navigation or DOM modification override.
 196 
 197     Ref<FrameNetworkingContext> createNetworkingContext() override;
 198 
 199     void registerForIconNotification() override;
 200 
 201     void setFrame(Frame* frame);
 202 
 203     bool isJavaFrameLoaderClient() override { return true; }
 204     void prefetchDNS(const String&) override;
 205 private:
 206     Page* m_page;
 207     Frame* m_frame;
 208     ResourceResponse m_response;
 209     unsigned long m_mainResourceRequestID;
 210     bool m_isPageRedirected;
 211     bool m_hasRepresentation;
 212 
 213     JGObject m_webPage;
 214 
 215     Page* page();
 216     Frame* frame();
 217 
 218     void setRequestURL(Frame* f, int identifier, String url);
 219     void removeRequestURL(Frame* f, int identifier);
 220 
 221     void postLoadEvent(Frame* f, int state, String url, String contentType, double progress, int errorCode = 0);
 222     void postResourceLoadEvent(Frame* f, int state, int id, String contentType, double progress, int errorCode = 0);
 223     // Plugin widget for handling data redirection
 224 //        PluginWidgetJava* m_pluginWidget;
 225 };
 226 } // namespace WebCore