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 CHROME_HTTP_DATA_SOURCE_H_
18
19#define CHROME_HTTP_DATA_SOURCE_H_
20
21#include <media/stagefright/foundation/AString.h>
22#include <utils/threads.h>
23
24#include "HTTPBase.h"
25
26namespace android {
27
28struct SfDelegate;
29
30struct ChromiumHTTPDataSource : public HTTPBase {
31    ChromiumHTTPDataSource(uint32_t flags = 0);
32
33    virtual status_t connect(
34            const char *uri,
35            const KeyedVector<String8, String8> *headers = NULL,
36            off64_t offset = 0);
37
38    virtual void disconnect();
39
40    virtual status_t initCheck() const;
41
42    virtual ssize_t readAt(off64_t offset, void *data, size_t size);
43    virtual status_t getSize(off64_t *size);
44    virtual uint32_t flags();
45
46    virtual sp<DecryptHandle> DrmInitialization(const char *mime);
47
48    virtual void getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client);
49
50    virtual String8 getUri();
51
52    virtual String8 getMIMEType() const;
53
54    virtual status_t reconnectAtOffset(off64_t offset);
55
56    static status_t UpdateProxyConfig(
57            const char *host, int32_t port, const char *exclusionList);
58
59protected:
60    virtual ~ChromiumHTTPDataSource();
61
62private:
63    friend struct SfDelegate;
64
65    enum State {
66        DISCONNECTED,
67        CONNECTING,
68        CONNECTED,
69        READING,
70        DISCONNECTING
71    };
72
73    const uint32_t mFlags;
74
75    mutable Mutex mLock;
76    Condition mCondition;
77
78    State mState;
79
80    SfDelegate *mDelegate;
81
82    AString mURI;
83    KeyedVector<String8, String8> mHeaders;
84
85    off64_t mCurrentOffset;
86
87    // Any connection error or the result of a read operation
88    // (for the lattter this is the number of bytes read, if successful).
89    ssize_t mIOResult;
90
91    int64_t mContentSize;
92
93    String8 mContentType;
94
95    sp<DecryptHandle> mDecryptHandle;
96    DrmManagerClient *mDrmManagerClient;
97
98    void disconnect_l();
99
100    status_t connect_l(
101            const char *uri,
102            const KeyedVector<String8, String8> *headers,
103            off64_t offset);
104
105    static void InitiateRead(
106            ChromiumHTTPDataSource *me, void *data, size_t size);
107
108    void initiateRead(void *data, size_t size);
109
110    void onConnectionEstablished(
111            int64_t contentSize, const char *contentType);
112
113    void onConnectionFailed(status_t err);
114    void onReadCompleted(ssize_t size);
115    void onDisconnectComplete();
116
117    void clearDRMState_l();
118
119    DISALLOW_EVIL_CONSTRUCTORS(ChromiumHTTPDataSource);
120};
121
122}  // namespace android
123
124#endif  // CHROME_HTTP_DATA_SOURCE_H_
125