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                CookieManager cookieManager = (CookieManager)CookieHandler.getDefault();
45                if (cookieManager == null) {
46                    cookieManager = new CookieManager();
47                    CookieHandler.setDefault(cookieManager);
48                    Log.v(TAG, "makeHTTPConnection: CookieManager created: " + cookieManager);
49                }
50                else {
51                    Log.v(TAG, "makeHTTPConnection: CookieManager(" + cookieManager + ") exists.");
52                }
53
54                // Applying the bootstrapping cookies
55                if ( mCookies != null ) {
56                    CookieStore store = cookieManager.getCookieStore();
57                    for ( HttpCookie cookie : mCookies ) {
58                        try {
59                            store.add(null, cookie);
60                        } catch ( Exception e ) {
61                            Log.v(TAG, "makeHTTPConnection: CookieStore.add" + e);
62                        }
63                        //for extended debugging when needed
64                        //Log.v(TAG, "MediaHTTPConnection adding Cookie[" + cookie.getName() +
65                        //        "]: " + cookie);
66                    }
67                }   // mCookies
68
69                mCookieStoreInitialized = true;
70
71                Log.v(TAG, "makeHTTPConnection(" + this + "): cookieManager: " + cookieManager +
72                        " Cookies: " + mCookies);
73            }   // mCookieStoreInitialized
74        }   // synchronized
75
76        return new MediaHTTPConnection();
77    }
78
79    /* package private */static IBinder createHttpServiceBinderIfNecessary(
80            String path) {
81        return createHttpServiceBinderIfNecessary(path, null);
82    }
83
84    // when cookies are provided
85    static IBinder createHttpServiceBinderIfNecessary(
86            String path, List<HttpCookie> cookies) {
87        if (path.startsWith("http://") || path.startsWith("https://")) {
88            return (new MediaHTTPService(cookies)).asBinder();
89        } else if (path.startsWith("widevine://")) {
90            Log.d(TAG, "Widevine classic is no longer supported");
91        }
92
93        return null;
94    }
95
96}
97