Add a new API on the resource loader to pause the current load. We use this when the plugin cannot accept any more data to prevent WebKit
buffering the data that the plugin can't take. This can result in a crash if the buffer gets too big (when watching an HD movie clip in
flash, for example).

Requires a frameworks/base change.

Change-Id: Ibb63d38df1014ff70fa4d65275613cbf3a217c1a
diff --git a/WebCore/loader/ResourceLoader.cpp b/WebCore/loader/ResourceLoader.cpp
index 95ce209..d14afc8 100644
--- a/WebCore/loader/ResourceLoader.cpp
+++ b/WebCore/loader/ResourceLoader.cpp
@@ -149,6 +149,15 @@
     }
 }
 
+#if PLATFORM(ANDROID)
+// TODO: This needs upstreaming to WebKit.
+void ResourceLoader::pauseLoad(bool pause)
+{
+    if (m_handle)
+        m_handle->pauseLoad(pause);
+}
+#endif
+
 FrameLoader* ResourceLoader::frameLoader() const
 {
     if (!m_frame)
diff --git a/WebCore/loader/ResourceLoader.h b/WebCore/loader/ResourceLoader.h
index 5239289..3178eb4 100644
--- a/WebCore/loader/ResourceLoader.h
+++ b/WebCore/loader/ResourceLoader.h
@@ -64,6 +64,10 @@
         ResourceError cannotShowURLError();
         
         virtual void setDefersLoading(bool);
+#if PLATFORM(ANDROID)
+// TODO: This needs upstreaming to WebKit.
+        virtual void pauseLoad(bool);
+#endif
 
         void setIdentifier(unsigned long identifier) { m_identifier = identifier; }
         unsigned long identifier() const { return m_identifier; }
diff --git a/WebCore/platform/network/ResourceHandle.h b/WebCore/platform/network/ResourceHandle.h
index b764add..e340aca 100644
--- a/WebCore/platform/network/ResourceHandle.h
+++ b/WebCore/platform/network/ResourceHandle.h
@@ -189,7 +189,11 @@
     void setClient(ResourceHandleClient*);
 
     void setDefersLoading(bool);
-      
+#if PLATFORM(ANDROID)
+// TODO: this needs upstreaming.
+    void pauseLoad(bool);
+#endif
+
     const ResourceRequest& request() const;
 
     void fireFailure(Timer<ResourceHandle>*);
diff --git a/WebCore/platform/network/android/ResourceHandleAndroid.cpp b/WebCore/platform/network/android/ResourceHandleAndroid.cpp
index 1154b47..6759852 100644
--- a/WebCore/platform/network/android/ResourceHandleAndroid.cpp
+++ b/WebCore/platform/network/android/ResourceHandleAndroid.cpp
@@ -84,6 +84,15 @@
     return false;
 }
 
