support.h revision a23456b306f35b9ecf973bf5818ca39295e9e029
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/String8.h>
31
32namespace android {
33
34struct SfNetLog : public net::NetLog {
35    SfNetLog();
36
37    virtual void AddEntry(
38            EventType type,
39            const base::TimeTicks &time,
40            const Source &source,
41            EventPhase phase,
42            EventParameters *params);
43
44    virtual uint32 NextID();
45    virtual LogLevel GetLogLevel() const;
46
47private:
48    uint32 mNextID;
49
50    DISALLOW_EVIL_CONSTRUCTORS(SfNetLog);
51};
52
53struct SfRequestContext : public net::URLRequestContext {
54    SfRequestContext();
55
56    virtual const std::string &GetUserAgent(const GURL &url) const;
57
58private:
59    std::string mUserAgent;
60
61    DISALLOW_EVIL_CONSTRUCTORS(SfRequestContext);
62};
63
64// This is required for https support, we don't really verify certificates,
65// we accept anything...
66struct SfNetworkLibrary : public net::AndroidNetworkLibrary {
67    SfNetworkLibrary();
68
69    virtual VerifyResult VerifyX509CertChain(
70            const std::vector<std::string>& cert_chain,
71            const std::string& hostname,
72            const std::string& auth_type);
73
74private:
75    DISALLOW_EVIL_CONSTRUCTORS(SfNetworkLibrary);
76};
77
78struct ChromiumHTTPDataSource;
79
80struct SfDelegate : public net::URLRequest::Delegate {
81    SfDelegate();
82    virtual ~SfDelegate();
83
84    void initiateConnection(
85            const char *uri,
86            const KeyedVector<String8, String8> *headers,
87            off64_t offset);
88
89    void initiateDisconnect();
90    void initiateRead(void *data, size_t size);
91
92    void setOwner(ChromiumHTTPDataSource *mOwner);
93
94    // Gets the UID of the calling process
95    bool getUID(uid_t *uid) const;
96
97    void setUID(uid_t uid);
98
99    virtual void OnReceivedRedirect(
100            net::URLRequest *request, const GURL &new_url, bool *defer_redirect);
101
102    virtual void OnAuthRequired(
103            net::URLRequest *request, net::AuthChallengeInfo *auth_info);
104
105    virtual void OnCertificateRequested(
106            net::URLRequest *request, net::SSLCertRequestInfo *cert_request_info);
107
108    virtual void OnSSLCertificateError(
109            net::URLRequest *request, int cert_error, net::X509Certificate *cert);
110
111    virtual void OnGetCookies(net::URLRequest *request, bool blocked_by_policy);
112
113    virtual void OnSetCookie(
114            net::URLRequest *request,
115            const std::string &cookie_line,
116            const net::CookieOptions &options,
117            bool blocked_by_policy);
118
119    virtual void OnResponseStarted(net::URLRequest *request);
120
121    virtual void OnReadCompleted(net::URLRequest *request, int bytes_read);
122
123private:
124    typedef Delegate inherited;
125
126    ChromiumHTTPDataSource *mOwner;
127
128    net::URLRequest *mURLRequest;
129    scoped_refptr<net::IOBufferWithSize> mReadBuffer;
130
131    size_t mNumBytesRead;
132    size_t mNumBytesTotal;
133    void *mDataDestination;
134
135    bool mRangeRequested;
136    bool mAtEOS;
137
138    void readMore(net::URLRequest *request);
139
140    static void OnInitiateConnectionWrapper(
141            SfDelegate *me,
142            GURL url,
143            const KeyedVector<String8, String8> *headers,
144            off64_t offset);
145
146    static void OnInitiateDisconnectWrapper(SfDelegate *me);
147
148    static void OnInitiateReadWrapper(
149            SfDelegate *me, void *data, size_t size);
150
151    void onInitiateConnection(
152            const GURL &url,
153            const KeyedVector<String8, String8> *headers,
154            off64_t offset);
155
156    void onInitiateDisconnect();
157    void onInitiateRead(void *data, size_t size);
158
159    DISALLOW_EVIL_CONSTRUCTORS(SfDelegate);
160};
161
162}  // namespace android
163
164#endif  // SUPPORT_H_
165