1 /*
   2  * Copyright (c) 2017, 2018, 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 #include "config.h"
  27 
  28 #include "ProgressTrackerClientJava.h"
  29 
  30 #include "Page.h"
  31 #include "FrameLoader.h"
  32 #include "DocumentLoader.h"
  33 #include "ProgressTracker.h"
  34 
  35 #include "WebPage.h"
  36 
  37 #include "com_sun_webkit_LoadListenerClient.h"
  38 
  39 namespace ProgressTrackerClientJavaInternal {
  40 static JGClass webPageClass;
  41 static jmethodID fireLoadEventMID;
  42 
  43 static void initRefs(JNIEnv* env)
  44 {
  45     if (!webPageClass) {
  46         webPageClass = JLClass(env->FindClass(
  47             "com/sun/webkit/WebPage"));
  48         ASSERT(webPageClass);
  49 
  50         fireLoadEventMID = env->GetMethodID(webPageClass,
  51                                     "fwkFireLoadEvent",
  52                                     "(JILjava/lang/String;Ljava/lang/String;DI)V");
  53         ASSERT(fireLoadEventMID);
  54     }
  55 }
  56 }
  57 
  58 namespace WebCore {
  59 
  60 ProgressTrackerClientJava::ProgressTrackerClientJava(const JLObject& webPage)
  61     : m_webPage(webPage)
  62 {
  63 }
  64 
  65 void ProgressTrackerClientJava::progressStarted(Frame&)
  66 {
  67 }
  68 
  69 void ProgressTrackerClientJava::progressEstimateChanged(Frame& originatingProgressFrame)
  70 {
  71     using namespace ProgressTrackerClientJavaInternal;
  72     JNIEnv* env = WebCore_GetJavaEnv();
  73     initRefs(env);
  74 
  75     double progress = originatingProgressFrame.page()->progress().estimatedProgress();
  76     // We have a redundant notification from webkit (with progress == 1)
  77     // after PAGE_FINISHED has already been posted.
  78     DocumentLoader* documentLoader = originatingProgressFrame.loader().activeDocumentLoader();
  79     if (documentLoader && progress < 1) {
  80         JLString urlJavaString(documentLoader->url().string().toJavaString(env));
  81         JLString contentTypeJavaString(documentLoader->responseMIMEType().toJavaString(env));
  82 
  83         if (documentLoader->mainResourceData()) {
  84             documentLoader->mainResourceData()->size(); // TODO-java: recheck
  85         }
  86         // Second, send a load event
  87         env->CallVoidMethod(m_webPage, fireLoadEventMID,
  88                             ptr_to_jlong(&originatingProgressFrame),
  89                             com_sun_webkit_LoadListenerClient_PROGRESS_CHANGED,
  90                             (jstring)urlJavaString,
  91                             (jstring)contentTypeJavaString,
  92                             progress,
  93                             0);
  94         CheckAndClearException(env);
  95     }
  96 }
  97 
  98 void ProgressTrackerClientJava::progressFinished(Frame&)
  99 {
 100     // shouldn't post PROGRESS_CHANGED after PAGE_FINISHED
 101 }
 102 
 103 void ProgressTrackerClientJava::progressTrackerDestroyed()
 104 {
 105     delete this;
 106 }
 107 
 108 } // namespace WebCore