+#if PLATFORM(ANDROID)
+// TODO: this needs upstreaming.
+void ResourceHandle::pauseLoad(bool pause)
+{
+    if (d->m_loader)
+        d->m_loader->pauseLoad(pause);
+}
+#endif
+
 void ResourceHandle::setDefersLoading(bool defers)
 {
     notImplemented();
diff --git a/WebCore/platform/network/android/ResourceLoaderAndroid.h b/WebCore/platform/network/android/ResourceLoaderAndroid.h
index 004675e..f627d62 100644
--- a/WebCore/platform/network/android/ResourceLoaderAndroid.h
+++ b/WebCore/platform/network/android/ResourceLoaderAndroid.h
@@ -42,6 +42,9 @@
 
     virtual void cancel() = 0;
     virtual void downloadFile() = 0;
+    // ANDROID TODO: This needs to be upstreamed.
+    virtual void pauseLoad(bool) = 0;
+    // END ANDROID TODO
 
     // Call to java to find out if this URL is in the cache
     static bool willLoadFromCache(const WebCore::KURL&, int64_t identifier);
diff --git a/WebCore/plugins/PluginStream.cpp b/WebCore/plugins/PluginStream.cpp
index bf35ba4..1e91c51 100644
--- a/WebCore/plugins/PluginStream.cpp
+++ b/WebCore/plugins/PluginStream.cpp
@@ -345,6 +345,11 @@
         int32 deliveryBytes = m_pluginFuncs->writeready(m_instance, &m_stream);
 
         if (deliveryBytes <= 0) {
+#if PLATFORM(ANDROID)
+// TODO: This needs to be upstreamed.
+            if (m_loader)
+                m_loader->pauseLoad(true);
+#endif
             m_delayDeliveryTimer.startOneShot(0);
             break;
         } else {
@@ -373,6 +378,11 @@
             memmove(m_deliveryData->data(), m_deliveryData->data() + totalBytesDelivered, remainingBytes);
             m_deliveryData->resize(remainingBytes);
         } else {
+#if PLATFORM(ANDROID)
+//TODO: This needs to be upstreamed to WebKit.
+            if (m_loader)
+                m_loader->pauseLoad(false);
+#endif
             m_deliveryData->resize(0);
             if (m_reason != WebReasonNone)
                 destroyStream();
diff --git a/WebKit/android/jni/WebCoreResourceLoader.cpp b/WebKit/android/jni/WebCoreResourceLoader.cpp
index b17c9a7..cf32c09 100644
--- a/WebKit/android/jni/WebCoreResourceLoader.cpp
+++ b/WebKit/android/jni/WebCoreResourceLoader.cpp
@@ -56,6 +56,7 @@
     jmethodID   mCancelMethodID;
     jmethodID   mDownloadFileMethodID;
     jmethodID   mWillLoadFromCacheMethodID;
+    jmethodID   mPauseLoadMethodID;
 } gResourceLoader;
 
 // ----------------------------------------------------------------------------
@@ -72,6 +73,7 @@
 }
 
 WebCoreResourceLoader::WebCoreResourceLoader(JNIEnv *env, jobject jLoadListener)
+    : mPausedLoad(false)
 {
     mJLoader = env->NewGlobalRef(jLoadListener);
 }
@@ -98,6 +100,17 @@
     checkException(env);
 }
 
+void WebCoreResourceLoader::pauseLoad(bool pause)
+{
+    if (mPausedLoad == pause)
+        return;
+
+    mPausedLoad = pause;
+    JNIEnv* env = JSC::Bindings::getJNIEnv();
+    env->CallVoidMethod(mJLoader, gResourceLoader.mPauseLoadMethodID, pause);
+    checkException(env);
+}
+
 /*
 * This static method is called to check to see if a POST response is in
 * the cache. This may be slow, but is only used during a navigation to
@@ -320,6 +333,11 @@
     LOG_FATAL_IF(gResourceLoader.mDownloadFileMethodID == NULL, 
         "Could not find method downloadFile on LoadListener");
 
+    gResourceLoader.mPauseLoadMethodID =
+        env->GetMethodID(resourceLoader, "pauseLoad", "(Z)V");
+    LOG_FATAL_IF(gResourceLoader.mPauseLoadMethodID == NULL,
+        "Could not find method pauseLoad on LoadListener");
+
     gResourceLoader.mWillLoadFromCacheMethodID = 
         env->GetStaticMethodID(resourceLoader, "willLoadFromCache", "(Ljava/lang/String;J)Z");
     LOG_FATAL_IF(gResourceLoader.mWillLoadFromCacheMethodID == NULL, 
diff --git a/WebKit/android/jni/WebCoreResourceLoader.h b/WebKit/android/jni/WebCoreResourceLoader.h
index d24a43e..c60b3f5 100644
--- a/WebKit/android/jni/WebCoreResourceLoader.h
+++ b/WebKit/android/jni/WebCoreResourceLoader.h
@@ -49,6 +49,8 @@
     */
     virtual void downloadFile();
 
+    virtual void pauseLoad(bool);
+
     /**
     * Call to java to find out if this URL is in the cache
     */
@@ -68,6 +70,7 @@
     WebCoreResourceLoader(JNIEnv *env, jobject jLoadListener);
 private:
     jobject     mJLoader;
+    bool        mPausedLoad;
 };
 
 } // end namespace android