[haiku-webkit-commits] r577 - in webkit/trunk/WebKit/haiku: API WebPositive

  • From: noreply@xxxxxxxxxxxx
  • To: haiku-webkit-commits@xxxxxxxxxxxxx
  • Date: Thu, 10 Mar 2011 19:00:43 +0100 (CET)

Author: phoudoin
Date: Thu Mar 10 19:00:40 2011
New Revision: 577
URL: http://webpositive.haiku-os.org/changeset/577

Log:
* Don't trust anymore provided MIME type (if any): use instead
the registrar's type guessing feature, which we can (and should) trust
way more.
* Notify back the download objet when it's file location has changed.

Modified:
   webkit/trunk/WebKit/haiku/API/WebDownload.cpp
   webkit/trunk/WebKit/haiku/API/WebDownload.h
   webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.cpp
   webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.h
   webkit/trunk/WebKit/haiku/WebPositive/DownloadProgressView.cpp

Modified: webkit/trunk/WebKit/haiku/API/WebDownload.cpp
==============================================================================
--- webkit/trunk/WebKit/haiku/API/WebDownload.cpp       Wed Mar  2 19:15:57 
2011        (r576)
+++ webkit/trunk/WebKit/haiku/API/WebDownload.cpp       Thu Mar 10 19:00:40 
2011        (r577)
@@ -22,7 +22,7 @@
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include "config.h"
@@ -65,6 +65,11 @@
     fData->start(path);
 }
 
+void BWebDownload::HasMovedTo(const BPath& path)
+{
+       fData->hasMovedTo(path);
+}
+
 void BWebDownload::Cancel()
 {
        // This is invoked from the client, within any thread, so we need to

Modified: webkit/trunk/WebKit/haiku/API/WebDownload.h
==============================================================================
--- webkit/trunk/WebKit/haiku/API/WebDownload.h Wed Mar  2 19:15:57 2011        
(r576)
+++ webkit/trunk/WebKit/haiku/API/WebDownload.h Thu Mar 10 19:00:40 2011        
(r577)
@@ -59,6 +59,8 @@
                        void                            Start(const BPath& 
path);
                        void                            Cancel();
 
+                       void                            HasMovedTo(const BPath& 
path);
+
                        void                            
SetProgressListener(const BMessenger& listener);
 
                        const BString&          URL() const;

Modified: webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.cpp
==============================================================================
--- webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.cpp        Wed Mar  2 
19:15:57 2011        (r576)
+++ webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.cpp        Thu Mar 10 
19:00:40 2011        (r577)
@@ -22,7 +22,7 @@
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include "config.h"
@@ -39,11 +39,14 @@
 #include <Entry.h>
 #include <Message.h>
 #include <Messenger.h>
+#include <MimeType.h>
 #include <NodeInfo.h>
 #include <stdio.h>
 
 namespace BPrivate {
 
+static const int kMaxMimeTypeGuessTries        = 5;
+
 WebDownloadPrivate::WebDownloadPrivate(const ResourceRequest& request)
     : m_webDownload(0)
     , m_resourceHandle(ResourceHandle::create(request, this, 0, false, false))
@@ -52,7 +55,8 @@
     , m_url(request.url().string())
     , m_path("/boot/home/Desktop/")
     , m_filename("Download")
-    , m_mimeType("application/octet-stream")
+    , m_mimeType()
+    , m_mimeTypeGuessTries(kMaxMimeTypeGuessTries)
     , m_file()
     , m_lastProgressReportTime(0)
 {
@@ -67,7 +71,8 @@
     , m_url()
     , m_path("/boot/home/Desktop/")
     , m_filename("Download")
-    , m_mimeType("application/octet-stream")
+    , m_mimeType()
+    , m_mimeTypeGuessTries(kMaxMimeTypeGuessTries)
     , m_file()
     , m_lastProgressReportTime(0)
 {
@@ -87,8 +92,19 @@
                url.removeFragmentIdentifier();
             m_filename = 
decodeURLEscapeSequences(url.lastPathComponent()).utf8().data();
         }
-        if (response.mimeType().length())
-            m_mimeType = response.mimeType();
+        if (response.mimeType().length()) {
+               // Do some checks, as no mime type yet is always better
+               // than set an invalid one
+               BString mimeType = response.mimeType();
+               BMimeType type(mimeType);
+               BMimeType superType;
+               if (type.IsValid() && type.GetSupertype(&superType) == B_OK
+                       && superType.IsValid()
+                       && strchr(mimeType, '*') == NULL) {
+                   m_mimeType = mimeType;
+               }
+               }
+
         m_expectedSize = response.expectedContentLength();
     }
 
@@ -107,6 +123,22 @@
     }
     m_currentSize += length;
 
+    if (m_currentSize > 0 && m_mimeTypeGuessTries > 0) {
+       // Try to guess the MIME type from its actual content
+       BMimeType type;
+       entry_ref ref;
+       BEntry entry(m_path.Path());
+       entry.GetRef(&ref);
+
+               if (BMimeType::GuessMimeType(&ref, &type) == B_OK
+                       && type.Type() != B_FILE_MIME_TYPE) {
+               BNodeInfo info(&m_file);
+                       info.SetType(type.Type());
+                       m_mimeTypeGuessTries = -1;
+       } else
+               m_mimeTypeGuessTries--;
+    }
+
     // FIXME: Report total size update, if m_currentSize greater than previous 
total size
     BMessage message(B_DOWNLOAD_PROGRESS);
     message.AddFloat("progress", m_currentSize * 100.0 / m_expectedSize);
@@ -150,6 +182,11 @@
                m_path = path;
 }
 
