Home | History | Annotate | Download | only in Downloads
      1 /*
      2  * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "Download.h"
     28 
     29 #include "AuthenticationManager.h"
     30 #include "Connection.h"
     31 #include "DataReference.h"
     32 #include "DownloadProxyMessages.h"
     33 #include "DownloadManager.h"
     34 #include "SandboxExtension.h"
     35 #include "WebCoreArgumentCoders.h"
     36 #include "WebProcess.h"
     37 
     38 using namespace WebCore;
     39 
     40 namespace WebKit {
     41 
     42 PassOwnPtr<Download> Download::create(uint64_t downloadID, const ResourceRequest& request)
     43 {
     44     return adoptPtr(new Download(downloadID, request));
     45 }
     46 
     47 Download::Download(uint64_t downloadID, const ResourceRequest& request)
     48     : m_downloadID(downloadID)
     49     , m_request(request)
     50 #if USE(CFNETWORK)
     51     , m_allowOverwrite(false)
     52 #endif
     53 {
     54     ASSERT(m_downloadID);
     55 
     56     WebProcess::shared().disableTermination();
     57 }
     58 
     59 Download::~Download()
     60 {
     61     platformInvalidate();
     62 
     63     WebProcess::shared().enableTermination();
     64 }
     65 
     66 CoreIPC::Connection* Download::connection() const
     67 {
     68     return WebProcess::shared().connection();
     69 }
     70 
     71 void Download::didStart()
     72 {
     73     send(Messages::DownloadProxy::DidStart(m_request));
     74 }
     75 
     76 void Download::didReceiveAuthenticationChallenge(const AuthenticationChallenge& authenticationChallenge)
     77 {
     78     AuthenticationManager::shared().didReceiveAuthenticationChallenge(this, authenticationChallenge);
     79 }
     80 
     81 void Download::didReceiveResponse(const ResourceResponse& response)
     82 {
     83     send(Messages::DownloadProxy::DidReceiveResponse(response));
     84 }
     85 
     86 void Download::didReceiveData(uint64_t length)
     87 {
     88     send(Messages::DownloadProxy::DidReceiveData(length));
     89 }
     90 
     91 bool Download::shouldDecodeSourceDataOfMIMEType(const String& mimeType)
     92 {
     93     bool result;
     94     if (!sendSync(Messages::DownloadProxy::ShouldDecodeSourceDataOfMIMEType(mimeType), Messages::DownloadProxy::ShouldDecodeSourceDataOfMIMEType::Reply(result)))
     95         return true;
     96 
     97     return result;
     98 }
     99 
    100 String Download::retrieveDestinationWithSuggestedFilename(const String& filename, bool& allowOverwrite)
    101 {
    102     String destination;
    103     SandboxExtension::Handle sandboxExtensionHandle;
    104     if (!sendSync(Messages::DownloadProxy::DecideDestinationWithSuggestedFilename(filename), Messages::DownloadProxy::DecideDestinationWithSuggestedFilename::Reply(destination, allowOverwrite, sandboxExtensionHandle)))
    105         return String();
    106 
    107     m_sandboxExtension = SandboxExtension::create(sandboxExtensionHandle);
    108     if (m_sandboxExtension)
    109         m_sandboxExtension->consume();
    110 
    111     return destination;
    112 }
    113 
    114 String Download::decideDestinationWithSuggestedFilename(const String& filename, bool& allowOverwrite)
    115 {
    116     String destination = retrieveDestinationWithSuggestedFilename(filename, allowOverwrite);
    117 
    118     didDecideDestination(destination, allowOverwrite);
    119 
    120     return destination;
    121 }
    122 
    123 void Download::didCreateDestination(const String& path)
    124 {
    125     send(Messages::DownloadProxy::DidCreateDestination(path));
    126 }
    127 
    128 void Download::didFinish()
    129 {
    130     platformDidFinish();
    131 
    132     send(Messages::DownloadProxy::DidFinish());
    133 
    134     if (m_sandboxExtension)
    135         m_sandboxExtension->invalidate();
    136     DownloadManager::shared().downloadFinished(this);
    137 }
    138 
    139 void Download::didFail(const ResourceError& error, const CoreIPC::DataReference& resumeData)
    140 {
    141     send(Messages::DownloadProxy::DidFail(error, resumeData));
    142 
    143     if (m_sandboxExtension)
    144         m_sandboxExtension->invalidate();
    145     DownloadManager::shared().downloadFinished(this);
    146 }
    147 
    148 void Download::didCancel(const CoreIPC::DataReference& resumeData)
    149 {
    150     send(Messages::DownloadProxy::DidCancel(resumeData));
    151 
    152     if (m_sandboxExtension)
    153         m_sandboxExtension->invalidate();
    154     DownloadManager::shared().downloadFinished(this);
    155 }
    156 
    157 } // namespace WebKit
    158