Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright 2009, The Android Open Source Project
      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  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * 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 THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "ResourceHandle.h"
     28 
     29 #include "CachedResourceLoader.h"
     30 #include "DocumentLoader.h"
     31 #include "Frame.h"
     32 #include "FrameLoader.h"
     33 #include "MainResourceLoader.h"
     34 #include "NotImplemented.h"
     35 #include "ResourceHandleClient.h"
     36 #include "ResourceHandleInternal.h"
     37 #include "ResourceLoaderAndroid.h"
     38 #include "Settings.h"
     39 #include <wtf/text/CString.h>
     40 
     41 namespace WebCore {
     42 
     43 ResourceHandleInternal::~ResourceHandleInternal()
     44 {
     45 }
     46 
     47 ResourceHandle::~ResourceHandle()
     48 {
     49 }
     50 
     51 bool ResourceHandle::start(NetworkingContext* context)
     52 {
     53     if (!context || !context->isValid())
     54         return false;
     55 
     56     MainResourceLoader* mainLoader = context->mainResourceLoader();
     57     bool isMainResource = static_cast<void*>(mainLoader) == static_cast<void*>(client());
     58     RefPtr<ResourceLoaderAndroid> loader = ResourceLoaderAndroid::start(this, d->m_firstRequest, context->frameLoaderClient(), isMainResource, false);
     59 
     60     if (loader) {
     61         d->m_loader = loader.release();
     62         return true;
     63     }
     64 
     65     return false;
     66 }
     67 
     68 void ResourceHandle::cancel()
     69 {
     70     if (d->m_loader)
     71         d->m_loader->cancel();
     72 }
     73 
     74 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
     75 {
     76     notImplemented();
     77     return 0;
     78 }
     79 
     80 bool ResourceHandle::supportsBufferedData()
     81 {
     82     // We don't support buffering data on the native side.
     83     notImplemented();
     84     return false;
     85 }
     86 
     87 #if PLATFORM(ANDROID)
     88 // TODO: this needs upstreaming.
     89 void ResourceHandle::pauseLoad(bool pause)
     90 {
     91     if (d->m_loader)
     92         d->m_loader->pauseLoad(pause);
     93 }
     94 #endif
     95 
     96 void ResourceHandle::platformSetDefersLoading(bool)
     97 {
     98     notImplemented();
     99 }
    100 
    101 // This static method is called to check to see if a POST response is in
    102 // the cache.
    103 bool ResourceHandle::willLoadFromCache(ResourceRequest& request, Frame*)
    104 {
    105     // set the cache policy correctly, copied from
    106     // network/mac/ResourceHandleMac.mm
    107     request.setCachePolicy(ReturnCacheDataDontLoad);
    108     FormData* formData = request.httpBody();
    109     return ResourceLoaderAndroid::willLoadFromCache(request.url(), formData ? formData->identifier() : 0);
    110 }
    111 
    112 bool ResourceHandle::loadsBlocked()
    113 {
    114     // FIXME, need to check whether connection pipe is blocked.
    115     // return false for now
    116     return false;
    117 }
    118 
    119 // Class to handle synchronized loading of resources.
    120 class SyncLoader : public ResourceHandleClient {
    121 public:
    122     SyncLoader(ResourceError& error, ResourceResponse& response, WTF::Vector<char>& data)
    123     {
    124         m_error = &error;
    125         m_response = &response;
    126         m_data = &data;
    127     }
    128     ~SyncLoader() {}
    129 
    130     virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
    131     {
    132         *m_response = response;
    133     }
    134 
    135     virtual void didReceiveData(ResourceHandle*, const char* data, int len, int encodedDataLength)
    136     {
    137         m_data->append(data, len);
    138     }
    139 
    140     virtual void didFail(ResourceHandle*, const ResourceError& error)
    141     {
    142         *m_error = error;
    143     }
    144 
    145 private:
    146     ResourceError* m_error;
    147     ResourceResponse* m_response;
    148     WTF::Vector<char>* m_data;
    149 };
    150 
    151 void ResourceHandle::loadResourceSynchronously(NetworkingContext* context, const ResourceRequest& request,
    152         StoredCredentials, ResourceError& error, ResourceResponse& response, WTF::Vector<char>& data)
    153 {
    154     SyncLoader s(error, response, data);
    155     RefPtr<ResourceHandle> h = adoptRef(new ResourceHandle(request, &s, false, false));
    156     // This blocks until the load is finished.
    157     // Use the request owned by the ResourceHandle. This has had the username
    158     // and password (if present) stripped from the URL in
    159     // ResourceHandleInternal::ResourceHandleInternal(). This matches the
    160     // behaviour in the asynchronous case.
    161     ResourceLoaderAndroid::start(h.get(), request, context->frameLoaderClient(), false, true);
    162 }
    163 
    164 } // namespace WebCore
    165