Home | History | Annotate | Download | only in WebCoreSupport
      1 /*
      2  * Copyright 2010, 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 #ifndef WebRequest_h
     27 #define WebRequest_h
     28 
     29 #include "ChromiumIncludes.h"
     30 #include <wtf/Vector.h>
     31 
     32 class MessageLoop;
     33 
     34 namespace android {
     35 
     36 enum LoadState {
     37     Created,
     38     Started,
     39     Response,
     40     GotData,
     41     Cancelled,
     42     Finished,
     43     Deleted
     44 };
     45 
     46 class UrlInterceptResponse;
     47 class WebFrame;
     48 class WebRequestContext;
     49 class WebResourceRequest;
     50 class WebUrlLoaderClient;
     51 
     52 // All methods in this class must be called on the io thread
     53 class WebRequest : public net::URLRequest::Delegate, public base::RefCountedThreadSafe<WebRequest> {
     54 public:
     55     WebRequest(WebUrlLoaderClient*, const WebResourceRequest&);
     56 
     57     // If this is an android specific url or the application wants to load
     58     // custom data, we load the data through an input stream.
     59     // Used for:
     60     // - file:///android_asset
     61     // - file:///android_res
     62     // - content://
     63     WebRequest(WebUrlLoaderClient*, const WebResourceRequest&, UrlInterceptResponse* intercept);
     64 
     65     // Optional, but if used has to be called before start
     66     void appendBytesToUpload(Vector<char>* data);
     67     void appendFileToUpload(const std::string& filename);
     68 
     69     void setRequestContext(WebRequestContext* context);
     70     void start();
     71     void cancel();
     72     void pauseLoad(bool pause);
     73 
     74     // From URLRequest::Delegate
     75     virtual void OnReceivedRedirect(net::URLRequest*, const GURL&, bool* deferRedirect);
     76     virtual void OnResponseStarted(net::URLRequest*);
     77     virtual void OnReadCompleted(net::URLRequest*, int bytesRead);
     78     virtual void OnAuthRequired(net::URLRequest*, net::AuthChallengeInfo*);
     79     virtual void OnSSLCertificateError(net::URLRequest* request, int cert_error, net::X509Certificate* cert);
     80     virtual void OnCertificateRequested(net::URLRequest* request, net::SSLCertRequestInfo* cert_request_info);
     81 
     82     // Methods called during a request by the UI code (via WebUrlLoaderClient).
     83     void setAuth(const string16& username, const string16& password);
     84     void cancelAuth();
     85     void followDeferredRedirect();
     86     void proceedSslCertError();
     87     void cancelSslCertError(int cert_error);
     88     void sslClientCert(EVP_PKEY* pkey, scoped_refptr<net::X509Certificate> chain);
     89 
     90     const std::string& getUrl() const;
     91     const std::string& getUserAgent() const;
     92 
     93     void setSync(bool sync) { m_isSync = sync; }
     94 private:
     95     void startReading();
     96     bool read(int* bytesRead);
     97 
     98     friend class base::RefCountedThreadSafe<WebRequest>;
     99     virtual ~WebRequest();
    100     void handleDataURL(GURL);
    101     void handleBrowserURL(GURL);
    102     void handleInterceptedURL();
    103     void finish(bool success);
    104     void updateLoadFlags(int& loadFlags);
    105 
    106     scoped_refptr<WebUrlLoaderClient> m_urlLoader;
    107     OwnPtr<net::URLRequest> m_request;
    108     scoped_refptr<net::IOBuffer> m_networkBuffer;
    109     scoped_ptr<UrlInterceptResponse> m_interceptResponse;
    110     bool m_androidUrl;
    111     std::string m_url;
    112     std::string m_userAgent;
    113     LoadState m_loadState;
    114     int m_authRequestCount;
    115     int m_cacheMode;
    116     ScopedRunnableMethodFactory<WebRequest> m_runnableFactory;
    117     bool m_wantToPause;
    118     bool m_isPaused;
    119     bool m_isSync;
    120 #ifdef LOG_REQUESTS
    121     time_t m_startTime;
    122 #endif
    123 };
    124 
    125 } // namespace android
    126 
    127 #endif
    128