+void WebDownloadPrivate::hasMovedTo(const BPath& path)
+{
+       m_path = path;
+}
+
 void WebDownloadPrivate::cancel()
 {
     m_resourceHandle->cancel();
@@ -164,6 +201,13 @@
 
 void WebDownloadPrivate::handleFinished(WebCore::ResourceHandle* handle, 
uint32 status)
 {
+    if (m_mimeTypeGuessTries != -1 && m_mimeType.Length() > 0) {
+       // In last resort, use the MIME type provided
+       // by the response, which pass our validation
+               BNodeInfo info(&m_file);
+               info.SetType(m_mimeType);
+    }
+
        handle->setClient(0);
        if (m_progressListener.IsValid()) {
         BMessage message(B_DOWNLOAD_REMOVED);
@@ -180,11 +224,8 @@
     // Don't overwrite existing files
     findAvailableFilename();
 
-       if (m_file.SetTo(m_path.Path(), B_CREATE_FILE | B_ERASE_FILE | 
B_WRITE_ONLY) == B_OK) {
-               BNodeInfo info(&m_file);
-               info.SetType(m_mimeType.String());
+       if (m_file.SetTo(m_path.Path(), B_CREATE_FILE | B_ERASE_FILE | 
B_WRITE_ONLY) == B_OK)
                m_file.WriteAttrString("META:url", &m_url);
-       }
 
     if (m_progressListener.IsValid()) {
         BMessage message(B_DOWNLOAD_STARTED);
@@ -212,7 +253,7 @@
 
         // Add i to file name before the extension
         char num[10];
-        snprintf(num, sizeof(num), "-%d", i);
+        snprintf(num, sizeof(num), "-%ld", i);
         baseName.Append(num).Append(extension);
         fileName = baseName;
         filePath = m_path;

Modified: webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.h
==============================================================================
--- webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.h  Wed Mar  2 19:15:57 
2011        (r576)
+++ webkit/trunk/WebKit/haiku/API/WebDownloadPrivate.h  Thu Mar 10 19:00:40 
2011        (r577)
@@ -68,6 +68,7 @@
 
     void setDownload(BWebDownload*);
     void start(const BPath& path);
+    void hasMovedTo(const BPath& path);
     void cancel();
     void setProgressListener(const BMessenger&);
 
@@ -86,13 +87,13 @@
     BWebDownload* m_webDownload;
 
     RefPtr<ResourceHandle> m_resourceHandle;
-    BString m_suggestedFileName;
     off_t m_currentSize;
     off_t m_expectedSize;
     BString m_url;
     BPath m_path;
     BString m_filename;
     BString m_mimeType;
+    int m_mimeTypeGuessTries;
     BFile m_file;
     bigtime_t m_lastProgressReportTime;
 

Modified: webkit/trunk/WebKit/haiku/WebPositive/DownloadProgressView.cpp
==============================================================================
--- webkit/trunk/WebKit/haiku/WebPositive/DownloadProgressView.cpp      Wed Mar 
 2 19:15:57 2011        (r576)
+++ webkit/trunk/WebKit/haiku/WebPositive/DownloadProgressView.cpp      Thu Mar 
10 19:00:40 2011        (r577)
@@ -451,6 +451,7 @@
                                        BEntry entry(&ref);
                                        if (entry.GetPath(&fPath) != B_OK)
                                                break;
+
                                        // Find out if the directory is the 
Trash for this
                                        // volume
                                        char trashPath[B_PATH_NAME_LENGTH];
@@ -474,6 +475,10 @@
                                                }
                                        }
 
+                                       // Inform download of the new path
+                                       if (fDownload)
+                                               fDownload->HasMovedTo(fPath);
+
                                        float value = 
fStatusBar->CurrentValue();
                                        fStatusBar->Reset(name);
                                        fStatusBar->SetTo(value);

Other related posts: