1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SUPPORT_H_
18
19#define SUPPORT_H_
20
21#include <assert.h>
22
23#include "net/base/net_log.h"
24#include "net/url_request/url_request.h"
25#include "net/url_request/url_request_context.h"
26#include "net/base/android_network_library.h"
27#include "net/base/io_buffer.h"
28
29#include <utils/KeyedVector.h>
30#include <utils/Mutex.h>
31#include <utils/String8.h>
32
33namespace net {
34    struct ProxyConfigServiceAndroid;
35};
36
37namespace android {
38
39struct SfNetLog : public net::NetLog {
40    SfNetLog();
41
42    virtual void AddEntry(
43            EventType type,
44            const base::TimeTicks &time,
45            const Source &source,
46            EventPhase phase,
47            EventParameters *params);
48
49    virtual uint32 NextID();
50    virtual LogLevel GetLogLevel() const;
51
52private:
53    uint32 mNextID;
54
55    DISALLOW_EVIL_CONSTRUCTORS(SfNetLog);
56};
57
58struct SfRequestContext : public net::URLRequestContext {
59    SfRequestContext();
60
61    virtual const std::string &GetUserAgent(const GURL &url) const;
62
63    status_t updateProxyConfig(
64            const char *host, int32_t port, const char *exclusionList);
65
66private:
67    Mutex mProxyConfigLock;
68
69    std::string mUserAgent;
70    net::ProxyConfigServiceAndroid *mProxyConfigService;
71
72    DISALLOW_EVIL_CONSTRUCTORS(SfRequestContext);
73};
74
75// This is required for https support, we don't really verify certificates,
76// we accept anything...
77struct SfNetworkLibrary : public net::AndroidNetworkLibrary {
78    SfNetworkLibrary();
79
80    virtual VerifyResult VerifyX509CertChain(
81            const std::vector<std::string>& cert_chain,
82            const std::string& hostname,
83            const std::string& auth_type);
84
85private:
86    DISALLOW_EVIL_CONSTRUCTORS(SfNetworkLibrary);
87};
88
89struct ChromiumHTTPDataSource;
90
91struct SfDelegate : public net::URLRequest::Delegate {
92    SfDelegate();
93    virtual ~SfDelegate();
94
95    void initiateConnection(
96            const char *uri,
97            const KeyedVector<String8, String8> *headers,
98            off64_t offset);
99
100    void initiateDisconnect();
101    void initiateRead(void *data, size_t size);
102
103    void setOwner(ChromiumHTTPDataSource *mOwner);
104
105    // Gets the UID of the calling process
106    bool getUID(uid_t *uid) const;
107
108    void setUID(uid_t uid);
109
110    virtual void OnReceivedRedirect(
111            net::URLRequest *request, const GURL &new_url, bool *defer_redirect);
112
113    virtual void OnAuthRequired(
114            net::URLRequest *request, net::AuthChallengeInfo *auth_info);
115
116    virtual void OnCertificateRequested(
117            net::URLRequest *request, net::SSLCertRequestInfo *cert_request_info);
118
119    virtual void OnSSLCertificateError(
120            net::URLRequest *request, int cert_error, net::X509Certificate *cert);
121
122    virtual void OnGetCookies(net::URLRequest *request, bool blocked_by_policy);
123
124    virtual void OnSetCookie(
125            net::URLRequest *request,
126            const std::string &cookie_line,
127            const net::CookieOptions &options,
128            bool blocked_by_policy);
129
130    virtual void OnResponseStarted(net::URLRequest *request);
131
132    virtual void OnReadCompleted(net::URLRequest *request, int bytes_read);
133
134    static status_t UpdateProxyConfig(
135            const char *host, int32_t port, const char *exclusionList);
136
137private:
138    typedef Delegate inherited;
139
140    ChromiumHTTPDataSource *mOwner;
141
142    net::URLRequest *mURLRequest;
143    scoped_refptr<net::IOBufferWithSize> mReadBuffer;
144
145    size_t mNumBytesRead;
146    size_t mNumBytesTotal;
147    void *mDataDestination;
148
149    bool mRangeRequested;
150    bool mAtEOS;
151
152    void readMore(net::URLRequest *request);
153
154    static void OnInitiateConnectionWrapper(
155            SfDelegate *me,
156            GURL url,
157            const KeyedVector<String8, String8> *headers,
158            off64_t offset);
159
160    static void OnInitiateDisconnectWrapper(SfDelegate *me);
161
162    static void OnInitiateReadWrapper(
163            SfDelegate *me, void *data, size_t size);
164
165    void onInitiateConnection(
166            const GURL &url,
167            const KeyedVector<String8, String8> *headers,
168            off64_t offset);
169
170    void onInitiateDisconnect();
171    void onInitiateRead(void *data, size_t size);
172
173    DISALLOW_EVIL_CONSTRUCTORS(SfDelegate);
174};
175
176}  // namespace android
177
178#endif  // SUPPORT_H_
179