11156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber/*
21156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * Copyright (C) 2011 The Android Open Source Project
31156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber *
41156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
51156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * you may not use this file except in compliance with the License.
61156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * You may obtain a copy of the License at
71156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber *
81156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
91156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber *
101156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * Unless required by applicable law or agreed to in writing, software
111156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
121156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * See the License for the specific language governing permissions and
141156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * limitations under the License.
151156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber */
161156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
171156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#ifndef SUPPORT_H_
181156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
191156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#define SUPPORT_H_
201156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
211156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include <assert.h>
221156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
231156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include "net/base/net_log.h"
241156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include "net/url_request/url_request.h"
251156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include "net/url_request/url_request_context.h"
261156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include "net/base/android_network_library.h"
271156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include "net/base/io_buffer.h"
281156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
291156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include <utils/KeyedVector.h>
301156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include <utils/String8.h>
311156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
321156dc913a5ba7b2bc86489468d4914430f03d14Andreas Hubernamespace android {
331156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
341156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberstruct SfNetLog : public net::NetLog {
351156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    SfNetLog();
361156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
371156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual void AddEntry(
381156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            EventType type,
391156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const base::TimeTicks &time,
401156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const Source &source,
411156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            EventPhase phase,
421156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            EventParameters *params);
431156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
441156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual uint32 NextID();
451156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual LogLevel GetLogLevel() const;
461156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
471156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberprivate:
481156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    uint32 mNextID;
491156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
501156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(SfNetLog);
511156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber};
521156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
5364125da2f3b76f2d88bb757cb6bf5bd5e29a0639Kristian Monsenstruct SfRequestContext : public net::URLRequestContext {
541156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    SfRequestContext();
551156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
561156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual const std::string &GetUserAgent(const GURL &url) const;
571156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
581156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberprivate:
591156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    std::string mUserAgent;
601156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
611156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(SfRequestContext);
621156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber};
631156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
641156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber// This is required for https support, we don't really verify certificates,
651156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber// we accept anything...
661156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberstruct SfNetworkLibrary : public net::AndroidNetworkLibrary {
671156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    SfNetworkLibrary();
681156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
691156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual VerifyResult VerifyX509CertChain(
701156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const std::vector<std::string>& cert_chain,
711156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const std::string& hostname,
721156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const std::string& auth_type);
731156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
741156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberprivate:
751156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(SfNetworkLibrary);
761156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber};
771156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
781156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberstruct ChromiumHTTPDataSource;
791156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
80cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsenstruct SfDelegate : public net::URLRequest::Delegate {
811156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    SfDelegate();
821156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual ~SfDelegate();
831156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
841156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    void initiateConnection(
851156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const char *uri,
861156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const KeyedVector<String8, String8> *headers,
871156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            off64_t offset);
881156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
891156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    void initiateDisconnect();
901156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    void initiateRead(void *data, size_t size);
911156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
921156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    void setOwner(ChromiumHTTPDataSource *mOwner);
931156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
94a23456b306f35b9ecf973bf5818ca39295e9e029Ashish Sharma    // Gets the UID of the calling process
95a23456b306f35b9ecf973bf5818ca39295e9e029Ashish Sharma    bool getUID(uid_t *uid) const;
96a23456b306f35b9ecf973bf5818ca39295e9e029Ashish Sharma
97a23456b306f35b9ecf973bf5818ca39295e9e029Ashish Sharma    void setUID(uid_t uid);
98a23456b306f35b9ecf973bf5818ca39295e9e029Ashish Sharma
991156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual void OnReceivedRedirect(
100cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen            net::URLRequest *request, const GURL &new_url, bool *defer_redirect);
1011156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1021156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual void OnAuthRequired(
103cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen            net::URLRequest *request, net::AuthChallengeInfo *auth_info);
1041156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1051156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual void OnCertificateRequested(
106cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen            net::URLRequest *request, net::SSLCertRequestInfo *cert_request_info);
1071156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1081156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual void OnSSLCertificateError(
109cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen            net::URLRequest *request, int cert_error, net::X509Certificate *cert);
1101156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
111cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen    virtual void OnGetCookies(net::URLRequest *request, bool blocked_by_policy);
1121156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1131156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual void OnSetCookie(
114cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen            net::URLRequest *request,
1151156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const std::string &cookie_line,
1161156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const net::CookieOptions &options,
1171156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            bool blocked_by_policy);
1181156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
119cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen    virtual void OnResponseStarted(net::URLRequest *request);
1201156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
121cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen    virtual void OnReadCompleted(net::URLRequest *request, int bytes_read);
1221156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1231156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberprivate:
1241156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    typedef Delegate inherited;
1251156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1261156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    ChromiumHTTPDataSource *mOwner;
1271156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
128cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen    net::URLRequest *mURLRequest;
1291156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    scoped_refptr<net::IOBufferWithSize> mReadBuffer;
1301156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1311156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    size_t mNumBytesRead;
1321156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    size_t mNumBytesTotal;
1331156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    void *mDataDestination;
1341156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1351156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    bool mRangeRequested;
1361156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    bool mAtEOS;
1371156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
138cd45ccddb00662f2af8409b895fbcdd0434da8b1Kristian Monsen    void readMore(net::URLRequest *request);
1391156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1401156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    static void OnInitiateConnectionWrapper(
1411156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            SfDelegate *me,
1421156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            GURL url,
1431156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const KeyedVector<String8, String8> *headers,
1441156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            off64_t offset);
1451156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1461156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    static void OnInitiateDisconnectWrapper(SfDelegate *me);
1471156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1481156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    static void OnInitiateReadWrapper(
1491156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            SfDelegate *me, void *data, size_t size);
1501156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1511156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    void onInitiateConnection(
1521156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const GURL &url,
1531156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const KeyedVector<String8, String8> *headers,
1541156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            off64_t offset);
1551156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1561156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    void onInitiateDisconnect();
1571156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    void onInitiateRead(void *data, size_t size);
1581156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1591156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(SfDelegate);
1601156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber};
1611156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1621156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber}  // namespace android
1631156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
1641156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#endif  // SUPPORT_H_
165