[BlackBerry] Loading media data with http authentication
https://bugs.webkit.org/show_bug.cgi?id=84214

Patch by Jonathan Dong <jonathan.dong@torchmobile.com.cn> on 2012-05-29
Reviewed by George Staikos.

.:

Added a manual test case which needs user to provide a HTTP server
with HTTP authentication support when loading the specified media
resource. The test case will test if the media resource is successfully
loaded.

* ManualTests/blackberry/video-load-with-authentication.html: Added.

Source/WebCore:

RIM PR: 117618
Implemented http authentication feature for media by implementing
two interface functions in class MediaPlayerPrivate:
onAuthenticationNeeded(): this function is triggered when MMR
engine requires http authentication. We search the CredentialStorage
to see if we have already stored existing credential information,
or challenge user to provide it.
OnAuthenticationAccepted(): this function is triggered when MMR
engine accepts the credential information, and we need to save
it in CredentialStorage for later use.

Internally reviewed by Max Feil <mfeil@qnx.com>.

Manual test case: blackberry/video-load-with-authentication.html

* platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.cpp:
(WebCore::generateProtectionSpaceFromMMRAuthChallenge):
(WebCore):
(WebCore::MediaPlayerPrivate::onAuthenticationNeeded):
(WebCore::MediaPlayerPrivate::onAuthenticationAccepted):
* platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.h:
(MediaPlayerPrivate):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@118887 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/ChangeLog b/ChangeLog
index 1fcba26..016b485 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2012-05-29  Jonathan Dong  <jonathan.dong@torchmobile.com.cn>
+
+        [BlackBerry] Loading media data with http authentication
+        https://bugs.webkit.org/show_bug.cgi?id=84214
+
+        Reviewed by George Staikos.
+
+        Added a manual test case which needs user to provide a HTTP server
+        with HTTP authentication support when loading the specified media
+        resource. The test case will test if the media resource is successfully
+        loaded.
+
+        * ManualTests/blackberry/video-load-with-authentication.html: Added.
+
 2012-05-29  Simon Fraser  <simon.fraser@apple.com>
 
         Incomplete repaint on twitter.com when replying to a tweet
diff --git a/ManualTests/blackberry/video-load-with-authentication.html b/ManualTests/blackberry/video-load-with-authentication.html
new file mode 100644
index 0000000..a2273f4
--- /dev/null
+++ b/ManualTests/blackberry/video-load-with-authentication.html
@@ -0,0 +1,68 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+    <title>video-load-with-authentication</title>
+    <script src="../../LayoutTests/media/video-test.js" type="text/javascript"></script>
+</head>
+<body>
+<p>
+  This test case is for <a href="https://bugs.webkit.org/show_bug.cgi?id=84214">https://bugs.webkit.org/show_bug.cgi?id=84214</a><br>
+  This test case is aming at testing video loading with http authentication challenge. To use this test case you should setup your apache2 http server
+  with http authentication support first.<br>
+  <p>
+    <h3>Ways to setup the apache2 http server with http authentication support</h3>
+    <ol>
+      <li>Add "AllowOverride AuthConfig" to &lt;Directory&gt; setting in your site configuration file;</li>
+      <li>Create the password file using command: <code>htpasswd -c &lt;passord_filename&gt; &lt;username&gt;</code>, and put this file into &lt;ServerRoot&gt;;</li>
+      <li>Create a directory under your server &lt;Directory&gt;, and create the file .htaccess in this directory;</li>
+      <li>Configure the .htaccess file with the following content:
+        <pre>AuthType &lt;Basic|Digest&gt;
+AuthName "Video HTTP Authentication"
+AuthUserFile &lt;/path/to/the/password/file&gt;
+Require user &lt;username&gt;</pre>
+      </li>
+      <li>Put your test video file into this directory, and restart your apache2 http server.</li>
+    </ol>
+    <p>More information about configuring apache2 http authentication, please refer to apache 2.0 document: <a href="http://httpd.apache.org/docs/2.0/howto/auth.html">Authentication, Authorization and Access Control</a>.</p>
+  </p>
+  <p>
+    <h3>Ways to test</h3>
+    <ol>
+      <li>Setup your apache2 http server and prepare the video file as mentioned above;</li>
+      <li>Enter the link pointing to the video file in the "Video Source" text area below;</li>
+      <li>Then press load button, and fill the authentication challenge pop up dialog with the credential information created before, then wait for the result.</li>
+    </ol>
+  </p>
+</p>
+<hr />
+<div>
+    Video Source:
+    <input type="url" id="url" name="videoSrc" size="100"></input><br>
+    <input type="button" value="Load" onclick="load()" />
+</div>
+<video id="video" hidden="hidden"></video>
+<script type="text/javascript">
+    function onLoaded()
+    {
+        logResult(true, "Video file successfully loaded, test case passed.");
+        endTest();
+    }
+
+    function onError()
+    {
+        failTest("Load video file error, test case failed.");
+    }
+
+    function load()
+    {
+        findMediaElement();
+        waitForEvent("loadstart");
+        waitForEvent("loadeddata", onLoaded);
+        waitForEvent("error", onError);
+
+        mediaElement.src = document.getElementById("url").value;
+        mediaElement.load();
+    }
+</script>
+</body>
+</html>
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 876b1a5..d9c12e9 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,33 @@
+2012-05-29  Jonathan Dong  <jonathan.dong@torchmobile.com.cn>
+
+        [BlackBerry] Loading media data with http authentication
+        https://bugs.webkit.org/show_bug.cgi?id=84214
+
+        Reviewed by George Staikos.
+
+        RIM PR: 117618
+        Implemented http authentication feature for media by implementing
+        two interface functions in class MediaPlayerPrivate:
+        onAuthenticationNeeded(): this function is triggered when MMR
+        engine requires http authentication. We search the CredentialStorage
+        to see if we have already stored existing credential information,
+        or challenge user to provide it.
+        OnAuthenticationAccepted(): this function is triggered when MMR
+        engine accepts the credential information, and we need to save
+        it in CredentialStorage for later use.
+
+        Internally reviewed by Max Feil <mfeil@qnx.com>.
+
+        Manual test case: blackberry/video-load-with-authentication.html
+
+        * platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.cpp:
+        (WebCore::generateProtectionSpaceFromMMRAuthChallenge):
+        (WebCore):
+        (WebCore::MediaPlayerPrivate::onAuthenticationNeeded):
+        (WebCore::MediaPlayerPrivate::onAuthenticationAccepted):
+        * platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.h:
+        (MediaPlayerPrivate):
+
 2012-05-29  MORITA Hajime  <morrita@google.com>
 
         [Shadow DOM] Node distribution should be orthogonal from node attachment
