1/*
2 * Copyright (C) 2013 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
17package android.media;
18
19import android.os.IBinder;
20import android.util.Log;
21
22import java.net.CookieHandler;
23import java.net.CookieManager;
24import java.net.CookieStore;
25import java.net.HttpCookie;
26import java.util.List;
27
28/** @hide */
29public class MediaHTTPService extends IMediaHTTPService.Stub {
30    private static final String TAG = "MediaHTTPService";
31    private List<HttpCookie> mCookies;
32    private Boolean mCookieStoreInitialized = new Boolean(false);
33
34    public MediaHTTPService(List<HttpCookie> cookies) {
35        mCookies = cookies;
36        Log.v(TAG, "MediaHTTPService(" + this + "): Cookies: " + cookies);
37    }
38
39    public IMediaHTTPConnection makeHTTPConnection() {
40
41        synchronized (mCookieStoreInitialized) {
42            // Only need to do it once for all connections
43            if ( !mCookieStoreInitialized )  {
44                CookieHandler cookieHandler = CookieHandler.getDefault();
45                if (cookieHandler == null) {
46                    cookieHandler = new CookieManager();
47                    CookieHandler.setDefault(cookieHandler);
48                    Log.v(TAG, "makeHTTPConnection: CookieManager created: " + cookieHandler);
49                } else {
50                    Log.v(TAG, "makeHTTPConnection: CookieHandler (" + cookieHandler + ") exists.");
51                }
52
53                // Applying the bootstrapping cookies
54                if ( mCookies != null ) {
55                    if ( cookieHandler instanceof CookieManager ) {
56                        CookieManager cookieManager = (CookieManager)cookieHandler;
57                        CookieStore store = cookieManager.getCookieStore();
58                        for ( HttpCookie cookie : mCookies ) {
59                            try {
60                                store.add(null, cookie);
61                            } catch ( Exception e ) {
62                                Log.v(TAG, "makeHTTPConnection: CookieStore.add" + e);
63                            }
64                            //for extended debugging when needed
65                            //Log.v(TAG, "MediaHTTPConnection adding Cookie[" + cookie.getName() +
66                            //        "]: " + cookie);
67                        }
68                    } else {
69                        Log.w(TAG, "makeHTTPConnection: The installed CookieHandler is not a "
70                                + "CookieManager. Can’t add the provided cookies to the cookie "
71                                + "store.");
72                    }
73                }   // mCookies
74
75                mCookieStoreInitialized = true;
76
77                Log.v(TAG, "makeHTTPConnection(" + this + "): cookieHandler: " + cookieHandler +
78                        " Cookies: " + mCookies);
79            }   // mCookieStoreInitialized
80        }   // synchronized
81
82        return new MediaHTTPConnection();
83    }
84
85    /* package private */static IBinder createHttpServiceBinderIfNecessary(
86            String path) {
87        return createHttpServiceBinderIfNecessary(path, null);
88    }
89
90    // when cookies are provided
91    static IBinder createHttpServiceBinderIfNecessary(
92            String path, List<HttpCookie> cookies) {
93        if (path.startsWith("http://") || path.startsWith("https://")) {
94            return (new MediaHTTPService(cookies)).asBinder();
95        } else if (path.startsWith("widevine://")) {
96            Log.d(TAG, "Widevine classic is no longer supported");
97        }
98
99        return null;
100    }
101}
102