diff --git a/Source/WebCore/platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.cpp b/Source/WebCore/platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.cpp
index dc9a534..aa78676 100644
--- a/Source/WebCore/platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.cpp
+++ b/Source/WebCore/platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.cpp
@@ -22,6 +22,8 @@
 #include "MediaPlayerPrivateBlackBerry.h"
 
 #include "CookieManager.h"
+#include "Credential.h"
+#include "CredentialStorage.h"
 #include "Frame.h"
 #include "FrameView.h"
 #include "GraphicsContext.h"
@@ -30,6 +32,7 @@
 #include "HostWindow.h"
 #include "NotImplemented.h"
 #include "PlatformContextSkia.h"
+#include "ProtectionSpace.h"
 #include "RenderBox.h"
 #include "TimeRanges.h"
 #include "WebPageClient.h"
@@ -657,6 +660,50 @@
 }
 #endif
 
+static ProtectionSpace generateProtectionSpaceFromMMRAuthChallenge(const MMRAuthChallenge& authChallenge)
+{
+    KURL url(ParsedURLString, String(authChallenge.url().c_str()));
+    ASSERT(url.isValid());
+
+    return ProtectionSpace(url.host(), url.port(),
+                           static_cast<ProtectionSpaceServerType>(authChallenge.serverType()),
+                           authChallenge.realm().c_str(),
+                           static_cast<ProtectionSpaceAuthenticationScheme>(authChallenge.authScheme()));
+}
+
+bool MediaPlayerPrivate::onAuthenticationNeeded(MMRAuthChallenge& authChallenge)
+{
+    KURL url(ParsedURLString, String(authChallenge.url().c_str()));
+    if (!url.isValid())
+        return false;
+
+    ProtectionSpace protectionSpace = generateProtectionSpaceFromMMRAuthChallenge(authChallenge);
+    Credential credential = CredentialStorage::get(protectionSpace);
+    bool isConfirmed = false;
+    if (credential.isEmpty()) {
+        if (frameView() && frameView()->hostWindow())
+            isConfirmed = frameView()->hostWindow()->platformPageClient()->authenticationChallenge(url, protectionSpace, credential);
+    } else
+        isConfirmed = true;
+
+    if (isConfirmed)
+        authChallenge.setCredential(credential.user().utf8().data(), credential.password().utf8().data(), static_cast<MMRAuthChallenge::CredentialPersistence>(credential.persistence()));
+
+    return isConfirmed;
+}
+
+void MediaPlayerPrivate::onAuthenticationAccepted(const MMRAuthChallenge& authChallenge) const
+{
+    KURL url(ParsedURLString, String(authChallenge.url().c_str()));
+    if (!url.isValid())
+        return;
+
+    ProtectionSpace protectionSpace = generateProtectionSpaceFromMMRAuthChallenge(authChallenge);
+    Credential savedCredential = CredentialStorage::get(protectionSpace);
+    if (savedCredential.isEmpty())
+        CredentialStorage::set(Credential(authChallenge.username().c_str(), authChallenge.password().c_str(), static_cast<CredentialPersistence>(authChallenge.persistence())), protectionSpace, url);
+}
+
 int MediaPlayerPrivate::showErrorDialog(MMRPlayer::Error type)
 {
     using namespace BlackBerry::WebKit;
diff --git a/Source/WebCore/platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.h b/Source/WebCore/platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.h
index bcc5473..36c36f8 100644
--- a/Source/WebCore/platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.h
+++ b/Source/WebCore/platform/graphics/blackberry/MediaPlayerPrivateBlackBerry.h
@@ -130,6 +130,8 @@
 #if USE(ACCELERATED_COMPOSITING)
     virtual void onBuffering(bool);
 #endif
+    virtual bool onAuthenticationNeeded(BlackBerry::Platform::MMRAuthChallenge&);
+    virtual void onAuthenticationAccepted(const BlackBerry::Platform::MMRAuthChallenge&) const;
 
     virtual bool isFullscreen() const;
     virtual bool isElementPaused() const;